Browse Source

feat: 新增接口封装(更新OPENID)

await 7 months ago
parent
commit
d125e620bd
1 changed files with 42 additions and 3 deletions
  1. 42 3
      src/helper/userHelper.ts

+ 42 - 3
src/helper/userHelper.ts

@@ -31,15 +31,16 @@ export default class UserHelper {
     this.cacheUserInfoToRedux(userInfo);
   }
 
+  
   /**缓存到redux */
   public static async cacheUserInfoToRedux(userInfo: WxLoginUsingGetResponse) {
     ReduxUtil.dispatch(reduxActions.changeUserInfo(userInfo));
   }
-
+  
   public static isLogin() {
     return !!ReduxUtil.store.getState().userInfo;
   }
-
+  
   /**推出登录 */
   public static outLogin() {
     Taro.clearStorageSync();
@@ -47,9 +48,28 @@ export default class UserHelper {
     RouteUtil.reLaunch("/pages/bottomNavBar/index");
   }
 
+  public static async UpdateOpenId () {
+    return new Promise((resolve, reject) => {
+      Taro.login({
+        success: async function (login_res) {
+          if (login_res.code) {
+            await UserApi.wxUpdateOpenIdGet({
+              params: { jsCode: login_res.code }
+            })
+            resolve(login_res.code)
+          }
+        },
+        fail(res) {
+          reject(res)
+        },
+      })
+    })
+  }
+  
   static callback?: () => Promise<any>;
+  
 
-  public static afterLogin<T = any>(
+  public static async afterLogin<T = any>(
     options: {
       toLoginPage?: boolean;
       callbackBeforeRoute?: boolean;
@@ -66,10 +86,29 @@ export default class UserHelper {
       page,
       params,
     } = options;
+    const updateOpenId = () => {
+      return new Promise((resolve, reject) => {
+        Taro.login({
+          success: async function (login_res) {
+            if (login_res.code) {
+              await UserApi.wxUpdateOpenIdGet({
+                params: { jsCode: login_res.code }
+              })
+              resolve(login_res.code)
+            }
+          },
+          fail(res) {
+            reject(res)
+          },
+        })
+      })
+    }
     this.callback = callback;
     if (UserHelper.isLogin()) {
+      await updateOpenId()
       callback?.();
     } else {
+      await updateOpenId()
       if (forceLogin) {
         toLoginPage &&
           RouteUtil.push<ILoginParms>("/pages/user/login/index", {