index.js 8.8 KB

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