Parcourir la source

小程序前端优化

hyq il y a 6 ans
Parent
commit
2d78ed8c3e

+ 186 - 188
wx-mall/pages/category/category.js

@@ -3,163 +3,161 @@ var goodsUtil = require('../../utils/goods.js');
 var api = require('../../config/api.js');
 
 Page({
-    data: {
-        // text:"这是一个页面"
-        navList: [],
-        goodsList: [],
-        id: 0,
-        goodsBizType: '00',
-        currentTab: 0,
-        navIndex: 0,
-        currentCategory: {},
-        scrollLeft: 0,
-        scrollTop: 0,
-        scrollHeight: 0,
-        page: 1,
-        size: 10,
-        showNavList: false,
-        footCart: {},
-        openAttr: false,
-        productList: {},
-        specificationList: {},
-        checkedSpecText: '请选择规格数量',
-        number: 1
-    },
-    toggleNav() {
-        this.setData({
-            showNavList: !this.data.showNavList
-        })
-    },
-    // 滚动切换标签样式
-    switchTab: function (e) {
-      let that = this;
-      that.setData({
-        goodsList: []
+  data: {
+    // text:"这是一个页面"
+    navList: [],
+    goodsList: [],
+    id: 0,
+    goodsBizType: '00',
+    currentTab: 0,
+    navIndex: 0,
+    currentCategory: {},
+    scrollLeft: 0,
+    scrollTop: 0,
+    scrollHeight: 0,
+    page: 1,
+    size: 10,
+    showNavList: false,
+    footCart: {},
+    openAttr: false,
+    productList: {},
+    specificationList: {},
+    checkedSpecText: '请选择规格数量',
+    number: 1
+  },
+  toggleNav() {
+    this.setData({
+      showNavList: !this.data.showNavList
+    })
+  },
+  // 滚动切换标签样式
+  switchTab: function (e) {
+    let that = this;
+    //当页面改变是会触发
+    console.log("e.detail.current:" + e.detail.current);
+    this.setData({
+      currentTab: e.detail.current
+    });
+    // console.log("currentTab:" + that.data.currentTab);
+    if (that.data.navIndex == e.detail.current) {
+      this.setData({
+        navIndex: -1
       });
-        //当页面改变是会触发
-      console.log("e.detail.current:" + e.detail.current);
-        this.setData({
-            currentTab: e.detail.current
-        });
-      // console.log("currentTab:" + that.data.currentTab);
-        if (that.data.navIndex == e.detail.current) {
-            this.setData({
-                navIndex: -1
-            });
-            return;
-        }
-        let navListCount = that.data.navList.length;
-        for (let i = 0; i < navListCount; i++) {
-            if (i == e.detail.current) {
-                that.setData({
-                    id: that.data.navList[i].id
-                });
-                break;
-            }
-        }
-        that.setData({
-            scrollLeft: (e.detail.current - 1) * 60
-        });
-        that.refreshCategoryInfo();
-    },
-    switchNav(event) {
-        let name = event.currentTarget.dataset.name;
-        wx.switchTab({
-            url: `/pages/${name}/${name}`,
-        });
-    },
-    onLoad: function (options) {
-        // 页面初始化 options为页面跳转所带来的参数
-      var that = this; 
-      if (options.id) {
-        that.setData({
-          id: parseInt(options.id)
-        });
-      } else {
+      return;
+    }
+    let navListCount = that.data.navList.length;
+    for (let i = 0; i < navListCount; i++) {
+      if (i == e.detail.current) {
         that.setData({
-          id: 0,
-          currentCategory: {}
+          id: that.data.navList[i].id
         });
+        break;
       }
-      if (options.goodsBizType) {
+    }
+    that.setData({
+      scrollLeft: (e.detail.current - 1) * 60
+    });
+    that.refreshCategoryInfo();
+  },
+  switchNav(event) {
+    let name = event.currentTarget.dataset.name;
+    wx.switchTab({
+      url: `/pages/${name}/${name}`,
+    });
+  },
+  onLoad: function (options) {
+    // 页面初始化 options为页面跳转所带来的参数
+    var that = this;
+    if (options.id) {
+      that.setData({
+        id: parseInt(options.id)
+      });
+    } else {
+      that.setData({
+        id: 0,
+        currentCategory: {}
+      });
+    }
+    if (options.goodsBizType) {
+      that.setData({
+        goodsBizType: options.goodsBizType
+      });
+    }
+    if (options.currentIndex) {
+      that.setData({
+        currentTab: options.currentIndex
+      });
+    }
+    wx.getSystemInfo({
+      success: function (res) {
+        var clientHeight = res.windowHeight,
+          clientWidth = res.windowWidth,
+          rpxR = 750 / clientWidth;
+        var calc = clientHeight * rpxR - 180;
         that.setData({
-          goodsBizType: options.goodsBizType
+          scrollHeight: calc
         });
       }
-      if (options.currentIndex) {
+    });
+    this.getCategoryInfo();
+  },
+  getFootCart: function () {
+    let that = this;
+    util.request(api.GetFootCart).then(function (res) {
+      if (res.errno === 0) {
         that.setData({
-          currentTab: options.currentIndex
+          footCart: res.data,
         });
       }
-      wx.getSystemInfo({
-        success: function (res) {
-          var clientHeight = res.windowHeight,
-          clientWidth = res.windowWidth,
-          rpxR = 750 / clientWidth;
-          var calc = clientHeight * rpxR - 180;
+    });
+  },
+  getCategoryInfo: function () {
+    let that = this;
+    util.request(api.GoodsCategory, { id: that.data.id, goodsBizType: that.data.goodsBizType })
+      .then(function (res) {
+        if (res.errno == 0) {
           that.setData({
-            scrollHeight: calc
+            navList: res.data.brotherCategory,
+            currentCategory: res.data.currentCategory
           });
-        }
-      });
-      this.getCategoryInfo();
-    },
-    getFootCart: function () {
-        let that = this;
-        util.request(api.GetFootCart).then(function (res) {
-            if (res.errno === 0) {
-                that.setData({
-                    footCart: res.data,
-                });
-            }
-        });
-    },
-    getCategoryInfo: function () {
-      let that = this;
-      util.request(api.GoodsCategory, { id: that.data.id, goodsBizType: that.data.goodsBizType})
-        .then(function (res) {
-          if (res.errno == 0) {
-            that.setData({
-              navList: res.data.brotherCategory,
-              currentCategory: res.data.currentCategory
-            });
-            //nav位置
-            let currentIndex = that.data.currentTab;
-            let navListCount = that.data.navList.length;
-            for (let i = 0; i < navListCount; i++) {
-              if (that.data.navList[i].id == that.data.id) {
-                break;
-              }
-            }
-            if (currentIndex > navListCount / 2 && navListCount > 5) {
-              that.setData({
-                scrollLeft: currentIndex * 60
-              });
+          //nav位置
+          let currentIndex = that.data.currentTab;
+          let navListCount = that.data.navList.length;
+          for (let i = 0; i < navListCount; i++) {
+            if (that.data.navList[i].id == that.data.id) {
+              break;
             }
+          }
+          if (currentIndex > navListCount / 2 && navListCount > 5) {
             that.setData({
-              currentTab: currentIndex,
-              navIndex: currentIndex
+              scrollLeft: currentIndex * 60
             });
-            that.getGoodsList();
-          } else {
-            //显示错误信息
           }
-        });
-    },
-    refreshCategoryInfo: function () {
-        let that = this;
-        util.request(api.GoodsCategory, {id: this.data.id})
-            .then(function (res) {
-                if (res.errno == 0) {
-                    that.setData({
-                        navList: res.data.brotherCategory,
-                        currentCategory: res.data.currentCategory
-                    });
-                    that.getGoodsList();
-                } else {
-                    //显示错误信息
-                }
-            });
+          that.setData({
+            currentTab: currentIndex,
+            navIndex: currentIndex
+          });
+          that.getGoodsList();
+        } else {
+          //显示错误信息
+        }
+      });
+  },
+  refreshCategoryInfo: function () {
+    let that = this;
+    util.request(api.GoodsCategory, { id: this.data.id })
+      .then(function (res) {
+        if (res.errno == 0) {
+          that.setData({
+            navList: res.data.brotherCategory,
+            currentCategory: res.data.currentCategory,
+            goodsList: []
+          });
+          that.getGoodsList();
+        } else {
+          //显示错误信息
+        }
+      });
     },
     onReady: function () {
         // 页面渲染完成
@@ -171,19 +169,19 @@ Page({
     onHide: function () {
         // 页面隐藏
     },
-    getGoodsList: function () {
-      wx.showLoading({
-        title: '加载中...',
-      });
-      var that = this;
-      util.request(api.GoodsList, { categoryId: that.data.id, goodsBizType: that.data.goodsBizType, page: that.data.page, size:   that.data.size })
-        .then(function (res) {
-          let goodsList = that.data.goodsList.concat(res.data.data);
-          that.setData({
-            goodsList: goodsList,
-          });
-          wx.hideLoading();
+  getGoodsList: function () {
+    var that = this;
+    wx.showLoading({
+      title: '加载中...',
+    });
+    util.request(api.GoodsList, { categoryId: that.data.id, goodsBizType: that.data.goodsBizType, page: that.data.page, size: that.data.size })
+      .then(function (res) {
+        let goodsList = that.data.goodsList.concat(res.data.data);
+        that.setData({
+          goodsList: goodsList,
         });
+        wx.hideLoading();
+      });
     },
     onUnload: function () {
         // 页面关闭
@@ -302,41 +300,41 @@ Page({
                 util.showErrorToast(res.errmsg)
             }
         });
-    }
-    ,
-    switchCate: function (event) {
-        if (this.data.id == event.currentTarget.dataset.id) {
-            return false;
-        }
-        var that = this;
-        that.setData({
-            goodsList: [],
-            page: 1,
-            currentTab: event.currentTarget.dataset.current
-        });
-        console.log("currentIndex:" + that.data.currentTab);
-        var clientX = event.detail.x;
-        var currentTarget = event.currentTarget;
-        if (clientX < 60) {
-            that.setData({
-                scrollLeft: currentTarget.offsetLeft - 60
-            });
-        } else if (clientX > 330) {
-            that.setData({
-                scrollLeft: currentTarget.offsetLeft
-            });
-        }
-        this.setData({
-            id: event.currentTarget.dataset.id,
-            goodsBizType: that.data.goodsBizType
-        });
-        this.getCategoryInfo();
     },
-    onReachBottom() {
-        var that = this;
-        that.setData({
-            page: that.data.page + 1,
-        });
-        that.getGoodsList();
+  switchCate: function (event) {
+    console.log(this.data.scrollLeft);
+    if (this.data.id == event.currentTarget.dataset.id) {
+      return false;
+    }
+    var that = this;
+    that.setData({
+      goodsList: [],
+      page: 1,
+      currentTab: event.currentTarget.dataset.current
+    });
+    console.log("currentIndex:" + that.data.currentTab);
+    var clientX = event.detail.x;
+    var currentTarget = event.currentTarget;
+    if (clientX < 60) {
+      that.setData({
+        scrollLeft: currentTarget.offsetLeft - 60
+      });
+    } else if (clientX > 330) {
+      that.setData({
+        scrollLeft: currentTarget.offsetLeft
+      });
     }
+    this.setData({
+      id: event.currentTarget.dataset.id,
+      goodsBizType: that.data.goodsBizType
+    });
+    this.getCategoryInfo();
+  },
+  onReachBottom() {
+    var that = this;
+    that.setData({
+      page: that.data.page + 1,
+    });
+    that.getGoodsList();
+  }
 })

+ 3 - 2
wx-mall/pages/category/category.wxml

@@ -50,13 +50,14 @@
     <view wx:if="{{showNavList}}" class="close" bindtap="toggleNav">X</view>
   </view>
   <view class="cate-nav">
-    <scroll-view scroll-x="true" class="cate-nav-body" style="width: 750rpx;" scroll-left="{{scrollLeft}}">
+    <scroll-view scroll-x="true" class="cate-nav-body" style="width: 750rpx;" scroll-left="{{scrollLeft}}" >
       <view wx:for="{{navList}}" class="item {{ id == item.id ? 'active' : ''}}" data-current="{{index}}" data-id="{{item.id}}" bindtap="switchCate" wx:key="{{index}}">
         <view class="name">{{item.name}}</view>
       </view>
     </scroll-view>
   </view>
-  <swiper class="" current="{{currentTab}}"  scroll-top="{{scrollTop}}" style="height:{{scrollHeight}}px;">
+  <!-- bindchange="switchTab" -->
+  <swiper class="" current="{{currentTab}}" scroll-top="{{scrollTop}}" style="height:{{scrollHeight}}px;">
     <swiper-item wx:for="{{navList}}"  wx:key="{{navIndex}}">
       <scroll-view scroll-y="true" class="cate-item">
         <view class="h">

+ 39 - 33
wx-mall/pages/index/index.js

@@ -130,8 +130,8 @@ Page({
     let that = this;
     if (wx.getStorageSync('storeId')) {
       // console.log(wx.getStorageSync('userId'));
-      // console.log(wx.getStorageSync('storeId'));
-      console.log(wx.getStorageSync('merchSn'));
+      console.log(wx.getStorageSync('storeId'));
+      // console.log(wx.getStorageSync('merchSn'));
       if (wx.getStorageSync('userId')){
         wx.request({
           url: api.updateLoginUser,
@@ -160,37 +160,43 @@ Page({
   // 同步门店
   syncStore: function () {
     let that = this;
-    if (!wx.getStorageSync('storeId')) {
-        util.getLocation((lng, lat) => {
-          wx.setStorageSync('location', JSON.stringify({ lng, lat }));
-          util.request(api.NearbyList, { longitude: lng, latitude: lat }).then((res) => {
-            let nlist = res.data;
-            if (!nlist.length) {
-              wx.removeStorageSync('nearStoreList');
-              wx.removeStorageSync('storeId');
-              wx.removeStorageSync('storeVo');
-              that.setData({
-                storeName: '附近暂无门店'
-              })
-            } else {
-              that.setData({
-                storeName: nlist[0].storeName,
-                storeId: nlist[0].id
-              })
-              that.chooseStore(nlist[0].id, nlist[0].merchSn);
-              wx.setStorageSync('nearStoreList', JSON.stringify(nlist));
-              wx.setStorageSync('storeVo', JSON.stringify(nlist[0]));
-            }
-          })
-        });
-    } else {
-      var storeVo = JSON.parse(wx.getStorageSync('storeVo'));
-      that.chooseStore(storeVo.id,storeVo.merchSn);
-      that.setData({
-        storeName: storeVo.storeName,
-        storeId: storeVo.id
-      });
-      }
+    //获取附件门店信息
+    util.getLocation((lng, lat) => {
+      wx.setStorageSync('location', JSON.stringify({ lng, lat }));
+      util.request(api.NearbyList, { longitude: lng, latitude: lat }).then((res) => {
+        let nlist = res.data;
+        // console.log(nlist);
+        if (!nlist.length) {
+          wx.removeStorageSync('nearStoreList');
+        } else {
+          wx.setStorageSync('nearStoreList', JSON.stringify(nlist));
+        }
+        if (!wx.getStorageSync('storeId')) {
+          if (!nlist.length) {
+            wx.removeStorageSync('storeId');
+            wx.removeStorageSync('storeVo');
+            that.setData({
+              storeName: '附近暂无门店'
+            })
+          } else {
+            that.setData({
+              storeName: nlist[0].storeName,
+              storeId: nlist[0].id
+            })
+            that.chooseStore(nlist[0].id, nlist[0].merchSn);
+            wx.setStorageSync('storeVo', JSON.stringify(nlist[0]));
+          }
+        } else {
+          var storeVo = JSON.parse(wx.getStorageSync('storeVo'));
+          that.chooseStore(storeVo.id, storeVo.merchSn);
+          that.setData({
+            storeName: storeVo.storeName,
+            storeId: storeVo.id
+          });
+        }
+      })
+    });
+    
   },
   // 更新门店Id
   chooseStore: function (storeId,merchSn) {

+ 15 - 12
wx-mall/pages/map/map.js

@@ -115,8 +115,8 @@ Page({
         wx.removeStorageSync('nearStoreList');
         wx.removeStorageSync('storeId');
         wx.removeStorageSync('storeVo');
-        wx.setStorageSync('storeId', storeId);
         wx.removeStorageSync('currentCategory');
+        wx.setStorageSync('storeId', storeId);
         var item = "";
         for (var i = 0; i < that.data.nearStoreList.length; i++) {
           if (storeId == that.data.nearStoreList[i].id) {
@@ -126,9 +126,9 @@ Page({
           }
         }
 
-        // var pages = getCurrentPages();
-        // var currPage = pages[pages.length - 1];  //当前页面
-        // var prevPage = pages[pages.length - 2]; //上一个页面
+        var pages = getCurrentPages();
+        var currPage = pages[pages.length - 1];  //当前页面
+        var prevPage = pages[pages.length - 2]; //上一个页面
         if (item == "") {
           wx.removeStorageSync('nearStoreList');
           wx.removeStorageSync('storeId');
@@ -138,18 +138,21 @@ Page({
           })
         } else {
           //直接调用上一个页面的setData()方法,把数据存到上一个页面中去
-          that.setData({
+          prevPage.setData({
             storeName: item.storeName
           })
+          // that.setData({
+          //   storeName: item.storeName
+          // })
         }
-        wx.switchTab({
-          url: '/pages/index/index'
-        });
+        // wx.switchTab({
+        //   url: '/pages/index/index'
+        // });
 
-        // prevPage.getIndexData();
-        // prevPage.enableActivity();
-        // prevPage.getGroupData();
-        // wx.navigateBack()
+        prevPage.getIndexData();
+        prevPage.enableActivity();
+        prevPage.getGroupData();
+        wx.navigateBack()
       }
     })
   }

+ 3 - 2
wx-mall/project.config.json

@@ -5,10 +5,11 @@
 		"es6": true,
 		"postcss": true,
 		"minified": true,
-		"newFeature": true
+		"newFeature": true,
+		"uglifyFileName": true
 	},
 	"compileType": "miniprogram",
-	"libVersion": "2.3.2",
+	"libVersion": "2.4.3",
 	"appid": "wxb6b30b1b14ee502a",
 	"projectname": "kmall-mp",
 	"condition": {