index.js 9.6 KB

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