index.js 5.9 KB

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