addons.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. define([], function () {
  2. require.config({
  3. paths: {
  4. 'designer': '../addons/poster/js/designer',
  5. 'jquery.contextMenu': '../addons/poster/js/jquery.contextMenu',
  6. 'jquery-colorpicker': '../addons/poster/js/jquery.colorpicker.min',
  7. }
  8. });
  9. //修改上传的接口调用
  10. require(['upload'], function (Upload) {
  11. var _onInit = Upload.events.onInit;
  12. //初始化中完成判断
  13. Upload.events.onInit = function () {
  14. _onInit.apply(this, Array.prototype.slice.apply(arguments));
  15. //如果上传接口不是七牛云,则不处理
  16. if (this.options.url !== Config.upload.uploadurl) {
  17. return;
  18. }
  19. var _success = this.options.success;
  20. $.extend(this.options, {
  21. //关闭自动处理队列功能
  22. autoQueue: false,
  23. chunkSuccess: function (chunk, file, response) {
  24. this.contexts = this.contexts ? this.contexts : [];
  25. this.contexts.push(typeof response.ctx !== 'undefined' ? response.ctx : response.data.ctx);
  26. },
  27. chunksUploaded: function (file, done) {
  28. var that = this;
  29. var params = $(that.element).data("params") || {};
  30. var category = typeof params.category !== 'undefined' ? params.category : ($(that.element).data("category") || '');
  31. category = typeof category === 'function' ? category.call(this, file) : category;
  32. Fast.api.ajax({
  33. url: "/addons/qiniu/index/upload",
  34. data: {
  35. action: 'merge',
  36. filesize: file.size,
  37. filename: file.name,
  38. chunkid: file.upload.uuid,
  39. chunkcount: file.upload.totalChunkCount,
  40. width: file.width || 0,
  41. height: file.height || 0,
  42. type: file.type,
  43. category: category,
  44. qiniutoken: Config.upload.multipart.qiniutoken,
  45. contexts: this.contexts
  46. },
  47. }, function (data, ret) {
  48. done(JSON.stringify(ret));
  49. return false;
  50. }, function (data, ret) {
  51. file.accepted = false;
  52. that._errorProcessing([file], ret.msg);
  53. return false;
  54. });
  55. },
  56. });
  57. //先移除已有的事件
  58. this.off("success", _success).on("success", function (file, response) {
  59. var that = this;
  60. var ret = {code: 0, msg: response};
  61. try {
  62. ret = typeof response === 'string' ? JSON.parse(response) : response;
  63. if (file.xhr.status === 200) {
  64. if (Config.upload.uploadmode === 'client') {
  65. if (typeof ret.key !== 'undefined') {
  66. ret = {code: 1, msg: "", data: {url: '/' + ret.key, hash: ret.hash}};
  67. }
  68. var url = ret.data.url || '';
  69. var params = $(that.element).data("params") || {};
  70. var category = typeof params.category !== 'undefined' ? params.category : ($(that.element).data("category") || '');
  71. category = typeof category === 'function' ? category.call(that, file) : category;
  72. Fast.api.ajax({
  73. url: "/addons/qiniu/index/notify",
  74. data: {name: file.name, url: ret.data.url, hash: ret.data.hash, size: file.size, width: file.width || 0, height: file.height || 0, type: file.type, category: category, qiniutoken: Config.upload.multipart.qiniutoken}
  75. }, function () {
  76. return false;
  77. }, function () {
  78. return false;
  79. });
  80. } else {
  81. console.error(ret);
  82. }
  83. } else {
  84. console.error(file.xhr);
  85. }
  86. } catch (e) {
  87. console.error(e);
  88. }
  89. _success.call(this, file, ret);
  90. });
  91. this.on("addedfile", function (file) {
  92. var that = this;
  93. setTimeout(function () {
  94. if (file.status === 'error') {
  95. return;
  96. }
  97. var md5 = ''; //七牛云无需本地获取文件MD5
  98. var chunk = that.options.chunking && file.size > that.options.chunkSize ? 1 : 0;
  99. var params = $(that.element).data("params") || {};
  100. var category = typeof params.category !== 'undefined' ? params.category : ($(that.element).data("category") || '');
  101. category = typeof category === 'function' ? category.call(that, file) : category;
  102. Fast.api.ajax({
  103. url: "/addons/qiniu/index/params",
  104. data: {method: 'POST', category: category, md5: md5, name: file.name, type: file.type, size: file.size, chunk: chunk, chunksize: that.options.chunkSize, qiniutoken: Config.upload.multipart.qiniutoken},
  105. }, function (data) {
  106. file.qiniutoken = data.qiniutoken;
  107. file.params = data;
  108. file.category = category;
  109. if (file.status != 'error') {
  110. //开始上传
  111. that.enqueueFile(file);
  112. } else {
  113. that.removeFile(file);
  114. }
  115. return false;
  116. }, function () {
  117. that.removeFile(file);
  118. });
  119. }, 0);
  120. });
  121. //如果是直传模式
  122. if (Config.upload.uploadmode === 'client') {
  123. var _url = this.options.url;
  124. //分片上传时URL链接不同
  125. this.options.url = function (files) {
  126. this.options.headers = {"Authorization": "UpToken " + Config.upload.multipart.qiniutoken};
  127. if (files[0].upload.chunked) {
  128. var chunk = null;
  129. files[0].upload.chunks.forEach(function (item) {
  130. if (item.status === 'uploading') {
  131. chunk = item;
  132. }
  133. });
  134. if (!chunk) {
  135. return Config.upload.uploadurl + '/mkfile/' + files[0].size;
  136. } else {
  137. return Config.upload.uploadurl + '/mkblk/' + chunk.dataBlock.data.size;
  138. }
  139. }
  140. return _url;
  141. };
  142. this.options.params = function (files, xhr, chunk) {
  143. var params = Config.upload.multipart;
  144. if (chunk) {
  145. return $.extend({}, params, {
  146. filesize: chunk.file.size,
  147. filename: chunk.file.name,
  148. chunkid: chunk.file.upload.uuid,
  149. chunkindex: chunk.index,
  150. chunkcount: chunk.file.upload.totalChunkCount,
  151. chunkfilesize: chunk.dataBlock.data.size,
  152. width: chunk.file.width || 0,
  153. height: chunk.file.height || 0,
  154. type: chunk.file.type,
  155. });
  156. } else {
  157. var retParams = $.extend({}, params, files[0].params || {});
  158. //七牛云直传使用的是token参数
  159. retParams.token = retParams.qiniutoken;
  160. delete retParams.qiniutoken;
  161. return retParams;
  162. }
  163. };
  164. //分片上传时需要变更提交的内容
  165. this.on("sending", function (file, xhr, formData) {
  166. if (file.upload.chunked) {
  167. var _send = xhr.send;
  168. xhr.send = function () {
  169. var chunk = null;
  170. file.upload.chunks.forEach(function (item) {
  171. if (item.status == 'uploading') {
  172. chunk = item;
  173. }
  174. });
  175. if (chunk) {
  176. _send.call(xhr, chunk.dataBlock.data);
  177. }
  178. };
  179. }
  180. });
  181. }
  182. };
  183. });
  184. require.config({
  185. paths: {
  186. 'summernote': '../addons/summernote/lang/summernote-zh-CN.min'
  187. },
  188. shim: {
  189. 'summernote': ['../addons/summernote/js/summernote.min', 'css!../addons/summernote/css/summernote.css'],
  190. }
  191. });
  192. require(['form', 'upload'], function (Form, Upload) {
  193. var _bindevent = Form.events.bindevent;
  194. Form.events.bindevent = function (form) {
  195. _bindevent.apply(this, [form]);
  196. try {
  197. //绑定summernote事件
  198. if ($(".summernote,.editor", form).size() > 0) {
  199. require(['summernote'], function () {
  200. var imageButton = function (context) {
  201. var ui = $.summernote.ui;
  202. var button = ui.button({
  203. contents: '<i class="fa fa-file-image-o"/>',
  204. tooltip: __('Choose'),
  205. click: function () {
  206. parent.Fast.api.open("general/attachment/select?element_id=&multiple=true&mimetype=image/*", __('Choose'), {
  207. callback: function (data) {
  208. var urlArr = data.url.split(/\,/);
  209. $.each(urlArr, function () {
  210. var url = Fast.api.cdnurl(this);
  211. context.invoke('editor.insertImage', url);
  212. });
  213. }
  214. });
  215. return false;
  216. }
  217. });
  218. return button.render();
  219. };
  220. var attachmentButton = function (context) {
  221. var ui = $.summernote.ui;
  222. var button = ui.button({
  223. contents: '<i class="fa fa-file"/>',
  224. tooltip: __('Choose'),
  225. click: function () {
  226. parent.Fast.api.open("general/attachment/select?element_id=&multiple=true&mimetype=*", __('Choose'), {
  227. callback: function (data) {
  228. var urlArr = data.url.split(/\,/);
  229. $.each(urlArr, function () {
  230. var url = Fast.api.cdnurl(this);
  231. var node = $("<a href='" + url + "'>" + url + "</a>");
  232. context.invoke('insertNode', node[0]);
  233. });
  234. }
  235. });
  236. return false;
  237. }
  238. });
  239. return button.render();
  240. };
  241. $(".summernote,.editor", form).summernote({
  242. height: 250,
  243. lang: 'zh-CN',
  244. fontNames: [
  245. 'Arial', 'Arial Black', 'Serif', 'Sans', 'Courier',
  246. 'Courier New', 'Comic Sans MS', 'Helvetica', 'Impact', 'Lucida Grande',
  247. "Open Sans", "Hiragino Sans GB", "Microsoft YaHei",
  248. '微软雅黑', '宋体', '黑体', '仿宋', '楷体', '幼圆',
  249. ],
  250. fontNamesIgnoreCheck: [
  251. "Open Sans", "Microsoft YaHei",
  252. '微软雅黑', '宋体', '黑体', '仿宋', '楷体', '幼圆'
  253. ],
  254. toolbar: [
  255. ['style', ['style', 'undo', 'redo']],
  256. ['font', ['bold', 'underline', 'strikethrough', 'clear']],
  257. ['fontname', ['color', 'fontname', 'fontsize']],
  258. ['para', ['ul', 'ol', 'paragraph', 'height']],
  259. ['table', ['table', 'hr']],
  260. ['insert', ['link', 'picture', 'video']],
  261. ['select', ['image', 'attachment']],
  262. ['view', ['fullscreen', 'codeview', 'help']],
  263. ],
  264. buttons: {
  265. image: imageButton,
  266. attachment: attachmentButton,
  267. },
  268. dialogsInBody: true,
  269. followingToolbar: false,
  270. callbacks: {
  271. onChange: function (contents) {
  272. $(this).val(contents);
  273. $(this).trigger('change');
  274. },
  275. onInit: function () {
  276. },
  277. onImageUpload: function (files) {
  278. var that = this;
  279. //依次上传图片
  280. for (var i = 0; i < files.length; i++) {
  281. Upload.api.send(files[i], function (data) {
  282. var url = Fast.api.cdnurl(data.url);
  283. $(that).summernote("insertImage", url, 'filename');
  284. });
  285. }
  286. }
  287. }
  288. });
  289. });
  290. }
  291. } catch (e) {
  292. }
  293. };
  294. });
  295. });