index.js 8.6 KB

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