yann 5 mesi fa
parent
commit
ccbc258d9a

+ 47 - 42
app.js

@@ -25,54 +25,59 @@ App({
   },
 
   onLaunch() {
-
-    // 登录
-    if (wx.getStorageSync('accessToken')) {
-      this.globalData.loginSuccess = true;
-    } else {
-    }
+    
   },
 
   getMUser() {
     return new Promise((resolve, reject) => {
-      wx.login({
-        success: res => {
-          // 发送 res.code 到后台换取 accessTokenDTO, userProfileDTO
-          console.log("code", res);
-          if (res.code) {
-            wx.request({
-              url: baseUrl + '/api/store/wxlogin',
-              method: 'POST',
-              data: {
-                jsCode: res.code
-              },
-              header: {
-                'content-type': 'application/x-www-form-urlencoded',
-                'X-Referrer-Id': '', // 旧租户字段
-                'X-Store-Id': '', // 旧租户字段
-              },
-              success: (res) => {
-                console.log(res);
-                this.globalData.mUser = res.data.data;
-                wx.setStorageSync('accessToken', this.globalData.mUser.accessTokenDTO.accessToken);
-                resolve();
-              },
-              fail: (res) => {
-                console.log(res);
-                reject(res);
-              }
+        wx.login({
+            success: res => {
+            // 发送 res.code 到后台换取 accessTokenDTO, userProfileDTO
+            console.log("code", res);
+            if (res.code) {
+                wx.request({
+                    url: baseUrl + '/api/store/wxlogin',
+                    method: 'POST',
+                    data: {
+                        jsCode: res.code
+                    },
+                    header: {
+                        'content-type': 'application/x-www-form-urlencoded',
+                        'X-Referrer-Id': '', // 旧租户字段
+                        'X-Store-Id': '', // 旧租户字段
+                    },
+                    success: (res) => {
+                        let {accessToken,expiresIn} = res.data.data.accessTokenDTO;
+                        this.globalData.mUser = res.data.data;
+                        wx.setStorageSync('accessToken', accessToken);
+                        wx.setStorageSync('accessTokenTime', new Date().getTime() + expiresIn*1000);
+                        resolve(true);
+                    },
+                    fail: (res) => {
+                        console.log(res);
+                        reject(res);
+                    }
+                });
+            }
+            }
+        });
+    })
+  },
+
+  // 检测是否需要重新
+  checkLoginStatus(){
+    return new Promise((resolve, reject) => {
+        let token = wx.getStorageSync('accessToken');
+        let time = new Date().getTime() - wx.getStorageSync('accessTokenTime') < 600000;
+        if (token && time) {
+            resolve(true);
+        } else {
+            this.getMUser().then(res=>{
+                resolve(true)
             });
-          } else {
-            reject(new Error('No code received from wx.login'));
-          }
-        },
-        fail: (e) => {
-          console.log(e);
-          reject(e);
         }
-      });
-    });
-  },
+    })
+  }
 })
 
 

+ 1 - 1
app.json

@@ -1,11 +1,11 @@
 {
   "pages": [
+      "pages/login/login",
       "pages/index/index",
       "pages/prodDetail/prodDetail",
       "pages/all/all",
       "pages/cart/cart",
       "pages/my/personalCenter/personalCenter",
-      "pages/login/login",
       "pages/Mall/sortCommodity/sortCommodity",
       "pages/Mall/presaleCommodity/presaleCommodity",
       "pages/Mall/newCommodity/newCommodity",

+ 71 - 1
pages/login/login.js

@@ -1 +1,71 @@
-Page({})
+const App = getApp();
+Page({
+
+  /**
+   * 页面的初始数据
+   */
+  data: {
+    
+  },
+
+  /**
+   * 生命周期函数--监听页面加载
+   */
+  onLoad: function (options) {
+    // 登录
+    App.checkLoginStatus().then(res=>{
+        wx.reLaunch({
+            url: '/pages/index/index',
+        })
+    });
+  },
+
+  /**
+   * 生命周期函数--监听页面初次渲染完成
+   */
+  onReady: function () {
+    
+  },
+
+  /**
+   * 生命周期函数--监听页面显示
+   */
+  onShow: function () {
+    
+  },
+
+  /**
+   * 生命周期函数--监听页面隐藏
+   */
+  onHide: function () {
+    
+  },
+
+  /**
+   * 生命周期函数--监听页面卸载
+   */
+  onUnload: function () {
+    
+  },
+
+  /**
+   * 页面相关事件处理函数--监听用户下拉动作
+   */
+  onPullDownRefresh: function () {
+    
+  },
+
+  /**
+   * 页面上拉触底事件的处理函数
+   */
+  onReachBottom: function () {
+    
+  },
+
+  /**
+   * 用户点击右上角分享
+   */
+  onShareAppMessage: function () {
+    
+  }
+})

+ 21 - 3
pages/order/confirmOrder/confirmOrder.js

@@ -29,7 +29,7 @@ Page({
   // 去地址管理页面
   goAddressMange(){
       wx.navigateTo({
-        url: '/pages/my/addressManage/addressManage',
+        url: '/pages/my/addressManage/addressManage?moldify=true',
       })
   },
   // 确认订单
@@ -58,6 +58,11 @@ Page({
       fail(err) {
         console.error('支付失败', err);
         wx.showToast({ title: '支付取消', icon: 'none' });
+        setTimeout(()=>{
+            wx.redirectTo({
+              url: '/pages/my/myOrder/myOrder',
+            })
+        },3000)
       }
     });
   },
@@ -84,7 +89,20 @@ Page({
         orderItemDTOS
     })
   },
-
+  // 修改收货地址
+  editAddress(){
+    const pages = getCurrentPages()
+    const currentPage = pages[pages.length - 1]
+    if (currentPage.data.returnData) {
+        let address = currentPage.data.returnData;
+        // console.log('收到返回参数:', currentPage.data.returnData)
+        // 清除参数避免重复触发
+        currentPage.setData({ returnData: null })
+        this.setData({address})
+    }else{
+        this.getAddressList();
+    }
+  },
   /**
    * 生命周期函数--监听页面初次渲染完成
    */
@@ -96,7 +114,7 @@ Page({
    * 生命周期函数--监听页面显示
    */
   onShow() {
-    this.getAddressList();
+    this.editAddress();
   },
 
   /**

+ 2 - 4
pages/prodDetail/prodDetail.js

@@ -32,11 +32,9 @@ Page({
   },
   // 添加购物车、立即购买
   addCard(e){
-      console.log(e);
+    console.log(e);
     let prodInfo = this.data.prodDetail;
-    if(e.currentProduct?.dataset.buy){
-        prodInfo.buy = true;
-    }
+    prodInfo.buy = e.currentTarget.dataset.buy;
     this.setData({
         showAddCartModal: true,
         currentProduct:prodInfo

+ 2 - 2
pages/prodDetail/prodDetail.wxml

@@ -2,7 +2,7 @@
 <view>
     <!-- banner -->
     <swiper class="swiper" indicator-dots="{{true}}" indicator-color="#FFFFFF" 	indicator-active-color="#CCCCCC" autoplay circular>
-        <block wx:for="{{prodDetail.prodPicList}}" wx:key='{{item.id}}'>
+        <block wx:for="{{prodDetail.prodPicList}}" wx:key='index'>
             <swiper-item class="swiperItem">
                 <image class="banner" src="{{item.picUrl}}" mode="widthFix"/>
             </swiper-item>
@@ -26,7 +26,7 @@
           <image class="card" src="/images/mall/card.png" mode="" catchtap="goCard"/>
        </view>
        <view>
-          <image class="button" src="/images/index/add-cart.png" mode="widthFix" catchtap="addCard"/>
+          <image class="button" src="/images/index/add-cart.png" mode="widthFix" data-buy="{{false}}" catchtap="addCard"/>
           <image class="button" src="/images/index/buy-now.png" mode="widthFix" data-buy="{{true}}" catchtap="addCard" />
        </view>
     </view>

+ 22 - 58
utils/http.js

@@ -28,67 +28,31 @@ function showErrToast(e) {
 
 function getPromise(url, data, method) {
   return new Promise((resolve, reject) => {
-    const accessToken = wx.getStorageSync("accessToken");
-
-    if (!accessToken) {
-      // Cache the current API call
-      const cachedCall = () => {
+    let quest = ()=>{
         wx.request({
-          url: `${baseUrl}${url}`,
-          header: getHeader(),
-          method: method,
-          data: data,
-          success: function (res) {
-            if (res.data.code === 200) {
-              resolve(res.data.data);
-            } else {
-              if (res.statusCode === 401) {
-                reject(401);
-              } else {
-                reject(res.data.message);
-              }
-            }
-          },
-          fail: function (err) {
-            reject(err);
-          },
-        });
-      };
-
-      // Call App.getMUser to fetch accessToken
-      App.getMUser()
-        .then(() => {
-          // Retry the cached API call after getting the token
-          cachedCall();
-        })
-        .catch((err) => {
-          console.error("Failed to fetch accessToken", err);
-          showErrToast("Failed to fetch accessToken");
-          reject(err);
+            url: `${baseUrl}${url}`,
+            header: getHeader(),
+            method: method,
+            data: data,
+            success: function (res) {
+                if (res.statusCode === 200) {
+                    resolve(res.data);
+                } else {
+                    if (res.statusCode === 401) {
+                        reject(401);
+                    } else {
+                        reject(res.data.message);
+                    }
+                }
+            },
+            fail: function (err) {
+                reject(err);
+            },
         });
-    } else {
-      // Proceed with the API call if accessToken exists
-      wx.request({
-        url: `${baseUrl}${url}`,
-        header: getHeader(),
-        method: method,
-        data: data,
-        success: function (res) {
-          if (res.statusCode === 200) {
-            resolve(res.data);
-          } else {
-            if (res.statusCode === 401) {
-              reject(401);
-            } else {
-              reject(res.data.message);
-            }
-          }
-        },
-        fail: function (err) {
-          reject(err);
-        },
-      });
     }
+    App.checkLoginStatus().then(()=>{
+        quest();
+    })
   }).catch((e) => {
     console.error(`in promise error, url: ${baseUrl}${url}`);
     console.error(e);