addons.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. define([], function () {
  2. if (typeof Config.upload.storage !== 'undefined' && Config.upload.storage === 'cos') {
  3. require(['upload'], function (Upload) {
  4. //获取文件MD5值
  5. var getFileMd5 = function (file, cb) {
  6. //如果savekey中未检测到md5,则无需获取文件md5,直接返回upload的uuid
  7. if (!Config.upload.savekey.match(/\{(file)?md5\}/)) {
  8. cb && cb(file.upload.uuid);
  9. return;
  10. }
  11. require(['../addons/cos/js/spark'], function (SparkMD5) {
  12. var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
  13. chunkSize = 10 * 1024 * 1024,
  14. chunks = Math.ceil(file.size / chunkSize),
  15. currentChunk = 0,
  16. spark = new SparkMD5.ArrayBuffer(),
  17. fileReader = new FileReader();
  18. fileReader.onload = function (e) {
  19. spark.append(e.target.result);
  20. currentChunk++;
  21. if (currentChunk < chunks) {
  22. loadNext();
  23. } else {
  24. cb && cb(spark.end());
  25. }
  26. };
  27. fileReader.onerror = function () {
  28. console.warn('文件读取错误');
  29. };
  30. function loadNext() {
  31. var start = currentChunk * chunkSize,
  32. end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;
  33. fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
  34. }
  35. loadNext();
  36. });
  37. };
  38. var _onInit = Upload.events.onInit;
  39. //初始化中完成判断
  40. Upload.events.onInit = function () {
  41. _onInit.apply(this, Array.prototype.slice.apply(arguments));
  42. //如果上传接口不是COS,则不处理
  43. if (this.options.url !== Config.upload.uploadurl) {
  44. return;
  45. }
  46. $.extend(this.options, {
  47. //关闭自动处理队列功能
  48. autoQueue: false,
  49. params: function (files, xhr, chunk) {
  50. var params = Config.upload.multipart;
  51. if (chunk) {
  52. return $.extend({}, params, {
  53. filesize: chunk.file.size,
  54. filename: chunk.file.name,
  55. chunkid: chunk.file.upload.uuid,
  56. chunkindex: chunk.index,
  57. chunkcount: chunk.file.upload.totalChunkCount,
  58. chunkfilesize: chunk.dataBlock.data.size,
  59. chunksize: this.options.chunkSize,
  60. width: chunk.file.width || 0,
  61. height: chunk.file.height || 0,
  62. type: chunk.file.type,
  63. uploadId: chunk.file.uploadId,
  64. key: chunk.file.key,
  65. });
  66. } else {
  67. params = $.extend({}, params, files[0].params);
  68. params.category = files[0].category || '';
  69. }
  70. return params;
  71. },
  72. chunkSuccess: function (chunk, file, response) {
  73. var etag = chunk.xhr.getResponseHeader("ETag").replace(/(^")|("$)/g, '');
  74. file.etags = file.etags ? file.etags : [];
  75. file.etags[chunk.index] = etag;
  76. },
  77. chunksUploaded: function (file, done) {
  78. var that = this;
  79. Fast.api.ajax({
  80. url: "/addons/cos/index/upload",
  81. data: {
  82. action: 'merge',
  83. filesize: file.size,
  84. filename: file.name,
  85. chunkid: file.upload.uuid,
  86. chunkcount: file.upload.totalChunkCount,
  87. md5: file.md5,
  88. key: file.key,
  89. uploadId: file.uploadId,
  90. etags: file.etags,
  91. category: file.category || '',
  92. costoken: Config.upload.multipart.costoken,
  93. },
  94. }, function (data, ret) {
  95. done(JSON.stringify(ret));
  96. return false;
  97. }, function (data, ret) {
  98. file.accepted = false;
  99. that._errorProcessing([file], ret.msg);
  100. return false;
  101. });
  102. },
  103. });
  104. var _success = this.options.success;
  105. //先移除已有的事件
  106. this.off("success", _success).on("success", function (file, response) {
  107. var ret = {code: 0, msg: response};
  108. try {
  109. if (response) {
  110. ret = typeof response === 'string' ? JSON.parse(response) : response;
  111. }
  112. if (file.xhr.status === 200 || file.xhr.status === 204) {
  113. if (Config.upload.uploadmode === 'client') {
  114. ret = {code: 1, data: {url: '/' + file.key}};
  115. }
  116. if (ret.code == 1) {
  117. var url = ret.data.url || '';
  118. Fast.api.ajax({
  119. url: "/addons/cos/index/notify",
  120. 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 || '', costoken: Config.upload.multipart.costoken}
  121. }, function () {
  122. return false;
  123. }, function () {
  124. return false;
  125. });
  126. } else {
  127. console.error(ret);
  128. }
  129. } else {
  130. console.error(file.xhr);
  131. }
  132. } catch (e) {
  133. console.error(e);
  134. }
  135. _success.call(this, file, ret);
  136. });
  137. this.on("addedfile", function (file) {
  138. var that = this;
  139. setTimeout(function () {
  140. if (file.status === 'error') {
  141. return;
  142. }
  143. getFileMd5(file, function (md5) {
  144. var chunk = that.options.chunking && file.size > that.options.chunkSize ? 1 : 0;
  145. var params = $(that.element).data("params") || {};
  146. var category = typeof params.category !== 'undefined' ? params.category : ($(that.element).data("category") || '');
  147. category = typeof category === 'function' ? category.call(that, file) : category;
  148. Fast.api.ajax({
  149. url: "/addons/cos/index/params",
  150. data: {method: 'POST', category: category, md5: md5, name: file.name, type: file.type, size: file.size, chunk: chunk, chunksize: that.options.chunkSize, costoken: Config.upload.multipart.costoken},
  151. }, function (data) {
  152. file.md5 = md5;
  153. file.id = data.id;
  154. file.key = data.key;
  155. file.date = data.date;
  156. file.uploadId = data.uploadId;
  157. file.policy = data.policy;
  158. file.signature = data.signature;
  159. file.partsAuthorization = data.partsAuthorization;
  160. file.params = data;
  161. file.category = category;
  162. if (file.status != 'error') {
  163. //开始上传
  164. that.enqueueFile(file);
  165. } else {
  166. that.removeFile(file);
  167. }
  168. return false;
  169. }, function () {
  170. that.removeFile(file);
  171. });
  172. });
  173. }, 0);
  174. });
  175. if (Config.upload.uploadmode === 'client') {
  176. var _method = this.options.method;
  177. var _url = this.options.url;
  178. this.options.method = function (files) {
  179. if (files[0].upload.chunked) {
  180. var chunk = null;
  181. files[0].upload.chunks.forEach(function (item) {
  182. if (item.status === 'uploading') {
  183. chunk = item;
  184. }
  185. });
  186. if (!chunk) {
  187. return "POST";
  188. } else {
  189. return "PUT";
  190. }
  191. }
  192. return _method;
  193. };
  194. this.options.url = function (files) {
  195. if (files[0].upload.chunked) {
  196. var chunk = null;
  197. files[0].upload.chunks.forEach(function (item) {
  198. if (item.status === 'uploading') {
  199. chunk = item;
  200. }
  201. });
  202. var index = chunk.dataBlock.chunkIndex;
  203. // debugger;
  204. this.options.headers = {"Authorization": files[0]['partsAuthorization'][index], "x-date": files[0]['date']};
  205. if (!chunk) {
  206. return Config.upload.uploadurl + "/" + files[0].key + "?uploadId=" + files[0].uploadId;
  207. } else {
  208. return Config.upload.uploadurl + "/" + files[0].key + "?partNumber=" + (index + 1) + "&uploadId=" + files[0].uploadId;
  209. }
  210. }
  211. return _url;
  212. };
  213. this.options.params = function (files, xhr, chunk) {
  214. var params = Config.upload.multipart;
  215. if (chunk) {
  216. return $.extend({}, params, {
  217. filesize: chunk.file.size,
  218. filename: chunk.file.name,
  219. chunkid: chunk.file.upload.uuid,
  220. chunkindex: chunk.index,
  221. chunkcount: chunk.file.upload.totalChunkCount,
  222. chunkfilesize: chunk.dataBlock.data.size,
  223. width: chunk.file.width || 0,
  224. height: chunk.file.height || 0,
  225. type: chunk.file.type,
  226. });
  227. } else {
  228. var retParams = $.extend({}, params, files[0].params || {});
  229. delete retParams.costoken;
  230. return retParams;
  231. }
  232. };
  233. this.on("sending", function (file, xhr, formData) {
  234. var that = this;
  235. if (file.upload.chunked) {
  236. var _send = xhr.send;
  237. xhr.send = function () {
  238. var chunk = null;
  239. file.upload.chunks.forEach(function (item) {
  240. if (item.status == 'uploading') {
  241. chunk = item;
  242. }
  243. });
  244. if (chunk) {
  245. _send.call(xhr, chunk.dataBlock.data);
  246. }
  247. };
  248. } else {
  249. }
  250. });
  251. }
  252. };
  253. });
  254. }
  255. require.config({
  256. paths: {
  257. 'designer': '../addons/poster/js/designer',
  258. 'jquery.contextMenu': '../addons/poster/js/jquery.contextMenu',
  259. 'jquery-colorpicker': '../addons/poster/js/jquery.colorpicker.min',
  260. }
  261. });
  262. if (Config.modulename == 'admin' && Config.controllername == 'index' && Config.actionname == 'index') {
  263. require.config({
  264. paths: {
  265. 'vue3': "../addons/shopro/libs/vue",
  266. 'vue': "../addons/shopro/libs/vue.amd",
  267. 'text': "../addons/shopro/libs/require-text",
  268. 'SaChat': '../addons/shopro/chat/index',
  269. 'ElementPlus': '../addons/shopro/libs/element-plus/index',
  270. 'ElementPlusIconsVue3': "../addons/shopro/libs/element-plus/icons-vue",
  271. 'ElementPlusIconsVue': '../addons/shopro/libs/element-plus/icons-vue.amd',
  272. 'io': '../addons/shopro/libs/socket.io',
  273. },
  274. shim: {
  275. 'ElementPlus': {
  276. deps: ['css!../addons/shopro/libs/element-plus/index.css']
  277. },
  278. },
  279. });
  280. require(['vue3', 'ElementPlusIconsVue3'], function (Vue3, ElementPlusIconsVue3) {
  281. require(['vue', 'jquery', 'SaChat', 'text!../addons/shopro/chat/index.html', 'ElementPlus', 'ElementPlusIconsVue', 'io'], function (Vue, $, SaChat, SaChatTemplate, ElementPlus, ElementPlusIconsVue, io) {
  282. if (Config.dark_type != 'none') {
  283. SaChatTemplate = SaChatTemplate.replaceAll('__DARK__', `<link rel="stylesheet" href="__CDN__/assets/addons/shopro/css/dark.css?v={$site.version|htmlentities}" />`)
  284. }
  285. SaChatTemplate = SaChatTemplate.replaceAll('__DARK__', ``)
  286. SaChatTemplate = SaChatTemplate.replaceAll('__CDN__', Config.__CDN__)
  287. Fast.api.ajax({
  288. url: 'shopro/chat/index/init',
  289. loading: false,
  290. type: 'GET'
  291. }, function (ret, res) {
  292. $("body").append(`<div id="SaChatTemplateContainer"></div>
  293. <div id="SaChatWrap"><sa-chat></sa-chat></div>`);
  294. $("#SaChatTemplateContainer").append(SaChatTemplate);
  295. const { createApp } = Vue
  296. const app = createApp({})
  297. app.use(ElementPlus)
  298. for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  299. app.component(key, component)
  300. }
  301. app.component('sa-chat', SaChat)
  302. app.mount(`#SaChatWrap`)
  303. return false;
  304. }, function (ret, res) {
  305. if (res.msg == '') {
  306. return false;
  307. }
  308. })
  309. });
  310. });
  311. }
  312. require.config({
  313. paths: {
  314. 'summernote': '../addons/summernote/lang/summernote-zh-CN.min'
  315. },
  316. shim: {
  317. 'summernote': ['../addons/summernote/js/summernote.min', 'css!../addons/summernote/css/summernote.min.css'],
  318. }
  319. });
  320. require(['form', 'upload'], function (Form, Upload) {
  321. var _bindevent = Form.events.bindevent;
  322. Form.events.bindevent = function (form) {
  323. _bindevent.apply(this, [form]);
  324. try {
  325. //绑定summernote事件
  326. if ($(Config.summernote.classname || '.editor', form).length > 0) {
  327. var selectUrl = typeof Config !== 'undefined' && Config.modulename === 'index' ? 'user/attachment' : 'general/attachment/select';
  328. require(['summernote'], function () {
  329. var imageButton = function (context) {
  330. var ui = $.summernote.ui;
  331. var button = ui.button({
  332. contents: '<i class="fa fa-file-image-o"/>',
  333. tooltip: __('Choose'),
  334. click: function () {
  335. parent.Fast.api.open(selectUrl + "?element_id=&multiple=true&mimetype=image/", __('Choose'), {
  336. callback: function (data) {
  337. var urlArr = data.url.split(/\,/);
  338. $.each(urlArr, function () {
  339. var url = Fast.api.cdnurl(this, true);
  340. context.invoke('editor.insertImage', url);
  341. });
  342. }
  343. });
  344. return false;
  345. }
  346. });
  347. return button.render();
  348. };
  349. var attachmentButton = function (context) {
  350. var ui = $.summernote.ui;
  351. var button = ui.button({
  352. contents: '<i class="fa fa-file"/>',
  353. tooltip: __('Choose'),
  354. click: function () {
  355. parent.Fast.api.open(selectUrl + "?element_id=&multiple=true&mimetype=*", __('Choose'), {
  356. callback: function (data) {
  357. var urlArr = data.url.split(/\,/);
  358. $.each(urlArr, function () {
  359. var url = Fast.api.cdnurl(this, true);
  360. var node = $("<a href='" + url + "'>" + url + "</a>");
  361. context.invoke('insertNode', node[0]);
  362. });
  363. }
  364. });
  365. return false;
  366. }
  367. });
  368. return button.render();
  369. };
  370. $(Config.summernote.classname || '.editor', form).each(function () {
  371. $(this).summernote($.extend(true, {}, {
  372. // height: 250,
  373. minHeight: 250,
  374. lang: 'zh-CN',
  375. fontNames: [
  376. 'Arial', 'Arial Black', 'Serif', 'Sans', 'Courier',
  377. 'Courier New', 'Comic Sans MS', 'Helvetica', 'Impact', 'Lucida Grande',
  378. "Open Sans", "Hiragino Sans GB", "Microsoft YaHei",
  379. '微软雅黑', '宋体', '黑体', '仿宋', '楷体', '幼圆',
  380. ],
  381. fontNamesIgnoreCheck: [
  382. "Open Sans", "Microsoft YaHei",
  383. '微软雅黑', '宋体', '黑体', '仿宋', '楷体', '幼圆'
  384. ],
  385. toolbar: [
  386. ['style', ['style', 'undo', 'redo']],
  387. ['font', ['bold', 'underline', 'strikethrough', 'clear']],
  388. ['fontname', ['color', 'fontname', 'fontsize']],
  389. ['para', ['ul', 'ol', 'paragraph', 'height']],
  390. ['table', ['table', 'hr']],
  391. ['insert', ['link', 'picture', 'video']],
  392. ['select', ['image', 'attachment']],
  393. ['view', ['fullscreen', 'codeview', 'help']],
  394. ],
  395. buttons: {
  396. image: imageButton,
  397. attachment: attachmentButton,
  398. },
  399. dialogsInBody: true,
  400. followingToolbar: false,
  401. callbacks: {
  402. onChange: function (contents) {
  403. $(this).val(contents);
  404. $(this).trigger('change');
  405. },
  406. onInit: function () {
  407. },
  408. onImageUpload: function (files) {
  409. var that = this;
  410. //依次上传图片
  411. for (var i = 0; i < files.length; i++) {
  412. Upload.api.send(files[i], function (data) {
  413. var url = Fast.api.cdnurl(data.url, true);
  414. $(that).summernote("insertImage", url, 'filename');
  415. });
  416. }
  417. }
  418. }
  419. }, $(this).data("summernote-options") || {}));
  420. });
  421. });
  422. }
  423. } catch (e) {
  424. }
  425. };
  426. });
  427. });