addons.js 16 KB

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