Parcourir la source

refactor(auth): 优化登录和鉴权逻辑

- 在 API 请求成功后检查状态码,无效 token 时清除缓存
- 改进登录流程,添加加载提示和自动初始化登录信息功能
- 修改学校页面未登录提示文案,提升用户体验
- 优化拍照页面初始化逻辑
- 重构鉴权函数,增加弹窗提示并跳转到登录页面
wzg il y a 8 mois
Parent
commit
59f989c5ba

+ 6 - 1
miniprogram/apis/apiConfig.js

@@ -18,7 +18,12 @@ function api(url, data, method) {
         "content-type": "application/json",
         accessToken: wx.getStorageSync("accessToken"),
       },
-      success: resolve,
+      success: (res) => {
+        if (res.data.status === "11012") {
+          wx.removeStorageSync("accessToken");
+        }
+        resolve(res);
+      },
       fail: reject,
     });
   });

+ 15 - 2
miniprogram/pages/index/index.js

@@ -1,4 +1,5 @@
 // pages/index/index.js
+import { wxSetSessionKey } from "../../utils/wxUtils";
 import { postApi } from "../../apis/api";
 Page({
   /**
@@ -9,7 +10,18 @@ Page({
   },
 
   async login() {
-    if (!wx.getStorageSync("openId")) return;
+    if (!wx.getStorageSync("openId")) {
+      wx.showToast({
+        title: "正在初始化登录信息...",
+        icon: "none",
+        duration: 1500,
+      });
+      wxSetSessionKey();
+      return;
+    }
+    wx.showLoading({
+      title: "正在登录...",
+    });
     let { data } = await postApi("/login/openid", {
       openId: wx.getStorageSync("openId"),
     });
@@ -31,10 +43,11 @@ Page({
     }
 
     setTimeout(() => {
+      wx.hideLoading();
       wx.switchTab({
         url: "/pages/takePhoto/takePhoto",
       });
-    }, 1500);
+    }, 1000);
   },
   onLoad: function (options) {
     if (wx.getStorageSync("userId") && wx.getStorageSync("shipName")) {

+ 1 - 1
miniprogram/pages/school/school.wxml

@@ -15,7 +15,7 @@
         </view>
       </view>
     </view>
-    <view class="tac fs28 c7 p40" wx:else>尚未登录,暂无数据</view>
+    <view class="tac fs28 c7 p40" wx:else>暂无数据</view>
     <!-- 运营数据 -->
     <view class="card" wx:if="{{school.crewSchoolName}}">
       <view class="card-header">运营数据</view>

+ 4 - 0
miniprogram/pages/takePhoto/takePhoto.js

@@ -31,6 +31,10 @@ Page({
    * 生命周期函数--监听页面加载
    */
   onLoad(options) {
+    this.init();
+  },
+
+  init() {
     this.getTerminalServices();
     this.getWaterLevel();
     this.getNewEnergyPolicy();

+ 24 - 3
miniprogram/utils/utils.js

@@ -4,12 +4,33 @@ function isImage(url) {
   return imgArr.indexOf(url.substring(lastIndex + 1, url.length)) != -1;
 }
 
-function checkToken(title = "尚未登录,正在体验") {
+// function checkToken(title = "尚未登录,正在体验") {
+//   if (!wx.getStorageSync("accessToken")) {
+//     wx.showToast({
+//       title,
+//       icon: "none",
+//     });
+//     return false;
+//   }
+//   return true;
+// }
+
+function checkToken(title = "提示", content = "尚未登录,是否去登录?") {
   if (!wx.getStorageSync("accessToken")) {
-    wx.showToast({
+    wx.showModal({
       title,
-      icon: "none",
+      content,
+      success(res) {
+        if (res.confirm) {
+          wx.navigateTo({
+            url: "/pages/index/index",
+          });
+        } else if (res.cancel) {
+          console.log("用户点击取消");
+        }
+      },
     });
+
     return false;
   }
   return true;