index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. },
  12. onLoad(options) {
  13. this.getList()
  14. },
  15. /* 提交 */
  16. submit() {
  17. console.log("提交订单", this.data.results)
  18. let obj = {};
  19. let list = this.data.results.map(v => {
  20. let data = this.data.list.find(value => value.sa_shoppingcartid == v);
  21. //根据领域分类
  22. if (obj[data.brandname]) {
  23. obj[data.brandname].list.push(data)
  24. } else {
  25. obj[data.brandname] = {
  26. type: data.brandname,
  27. list: [data]
  28. }
  29. };
  30. return data;
  31. });
  32. _Http.basic({
  33. "id": 20221128183202,
  34. "content": {
  35. "tradefield": "消防", //必选
  36. "items": list.map(v => {
  37. return {
  38. "sa_orderitemsid": 0,
  39. "itemid": v.itemid,
  40. "sa_brandid": v.sa_brandid,
  41. "qty": v.qty
  42. }
  43. })
  44. }
  45. }).then(res => {
  46. console.log("转化订单", res)
  47. if (res.msg != '成功') return wx.showToast({
  48. title: 'res.msg',
  49. icon: "none"
  50. });
  51. wx.showModal({
  52. title: '提示',
  53. content: '生成成功!是否立即前往',
  54. complete: (s) => {
  55. if (s.confirm) {
  56. wx.navigateTo({
  57. url: '/packageA/orderForm/detail?id=' + res.data.sa_orderid,
  58. })
  59. }
  60. }
  61. });
  62. this.getList();
  63. })
  64. },
  65. /* 获取列表 */
  66. getList() {
  67. _Http.basic({
  68. "id": 20220924095302,
  69. "content": {
  70. nocache: true,
  71. "pageNumber": 1,
  72. "pageSize": getApp().globalData.num + 5,
  73. "where": {
  74. "condition": ""
  75. }
  76. }
  77. }).then(res => {
  78. console.log('购物车列表', res)
  79. if (res.msg != '成功') return wx.showToast({
  80. title: res.msg,
  81. icon: "none"
  82. })
  83. this.setData({
  84. list: res.data,
  85. loading: false
  86. });
  87. if (wx.getStorageSync('shopping')) {
  88. this.setData({
  89. ...wx.getStorageSync('shopping')
  90. });
  91. this.computeSum();
  92. }
  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. 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. } else {
  132. deteleList.push(i)
  133. }
  134. });
  135. if (deteleList.length) {
  136. deteleList.forEach(v => {
  137. results.splice(v, 1);
  138. });
  139. getApp().globalData.num = getApp().globalData.num - deteleList.length;
  140. };
  141. let sa_brandid = results.length ? this.data.sa_brandid : null;
  142. wx.setStorageSync('shopping', {
  143. results,
  144. sa_brandid
  145. })
  146. this.setData({
  147. sum,
  148. yfsum,
  149. results,
  150. sa_brandid
  151. })
  152. },
  153. /* 删除产品 */
  154. deteleItem(e) {
  155. const {
  156. item
  157. } = e.currentTarget.dataset;
  158. _Http.basic({
  159. "id": 20220924095202,
  160. "content": {
  161. "sa_shoppingcartids": [item.sa_shoppingcartid]
  162. }
  163. }).then(res => {
  164. if (res.msg != '成功') return wx.showToast({
  165. title: res.msg,
  166. icon: "none"
  167. });
  168. this.setData({
  169. list: this.data.list.filter(v => v.sa_shoppingcartid != item.sa_shoppingcartid)
  170. })
  171. if (this.data.results.some(v => v == item.sa_shoppingcartid)) this.changeResults({
  172. item
  173. }, true);
  174. getApp().globalData.num = getApp().globalData.num - 1;
  175. })
  176. },
  177. /* 步进器调整 */
  178. stepperChange(e) {
  179. const {
  180. index
  181. } = e.currentTarget.dataset;
  182. let item = this.data.list[index];
  183. item.qty = e.detail;
  184. this.setData({
  185. [`list[${index}]`]: item
  186. })
  187. this.computeSum();
  188. clearTimeout(downCount['count' + index])
  189. downCount['count' + index] = setTimeout(() => {
  190. _Http.basic({
  191. "id": 20220924104302,
  192. "content": {
  193. "sa_shoppingcartid": item.sa_shoppingcartid,
  194. "qty": item.qty
  195. },
  196. }, false).then(res => {
  197. console.log("修改数量", res)
  198. })
  199. }, 2000)
  200. },
  201. })