card.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2. var Controller = {
  3. index: function () {
  4. // 初始化表格参数配置
  5. Table.api.init({
  6. extend: {
  7. index_url: 'shop/card/index' + location.search,
  8. add_url: 'shop/card/add',
  9. edit_url: 'shop/card/edit',
  10. del_url: 'shop/card/del',
  11. multi_url: 'shop/card/multi',
  12. import_url: 'shop/card/import',
  13. table: 'shop_card',
  14. }
  15. });
  16. var table = $("#table");
  17. // 初始化表格
  18. table.bootstrapTable({
  19. url: $.fn.bootstrapTable.defaults.extend.index_url,
  20. pk: 'id',
  21. sortName: 'id',
  22. columns: [
  23. [
  24. {checkbox: true},
  25. {field: 'id', title: __('Id')},
  26. {field: 'title', title: __('Title'), operate: 'LIKE'},
  27. {field: 'type', title: __('Type'), searchList: {"0": __('Type 0'), "1": __('Type 1')}, formatter: Table.api.formatter.normal},
  28. {field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime},
  29. {field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime},
  30. {field: 'status', title: __('Status'), searchList: {"normal": __('Normal'), "hidden": __('Hidden')}, formatter: Table.api.formatter.status},
  31. {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  32. ]
  33. ]
  34. });
  35. // 为表格绑定事件
  36. Table.api.bindevent(table);
  37. },
  38. add: function () {
  39. Controller.api.bindevent();
  40. },
  41. edit: function () {
  42. Controller.api.bindevent();
  43. },
  44. api: {
  45. bindevent: function () {
  46. Controller.api.preview();
  47. $(document).on('input', '#c-content', function () {
  48. Controller.api.preview();
  49. });
  50. Form.api.bindevent($("form[role=form]"));
  51. },
  52. bindcardevent: function () {
  53. //替换内容
  54. let formatHtml = function (oldHtml, newHtml) {
  55. let html;
  56. if (oldHtml && /<div\sdata\-id="placeholder">(\s|[\r\n]|&nbsp;)*<\/div>/.test(oldHtml)) {
  57. html = oldHtml.replace(/<div\sdata\-id="placeholder">(\s|[\r\n]|&nbsp;)*<\/div>/g, newHtml);
  58. } else {
  59. html = oldHtml + '<p><br></p>' + newHtml;
  60. }
  61. let str = html.substring(html.length - 10);
  62. if (!str.includes('<br')) {
  63. html += '<p><br></p>';
  64. }
  65. return html;
  66. };
  67. //插入商品卡片
  68. let insertHtml = function (html, insert = false) {
  69. let editor;
  70. if (typeof KindEditor !== 'undefined') {
  71. if (insert) {
  72. KindEditor.sync("#c-content");
  73. KindEditor.html('#c-content', formatHtml(KindEditor('#c-content').val(), html));
  74. } else {
  75. KindEditor.insertHtml("#c-content", html);
  76. }
  77. } else if (typeof UM !== 'undefined' && typeof UM.list["c-content"] !== 'undefined') {
  78. editor = UM.list["c-content"];
  79. if (insert) {
  80. editor.setContent(formatHtml(editor.getContent(), html));
  81. } else {
  82. editor.execCommand("insertHtml", html);
  83. }
  84. } else if (typeof UE !== 'undefined' && typeof UE.list["c-content"] !== 'undefined') {
  85. editor = UE.list["c-content"];
  86. if (insert) {
  87. editor.setContent(formatHtml(editor.getContent(), html));
  88. } else {
  89. editor.execCommand("insertHtml", html);
  90. }
  91. } else if ($("#c-content").data("summernote")) {
  92. editor = $('#c-content');
  93. if (insert) {
  94. editor.summernote('code', formatHtml(editor.summernote('code'), html))
  95. } else {
  96. editor.summernote('pasteHTML', html);
  97. }
  98. } else if (typeof Simditor !== 'undefined' && typeof Simditor.list['c-content'] !== 'undefined') {
  99. editor = Simditor.list['c-content'];
  100. if (insert) {
  101. editor.setValue(formatHtml(editor.getValue(), html));
  102. } else {
  103. editor.setValue(editor.getValue() + html);
  104. }
  105. } else if (typeof tinymce !== 'undefined' && typeof tinymce.get('c-content') !== 'undefined') {
  106. editor = tinymce.get('c-content');
  107. if (insert) {
  108. editor.setContent(formatHtml(editor.getContent(), html));
  109. } else {
  110. editor.insertContent(html);
  111. }
  112. } else if (html && insert) {
  113. Layer.open({
  114. content: "你的编辑器暂不支持插入HTML代码,请手动复制以下代码到你的编辑器" + "<textarea class='form-control' rows='5'>" + html + "</textarea>", title: "温馨提示"
  115. });
  116. }
  117. };
  118. //点击插入卡片按钮
  119. $(document).on('click', '.btn-card', function () {
  120. insertHtml('<div data-id="placeholder"></div>');
  121. let cancelCallback = function () {
  122. insertHtml('', 1);
  123. };
  124. Layer.open({
  125. id: 'goods-tpl-select',
  126. type: 1,
  127. title: '请选择卡片模板',
  128. area: ['600px', '210px'],
  129. content: Template('tplselect', {}),
  130. zIndex: 99,
  131. shade: 0,
  132. btn: ["确定", "取消"],
  133. success: function (layero, index) {
  134. Form.events.selectpage($(".c-select-goods", layero));
  135. require(['selectpage'], function () {
  136. $('.selectpage', $('.select-template', layero)).selectPage({
  137. eAjaxSuccess: function (data) {
  138. data.totalRow = data.total;
  139. return data;
  140. },
  141. eSelect: function (row) {
  142. switch (row.type) {
  143. case 0://商品
  144. $('.c-select-goods', layero).html(Template('goodstemplte', {}));
  145. break;
  146. case 1://优惠券
  147. $('.c-select-goods', layero).html(Template('coupontemplte', {}));
  148. break;
  149. }
  150. setTimeout(function () {
  151. Form.events.selectpage($(".c-select-goods", layero));
  152. }, 100);
  153. }
  154. });
  155. });
  156. },
  157. yes: function (index, layero) {
  158. let tpl = $('#c-template', layero).val();
  159. let source_id = $('#c-source_id', layero).val();
  160. if (!tpl) {
  161. Toastr.error('请选择卡片模板');
  162. return;
  163. }
  164. if (!source_id) {
  165. Toastr.error('请选择资源数据');
  166. return;
  167. }
  168. Fast.api.ajax({
  169. url: 'shop/ajax/get_tpl',
  170. data: {
  171. tpl_id: tpl,
  172. source_id
  173. }
  174. }, function (html) {
  175. insertHtml(html, 1);
  176. Layer.close(index);
  177. return false;
  178. })
  179. },
  180. cancel: cancelCallback,
  181. btn2: cancelCallback
  182. });
  183. });
  184. },
  185. preview: function () {
  186. let content = $('#c-content').val();
  187. content = content.replace(/\{\$.*?title\s{0,}\}/g, '标题')
  188. .replace(/\{\$.*?name\s{0,}\}/g, '名称')
  189. .replace(/\{\$.*?price\s{0,}\}|\{\$.*?marketprice\s{0,}\}|\{\$.*?number\s{0,}\}/g, '0.0')
  190. .replace(/\{\$.*?result_tips\s{0,}\}/g, '满?减?')
  191. .replace(/\{\$.*?expire_time\s{0,}\}/g, '过期时间')
  192. .replace(/\{if.*?\/if\}/g, '?')
  193. .replace(/\{.*?\}/g, '--');
  194. let dom = $(content)
  195. dom.find('img').attr('src', '/assets/addons/shop/img/swiper2.jpg')
  196. dom.find('a').attr('href', 'javascript:;').attr('target', '');
  197. $('#preview').html(dom.html());
  198. }
  199. }
  200. };
  201. return Controller;
  202. });