index.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. const _Http = getApp().globalData.http,
  2. currency = require("../../../../utils/currency"),
  3. CNY = sum => currency(sum, {
  4. symbol: "¥",
  5. precision: 2
  6. }).format();
  7. let downCount = {};
  8. let ids = {};
  9. import {
  10. getCustomText
  11. } from "../../../../utils/customItemType";
  12. Page({
  13. data: {
  14. list: [],
  15. results: [], //选中结果
  16. resultsList: [],
  17. sa_brandid: null, //当前选中品牌id
  18. classList: [], //生成订单时所选
  19. sum: 0, //价格合
  20. allBrandList: [],
  21. loading: false,
  22. privacyFieldC: []
  23. },
  24. onLoad(options) {
  25. ids = JSON.parse(options.data);
  26. this.getList()
  27. try {
  28. let privacyFieldC = wx.getStorageSync('auth').wdiscounts.forms.wdiscounts.formcols.map(v => v.title);
  29. this.setData({
  30. privacyFieldC
  31. })
  32. console.log("privacyFieldC", privacyFieldC)
  33. } catch (error) {
  34. console.error(error)
  35. }
  36. },
  37. /* 获取列表 */
  38. getList() {
  39. _Http.basic({
  40. "id": 20220924095302,
  41. "content": {
  42. nocache: true,
  43. istool: 0,
  44. ...ids,
  45. "pageNumber": 1,
  46. "pageSize": 9999,
  47. "where": {
  48. "condition": ""
  49. }
  50. }
  51. }).then(res => {
  52. console.log('购物车列表', res)
  53. this.selectComponent('#ListBox').RefreshToComplete();
  54. this.selectComponent("#ListBox").setHeight(".head", this);
  55. if (res.msg != '成功') return wx.showToast({
  56. title: res.msg,
  57. icon: "none"
  58. })
  59. let list = [],
  60. allBrandList = [];
  61. list = res.data.map(v => {
  62. v.showPrice = CNY(v.gradeprice)
  63. let obj = allBrandList.find(s => s.sa_brandid == v.sa_brandid);
  64. if (obj) {
  65. obj.results.push(v.sa_shoppingcartid)
  66. obj.resultsList.push(v)
  67. } else {
  68. allBrandList.push({
  69. brandname: v.brandname,
  70. sa_brandid: v.sa_brandid,
  71. results: [v.sa_shoppingcartid],
  72. resultsList: [v]
  73. })
  74. }
  75. if (v.iscustomsize) v.customText = getCustomText(v);
  76. return v
  77. });
  78. this.setData({
  79. list,
  80. allBrandList,
  81. isGet: true
  82. });
  83. if (wx.getStorageSync('shopping1')) this.setData({
  84. ...wx.getStorageSync('shopping1')
  85. });
  86. this.computeSum();
  87. })
  88. },
  89. clickBut(e) {
  90. this.handleSubmit();
  91. /* this.data.classList.length >= 2 ? wx.showToast({
  92. title: '请选择订单领域(订单只允许同品牌/同领域的商品)',
  93. icon: "none",
  94. duration: 3000
  95. }) : this.handleSubmit(0);
  96. */
  97. },
  98. /* 提交 */
  99. submit(e) {
  100. this.handleSubmit()
  101. },
  102. handleSubmit() {
  103. let that = this;
  104. let items = that.data.resultsList.map(v => {
  105. return {
  106. "sa_orderitemsid": 0,
  107. "itemid": v.itemid,
  108. "sa_brandid": v.sa_brandid,
  109. "qty": v.qty,
  110. width: v.width || 0,
  111. length: v.length || 0,
  112. sa_shoppingcartid: v.sa_shoppingcartid || 0,
  113. sa_promotion_itemsid: v.sa_promotion_itemsid
  114. }
  115. })
  116. console.log("items", items)
  117. wx.showModal({
  118. title: '提示',
  119. content: '是否确定生成促销订单',
  120. complete: ({
  121. confirm
  122. }) => {
  123. if (confirm) {
  124. that.setData({
  125. loading: true
  126. })
  127. _Http.basic({
  128. "id": 20221128183202,
  129. "content": {
  130. istool: 0,
  131. type: "促销订单",
  132. //"tradefield": data.type, 必选
  133. ...ids,
  134. items
  135. }
  136. }).then(res => {
  137. console.log("转化订单", res)
  138. that.setData({
  139. loading: false
  140. })
  141. if (res.msg != '成功') return wx.showToast({
  142. title: res.msg,
  143. icon: "none"
  144. });
  145. getApp().globalData.getCollectCount()
  146. that.getList();
  147. wx.showModal({
  148. title: '提示',
  149. content: '生成成功!是否立即前往',
  150. complete: (s) => {
  151. if (s.confirm) {
  152. wx.navigateTo({
  153. url: '/packageA/orderForm/detail?id=' + res.data.sa_orderid,
  154. })
  155. }
  156. }
  157. });
  158. })
  159. }
  160. }
  161. })
  162. },
  163. /* 是否选择全部 */
  164. setIsAll() {
  165. let isAll = this.data.isAll;
  166. //取消全选
  167. if (isAll) {
  168. this.setData({
  169. sa_brandid: null,
  170. results: [],
  171. resultsList: []
  172. })
  173. } else {
  174. //已选品牌产品情况下
  175. if (this.data.sa_brandid) {
  176. let obj = this.data.allBrandList.find(v => v.sa_brandid == this.data.sa_brandid)
  177. this.setData({
  178. results: obj.results,
  179. resultsList: obj.resultsList
  180. })
  181. } else {
  182. if (this.data.allBrandList.length == 0) return;
  183. this.setData({
  184. sa_brandid: this.data.allBrandList[0].sa_brandid,
  185. results: this.data.allBrandList[0].results,
  186. resultsList: this.data.allBrandList[0].resultsList,
  187. })
  188. }
  189. };
  190. this.computeSum();
  191. },
  192. /* 切换选中项 */
  193. changeResults(e, my = false) {
  194. const {
  195. item
  196. } = my ? e : e.currentTarget.dataset;
  197. let results = this.data.results,
  198. resultsList = this.data.resultsList,
  199. sa_brandid = this.data.sa_brandid;
  200. if (sa_brandid && sa_brandid != item.sa_brandid) return;
  201. if (results.length == 0) {
  202. results.push(item.sa_shoppingcartid);
  203. resultsList.push(item)
  204. sa_brandid = item.sa_brandid;
  205. } else {
  206. let index = results.findIndex(v => v == item.sa_shoppingcartid)
  207. if (index == -1) {
  208. results.push(item.sa_shoppingcartid);
  209. resultsList.push(item)
  210. } else {
  211. results.splice(index, 1);
  212. resultsList.splice(index, 1);
  213. if (results.length == 0) sa_brandid = null;
  214. }
  215. };
  216. this.setData({
  217. results,
  218. sa_brandid
  219. })
  220. this.computeSum();
  221. },
  222. customization(e) {
  223. const {
  224. item
  225. } = e.target.dataset;
  226. getApp().globalData.customizedProduct = (item, custom) => {
  227. return new Promise((resolve) => {
  228. _Http.basic({
  229. "id": 20220924104302,
  230. "content": {
  231. "sa_shoppingcartid": item.sa_shoppingcartid,
  232. "qty": item.qty,
  233. ...custom
  234. },
  235. }).then(res => {
  236. console.log("修改定制", res)
  237. wx.showToast({
  238. title: res.msg != '成功' ? res.msg : '修改成功',
  239. icon: "none"
  240. });
  241. this.getList()
  242. resolve(true)
  243. })
  244. })
  245. }
  246. if (item) this.selectComponent("#Custom").onClick(item)
  247. },
  248. /* 计算总价/产品领域分类 */
  249. computeSum() {
  250. let results = this.data.results,
  251. resultsList = [],
  252. sum = 0,
  253. classList = [];
  254. if (results.length) results = results.filter(v => {
  255. let item = this.data.list.find(va => va.sa_shoppingcartid == v);
  256. if (item) {
  257. sum = currency(sum).add(currency(item.qty).multiply(item.gradeprice)).value;
  258. resultsList.push(item)
  259. /* 领域分类 */
  260. let index = classList.findIndex(value => value.type == item.tradefield_shoppingcart);
  261. if (index == -1) {
  262. classList.push({
  263. type: item.tradefield_shoppingcart,
  264. list: [item],
  265. name: item.tradefield_shoppingcart + "(1件商品)"
  266. })
  267. } else {
  268. classList[index].list.push(item)
  269. classList[index].name = classList[index].type + `(${classList[index].list.length}件商品)`
  270. }
  271. };
  272. return item
  273. });
  274. let sa_brandid = results.length ? this.data.sa_brandid : null;
  275. wx.setStorageSync('shopping1', {
  276. results,
  277. sa_brandid
  278. })
  279. let isAll = false;
  280. if (sa_brandid) {
  281. let brand = this.data.allBrandList.find(v => v.sa_brandid == sa_brandid)
  282. isAll = brand.results.length == results.length
  283. }
  284. this.setData({
  285. sum: CNY(sum),
  286. isAll,
  287. results,
  288. sa_brandid,
  289. resultsList,
  290. classList
  291. });
  292. },
  293. /* 删除产品 */
  294. deteleItem(e) {
  295. const {
  296. item
  297. } = e.currentTarget.dataset;
  298. wx.showModal({
  299. title: '提示',
  300. content: `是否确认删除${item.itemname}?`,
  301. complete: ({
  302. confirm
  303. }) => {
  304. e.detail.instance.close();
  305. if (confirm) _Http.basic({
  306. "id": 20220924095202,
  307. "content": {
  308. ...ids,
  309. "sa_shoppingcartids": [item.sa_shoppingcartid],
  310. }
  311. }).then(res => {
  312. wx.showToast({
  313. title: res.msg != '成功' ? res.msg : "删除成功",
  314. icon: "none"
  315. });
  316. if (res.msg != '成功') return;
  317. this.getList(true)
  318. getApp().globalData.getCollectCount()
  319. })
  320. }
  321. })
  322. },
  323. /* 输入框失去焦点调整数量 */
  324. inputBlur(e) {
  325. const {
  326. index
  327. } = e.currentTarget.dataset;
  328. let item = this.data.list[index];
  329. let qty = 0;
  330. if (item.orderminqty > e.detail.value) {
  331. wx.showToast({
  332. title: '输入数量低于最低起订量!',
  333. icon: "none"
  334. })
  335. qty = item.orderminqty;
  336. } else if (item.orderminqty < e.detail.value) {
  337. var currencyRounding = value => currency(value, {
  338. increment: item.orderaddqty
  339. });
  340. qty = currency(currencyRounding(currency(e.detail.value).subtract(item.orderminqty)).format()).add(item.orderminqty).value;
  341. } else {
  342. qty = e.detail.value;
  343. }
  344. this.setData({
  345. [`list[${index}].qty`]: 0
  346. });
  347. this.setData({
  348. [`list[${index}].qty`]: qty
  349. });
  350. this.computeSum();
  351. clearTimeout(downCount['count' + index])
  352. downCount['count' + index] = setTimeout(() => {
  353. _Http.basic({
  354. "id": 20220924104302,
  355. "content": {
  356. "sa_shoppingcartid": item.sa_shoppingcartid,
  357. "qty": item.qty,
  358. "width": item.width || 0,
  359. "length": item.length || 0
  360. },
  361. }, false).then(res => {
  362. console.log("修改数量", res)
  363. })
  364. }, 2000)
  365. },
  366. /* 步进器调整数量 */
  367. stepperChange(e) {
  368. const {
  369. index
  370. } = e.currentTarget.dataset;
  371. let item = this.data.list[index];
  372. if (e.type == 'plus') {
  373. item.qty += item.orderaddqty
  374. } else {
  375. item.qty -= item.orderaddqty
  376. }
  377. this.setData({
  378. [`list[${index}]`]: item
  379. })
  380. this.computeSum();
  381. clearTimeout(downCount['count' + index])
  382. downCount['count' + index] = setTimeout(() => {
  383. _Http.basic({
  384. "id": 20220924104302,
  385. "content": {
  386. "sa_shoppingcartid": item.sa_shoppingcartid,
  387. "qty": item.qty,
  388. "width": item.width || 0,
  389. "length": item.length || 0
  390. },
  391. }, false).then(res => {
  392. console.log("修改数量", res)
  393. })
  394. }, 2000)
  395. }
  396. })