index.js 6.6 KB

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