|
@@ -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);
|
|
|
- }
|
|
|
+ });
|
|
|
}
|
|
|
}
|