index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. classList: [], //生成订单时所选
  12. },
  13. onLoad(options) {
  14. this.getList()
  15. },
  16. clickBut(e) {
  17. wx.showToast({
  18. title: '请选择订单领域(订单只允许同品牌/同领域的商品)',
  19. icon: "none",
  20. duration: 3000
  21. })
  22. },
  23. /* 提交 */
  24. submit(e) {
  25. let data = this.data.classList[e.detail.value];
  26. _Http.basic({
  27. "id": 20221128183202,
  28. "content": {
  29. "tradefield": data.type, //必选
  30. "items": data.list.map(v => {
  31. return {
  32. "sa_orderitemsid": 0,
  33. "itemid": v.itemid,
  34. "sa_brandid": v.sa_brandid,
  35. "qty": v.qty
  36. }
  37. })
  38. }
  39. }).then(res => {
  40. console.log("转化订单", res)
  41. if (res.msg != '成功') return wx.showToast({
  42. title: 'res.msg',
  43. icon: "none"
  44. });
  45. wx.showModal({
  46. title: '提示',
  47. content: '生成成功!是否立即前往',
  48. complete: (s) => {
  49. if (s.confirm) {
  50. wx.navigateTo({
  51. url: '/packageA/orderForm/detail?id=' + res.data.sa_orderid,
  52. })
  53. }
  54. }
  55. });
  56. this.getList();
  57. })
  58. },
  59. /* 获取列表 */
  60. getList() {
  61. _Http.basic({
  62. "id": 20220924095302,
  63. "content": {
  64. nocache: true,
  65. "pageNumber": 1,
  66. "pageSize": getApp().globalData.num + 5,
  67. "where": {
  68. "condition": ""
  69. }
  70. }
  71. }).then(res => {
  72. console.log('购物车列表', res)
  73. if (res.msg != '成功') return wx.showToast({
  74. title: res.msg,
  75. icon: "none"
  76. })
  77. this.setData({
  78. list: res.data,
  79. loading: false
  80. });
  81. if (wx.getStorageSync('shopping')) {
  82. this.setData({
  83. ...wx.getStorageSync('shopping')
  84. });
  85. this.computeSum();
  86. }
  87. })
  88. },
  89. /* 切换选中项 */
  90. changeResults(e, my = false) {
  91. const {
  92. item
  93. } = my ? e : e.currentTarget.dataset;
  94. let results = this.data.results,
  95. sa_brandid = this.data.sa_brandid;
  96. if (sa_brandid && sa_brandid != item.sa_brandid) return;
  97. if (results.length == 0) {
  98. results.push(item.sa_shoppingcartid);
  99. sa_brandid = item.sa_brandid;
  100. } else {
  101. let index = results.findIndex(v => v == item.sa_shoppingcartid)
  102. if (index == -1) {
  103. results.push(item.sa_shoppingcartid);
  104. } else {
  105. results.splice(index, 1);
  106. if (results.length == 0) sa_brandid = null;
  107. }
  108. };
  109. this.setData({
  110. results,
  111. sa_brandid
  112. })
  113. this.computeSum();
  114. },
  115. /* 计算总价/产品领域分类 */
  116. computeSum() {
  117. let results = this.data.results,
  118. sum = 0,
  119. yfsum = 0,
  120. deteleList = [],
  121. classList = [];
  122. if (results.length) results.forEach((v, i) => {
  123. let item = this.data.list.find(va => va.sa_shoppingcartid == v);
  124. if (item) {
  125. sum += (item.qty * item.gradeprice).toFixed(2) - 0;
  126. /* 领域分类 */
  127. let index = classList.findIndex(value => value.type == item.tradefield_shoppingcart);
  128. if (index == -1) {
  129. classList.push({
  130. type: item.tradefield_shoppingcart,
  131. list: [item],
  132. name: item.tradefield_shoppingcart + "(1件商品)"
  133. })
  134. } else {
  135. classList[index].list.push(item)
  136. classList[index].name = classList[index].type + `(${classList[index].list.length}件商品)`
  137. }
  138. } else {
  139. deteleList.push(i)
  140. }
  141. });
  142. if (deteleList.length) {
  143. deteleList.forEach(v => {
  144. results.splice(v, 1);
  145. });
  146. getApp().globalData.num = getApp().globalData.num - deteleList.length;
  147. };
  148. let sa_brandid = results.length ? this.data.sa_brandid : null;
  149. wx.setStorageSync('shopping', {
  150. results,
  151. sa_brandid
  152. })
  153. this.setData({
  154. sum,
  155. yfsum,
  156. results,
  157. sa_brandid,
  158. classList
  159. })
  160. },
  161. /* 删除产品 */
  162. deteleItem(e) {
  163. const {
  164. item
  165. } = e.currentTarget.dataset;
  166. _Http.basic({
  167. "id": 20220924095202,
  168. "content": {
  169. "sa_shoppingcartids": [item.sa_shoppingcartid]
  170. }
  171. }).then(res => {
  172. if (res.msg != '成功') return wx.showToast({
  173. title: res.msg,
  174. icon: "none"
  175. });
  176. this.setData({
  177. list: this.data.list.filter(v => v.sa_shoppingcartid != item.sa_shoppingcartid)
  178. })
  179. if (this.data.results.some(v => v == item.sa_shoppingcartid)) this.changeResults({
  180. item
  181. }, true);
  182. getApp().globalData.num = getApp().globalData.num - 1;
  183. })
  184. },
  185. /* 步进器调整 */
  186. stepperChange(e) {
  187. const {
  188. index
  189. } = e.currentTarget.dataset;
  190. let item = this.data.list[index];
  191. item.qty = e.detail;
  192. this.setData({
  193. [`list[${index}]`]: item
  194. })
  195. this.computeSum();
  196. clearTimeout(downCount['count' + index])
  197. downCount['count' + index] = setTimeout(() => {
  198. _Http.basic({
  199. "id": 20220924104302,
  200. "content": {
  201. "sa_shoppingcartid": item.sa_shoppingcartid,
  202. "qty": item.qty
  203. },
  204. }, false).then(res => {
  205. console.log("修改数量", res)
  206. })
  207. }, 2000)
  208. },
  209. })