index.js 7.6 KB

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