index.js 10 KB

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