index.js 9.2 KB

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