index.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. data: {
  14. list: [],
  15. results: [], //选中结果
  16. sa_brandid: null, //当前选中品牌id
  17. classList: [], //生成订单时所选
  18. sum: 0, //价格合
  19. },
  20. methods: {
  21. init() {
  22. this.getList()
  23. },
  24. /* 获取列表 */
  25. getList() {
  26. _Http.basic({
  27. "id": 20220924095302,
  28. "content": {
  29. nocache: true,
  30. istool: 0,
  31. "pageNumber": 1,
  32. "pageSize": getApp().globalData.collectCount + 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. let list = [],
  44. allBrandList = [];
  45. list = res.data.map(v => {
  46. v.showPrice = CNY(v.oldprice)
  47. let obj = allBrandList.find(s => s.sa_brandid == v.sa_brandid);
  48. if (obj) {
  49. obj.results.push(v.sa_shoppingcartid)
  50. } else {
  51. allBrandList.push({
  52. brandname: v.brandname,
  53. sa_brandid: v.sa_brandid,
  54. results: [v.sa_shoppingcartid],
  55. })
  56. }
  57. return v
  58. });
  59. this.setData({
  60. list,
  61. allBrandList,
  62. isGet: true
  63. });
  64. if (wx.getStorageSync('shopping')) {
  65. this.setData({
  66. ...wx.getStorageSync('shopping')
  67. });
  68. this.computeSum();
  69. }
  70. })
  71. },
  72. clickBut(e) {
  73. this.data.classList.length >= 2 ? wx.showToast({
  74. title: '请选择订单领域(订单只允许同品牌/同领域的商品)',
  75. icon: "none",
  76. duration: 3000
  77. }) : this.handleSubmit(0);
  78. },
  79. /* 提交 */
  80. submit(e) {
  81. this.handleSubmit(e.detail.value)
  82. },
  83. handleSubmit(index) {
  84. let data = this.data.classList[index];
  85. _Http.basic({
  86. "id": 20221128183202,
  87. "content": {
  88. istool: 0,
  89. "tradefield": data.type, //必选
  90. "items": data.list.map(v => {
  91. return {
  92. "sa_orderitemsid": 0,
  93. "itemid": v.itemid,
  94. "sa_brandid": v.sa_brandid,
  95. "qty": v.qty
  96. }
  97. })
  98. }
  99. }).then(res => {
  100. console.log("转化订单", res)
  101. if (res.msg != '成功') return wx.showToast({
  102. title: res.msg,
  103. icon: "none"
  104. });
  105. getApp().globalData.getCollectCount()
  106. wx.showModal({
  107. title: '提示',
  108. content: '生成成功!是否立即前往',
  109. complete: (s) => {
  110. if (s.confirm) {
  111. wx.navigateTo({
  112. url: '/packageA/orderForm/detail?id=' + res.data.sa_orderid,
  113. })
  114. }
  115. }
  116. });
  117. this.getList();
  118. })
  119. },
  120. /* 是否选择全部 */
  121. setIsAll() {
  122. let isAll = this.data.isAll;
  123. //取消全选
  124. if (isAll) {
  125. this.setData({
  126. sa_brandid: null,
  127. results: []
  128. })
  129. } else {
  130. //已选品牌产品情况下
  131. if (this.data.sa_brandid) {
  132. let obj = this.data.allBrandList.find(v => v.sa_brandid == this.data.sa_brandid)
  133. this.setData({
  134. results: obj.results
  135. })
  136. } else {
  137. if (this.data.allBrandList.length == 0) return;
  138. this.setData({
  139. sa_brandid: this.data.allBrandList[0].sa_brandid,
  140. results: this.data.allBrandList[0].results
  141. })
  142. }
  143. };
  144. this.computeSum();
  145. },
  146. /* 切换选中项 */
  147. changeResults(e, my = false) {
  148. const {
  149. item
  150. } = my ? e : e.currentTarget.dataset;
  151. let results = this.data.results,
  152. sa_brandid = this.data.sa_brandid;
  153. if (sa_brandid && sa_brandid != item.sa_brandid) return;
  154. if (results.length == 0) {
  155. results.push(item.sa_shoppingcartid);
  156. sa_brandid = item.sa_brandid;
  157. } else {
  158. let index = results.findIndex(v => v == item.sa_shoppingcartid)
  159. if (index == -1) {
  160. results.push(item.sa_shoppingcartid);
  161. } else {
  162. results.splice(index, 1);
  163. if (results.length == 0) sa_brandid = null;
  164. }
  165. };
  166. this.setData({
  167. results,
  168. sa_brandid
  169. })
  170. this.computeSum();
  171. },
  172. /* 计算总价/产品领域分类 */
  173. computeSum() {
  174. let results = this.data.results,
  175. sum = 0,
  176. classList = [];
  177. if (results.length) results = results.filter(v => {
  178. let item = this.data.list.find(va => va.sa_shoppingcartid == v);
  179. if (item) {
  180. sum = currency(sum).add(currency(item.qty).multiply(item.oldprice)).value;
  181. /* 领域分类 */
  182. let index = classList.findIndex(value => value.type == item.tradefield_shoppingcart);
  183. if (index == -1) {
  184. classList.push({
  185. type: item.tradefield_shoppingcart,
  186. list: [item],
  187. name: item.tradefield_shoppingcart + "(1件商品)"
  188. })
  189. } else {
  190. classList[index].list.push(item)
  191. classList[index].name = classList[index].type + `(${classList[index].list.length}件商品)`
  192. }
  193. };
  194. return item
  195. });
  196. let sa_brandid = results.length ? this.data.sa_brandid : null;
  197. wx.setStorageSync('shopping', {
  198. results,
  199. sa_brandid
  200. })
  201. let isAll = false;
  202. if (sa_brandid) {
  203. let brand = this.data.allBrandList.find(v => v.sa_brandid == sa_brandid)
  204. isAll = brand.results.length == results.length
  205. }
  206. console.log(results)
  207. this.setData({
  208. sum: CNY(sum),
  209. isAll,
  210. results,
  211. sa_brandid,
  212. classList
  213. });
  214. },
  215. /* 删除产品 */
  216. deteleItem(e) {
  217. const {
  218. item
  219. } = e.currentTarget.dataset;
  220. _Http.basic({
  221. "id": 20220924095202,
  222. "content": {
  223. "sa_shoppingcartids": [item.sa_shoppingcartid]
  224. }
  225. }).then(res => {
  226. if (res.msg != '成功') return wx.showToast({
  227. title: res.msg,
  228. icon: "none"
  229. });
  230. this.setData({
  231. list: this.data.list.filter(v => v.sa_shoppingcartid != item.sa_shoppingcartid)
  232. })
  233. if (this.data.results.some(v => v == item.sa_shoppingcartid)) this.changeResults({
  234. item
  235. }, true);
  236. getApp().globalData.getCollectCount()
  237. })
  238. },
  239. /* 输入框失去焦点调整数量 */
  240. inputBlur(e) {
  241. const {
  242. index
  243. } = e.currentTarget.dataset;
  244. let item = this.data.list[index];
  245. let qty = 0;
  246. if (item.orderminqty > e.detail.value) {
  247. wx.showToast({
  248. title: '输入数量低于最低起订量!',
  249. icon: "none"
  250. })
  251. qty = item.orderminqty;
  252. } else if (item.orderminqty < e.detail.value) {
  253. var currencyRounding = value => currency(value, {
  254. increment: item.orderaddqty
  255. });
  256. qty = currency(currencyRounding(currency(e.detail.value).subtract(item.orderminqty)).format()).add(item.orderminqty).value;
  257. } else {
  258. qty = e.detail.value;
  259. }
  260. this.setData({
  261. [`list[${index}].qty`]: 0
  262. });
  263. this.setData({
  264. [`list[${index}].qty`]: qty
  265. });
  266. this.computeSum();
  267. clearTimeout(downCount['count' + index])
  268. downCount['count' + index] = setTimeout(() => {
  269. _Http.basic({
  270. "id": 20220924104302,
  271. "content": {
  272. "sa_shoppingcartid": item.sa_shoppingcartid,
  273. "qty": item.qty
  274. },
  275. }, false).then(res => {
  276. console.log("修改数量", res)
  277. })
  278. }, 2000)
  279. },
  280. /* 步进器调整数量 */
  281. stepperChange(e) {
  282. const {
  283. index
  284. } = e.currentTarget.dataset;
  285. let item = this.data.list[index];
  286. if (e.type == 'plus') {
  287. item.qty += item.orderaddqty
  288. } else {
  289. item.qty -= item.orderaddqty
  290. }
  291. this.setData({
  292. [`list[${index}]`]: item
  293. })
  294. this.computeSum();
  295. clearTimeout(downCount['count' + index])
  296. downCount['count' + index] = setTimeout(() => {
  297. _Http.basic({
  298. "id": 20220924104302,
  299. "content": {
  300. "sa_shoppingcartid": item.sa_shoppingcartid,
  301. "qty": item.qty
  302. },
  303. }, false).then(res => {
  304. console.log("修改数量", res)
  305. })
  306. }, 2000)
  307. }
  308. }
  309. })