Browse Source

微信openid刷新优化

pengjing 10 months ago
parent
commit
c66e8acdf5
3 changed files with 35 additions and 28 deletions
  1. 1 1
      h5app/src/App.vue
  2. 32 26
      h5app/src/utils/wechat.ts
  3. 2 1
      h5app/src/views/index.vue

+ 1 - 1
h5app/src/App.vue

@@ -32,7 +32,7 @@ if (user.value != null) {
     redirectUrl = splits[1];
   }
 
-  router.push("/index?redirectUrl=" + redirectUrl)
+  router.push("/index?redirectUrl=" + redirectUrl.replace("/index?redirectUrl=",""))
 }
 
 </script>

+ 32 - 26
h5app/src/utils/wechat.ts

@@ -18,36 +18,42 @@ export const wxAuth = async <Object>(redirectUrl: string): Promise<string> => {
             })
             const urlParams = getUrlParams() as any;
             const code = urlParams["code"];
-
-            if (!code) {
-                const loading = await loadingController.create({
-                    cssClass: 'my-custom-class',
-                    message: '微信授权中,请稍等...',
-                    duration: 2000,
-                });
-                await loading.present();
-                getOAuthUrl(encodeURIComponent(redirectUrl)).then((res: any) => {
-                    if (res) {
-                        window.location.replace(res);
-                    } else {
-                        presentAlert("微信授权失败!");
-                    }
-                });
+            const openId = urlParams["openId"];
+            if (openId) {
+                userStore.setOpenId(openId);
             } else {
-                const loading = await loadingController.create({
-                    cssClass: 'my-custom-class',
-                    message: '微信授权成功,请稍等...',
-                    duration: 2000,
-                });
-                await loading.present();
-                await getWxOpenId(code).then((res: any) => {
-                    userStore.setOpenId(res);
-                });
+                if (!code) {
+                    const loading = await loadingController.create({
+                        cssClass: 'my-custom-class',
+                        message: '微信授权中,请稍等...',
+                        duration: 2000,
+                    });
+                    await loading.present();
+                    getOAuthUrl(encodeURIComponent(redirectUrl)).then((res: any) => {
+                        if (res) {
+                            window.location.replace(res);
+                        } else {
+                            presentAlert("微信授权失败!");
+                        }
+                    });
+                } else {
+                    const loading = await loadingController.create({
+                        cssClass: 'my-custom-class',
+                        message: '微信授权成功,请稍等...',
+                        duration: 2000,
+                    });
+                    await loading.present();
+                    await getWxOpenId(code).then((res: any) => {
+                        userStore.setOpenId(res);
+
+                        /*const currentUrl = redirectUrl + (redirectUrl.indexOf("?") >= 0 ? "&" : "?") + "openId=" + res;
+                        window.location.replace(currentUrl);*/
+                    });
+                }
             }
         }
         return "";
-    }
-    catch (e){
+    } catch (e) {
         return Promise.reject(null);
     }
 }

+ 2 - 1
h5app/src/views/index.vue

@@ -29,7 +29,8 @@ export default defineComponent({
 
     if (isWechat() && redirectUrl.indexOf("wxMessage") < 0) {
       wxLogin().then(res => {
-        router.push(redirectUrl);
+        const currentUrl = redirectUrl + (redirectUrl.indexOf("?") >= 0 ? "&" : "?") + "openId=" + userStore.getOpenId
+        router.push(currentUrl);
       });
     } else {
       router.push(redirectUrl);