Browse Source

企业微信验证

pengjing 7 months ago
parent
commit
05df4b38cc

+ 49 - 47
parth5/parth5/src/app/service/wxcp.service.ts

@@ -26,59 +26,61 @@ export class WxcpService {
     return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
   }
 
-  wxAuth = async <Object>(routePath: string): Promise<string> => {
-    try {
-      const urlParams = getUrlParams() as any;
-      let wxUserId = urlParams["wxUserId"] || this.userService.getWxUserID();
-      const code = urlParams["code"];
-
-      if (this.isWechat()) {
-        if (wxUserId) {
-          this.userService.setWxUserID(wxUserId);
-          this.accountService.loginByWxUserId(wxUserId, routePath);
-        } else {
-          if (!code) {
-            this.alertModal.loading('微信授权中,请稍等...').then(loading => {
-              let oAuthUrl_redirectUrl = this.siteUrl + "?routePath=" + encodeURIComponent(routePath); //www.aa.com/mobile?routePath=xxxxx
-              this.wechatApi.getOAuthUrl(encodeURIComponent(oAuthUrl_redirectUrl)).subscribe((res: any) => {
-                loading.dismiss();
-                if (res.success) {
-                  window.location.replace(res.item);
-                } else {
-                  this.alertModal.alert("微信授权失败!");
-                }
-              }, () => {
-                loading.dismiss();
-              });
-            });
+  wxAuth = async <Object>(routePath: string): Promise<boolean> => {
+    return new Promise(async (resolve, reject) => {
+      try {
+        const urlParams = getUrlParams() as any;
+        let wxUserId = urlParams["wxUserId"] || this.userService.getWxUserID();
+        const code = urlParams["code"];
 
+        if (this.isWechat()) {
+          if (wxUserId) {
+            this.userService.setWxUserID(wxUserId);
+            this.accountService.loginByWxUserId(wxUserId, routePath);
           } else {
-            this.alertModal.loading('微信授权成功,请稍等...').then(loading => {
-              this.wechatApi.getWxCpUserId(code).subscribe((res: any) => {
-                loading.dismiss();
-                if (res.success && res.item) {
-                  wxUserId = res.item;
-                  this.userService.setWxUserID(wxUserId);
-                  const currentUrl = routePath + (routePath.indexOf("?") >= 0 ? "&" : "?") + "wxUserId=" + wxUserId;
-                  this.accountService.loginByWxUserId(wxUserId, currentUrl);
-                } else {
+            if (!code) {
+              await this.alertModal.loading('正在获取企业微信授权链接,请稍等...').then(async loading => {
+                let oAuthUrl_redirectUrl = this.siteUrl + "?routePath=" + encodeURIComponent(routePath); //www.aa.com/mobile?routePath=xxxxx
+                await this.wechatApi.getOAuthUrl(encodeURIComponent(oAuthUrl_redirectUrl)).toPromise().then((res: any) => {
+                  loading.dismiss();
+                  if (res != null && res.success) {
+                    window.location.replace(res.item);
+                  } else {
+                    this.alertModal.alert("企业微信授权链接获取失败!");
+                    resolve(false);
+                  }
+                }, () => {
+                  loading.dismiss();
+                  this.alertModal.alert("企业微信授权链接获取失败!");
+                  resolve(false);
+                });
+              });
+            } else {
+              await this.alertModal.loading('企业微信授权成功,正在获取企业微信用户信息,请稍等...').then(async loading => {
+                await this.wechatApi.getWxCpUserId(code).toPromise().then((res: any) => {
+                  loading.dismiss();
+                  if (res != null && res.success && res.item) {
+                    wxUserId = res.item;
+                    this.userService.setWxUserID(wxUserId);
+                    const currentUrl = routePath + (routePath.indexOf("?") >= 0 ? "&" : "?") + "wxUserId=" + wxUserId;
+                    this.accountService.loginByWxUserId(wxUserId, currentUrl);
+                  } else {
+                    this.alertModal.alert("获取企业微信用户信息失败!");
+                    resolve(false);
+                  }
+                }, () => {
+                  loading.dismiss();
                   this.alertModal.alert("获取企业微信用户信息失败!");
-                  this.router.navigate(['/login'], {
-                    relativeTo: this.routeInfo,
-                    queryParams: {random: Math.random()}
-                  });
-                }
-              }, () => {
-                loading.dismiss();
+                  resolve(false);
+                });
               });
-            });
+            }
           }
         }
+      } catch (e) {
+        this.alertModal.alert("调用微信接口异常!" + e);
+        resolve(false);
       }
-
-      return Promise.reject(true);
-    } catch (e) {
-      return Promise.reject(false);
-    }
+    });
   }
 }

+ 9 - 2
parth5/parth5/src/app/views/index/index.page.ts

@@ -13,7 +13,7 @@ import {AccountService} from "../../service/account.service";
 export class IndexPage implements OnInit {
 
   constructor(private router: Router, private route: ActivatedRoute, private wxcpService: WxcpService, private userService: UserService,
-              private accountService: AccountService) {
+              private accountService: AccountService, private routeInfo: ActivatedRoute) {
   }
 
   ngOnInit() {
@@ -30,7 +30,14 @@ export class IndexPage implements OnInit {
         //根据微信用户id登录系统
         this.accountService.loginByWxUserId(wxUserId, routePath);
       } else {
-        this.wxcpService.wxAuth(routePath).then(res=>{});
+        this.wxcpService.wxAuth(routePath).then(res => {
+          if (res === false) {
+            this.router.navigate(['/login'], {
+              relativeTo: this.routeInfo,
+              queryParams: {random: Math.random()}
+            });
+          }
+        });
       }
     } else {
       this.router.navigate(['/login'], {queryParams: {random: Math.random()}});