index.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. type: "标准订单",
  90. "tradefield": data.type, //必选
  91. "items": data.list.map(v => {
  92. return {
  93. "sa_orderitemsid": 0,
  94. "itemid": v.itemid,
  95. "sa_brandid": v.sa_brandid,
  96. "qty": v.qty
  97. }
  98. })
  99. }
  100. }).then(res => {
  101. console.log("转化订单", res)
  102. if (res.msg != '成功') return wx.showToast({
  103. title: res.msg,
  104. icon: "none"
  105. });
  106. getApp().globalData.getCollectCount()
  107. wx.showModal({
  108. title: '提示',
  109. content: '生成成功!是否立即前往',
  110. complete: (s) => {
  111. if (s.confirm) {
  112. wx.navigateTo({
  113. url: '/packageA/orderForm/detail?id=' + res.data.sa_orderid,
  114. })
  115. }
  116. }
  117. });
  118. this.getList();
  119. })
  120. },
  121. /* 是否选择全部 */
  122. setIsAll() {
  123. let isAll = this.data.isAll;
  124. //取消全选
  125. if (isAll) {
  126. this.setData({
  127. sa_brandid: null,
  128. results: []
  129. })
  130. } else {
  131. //已选品牌产品情况下
  132. if (this.data.sa_brandid) {
  133. let obj = this.data.allBrandList.find(v => v.sa_brandid == this.data.sa_brandid)
  134. this.setData({
  135. results: obj.results
  136. })
  137. } else {
  138. if (this.data.allBrandList.length == 0) return;
  139. this.setData({
  140. sa_brandid: this.data.allBrandList[0].sa_brandid,
  141. results: this.data.allBrandList[0].results
  142. })
  143. }
  144. };
  145. this.computeSum();
  146. },
  147. /* 切换选中项 */
  148. changeResults(e, my = false) {
  149. const {
  150. item
  151. } = my ? e : e.currentTarget.dataset;
  152. let results = this.data.results,
  153. sa_brandid = this.data.sa_brandid;
  154. if (sa_brandid && sa_brandid != item.sa_brandid) return;
  155. if (results.length == 0) {
  156. results.push(item.sa_shoppingcartid);
  157. sa_brandid = item.sa_brandid;
  158. } else {
  159. let index = results.findIndex(v => v == item.sa_shoppingcartid)
  160. if (index == -1) {
  161. results.push(item.sa_shoppingcartid);
  162. } else {
  163. results.splice(index, 1);
  164. if (results.length == 0) sa_brandid = null;
  165. }
  166. };
  167. this.setData({
  168. results,
  169. sa_brandid
  170. })
  171. this.computeSum();
  172. },
  173. /* 计算总价/产品领域分类 */
  174. computeSum() {
  175. let results = this.data.results,
  176. sum = 0,
  177. classList = [];
  178. if (results.length) results = results.filter(v => {
  179. let item = this.data.list.find(va => va.sa_shoppingcartid == v);
  180. if (item) {
  181. sum = currency(sum).add(currency(item.qty).multiply(item.oldprice)).value;
  182. /* 领域分类 */
  183. let index = classList.findIndex(value => value.type == item.tradefield_shoppingcart);
  184. if (index == -1) {
  185. classList.push({
  186. type: item.tradefield_shoppingcart,
  187. list: [item],
  188. name: item.tradefield_shoppingcart + "(1件商品)"
  189. })
  190. } else {
  191. classList[index].list.push(item)
  192. classList[index].name = classList[index].type + `(${classList[index].list.length}件商品)`
  193. }
  194. };
  195. return item
  196. });
  197. let sa_brandid = results.length ? this.data.sa_brandid : null;
  198. wx.setStorageSync('shopping', {
  199. results,
  200. sa_brandid
  201. })
  202. let isAll = false;
  203. if (sa_brandid) {
  204. let brand = this.data.allBrandList.find(v => v.sa_brandid == sa_brandid)
  205. isAll = brand.results.length == results.length
  206. }
  207. console.log(results)
  208. this.setData({
  209. sum: CNY(sum),
  210. isAll,
  211. results,
  212. sa_brandid,
  213. classList
  214. });
  215. },
  216. /* 删除产品 */
  217. deteleItem(e) {
  218. const {
  219. item
  220. } = e.currentTarget.dataset;
  221. _Http.basic({
  222. "id": 20220924095202,
  223. "content": {
  224. "sa_shoppingcartids": [item.sa_shoppingcartid]
  225. }
  226. }).then(res => {
  227. if (res.msg != '成功') return wx.showToast({
  228. title: res.msg,
  229. icon: "none"
  230. });
  231. this.setData({
  232. list: this.data.list.filter(v => v.sa_shoppingcartid != item.sa_shoppingcartid)
  233. })
  234. if (this.data.results.some(v => v == item.sa_shoppingcartid)) this.changeResults({
  235. item
  236. }, true);
  237. getApp().globalData.getCollectCount()
  238. })
  239. },
  240. /* 输入框失去焦点调整数量 */
  241. inputBlur(e) {
  242. const {
  243. index
  244. } = e.currentTarget.dataset;
  245. let item = this.data.list[index];
  246. let qty = 0;
  247. if (item.orderminqty > e.detail.value) {
  248. wx.showToast({
  249. title: '输入数量低于最低起订量!',
  250. icon: "none"
  251. })
  252. qty = item.orderminqty;
  253. } else if (item.orderminqty < e.detail.value) {
  254. var currencyRounding = value => currency(value, {
  255. increment: item.orderaddqty
  256. });
  257. qty = currency(currencyRounding(currency(e.detail.value).subtract(item.orderminqty)).format()).add(item.orderminqty).value;
  258. } else {
  259. qty = e.detail.value;
  260. }
  261. this.setData({
  262. [`list[${index}].qty`]: 0
  263. });
  264. this.setData({
  265. [`list[${index}].qty`]: qty
  266. });
  267. this.computeSum();
  268. clearTimeout(downCount['count' + index])
  269. downCount['count' + index] = setTimeout(() => {
  270. _Http.basic({
  271. "id": 20220924104302,
  272. "content": {
  273. "sa_shoppingcartid": item.sa_shoppingcartid,
  274. "qty": item.qty
  275. },
  276. }, false).then(res => {
  277. console.log("修改数量", res)
  278. })
  279. }, 2000)
  280. },
  281. /* 步进器调整数量 */
  282. stepperChange(e) {
  283. const {
  284. index
  285. } = e.currentTarget.dataset;
  286. let item = this.data.list[index];
  287. if (e.type == 'plus') {
  288. item.qty += item.orderaddqty
  289. } else {
  290. item.qty -= item.orderaddqty
  291. }
  292. this.setData({
  293. [`list[${index}]`]: item
  294. })
  295. this.computeSum();
  296. clearTimeout(downCount['count' + index])
  297. downCount['count' + index] = setTimeout(() => {
  298. _Http.basic({
  299. "id": 20220924104302,
  300. "content": {
  301. "sa_shoppingcartid": item.sa_shoppingcartid,
  302. "qty": item.qty
  303. },
  304. }, false).then(res => {
  305. console.log("修改数量", res)
  306. })
  307. }, 2000)
  308. }
  309. }
  310. })