index.js 6.6 KB

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