|
@@ -0,0 +1,75 @@
|
|
|
+import {Inject, Injectable} from '@angular/core';
|
|
|
+import {ConfigService, RequsetData} from "./config.service";
|
|
|
+import {UserService} from "./user.service";
|
|
|
+import {from, interval, Observable, of, Subject} from "rxjs";
|
|
|
+import * as moment from 'moment';
|
|
|
+import {DOCUMENT} from "@angular/common";
|
|
|
+import {AlertController, LoadingController} from "@ionic/angular";
|
|
|
+import {ActivatedRoute, Router} from "@angular/router";
|
|
|
+import {AlertModal} from "../comm/modal/alert";
|
|
|
+import {WechatApi} from "../api/wechat";
|
|
|
+import {getUrlParams} from "../utils/urlUtils";
|
|
|
+
|
|
|
+@Injectable({
|
|
|
+ providedIn: 'root'
|
|
|
+})
|
|
|
+
|
|
|
+export class WxcpService {
|
|
|
+ private siteUrl: string = '';
|
|
|
+
|
|
|
+ constructor(@Inject(DOCUMENT) private document: any, private router: Router, private configService: ConfigService, private userService: UserService
|
|
|
+ , private alertModal: AlertModal, private wechatApi: WechatApi, private routeInfo: ActivatedRoute) {
|
|
|
+ this.configService.GetConfig().subscribe((config) => {
|
|
|
+ this.siteUrl = config.siteUrl;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ isWechat = () => {
|
|
|
+ return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
|
|
|
+ }
|
|
|
+
|
|
|
+ wxAuth = async <Object>(routePath: string): Promise<string> => {
|
|
|
+ try {
|
|
|
+ const urlParams = getUrlParams() as any;
|
|
|
+ const openId = urlParams["wxUserId"] || this.userService.getWxUserID();
|
|
|
+ const code = urlParams["code"];
|
|
|
+
|
|
|
+ if (this.isWechat()) {
|
|
|
+ if (openId) {
|
|
|
+ this.userService.setWxUserID(openId);
|
|
|
+ } else {
|
|
|
+ if (!code) {
|
|
|
+ const loading = await this.alertModal.loading('微信授权中,请稍等...');
|
|
|
+ await loading.present();
|
|
|
+
|
|
|
+ let oAuthUrl_redirectUrl = this.siteUrl + "?routePath=" + encodeURIComponent(routePath); //www.aa.com/mobile?routePath=xxxxx
|
|
|
+ this.wechatApi.getOAuthUrl(encodeURIComponent(oAuthUrl_redirectUrl)).subscribe((res: any) => {
|
|
|
+ if (res) {
|
|
|
+ window.location.replace(res);
|
|
|
+ } else {
|
|
|
+ this.alertModal.alert("微信授权失败!");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ const loading = await this.alertModal.loading('微信授权成功,请稍等...');
|
|
|
+ await loading.present();
|
|
|
+ await this.wechatApi.getWxCpUserId(code).subscribe((res: any) => {
|
|
|
+ this.userService.setWxUserID(res);
|
|
|
+
|
|
|
+ const currentUrl = routePath + (routePath.indexOf("?") >= 0 ? "&" : "?") + "wxUserId=" + res;
|
|
|
+
|
|
|
+ this.router.navigate([currentUrl], {
|
|
|
+ relativeTo: this.routeInfo,
|
|
|
+ queryParams: {random: Math.random(), op: 0}
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return "";
|
|
|
+ } catch (e) {
|
|
|
+ return Promise.reject(null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|