index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. const _Http = getApp().globalData.http;
  2. let downCount = {};
  3. Page({
  4. data: {
  5. loading: true,
  6. list: [],
  7. results: [], //选中结果
  8. sa_brandid: null, //当前选中品牌id
  9. sum: 0, //价格合
  10. yfsum: 0, //运费合
  11. },
  12. onLoad(options) {
  13. this.getList()
  14. },
  15. /* 提交 */
  16. submit() {
  17. console.log("提交订单")
  18. },
  19. /* 获取列表 */
  20. getList() {
  21. _Http.basic({
  22. "id": 20220924095302,
  23. "content": {
  24. nocache: true,
  25. "pageNumber": 1,
  26. "pageSize": getApp().globalData.num + 5,
  27. "where": {
  28. "condition": ""
  29. }
  30. }
  31. }).then(res => {
  32. console.log('购物车列表', res)
  33. if (res.msg != '成功') return wx.showToast({
  34. title: res.msg,
  35. icon: "none"
  36. })
  37. this.setData({
  38. list: res.data,
  39. loading: false
  40. });
  41. if (wx.getStorageSync('shopping')) {
  42. this.setData({
  43. ...wx.getStorageSync('shopping')
  44. })
  45. this.computeSum();
  46. }
  47. })
  48. },
  49. /* 切换选中项 */
  50. changeResults(e, my = false) {
  51. const {
  52. item
  53. } = my ? e : e.currentTarget.dataset;
  54. let results = this.data.results,
  55. sa_brandid = this.data.sa_brandid;
  56. if (sa_brandid && sa_brandid != item.sa_brandid) return;
  57. if (results.length == 0) {
  58. results.push(item.sa_shoppingcartid);
  59. sa_brandid = item.sa_brandid;
  60. } else {
  61. let index = results.findIndex(v => v == item.sa_shoppingcartid)
  62. if (index == -1) {
  63. results.push(item.sa_shoppingcartid);
  64. } else {
  65. results.splice(index, 1);
  66. if (results.length == 0) sa_brandid = null;
  67. }
  68. };
  69. this.setData({
  70. results,
  71. sa_brandid
  72. })
  73. this.computeSum();
  74. },
  75. /* 计算总价 */
  76. computeSum() {
  77. let results = this.data.results,
  78. sum = 0,
  79. yfsum = 0,
  80. deteleList = [];
  81. if (results.length) results.forEach((v, i) => {
  82. let item = this.data.list.find(va => va.sa_shoppingcartid == v);
  83. if (item) {
  84. sum += (item.qty * item.gradeprice).toFixed(2) - 0;
  85. } else {
  86. deteleList.push(i)
  87. }
  88. });
  89. if (deteleList.length) deteleList.forEach(v => {
  90. results.splice(v, 1);
  91. });
  92. wx.setStorageSync('shopping', {
  93. results,
  94. sa_brandid: this.data.sa_brandid
  95. })
  96. this.setData({
  97. sum,
  98. yfsum,
  99. results
  100. })
  101. },
  102. /* 删除产品 */
  103. deteleItem(e) {
  104. const {
  105. item
  106. } = e.currentTarget.dataset;
  107. _Http.basic({
  108. "id": 20220924095202,
  109. "content": {
  110. "sa_shoppingcartids": [item.sa_shoppingcartid]
  111. }
  112. }).then(res => {
  113. if (res.msg != '成功') return wx.showToast({
  114. title: res.msg,
  115. icon: "none"
  116. });
  117. this.setData({
  118. list: this.data.list.filter(v => v.sa_shoppingcartid != item.sa_shoppingcartid)
  119. })
  120. if (this.data.results.some(v => v == item.sa_shoppingcartid)) this.changeResults({
  121. item
  122. }, true);
  123. })
  124. },
  125. /* 步进器调整 */
  126. stepperChange(e) {
  127. const {
  128. index
  129. } = e.currentTarget.dataset;
  130. let item = this.data.list[index];
  131. item.qty = e.detail;
  132. this.setData({
  133. [`list[${index}]`]: item
  134. })
  135. this.computeSum();
  136. clearTimeout(downCount['count' + index])
  137. downCount['count' + index] = setTimeout(() => {
  138. _Http.basic({
  139. "id": 20220924104302,
  140. "content": {
  141. "sa_shoppingcartid": item.sa_shoppingcartid,
  142. "qty": item.qty
  143. },
  144. }, false).then(res => {
  145. console.log("修改数量", res)
  146. })
  147. }, 2000)
  148. },
  149. })