index.js 10 KB

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