index.js 9.2 KB

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