index.js 8.5 KB

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