فهرست منبع

refactor(miniprogram): 优化代码结构和错误处理

- 注释掉 app.js 中的条件判断逻辑
- 优化 index.js 中的异步请求和数据处理
- 添加错误处理逻辑
- 使用可选链和空值合并操作符提高代码健壮性
wzg 8 ماه پیش
والد
کامیت
89a7ba24e4
2فایلهای تغییر یافته به همراه40 افزوده شده و 16 حذف شده
  1. 6 6
      miniprogram/app.js
  2. 34 10
      miniprogram/pages/index/index.js

+ 6 - 6
miniprogram/app.js

@@ -54,12 +54,12 @@ App({
       wx.setStorageSync("appVersion", envVersion);
       wx.setStorageSync("openId", openId);
       // 判断scene.path 是否有sharePage/sharePage
-      if (scene.path.indexOf("sharePage/sharePage") === -1) {
-        wx.setStorageSync("appVersion", envVersion);
-        wx.redirectTo({
-          url: "/pages/index/index",
-        });
-      }
+      // if (scene.path.indexOf("sharePage/sharePage") === -1) {
+      //   wx.setStorageSync("appVersion", envVersion);
+      //   wx.redirectTo({
+      //     url: "/pages/index/index",
+      //   });
+      // }
       clearTimeout(timer);
     }, 1000);
   },

+ 34 - 10
miniprogram/pages/index/index.js

@@ -47,13 +47,19 @@ Page({
       Object.keys(obj).forEach(function (key) {
         wx.setStorageSync(key, obj[key]);
       });
-      wx.setStorageSync("accessToken", data.result.tokenInfo.tokenValue);
+      wx.setStorageSync(
+        "accessToken",
+        data?.result?.tokenInfo?.tokenValue || ""
+      );
       wx.switchTab({
         url: "/pages/takePhoto/takePhoto",
       });
     } else {
       if (data?.result?.userInfo?.userId) {
-        wx.setStorageSync("accessToken", data.result.tokenInfo.tokenValue);
+        wx.setStorageSync(
+          "accessToken",
+          data?.result?.tokenInfo?.tokenValue || ""
+        );
         wx.showModal({
           title: "提示",
           content: "您已注册但尚未绑定船舶,去绑定?",
@@ -98,9 +104,8 @@ Page({
         });
         console.log("data1", data);
         if (data.status === 0) {
-          let { phoneNumber: phone } = data.result;
-          // let phone = "19213811213";
-          wx.setStorageSync("phone", phone);
+          // let { phoneNumber: phone } = data.result;
+          let phone = "15136162222";
           let { data } = await postApi("/login", {
             phone,
             openId: wx.getStorageSync("openId"),
@@ -114,11 +119,19 @@ Page({
           });
           if (data.status === 0) {
             console.log(data, "data2");
-            wx.setStorageSync("accessToken", data.result.tokenInfo.tokenValue);
+            wx.setStorageSync("phone", phone);
+            wx.setStorageSync(
+              "accessToken",
+              data?.result?.tokenInfo?.tokenValue || ""
+            );
           } else {
             console.log(data, "data23");
-            wx.setStorageSync("accessToken", data.result.tokenInfo.tokenValue);
+            wx.setStorageSync(
+              "accessToken",
+              data?.result?.tokenInfo?.tokenValue || ""
+            );
             if (data?.result?.userInfo?.userId) {
+              console.log(data, "data24");
               wx.showModal({
                 title: "提示",
                 content: "您已注册但尚未绑定船舶,去绑定?",
@@ -135,8 +148,9 @@ Page({
                 },
               });
             } else {
+              console.log(data, "data25");
               wx.showToast({
-                title: data2.msg,
+                title: data.msg,
                 icon: "none",
                 duration: 5000,
               });
@@ -158,7 +172,14 @@ Page({
           icon: "error",
         });
       }
-    } catch (error) {}
+    } catch (error) {
+      wx.showToast({
+        title: "系统错误",
+        icon: "none",
+        duration: 5000,
+      });
+      console.error("Error:", error);
+    }
   },
   onLoad: function (options) {},
 
@@ -229,7 +250,10 @@ Page({
       Object.keys(obj).forEach(function (key) {
         wx.setStorageSync(key, obj[key]);
       });
-      wx.setStorageSync("accessToken", data.result.tokenInfo.tokenValue);
+      wx.setStorageSync(
+        "accessToken",
+        data?.result?.tokenInfo?.tokenValue || ""
+      );
       wx.showToast({
         title: "绑定成功",
         icon: "success",