index.js 7.8 KB

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