|
@@ -0,0 +1,105 @@
|
|
|
+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 { Router } from "@angular/router";
|
|
|
+import { AccountService } from './account.service';
|
|
|
+
|
|
|
+@Injectable({
|
|
|
+ providedIn: 'root'
|
|
|
+})
|
|
|
+
|
|
|
+export class WxService {
|
|
|
+ win: any;
|
|
|
+ isWxLogin: boolean = true;
|
|
|
+ private siteUrl: string = '';
|
|
|
+ private getOauthUrl: string = '/api/wx/GetOauthUrlForBase';
|
|
|
+ private getOpenIdUrl: string = '/api/wx/GetOpenID';
|
|
|
+
|
|
|
+ constructor(@Inject(DOCUMENT) private document: any, private router: Router, private configService: ConfigService, private userService: UserService, private alertController: AlertController, public loadingController: LoadingController,
|
|
|
+ private accountService: AccountService ) {
|
|
|
+ this.configService.GetConfig().subscribe((config) => {
|
|
|
+ this.siteUrl = config.siteUrl;
|
|
|
+ this.isWxLogin = config.isWxLogin;
|
|
|
+ });
|
|
|
+
|
|
|
+ this.win = this.document.defaultView;
|
|
|
+ }
|
|
|
+
|
|
|
+ isWechat = () => {
|
|
|
+ return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
|
|
|
+ }
|
|
|
+
|
|
|
+ async getWechatOpenId() {
|
|
|
+ if (this.isWxLogin && this.isWechat()) {
|
|
|
+ if (this.userService.getOpenID()) {
|
|
|
+ this.accountService.loginByOpenID();
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ let code = this.getUrlCode()["code"];
|
|
|
+
|
|
|
+ const loading = await this.loadingController.create({
|
|
|
+ cssClass: 'my-custom-class',
|
|
|
+ message: '微信授权中,请稍后...',
|
|
|
+ duration: 2000,
|
|
|
+ });
|
|
|
+ await loading.present();
|
|
|
+
|
|
|
+ if (!code) {
|
|
|
+ this.configService.HttpGetRomote(this.getOauthUrl, null).subscribe(result => {
|
|
|
+ loading.dismiss();
|
|
|
+ if (result.IsSuccess)
|
|
|
+ window.location.href = result.Data;
|
|
|
+ else
|
|
|
+ this.presentAlert("获取微信授权地址失败!" + result.Message);
|
|
|
+ }, () => {
|
|
|
+ loading.dismiss();
|
|
|
+ this.presentAlert("微信授权地址请求失败!");
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ this.configService.HttpGetRomote(this.getOpenIdUrl, { code: code }).subscribe(result => {
|
|
|
+ loading.dismiss();
|
|
|
+ if (result.IsSuccess) {
|
|
|
+ this.userService.setOpenID(result.Data);
|
|
|
+ this.accountService.loginByOpenID();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ this.presentAlert("获取微信openID失败!" + result.Message);
|
|
|
+ }, () => {
|
|
|
+ loading.dismiss();
|
|
|
+ this.presentAlert("微信openID地址请求失败!");
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ getUrlCode() {
|
|
|
+ // 截取url中的code方法
|
|
|
+ var url = location.href;
|
|
|
+ var theRequest = new Object();
|
|
|
+ if (url.indexOf("?") != -1) {
|
|
|
+ var paramsUrl = url.split('?')[1];
|
|
|
+ var strs = paramsUrl.split("&");
|
|
|
+ for (var i = 0; i < strs.length; i++) {
|
|
|
+ theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return theRequest;
|
|
|
+ }
|
|
|
+
|
|
|
+ async presentAlert(msg: string) {
|
|
|
+ const alert = await this.alertController.create({
|
|
|
+ header: '提示',
|
|
|
+ subHeader: '',
|
|
|
+ message: msg,
|
|
|
+ buttons: ['确定']
|
|
|
+ });
|
|
|
+
|
|
|
+ await alert.present();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|