index.js 5.9 KB

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