index.js 8.9 KB

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