index.js 7.6 KB

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