index.js 12 KB

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