index.js 9.1 KB

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