浏览代码

微信小程序登录逻辑梳理

John-Hong 2 年之前
父节点
当前提交
f416f0f97c
共有 5 个文件被更改,包括 36 次插入14 次删除
  1. 6 6
      nginx_parking.conf
  2. 22 0
      src/App.vue
  3. 5 5
      src/pages/login/openWx.vue
  4. 2 2
      src/pages/parkingFee/parkingFeeDetail.vue
  5. 1 1
      src/utils/request-kip.js

+ 6 - 6
nginx_parking.conf

@@ -24,13 +24,13 @@ server {
         rewrite ^.*$ /index.html last;
     }
 
-    location /qaPayment {
-        proxy_pass https://qa-apim.kerryplus.com/c/api/payment/v1;
-    }
+    # location /qaPayment {
+    #     proxy_pass https://qa-apim.kerryplus.com/c/api/payment/v1;
+    # }
 
-    location /profileApi {
-        proxy_pass https://qa-apim.kerryplus.com/c/api;
-    }
+    # location /profileApi {
+    #     proxy_pass https://qa-apim.kerryplus.com/c/api;
+    # }
 
     location /sns {
         proxy_pass https://api.weixin.qq.com/sns;

+ 22 - 0
src/App.vue

@@ -102,6 +102,7 @@ export default {
         this.$store.commit('SET_USER_INFO', uni.getStorageSync('member'));
         this.$store.commit('SET_MEMBER', uni.getStorageSync('member'));
       }
+
       // 如果用户没有openid
       const openid = uni.getStorageSync('openid');
       // console.log(949494, openid);
@@ -110,6 +111,12 @@ export default {
         this.$store.commit('SET_OPENID', openid);
       }
       if (!openid) {
+        // 如果是微信小程序入口,先把 传过来的 token 和 pageId 保存下来
+        if (platform === 'miniprogram') {
+          const { pageId, token } = query;
+          uni.setStorageSync('pageId', pageId);
+          uni.setStorageSync('token', token);
+        }
         // 当进入其他页面时,需要先打开路由展示
         this.isInit = true;
         // 前往授权页面
@@ -220,6 +227,21 @@ export default {
       }
       // 当前页面是否是在微信公众号运行:end
 
+      // 当判断缓存中存在 pageId, token,并且地址栏中不存在 pageId, token。我们需要刷新页面,确保用户在微信小程序和H5公众号中使用的 open ID 保存一致
+      if (
+        uni.getStorageSync('pageId') &&
+        uni.getStorageSync('token') &&
+        (!query?.pageId || !query.token)
+      ) {
+        this.$router.replace({
+          path: '/home',
+          query: {
+            pageId: uni.getStorageSync('pageId'),
+            token: uni.getStorageSync('token'),
+          },
+        });
+        return;
+      }
       /* 微信小程序 webview 配置 */
       if (!query?.pageId?.length || !query?.token?.length) {
         return;

+ 5 - 5
src/pages/login/openWx.vue

@@ -18,7 +18,7 @@ export default {
   },
   methods: {
     getCode() {
-      const code = getUrlParams()?.code; // 截取路径中的code,如果没有就去微信授权,如果已经获取到了就直接传code给后台获取openId
+      const { code = '' } = getUrlParams(); // 截取路径中的code,如果没有就去微信授权,如果已经获取到了就直接传code给后台获取openId
       const local = window.location.origin;
       // return
       if (code == null || code === '') {
@@ -44,11 +44,11 @@ export default {
         code,
         grant_type: 'authorization_code',
       });
-      if ( res?.expires_in && res?.openid ) {
-        this.$store.commit('SET_OPENID', res.openid)
+      if (res?.expires_in && res?.openid) {
+        this.$store.commit('SET_OPENID', res.openid);
         this.$router.replace({
-          path: '/home'
-        })
+          path: '/home',
+        });
       }
       //   this.$http
       //     .post('/api/user/wechat/login', { code: code })

+ 2 - 2
src/pages/parkingFee/parkingFeeDetail.vue

@@ -1827,8 +1827,8 @@ export default {
       // const openId = MemberCacheTool.getOpenId(app);
       // const appId = uni.getAccountInfoSync().miniProgram.appId;
       // const appId = 'wx192b7d2e8dcbefd0';
-      // const appId = 'wx92c3e55fbef6b2af';
-      const appId = 'wx907c27f16841a919';
+      const appId = 'wx92c3e55fbef6b2af';
+      // const appId = 'wxd830fe4d1e04988e';
       // const appId = 'wx92c3e55fbef6b2af';
       const params = {
         region: 'cn',

+ 1 - 1
src/utils/request-kip.js

@@ -62,7 +62,7 @@ function getUrl(path) {
       return path;
     }
     if (/\/qaPayment/g.test(path)) {
-      return `${window.ininjectConfig.qaPayment}${path}`
+      return `${window.ininjectConfig.qaPayment}${path.replace(/\/qaPayment/g, '')}`
     }
   }
   // 本地开发环境