index.js 7.7 KB

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