goods.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. define(["jquery", "easy-admin"], function ($, ea) {
  2. var init = {
  3. table_elem: '#currentTable',
  4. table_render_id: 'currentTableRenderId',
  5. index_url: 'mall.goods/index',
  6. add_url: 'mall.goods/add',
  7. edit_url: 'mall.goods/edit',
  8. delete_url: 'mall.goods/delete',
  9. export_url: 'mall.goods/export',
  10. modify_url: 'mall.goods/modify',
  11. stock_url: 'mall.goods/stock',
  12. recycle_url: 'mall.goods/recycle',
  13. };
  14. return {
  15. index: function () {
  16. ea.table.render({
  17. init: init,
  18. toolbar: ['refresh',
  19. [{
  20. text: '添加',
  21. url: init.add_url,
  22. method: 'open',
  23. auth: 'add',
  24. class: 'layui-btn layui-btn-normal layui-btn-sm',
  25. icon: 'fa fa-plus ',
  26. extend: 'data-width="90%" data-height="95%"',
  27. }],
  28. 'delete', 'export', 'recycle'],
  29. cols: [[
  30. {type: "checkbox"},
  31. {field: 'id', width: 80, title: 'ID', searchOp: '='},
  32. {field: 'sort', width: 80, title: '排序', edit: 'text'},
  33. {field: 'cate_id', width: 100, title: '商品分类', search: 'select', selectList: cateSelects, laySearch: true},
  34. {field: 'title', width: 100, title: '商品名称'},
  35. {field: 'logo', width: 100, title: '分类图片', search: false, templet: ea.table.image},
  36. {field: 'market_price', width: 100, title: '市场价', templet: ea.table.price},
  37. {field: 'discount_price', width: 100, title: '折扣价', templet: ea.table.price},
  38. {field: 'total_stock', width: 100, title: '库存统计'},
  39. {field: 'stock', width: 100, title: '剩余库存'},
  40. {field: 'virtual_sales', width: 100, title: '虚拟销量'},
  41. {field: 'sales', width: 80, title: '销量'},
  42. {field: 'status', title: '状态', width: 85, selectList: {0: '禁用', 1: '启用'}, templet: ea.table.switch},
  43. // 演示多选,实际数据库并无 status2 字段,搜索后会报错
  44. {
  45. field: 'status2', title: '演示多选', width: 105, search: 'xmSelect', selectList: {1: '模拟选项1', 2: '模拟选项2', 3: '模拟选项3', 4: '模拟选项4', 5: '模拟选项5'}, hide: true,
  46. searchOp: 'in', templet: function (res) {
  47. // 根据自己实际项目进行输出
  48. return res?.status2 || '模拟数据'
  49. }
  50. },
  51. {field: 'create_time', minWidth: 80, title: '创建时间', search: 'range'},
  52. {
  53. width: 250,
  54. title: '操作',
  55. templet: ea.table.tool,
  56. operat: [
  57. [{
  58. templet: function (d) {
  59. return `<button type="button" class="layui-btn layui-btn-xs">自定义 ${d.id}</button>`
  60. }
  61. }, {
  62. text: '编辑',
  63. url: init.edit_url,
  64. method: 'open',
  65. auth: 'edit',
  66. class: 'layui-btn layui-btn-xs layui-btn-success',
  67. extend: 'data-width="90%" data-height="95%"',
  68. }, {
  69. text: '入库',
  70. url: init.stock_url,
  71. method: 'open',
  72. auth: 'stock',
  73. class: 'layui-btn layui-btn-xs layui-btn-normal',
  74. visible: function (row) {
  75. return row.status === 1;
  76. },
  77. }],
  78. 'delete']
  79. }
  80. ]],
  81. done: (res) => {
  82. // 状态为1的商品背景高亮 展示写法 可根据自己项目自定义
  83. $.each(res.data, function (idx, item) {
  84. if (item.status === 1) {
  85. $(`tr[data-index="${idx}"]`).css({
  86. 'background': 'linear-gradient(to left, #77eb7c, #bbffbe, #ffffff, transparent)',
  87. 'border': 'none',
  88. })
  89. }
  90. })
  91. }
  92. });
  93. ea.listen();
  94. },
  95. add: function () {
  96. layui.util.on({
  97. AiOptimization: function (data) {
  98. let layOn = $(data).attr('lay-on')
  99. $(data).attr('lay-on', layOn + 'Loading')
  100. aiOptimization(data)
  101. },
  102. })
  103. var demo1 = xmSelect.render({
  104. el: '#demo1',
  105. name: 'xxx', // form表单提交的name
  106. theme: {color: getComputedStyle(document.documentElement).getPropertyValue('--ea8-theme-main-color') || '#16b777'},
  107. data: [
  108. {name: 'Make', value: 1},
  109. {name: 'PHP', value: 2},
  110. {name: 'Great Again', value: 3},
  111. ]
  112. })
  113. ea.listen();
  114. },
  115. edit: function () {
  116. layui.util.on({
  117. AiOptimization: function (data) {
  118. let layOn = $(data).attr('lay-on')
  119. $(data).attr('lay-on', layOn + 'Loading')
  120. aiOptimization(data)
  121. },
  122. })
  123. var demo1 = xmSelect.render({
  124. el: '#demo1',
  125. name: 'xxx', // form表单提交的name
  126. theme: {color: getComputedStyle(document.documentElement).getPropertyValue('--ea8-theme-main-color') || '#16b777'},
  127. data: [
  128. {name: 'Make', value: 1},
  129. {name: 'PHP', value: 2, selected: true,},
  130. {name: 'Great Again', value: 3, selected: true,},
  131. ]
  132. })
  133. ea.listen();
  134. },
  135. stock: function () {
  136. ea.listen();
  137. },
  138. recycle: function () {
  139. init.index_url = init.recycle_url;
  140. ea.table.render({
  141. init: init,
  142. toolbar: ['refresh',
  143. [{
  144. class: 'layui-btn layui-btn-sm',
  145. method: 'get',
  146. field: 'id',
  147. icon: 'fa fa-refresh',
  148. text: '全部恢复',
  149. title: '确定恢复?',
  150. auth: 'recycle',
  151. url: init.recycle_url + '?type=restore',
  152. checkbox: true
  153. }, {
  154. class: 'layui-btn layui-btn-danger layui-btn-sm',
  155. method: 'get',
  156. field: 'id',
  157. icon: 'fa fa-delete',
  158. text: '彻底删除',
  159. title: '确定彻底删除?',
  160. auth: 'recycle',
  161. url: init.recycle_url + '?type=delete',
  162. checkbox: true
  163. }], 'export',
  164. ],
  165. cols: [[
  166. {type: "checkbox"},
  167. {field: 'id', width: 80, title: 'ID', searchOp: '='},
  168. {field: 'sort', width: 80, title: '排序', edit: 'text'},
  169. {field: 'cate_id', minWidth: 80, title: '商品分类', search: 'select', selectList: cateSelects, laySearch: true},
  170. {field: 'title', minWidth: 80, title: '商品名称'},
  171. {field: 'logo', minWidth: 80, title: '分类图片', search: false, templet: ea.table.image},
  172. {field: 'status', title: '状态', width: 85, selectList: {0: '禁用', 1: '启用'}},
  173. // 演示多选,实际数据库并无 status2 字段,搜索后会报错
  174. {
  175. field: 'status2', title: '演示多选', width: 105, search: 'xmSelect', selectList: {1: '模拟选项1', 2: '模拟选项2', 3: '模拟选项3', 4: '模拟选项4', 5: '模拟选项5'}, hide: true,
  176. searchOp: 'in', templet: function (res) {
  177. // 根据自己实际项目进行输出
  178. return res?.status2 || '模拟数据'
  179. }
  180. },
  181. {field: 'create_time', minWidth: 80, title: '创建时间', search: 'range'},
  182. {field: 'delete_time', minWidth: 80, title: '删除时间', search: 'range'},
  183. {
  184. width: 250,
  185. title: '操作',
  186. templet: ea.table.tool,
  187. operat: [
  188. [{
  189. title: '确认恢复?',
  190. text: '恢复数据',
  191. filed: 'id',
  192. url: init.recycle_url + '?type=restore',
  193. method: 'get',
  194. auth: 'recycle',
  195. class: 'layui-btn layui-btn-xs layui-btn-success',
  196. }, {
  197. title: '想好了吗?',
  198. text: '彻底删除',
  199. filed: 'id',
  200. method: 'get',
  201. url: init.recycle_url + '?type=delete',
  202. auth: 'recycle',
  203. class: 'layui-btn layui-btn-xs layui-btn-normal layui-bg-red',
  204. }]]
  205. }
  206. ]],
  207. });
  208. ea.listen();
  209. },
  210. };
  211. function aiOptimization(data) {
  212. let layOn = $(data).attr('lay-on')
  213. let title = $('input[name="title"]').val()
  214. // 告诉AI 你需要做什么
  215. let message = `优化这个标题 ${title}`
  216. if ($.trim(title) === '') {
  217. ea.msg.error('标题不能为空', function () {
  218. $(data).attr('lay-on', layOn.split('Loading')[0])
  219. })
  220. return false
  221. }
  222. let url = ea.url('mall.goods/aiOptimization')
  223. ea.request.post({url: url, data: {message: message}}, function (res) {
  224. let content = res.data?.choices[0]?.message?.content
  225. // stream 为true 时,AI 内容会逐字输出
  226. let stream = true
  227. ea.ai.chat(content, {stream: stream}, function () {
  228. $(data).attr('lay-on', layOn.split('Loading')[0])
  229. })
  230. }, function (error) {
  231. ea.msg.error(error.msg, function () {
  232. $(data).attr('lay-on', layOn.split('Loading')[0])
  233. })
  234. })
  235. }
  236. });