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