index.js 5.9 KB

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