index.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. wx.showModal({
  154. title: '提示',
  155. content: '生成成功!是否立即前往',
  156. complete: (s) => {
  157. if (s.confirm) {
  158. wx.navigateTo({
  159. url: '/packageA/orderForm/detail?id=' + res.data.sa_orderid,
  160. })
  161. }
  162. }
  163. });
  164. that.getList();
  165. })
  166. }
  167. }
  168. })
  169. },
  170. /* 是否选择全部 */
  171. setIsAll() {
  172. let isAll = this.data.isAll;
  173. //取消全选
  174. if (isAll) {
  175. this.setData({
  176. sa_brandid: null,
  177. results: []
  178. })
  179. } else {
  180. //已选品牌产品情况下
  181. if (this.data.sa_brandid) {
  182. let obj = this.data.allBrandList.find(v => v.sa_brandid == this.data.sa_brandid)
  183. this.setData({
  184. results: obj.results
  185. })
  186. } else {
  187. if (this.data.allBrandList.length == 0) return;
  188. this.setData({
  189. sa_brandid: this.data.allBrandList[0].sa_brandid,
  190. results: this.data.allBrandList[0].results
  191. })
  192. }
  193. };
  194. this.computeSum();
  195. },
  196. /* 切换选中项 */
  197. changeResults(e, my = false) {
  198. const {
  199. item
  200. } = my ? e : e.currentTarget.dataset;
  201. let results = this.data.results,
  202. sa_brandid = this.data.sa_brandid;
  203. if (sa_brandid && sa_brandid != item.sa_brandid) return;
  204. if (results.length == 0) {
  205. results.push(item.sa_shoppingcartid);
  206. sa_brandid = item.sa_brandid;
  207. } else {
  208. let index = results.findIndex(v => v == item.sa_shoppingcartid)
  209. if (index == -1) {
  210. results.push(item.sa_shoppingcartid);
  211. } else {
  212. results.splice(index, 1);
  213. if (results.length == 0) sa_brandid = null;
  214. }
  215. };
  216. this.setData({
  217. results,
  218. sa_brandid
  219. })
  220. this.computeSum();
  221. },
  222. customization(e) {
  223. const {
  224. item
  225. } = e.target.dataset;
  226. if (item) this.selectComponent("#Custom").onClick(item)
  227. },
  228. /* 计算总价/产品领域分类 */
  229. computeSum() {
  230. let results = this.data.results,
  231. sum = 0,
  232. classList = [];
  233. if (results.length) results = results.filter(v => {
  234. let item = this.data.list.find(va => va.sa_shoppingcartid == v);
  235. if (item) {
  236. console.log(item.gradeprice)
  237. sum = currency(sum).add(currency(item.qty).multiply(item.gradeprice)).value;
  238. /* 领域分类 */
  239. let index = classList.findIndex(value => value.type == item.tradefield_shoppingcart);
  240. if (index == -1) {
  241. classList.push({
  242. type: item.tradefield_shoppingcart,
  243. list: [item],
  244. name: item.tradefield_shoppingcart + "(1件商品)"
  245. })
  246. } else {
  247. classList[index].list.push(item)
  248. classList[index].name = classList[index].type + `(${classList[index].list.length}件商品)`
  249. }
  250. };
  251. return item
  252. });
  253. let sa_brandid = results.length ? this.data.sa_brandid : null;
  254. wx.setStorageSync('shopping1', {
  255. results,
  256. sa_brandid
  257. })
  258. let isAll = false;
  259. if (sa_brandid) {
  260. let brand = this.data.allBrandList.find(v => v.sa_brandid == sa_brandid)
  261. isAll = brand.results.length == results.length
  262. }
  263. console.log(sum)
  264. this.setData({
  265. sum: CNY(sum),
  266. isAll,
  267. results,
  268. sa_brandid,
  269. classList
  270. });
  271. },
  272. /* 删除产品 */
  273. deteleItem(e) {
  274. const {
  275. item
  276. } = e.currentTarget.dataset;
  277. wx.showModal({
  278. title: '提示',
  279. content: `是否确认删除${item.itemname}?`,
  280. complete: ({
  281. confirm
  282. }) => {
  283. e.detail.instance.close();
  284. if (confirm) _Http.basic({
  285. "id": 20220924095202,
  286. "content": {
  287. ...ids,
  288. "sa_shoppingcartids": [item.sa_shoppingcartid],
  289. }
  290. }).then(res => {
  291. wx.showToast({
  292. title: res.msg != '成功' ? res.msg : "删除成功",
  293. icon: "none"
  294. });
  295. if (res.msg != '成功') return;
  296. this.getList(true)
  297. })
  298. }
  299. })
  300. },
  301. /* 输入框失去焦点调整数量 */
  302. inputBlur(e) {
  303. const {
  304. index
  305. } = e.currentTarget.dataset;
  306. let item = this.data.list[index];
  307. let qty = 0;
  308. if (item.orderminqty > e.detail.value) {
  309. wx.showToast({
  310. title: '输入数量低于最低起订量!',
  311. icon: "none"
  312. })
  313. qty = item.orderminqty;
  314. } else if (item.orderminqty < e.detail.value) {
  315. var currencyRounding = value => currency(value, {
  316. increment: item.orderaddqty
  317. });
  318. qty = currency(currencyRounding(currency(e.detail.value).subtract(item.orderminqty)).format()).add(item.orderminqty).value;
  319. } else {
  320. qty = e.detail.value;
  321. }
  322. this.setData({
  323. [`list[${index}].qty`]: 0
  324. });
  325. this.setData({
  326. [`list[${index}].qty`]: qty
  327. });
  328. this.computeSum();
  329. clearTimeout(downCount['count' + index])
  330. downCount['count' + index] = setTimeout(() => {
  331. _Http.basic({
  332. "id": 20220924104302,
  333. "content": {
  334. "sa_shoppingcartid": item.sa_shoppingcartid,
  335. "qty": item.qty,
  336. "width": item.width || 0,
  337. "length": item.length || 0
  338. },
  339. }, false).then(res => {
  340. console.log("修改数量", res)
  341. })
  342. }, 2000)
  343. },
  344. /* 步进器调整数量 */
  345. stepperChange(e) {
  346. const {
  347. index
  348. } = e.currentTarget.dataset;
  349. let item = this.data.list[index];
  350. if (e.type == 'plus') {
  351. item.qty += item.orderaddqty
  352. } else {
  353. item.qty -= item.orderaddqty
  354. }
  355. this.setData({
  356. [`list[${index}]`]: item
  357. })
  358. this.computeSum();
  359. clearTimeout(downCount['count' + index])
  360. downCount['count' + index] = setTimeout(() => {
  361. _Http.basic({
  362. "id": 20220924104302,
  363. "content": {
  364. "sa_shoppingcartid": item.sa_shoppingcartid,
  365. "qty": item.qty,
  366. "width": item.width || 0,
  367. "length": item.length || 0
  368. },
  369. }, false).then(res => {
  370. console.log("修改数量", res)
  371. })
  372. }, 2000)
  373. }
  374. })