index.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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_promotion_itemsid": v.sa_promotion_itemsid,
  109. "sa_brandid": v.sa_brandid,
  110. "qty": v.qty,
  111. width: v.width || 0,
  112. length: v.length || 0,
  113. sa_shoppingcartid: v.sa_shoppingcartid || 0,
  114. sa_promotion_itemsid: v.sa_promotion_itemsid
  115. }
  116. })
  117. console.log("items", items)
  118. wx.showModal({
  119. title: '提示',
  120. content: '是否确定生成促销订单',
  121. complete: ({
  122. confirm
  123. }) => {
  124. if (confirm) {
  125. that.setData({
  126. loading: true
  127. })
  128. _Http.basic({
  129. "id": 20221128183202,
  130. "content": {
  131. istool: 0,
  132. type: "促销订单",
  133. //"tradefield": data.type, 必选
  134. ...ids,
  135. items
  136. }
  137. }).then(res => {
  138. console.log("转化订单", res)
  139. that.setData({
  140. loading: false
  141. })
  142. if (res.msg != '成功') return wx.showToast({
  143. title: res.msg,
  144. icon: "none"
  145. });
  146. getApp().globalData.getCollectCount()
  147. that.getList();
  148. wx.showModal({
  149. title: '提示',
  150. content: '生成成功!是否立即前往',
  151. complete: (s) => {
  152. if (s.confirm) {
  153. wx.navigateTo({
  154. url: '/packageA/orderForm/detail?id=' + res.data.sa_orderid,
  155. })
  156. }
  157. }
  158. });
  159. })
  160. }
  161. }
  162. })
  163. },
  164. /* 是否选择全部 */
  165. setIsAll() {
  166. let isAll = this.data.isAll;
  167. //取消全选
  168. if (isAll) {
  169. this.setData({
  170. sa_brandid: null,
  171. results: [],
  172. resultsList: []
  173. })
  174. } else {
  175. //已选品牌产品情况下
  176. if (this.data.sa_brandid) {
  177. let obj = this.data.allBrandList.find(v => v.sa_brandid == this.data.sa_brandid)
  178. this.setData({
  179. results: obj.results,
  180. resultsList: obj.resultsList
  181. })
  182. } else {
  183. if (this.data.allBrandList.length == 0) return;
  184. this.setData({
  185. sa_brandid: this.data.allBrandList[0].sa_brandid,
  186. results: this.data.allBrandList[0].results,
  187. resultsList: this.data.allBrandList[0].resultsList,
  188. })
  189. }
  190. };
  191. this.computeSum();
  192. },
  193. /* 切换选中项 */
  194. changeResults(e, my = false) {
  195. const {
  196. item
  197. } = my ? e : e.currentTarget.dataset;
  198. let results = this.data.results,
  199. resultsList = this.data.resultsList,
  200. sa_brandid = this.data.sa_brandid;
  201. if (sa_brandid && sa_brandid != item.sa_brandid) return;
  202. if (results.length == 0) {
  203. results.push(item.sa_shoppingcartid);
  204. resultsList.push(item)
  205. sa_brandid = item.sa_brandid;
  206. } else {
  207. let index = results.findIndex(v => v == item.sa_shoppingcartid)
  208. if (index == -1) {
  209. results.push(item.sa_shoppingcartid);
  210. resultsList.push(item)
  211. } else {
  212. results.splice(index, 1);
  213. resultsList.splice(index, 1);
  214. if (results.length == 0) sa_brandid = null;
  215. }
  216. };
  217. this.setData({
  218. results,
  219. sa_brandid
  220. })
  221. this.computeSum();
  222. },
  223. customization(e) {
  224. const {
  225. item
  226. } = e.target.dataset;
  227. getApp().globalData.customizedProduct = (item, custom) => {
  228. return new Promise((resolve) => {
  229. _Http.basic({
  230. "id": 20220924104302,
  231. "content": {
  232. "sa_shoppingcartid": item.sa_shoppingcartid,
  233. "qty": item.qty,
  234. ...custom
  235. },
  236. }).then(res => {
  237. console.log("修改定制", res)
  238. wx.showToast({
  239. title: res.msg != '成功' ? res.msg : '修改成功',
  240. icon: "none"
  241. });
  242. this.getList()
  243. resolve(true)
  244. })
  245. })
  246. }
  247. if (item) this.selectComponent("#Custom").onClick(item)
  248. },
  249. /* 计算总价/产品领域分类 */
  250. computeSum() {
  251. let results = this.data.results,
  252. resultsList = [],
  253. sum = 0,
  254. classList = [];
  255. if (results.length) results = results.filter(v => {
  256. let item = this.data.list.find(va => va.sa_shoppingcartid == v);
  257. if (item) {
  258. sum = currency(sum).add(currency(item.qty).multiply(item.gradeprice)).value;
  259. resultsList.push(item)
  260. /* 领域分类 */
  261. let index = classList.findIndex(value => value.type == item.tradefield_shoppingcart);
  262. if (index == -1) {
  263. classList.push({
  264. type: item.tradefield_shoppingcart,
  265. list: [item],
  266. name: item.tradefield_shoppingcart + "(1件商品)"
  267. })
  268. } else {
  269. classList[index].list.push(item)
  270. classList[index].name = classList[index].type + `(${classList[index].list.length}件商品)`
  271. }
  272. };
  273. return item
  274. });
  275. let sa_brandid = results.length ? this.data.sa_brandid : null;
  276. wx.setStorageSync('shopping1', {
  277. results,
  278. sa_brandid
  279. })
  280. let isAll = false;
  281. if (sa_brandid) {
  282. let brand = this.data.allBrandList.find(v => v.sa_brandid == sa_brandid)
  283. isAll = brand.results.length == results.length
  284. }
  285. this.setData({
  286. sum: CNY(sum),
  287. isAll,
  288. results,
  289. sa_brandid,
  290. resultsList,
  291. classList
  292. });
  293. },
  294. /* 删除产品 */
  295. deteleItem(e) {
  296. const {
  297. item
  298. } = e.currentTarget.dataset;
  299. wx.showModal({
  300. title: '提示',
  301. content: `是否确认删除${item.itemname}?`,
  302. complete: ({
  303. confirm
  304. }) => {
  305. e.detail.instance.close();
  306. if (confirm) _Http.basic({
  307. "id": 20220924095202,
  308. "content": {
  309. ...ids,
  310. "sa_shoppingcartids": [item.sa_shoppingcartid],
  311. }
  312. }).then(res => {
  313. wx.showToast({
  314. title: res.msg != '成功' ? res.msg : "删除成功",
  315. icon: "none"
  316. });
  317. if (res.msg != '成功') return;
  318. this.getList(true)
  319. getApp().globalData.getCollectCount()
  320. })
  321. }
  322. })
  323. },
  324. /* 输入框失去焦点调整数量 */
  325. inputBlur(e) {
  326. const {
  327. index
  328. } = e.currentTarget.dataset;
  329. let item = this.data.list[index];
  330. let qty = 0;
  331. if (item.orderminqty > e.detail.value) {
  332. wx.showToast({
  333. title: '输入数量低于最低起订量!',
  334. icon: "none"
  335. })
  336. qty = item.orderminqty;
  337. } else if (item.orderminqty < e.detail.value) {
  338. var currencyRounding = value => currency(value, {
  339. increment: item.orderaddqty
  340. });
  341. qty = currency(currencyRounding(currency(e.detail.value).subtract(item.orderminqty)).format()).add(item.orderminqty).value;
  342. } else {
  343. qty = e.detail.value;
  344. }
  345. this.setData({
  346. [`list[${index}].qty`]: 0
  347. });
  348. this.setData({
  349. [`list[${index}].qty`]: qty
  350. });
  351. this.computeSum();
  352. clearTimeout(downCount['count' + index])
  353. downCount['count' + index] = setTimeout(() => {
  354. _Http.basic({
  355. "id": 20220924104302,
  356. "content": {
  357. "sa_shoppingcartid": item.sa_shoppingcartid,
  358. "qty": item.qty,
  359. "width": item.width || 0,
  360. "length": item.length || 0
  361. },
  362. }, false).then(res => {
  363. console.log("修改数量", res)
  364. })
  365. }, 2000)
  366. },
  367. /* 步进器调整数量 */
  368. stepperChange(e) {
  369. const {
  370. index
  371. } = e.currentTarget.dataset;
  372. let item = this.data.list[index];
  373. if (e.type == 'plus') {
  374. item.qty += item.orderaddqty
  375. } else {
  376. item.qty -= item.orderaddqty
  377. }
  378. this.setData({
  379. [`list[${index}]`]: item
  380. })
  381. this.computeSum();
  382. clearTimeout(downCount['count' + index])
  383. downCount['count' + index] = setTimeout(() => {
  384. _Http.basic({
  385. "id": 20220924104302,
  386. "content": {
  387. "sa_shoppingcartid": item.sa_shoppingcartid,
  388. "qty": item.qty,
  389. "width": item.width || 0,
  390. "length": item.length || 0
  391. },
  392. }, false).then(res => {
  393. console.log("修改数量", res)
  394. })
  395. }, 2000)
  396. }
  397. })