index.js 12 KB

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