Sanakey
2020-10-13 245dd6f1b9a206019ba1680b5214d3bbcd870d72
提交 | 用户 | age
492b82 1 // pages/posterList/posterList.js
S 2 // import Notify from '../../src/vant-weapp/notify/notify';
92f679 3 import utils from "../../utils/util";
S 4 import Dialog from "../../src/vant-weapp/dialog/dialog";
492b82 5 const app = getApp();
S 6
7 Page({
8   /**
9    * 页面的初始数据
10    */
11   data: {
12     activeKey: 0,
13     activeTabIndex: 0,
14     windowHeight: 600,
92f679 15     selectedTag: "全部",
492b82 16     isShowMyPoster: false,
S 17     isHiddenTypePoster: false,
92f679 18     scrollTop: 0,
245dd6 19     isFirstShow: true
92f679 20   },
S 21
22   onClickNavBarLeft() {
23     wx.reLaunch({
24       url: "../user/user",
25     });
492b82 26   },
S 27
28   /**
29    * 生命周期函数--监听页面加载
30    */
31   onLoad: async function (options) {
32     // console.log(wx.$http)
92f679 33     console.log("。。。首页onload开始了。。。");
S 34     if (!app.globalData.openID || !app.globalData.FromUserId) {
35       app.globalData.indexCallback = async (res) => {
36         // wx.setNavigationBarTitle({
37         //   title: app.globalData.shopName || "",
38         // });
39         this.setData({
40           navTitle: app.globalData.shopName || "首页",
41           avatarUrl: app.globalData.avatarUrl,
42         });
43         await this.setLeftMenuData();
44         await this.setGoodsData(this.data.menuList[0].MatGroup);
45         await this.setCartList();
46       };
47     } else {
48       await this.setLeftMenuData();
49       await this.setGoodsData(this.data.menuList[0].MatGroup);
50       await this.setCartList();
51     }
52   },
53
54   async setGoodsData(currentId) {
55     let [err, res] = await this.getGoodsList(currentId);
56     if (err) {
57       console.log("出错了。。。", err);
492b82 58       return;
S 59     }
60     this.setData({
92f679 61       goodsList: res.list,
492b82 62     });
92f679 63     console.log("商品List", res.list);
S 64   },
65
66   async getGoodsList(currentId) {
67     let [err, res] = await utils.to(
68       wx.$http.request({
69         url:
70           "/shopping/getMatCodeList.do?fromdata=2&isShowSubMatGroup=true&path=" +
71           currentId,
72       })
73     );
74     return [err, res];
75   },
76
77   async setLeftMenuData() {
78     let [err, res] = await this.getLeftMenu();
79     if (err) {
80       console.log("出错了。。。", err);
81       return;
82     }
492b82 83     this.setData({
92f679 84       menuList: res.list,
S 85     });
86     console.log("左侧菜单List", res.list);
87   },
88   async getLeftMenu() {
89     let [err, res] = await utils.to(
90       wx.$http.request({
91         url: "/shopping/getMatGroupInHomePage.do?isShowMatGroup=true",
92       })
93     );
94     return [err, res];
492b82 95   },
S 96
97   // 左侧菜单点击
92f679 98   async goCurrentMenu(event) {
S 99     console.log(event);
492b82 100     // let tagList = this.getTagList(event.detail);
S 101     this.setData({
102       activeKey: event.detail,
92f679 103       // activeMatGroup:event.currentTarget.dataset.id
S 104     });
105     await this.setGoodsData(event.currentTarget.dataset.id);
106     this.handleGoodsListInCart();
107   },
108   openHideLayer() {
109     this.setData({
110       showHideLayer: true,
111     });
112   },
113   maskTap(event) {
114     this.setData({
115       showHideLayer: event.detail.isShow,
116     });
117   },
118   async getCartList() {
119     let [err, res] = await utils.to(
120       wx.$http.request({
121         url: "/shopping/cart.do?m=getCartList",
122       })
123     );
124     return [err, res];
492b82 125   },
S 126
92f679 127   async setCartList() {
S 128     let [err, res] = await this.getCartList();
129     if (err) {
130       console.log(err);
131       return;
132     }
133     console.log("购物车...", res);
134     this.setData({
135       cartList: res.list,
136       selectTotalAmount: res.SelectTotalAmount,
137       selectItems: res.SelectItems,
138       cartItems: res.CartItems,
139     });
245dd6 140     try {
S 141       wx.setTabBarBadge({
142         index: app.globalData.cartIndex,
143         text: res.list.length.toString()
144       })
145     } catch (error) {
146       console.log(error);
147       
148     }
92f679 149     this.handleGoodsListInCart();
492b82 150   },
S 151
92f679 152   handleGoodsListInCart() {
S 153     this.data.goodsList.map((goodsItem) => {
154       delete goodsItem.CartId;
155       delete goodsItem.selectedQuantity;
156       this.data.cartList.map((cartItem) => {
157         if (goodsItem.MatCode === cartItem.MatCode) {
158           goodsItem.CartId = cartItem.CartId;
159           goodsItem.selectedQuantity = cartItem.Quantity;
160         }
161       });
162     });
163     console.log("处理后。。", this.data.goodsList);
164     this.setData({
165       goodsList: this.data.goodsList,
166     });
167   },
492b82 168
92f679 169   async addCartItem(event) {
S 170     const matcode = event.currentTarget.dataset.matcode;
171     let [err, res] = await this.addCartItemRequest(matcode);
172     if (err) {
173       return;
174     }
175     console.log(res);
176     this.setCartList();
177   },
178   async addCartItemRequest(matcode) {
179     let [err, res] = await utils.to(
180       wx.$http.request({
181         url: "/shopping/cart.do",
182         data: {
183           m: "add",
184           matcode,
185           quantity: 1,
245dd6 186         },
S 187       })
188     );
189     return [err, res];
190   },
191
192   async checkedCartItems(cartId) {
193     let [err, res] = await utils.to(
194       wx.$http.request({
195         url: "/shopping/cart.do",
196         data: {
197           m: "checkbox",
198           checkbox: cartId,
92f679 199         },
S 200       })
201     );
202     return [err, res];
203   },
204
205   onDeleteCartItem(event) {
206     const { position, instance } = event.detail;
207     const cartId = event.currentTarget.dataset.id;
208     switch (position) {
209       // case "left":
210       // case "cell":
211       //   instance.close();
212       //   break;
213       case "right":
214         wx.showModal({
215           title: "提示",
216           content: "您确定要从购物车中删除该商品吗?",
217           success: async (res) => {
218             if (res.confirm) {
219               console.log("用户点击确定");
220               let [err, res] = await this.deleteCartItem(cartId);
221               if (err) {
222                 return;
223               }
224               console.log(res);
225               await this.setCartList();
226               instance.close();
227             } else if (res.cancel) {
228               console.log("用户点击取消");
229             }
230           },
231         });
232         break;
233     }
234   },
235   clearCart() {
236     const cartList = this.data.cartList;
237     wx.showModal({
238       title: "提示",
239       content: "您确定要清空购物车吗?",
240       success: async (res) => {
241         if (res.confirm) {
242           console.log("用户点击确定");
243           let [err, res] = await this.deleteCartItem(getAllCartId());
244           if (err) {
245             return;
246           }
247           console.log(res);
248           await this.setCartList();
249         } else if (res.cancel) {
250           console.log("用户点击取消");
251         }
252       },
253     });
254
255     function getAllCartId() {
256       let cartIdArr = [];
257       cartList.forEach((item) => {
258         cartIdArr.push(item.CartId);
259       });
260       return cartIdArr.join(";");
261     }
262   },
263   async deleteCartItem(cartId) {
264     let [err, res] = await utils.to(
265       wx.$http.request({
266         url: "/shopping/cart.do",
267         data: {
268           m: "del",
269           key: cartId,
270         },
271       })
272     );
273     return [err, res];
274   },
275
276   async onCartItemMinus(event) {
277     console.log(event);
278     const cartId = event.currentTarget.dataset.id;
279     let quantity = event.currentTarget.dataset.quantity;
280     quantity--;
281     if (quantity <= 0) {
282       let [err, res] = await this.deleteCartItem(cartId);
283       if (err) {
284         return;
492b82 285       }
92f679 286       console.log(res);
S 287       await this.setCartList();
288     } else {
289       await this.sendCartItemChange(cartId, quantity);
290     }
492b82 291   },
92f679 292   async onCartItemPlus(event) {
S 293     console.log(event);
294     const cartId = event.currentTarget.dataset.id;
295     let quantity = event.currentTarget.dataset.quantity;
296     quantity++;
297     if (cartId) {
298       await this.sendCartItemChange(cartId, quantity);
299     } else {
300       await this.addCartItem(event);
301     }
302   },
303   async onCartItemInputBlur(event) {
304     console.log(event);
305     const cartId = event.currentTarget.dataset.id;
306     const quantity = event.detail.value;
307     if (quantity <= 0) {
308       let [err, res] = await this.deleteCartItem(cartId);
309       if (err) {
310         return;
311       }
312       console.log(res);
313       await this.setCartList();
314     } else {
315       await this.sendCartItemChange(cartId, quantity);
316     }
317   },
245dd6 318   async submitCart(e) {
92f679 319     // console.log(e);
S 320     // if (e.detail.isAuthorize) {
321     //
322     // }
245dd6 323     const cartList = this.data.cartList;
S 324     
325     let cartIdList = cartList.map((item)=>{
326       return item.CartId
327     })
328     let allCartId = cartIdList.join(';');
329     console.log(allCartId);
330
331     let [err,res] = await this.checkedCartItems(allCartId);
332     if (err) {
333       return;
334     }
335     console.log(res);
336     
92f679 337     wx.navigateTo({
S 338       url: "../order/order",
339     });
340   },
341   // async onCartItemChange(event) {
342   //   console.log(event);
343   //   const cartId = event.currentTarget.dataset.id;
344   //   const quantity = event.currentTarget.dataset.quantity;
345   //   let [err, res] = await this.sendCartItemChange(cartId, quantity);
346   //   if (err) {
347   //     return;
348   //   }
349   //   console.log(res);
350   //   this.setCartList();
351   // },
352   async sendCartItemChange(cartId, quantity) {
353     let [err, res] = await utils.to(
354       wx.$http.request({
355         url: "/shopping/cart.do",
356         data: {
357           m: "edit",
358           [`quantity_${cartId}`]: quantity,
359         },
360       })
361     );
362     if (err) {
363       return;
364     }
365     console.log(res);
366     await this.setCartList();
367   },
492b82 368   /**
S 369    * 生命周期函数--监听页面初次渲染完成
370    */
371   onReady: function () {
372     var res = wx.getSystemInfoSync();
373     this.setData({
374       // 可使用窗口高度-状态栏的高度
245dd6 375       windowHeight: res.windowHeight - 105,
92f679 376     });
S 377     console.log(this.data.windowHeight);
492b82 378   },
S 379
380   /**
381    * 生命周期函数--监听页面显示
382    */
383   onShow: function () {
92f679 384     console.log("用户信息", app.globalData.userInfo);
S 385     this.setData({
386       userInfo: app.globalData.userInfo,
387     });
245dd6 388     if (!this.data.isFirstShow) {
S 389       this.setCartList();
390     }
492b82 391   },
S 392
393   /**
394    * 生命周期函数--监听页面隐藏
395    */
245dd6 396   onHide: function () {
S 397     this.setData({
398       isFirstShow: false
399     })
400   },
492b82 401
S 402   /**
403    * 生命周期函数--监听页面卸载
404    */
92f679 405   onUnload: function () {},
492b82 406
S 407   /**
408    * 用户点击右上角分享
409    */
410   // onShareAppMessage: function () {
411
412   // }
92f679 413 });