goods.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2. var Controller = {
  3. index: () => {
  4. const { reactive, onMounted } = Vue
  5. const index = {
  6. setup() {
  7. const state = reactive({
  8. data: [],
  9. filter: {
  10. drawer: false,
  11. data: {
  12. keyword: { field: 'id', value: '' },
  13. 'commission_goods.status': '',
  14. },
  15. tools: {
  16. keyword: {
  17. type: 'tinputprepend',
  18. label: '商品信息',
  19. placeholder: '请输入查询内容',
  20. value: {
  21. field: 'id',
  22. value: '',
  23. },
  24. options: {
  25. data: [{
  26. label: '商品ID',
  27. value: 'id',
  28. },
  29. {
  30. label: '商品名称',
  31. value: 'title',
  32. },
  33. {
  34. label: '商品副标题',
  35. value: 'subtitle',
  36. }],
  37. }
  38. },
  39. 'commission_goods.status': {
  40. type: 'tselect',
  41. label: '分销状态',
  42. value: '',
  43. options: {
  44. data: [{
  45. label: '不参与',
  46. value: '0',
  47. },
  48. {
  49. label: '参与中',
  50. value: '1',
  51. }],
  52. },
  53. },
  54. },
  55. condition: {},
  56. },
  57. goodsStatusStyle: {
  58. 0: 'danger',
  59. 1: 'success',
  60. },
  61. statusStyle: {
  62. up: 'success',
  63. down: 'danger',
  64. hidden: 'info',
  65. },
  66. })
  67. function getData() {
  68. let tempSearch = JSON.parse(JSON.stringify(state.filter.data));
  69. let search = composeFilter(tempSearch, {
  70. title: 'like',
  71. subtitle: 'like',
  72. });
  73. Fast.api.ajax({
  74. url: 'shopro/commission/goods',
  75. type: 'GET',
  76. data: {
  77. page: pagination.page,
  78. list_rows: pagination.list_rows,
  79. ...search,
  80. },
  81. }, function (ret, res) {
  82. state.data = res.data.data
  83. pagination.total = res.data.total
  84. return false
  85. }, function (ret, res) { })
  86. }
  87. function onOpenFilter() {
  88. state.filter.drawer = true
  89. }
  90. function onChangeFilter() {
  91. pagination.page = 1
  92. getData()
  93. state.filter.drawer && (state.filter.drawer = false)
  94. }
  95. const pagination = reactive({
  96. page: 1,
  97. list_rows: 10,
  98. total: 0,
  99. })
  100. const batchHandle = reactive({
  101. data: [],
  102. })
  103. function onChangeSelection(val) {
  104. batchHandle.data = val
  105. }
  106. function onBatchHandle(type) {
  107. let ids = []
  108. batchHandle.data.forEach((item) => {
  109. ids.push(item.id)
  110. })
  111. switch (type) {
  112. case 'edit':
  113. onEdit(ids.join(','), 'batch')
  114. break;
  115. }
  116. }
  117. function onEdit(ids, rulesType = '') {
  118. Fast.api.open(`shopro/commission/goods/edit?type=edit&id=${ids}&rulesType=${rulesType}`, "设置佣金", {
  119. callback() {
  120. getData()
  121. }
  122. })
  123. }
  124. onMounted(() => {
  125. getData()
  126. })
  127. return {
  128. state,
  129. getData,
  130. onOpenFilter,
  131. onChangeFilter,
  132. pagination,
  133. batchHandle,
  134. onChangeSelection,
  135. onBatchHandle,
  136. onEdit,
  137. }
  138. }
  139. }
  140. createApp('index', index);
  141. },
  142. edit: () => {
  143. Controller.form();
  144. },
  145. form: () => {
  146. const { reactive, onMounted } = Vue
  147. const { ElMessage } = ElementPlus
  148. const addEdit = {
  149. setup() {
  150. const state = reactive({
  151. type: new URLSearchParams(location.search).get('type'),
  152. id: new URLSearchParams(location.search).get('id'),
  153. rulesType: new URLSearchParams(location.search).get('rulesType'),
  154. detailData: {},
  155. commission_goods: {
  156. status: 1,
  157. self_rules: new URLSearchParams(location.search).get('rulesType') == 'batch' ? 2 : 0,
  158. commission_order_status: 1,
  159. commission_config: {},
  160. commission_rules: {},
  161. },
  162. defaultCommissionConfig: {
  163. status: 0,
  164. level: 2,
  165. self_buy: 0,
  166. reward_type: 'goods_price',
  167. reward_event: 'paid',
  168. },
  169. commission_config_temp: {},
  170. commission_rules: {},
  171. levelData: [],
  172. })
  173. function getDetail() {
  174. Fast.api.ajax({
  175. url: `shopro/commission/goods/detail/id/${state.id}`,
  176. type: 'GET',
  177. }, function (ret, res) {
  178. state.data = res.data;
  179. if (state.rulesType != 'batch') {
  180. state.detailData = state.data.goods[0];
  181. }
  182. Object.keys(state.defaultCommissionConfig).forEach((key) => {
  183. if (key != 'status') state.defaultCommissionConfig[key] = state.data.config[key];
  184. });
  185. state.commission_config_temp = JSON.parse(JSON.stringify(state.defaultCommissionConfig));
  186. if (isEmpty(state.detailData.commission_goods)) {
  187. initCommission(state.commission_goods.self_rules);
  188. } else {
  189. state.commission_goods = state.detailData.commission_goods;
  190. if (
  191. state.detailData.commission_goods.commission_config &&
  192. state.detailData.commission_goods.commission_config.status
  193. ) {
  194. state.commission_config_temp = state.detailData.commission_goods.commission_config;
  195. }
  196. initCommission(state.detailData.commission_goods.self_rules);
  197. }
  198. return false
  199. }, function (ret, res) { })
  200. }
  201. function getLevelSelect() {
  202. Fast.api.ajax({
  203. url: `shopro/commission/level/select`,
  204. type: 'GET',
  205. data: {
  206. sort: 'level',
  207. order: 'asc',
  208. }
  209. }, function (ret, res) {
  210. state.levelData = res.data;
  211. state.type == 'edit' && getDetail()
  212. return false
  213. }, function (ret, res) { })
  214. }
  215. function initCommission(type) {
  216. let rate_money = {
  217. 1: {
  218. rate: '',
  219. money: '',
  220. },
  221. 2: {
  222. rate: '',
  223. money: '',
  224. },
  225. 3: {
  226. rate: '',
  227. money: '',
  228. },
  229. };
  230. if (!isObject(state.commission_goods.commission_rules)) {
  231. state.commission_goods.commission_rules = {};
  232. }
  233. if (type == 0) {
  234. state.commission_goods.commission_rules = null;
  235. } else if (type == 1) {
  236. state.detailData.sku_prices.forEach((i) => {
  237. if (state.commission_goods.commission_rules[i.id]) {
  238. state.levelData.forEach((j) => {
  239. if (state.commission_goods.commission_rules[i.id][j.level]) {
  240. for (let key in state.commission_goods.commission_rules[i.id][j.level]) {
  241. if (!state.commission_goods.commission_rules[i.id][j.level][key].rate) {
  242. state.commission_goods.commission_rules[i.id][j.level][key].rate = '';
  243. } else if (!state.commission_goods.commission_rules[i.id][j.level][key].money) {
  244. state.commission_goods.commission_rules[i.id][j.level][key].money = '';
  245. }
  246. }
  247. } else {
  248. state.commission_goods.commission_rules[i.id][j.level] = JSON.parse(JSON.stringify(rate_money));
  249. }
  250. });
  251. for (var level in state.commission_goods.commission_rules[i.id]) {
  252. let index = state.levelData.findIndex((l) => Number(l.level) == Number(level));
  253. if (index == -1) {
  254. delete state.commission_goods.commission_rules[i.id][level];
  255. }
  256. }
  257. } else {
  258. state.commission_goods.commission_rules[i.id] = {};
  259. state.levelData.forEach((l) => {
  260. state.commission_goods.commission_rules[i.id][l.level] = JSON.parse(JSON.stringify(rate_money));
  261. });
  262. }
  263. });
  264. for (var sku in state.commission_goods.commission_rules) {
  265. let index = state.detailData.sku_prices.findIndex((s) => Number(s.id) == Number(sku));
  266. if (index == -1) {
  267. delete state.commission_goods.commission_rules[sku];
  268. }
  269. }
  270. } else if (type == 2) {
  271. state.levelData.forEach((l) => {
  272. if (state.commission_goods.commission_rules[l.level]) {
  273. } else {
  274. state.commission_goods.commission_rules[l.level] = JSON.parse(JSON.stringify(rate_money));
  275. }
  276. });
  277. for (var level in state.commission_goods.commission_rules) {
  278. let index = state.levelData.findIndex((l) => Number(l.level) == Number(level));
  279. if (index == -1) {
  280. delete state.commission_goods.commission_rules[level];
  281. }
  282. }
  283. }
  284. }
  285. function onChangeSelfRules(self_rules) {
  286. if (self_rules == 1 || self_rules == 2) {
  287. state.commission_goods.commission_rules = {};
  288. if (state.commission_config_temp.status == 0) {
  289. state.commission_config_temp = JSON.parse(JSON.stringify(state.defaultCommissionConfig));
  290. }
  291. } else if (self_rules == 0) {
  292. state.commission_goods.commission_rules = null;
  293. state.commission_config_temp = JSON.parse(JSON.stringify(state.defaultCommissionConfig));
  294. }
  295. initCommission(self_rules);
  296. }
  297. function onChangeCommissionConfigStatus(val) {
  298. if (val == 0) {
  299. // 默认
  300. state.commission_config_temp = JSON.parse(JSON.stringify(state.defaultCommissionConfig));
  301. } else {
  302. // 自定义
  303. if (
  304. state.detailData.commission_goods &&
  305. state.detailData.commission_goods.commission_config
  306. ) {
  307. state.commission_config_temp = state.detailData.commission_goods.commission_config;
  308. }
  309. state.commission_config_temp.status = 1;
  310. }
  311. }
  312. const commissionPopover = reactive({
  313. flag: {},
  314. form: {
  315. money: '',
  316. rate: '',
  317. },
  318. });
  319. function onConfirmCommissionPopover(cl) {
  320. if (state.commission_goods.self_rules == 1) {
  321. for (let sku in state.commission_goods.commission_rules) {
  322. for (let level in state.commission_goods.commission_rules[sku]) {
  323. state.commission_goods.commission_rules[sku][level][cl] = JSON.parse(JSON.stringify(commissionPopover.form))
  324. }
  325. }
  326. } else if (state.commission_goods.self_rules == 2) {
  327. for (let level in state.commission_goods.commission_rules) {
  328. state.commission_goods.commission_rules[level][cl] = JSON.parse(JSON.stringify(commissionPopover.form))
  329. }
  330. }
  331. onCancelCommissionPopover(cl);
  332. }
  333. function onCancelCommissionPopover(cl) {
  334. commissionPopover.flag[cl] = false;
  335. commissionPopover.form = {
  336. money: '',
  337. rate: '',
  338. };
  339. }
  340. function onConfirm() {
  341. const commission_goods = {
  342. ...state.commission_goods,
  343. };
  344. commission_goods.commission_config = JSON.stringify({
  345. ...state.commission_config_temp,
  346. });
  347. let flag = true
  348. if (commission_goods.self_rules == 1) {
  349. for (var key1 in commission_goods.commission_rules) {
  350. for (var key2 in commission_goods.commission_rules[key1]) {
  351. for (var key3 in commission_goods.commission_rules[key1][key2]) {
  352. if (key3 <= state.commission_config_temp.level) {
  353. if ((commission_goods.commission_rules[key1][key2][key3].rate === '' || commission_goods.commission_rules[key1][key2][key3].rate < 0)
  354. && (commission_goods.commission_rules[key1][key2][key3].money === '' || commission_goods.commission_rules[key1][key2][key3].money < 0)) {
  355. flag = false
  356. }
  357. }
  358. }
  359. }
  360. }
  361. }
  362. if (commission_goods.self_rules == 2) {
  363. for (var key1 in commission_goods.commission_rules) {
  364. for (var key2 in commission_goods.commission_rules[key1]) {
  365. if (key2 <= state.commission_config_temp.level) {
  366. if (commission_goods.commission_rules[key1][key2].rate === '' || commission_goods.commission_rules[key1][key2].rate < 0) {
  367. flag = false
  368. }
  369. }
  370. }
  371. }
  372. }
  373. if (!flag) {
  374. ElMessage({
  375. message: '请将数据填写完整',
  376. type: 'warning',
  377. })
  378. return false
  379. }
  380. Fast.api.ajax({
  381. url: `shopro/commission/goods/edit/id/${state.id}`,
  382. type: 'POST',
  383. data: JSON.parse(JSON.stringify(commission_goods))
  384. }, function (ret, res) {
  385. Fast.api.close()
  386. }, function (ret, res) { })
  387. }
  388. onMounted(() => {
  389. getLevelSelect()
  390. })
  391. return {
  392. state,
  393. onChangeSelfRules,
  394. onChangeCommissionConfigStatus,
  395. commissionPopover,
  396. onConfirmCommissionPopover,
  397. onCancelCommissionPopover,
  398. onConfirm,
  399. }
  400. }
  401. }
  402. createApp('addEdit', addEdit);
  403. }
  404. };
  405. return Controller;
  406. });