index.js 8.7 KB

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