index.js 7.4 KB

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