index.js 11 KB

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