auth.service.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import { Injectable } from '@angular/core';
  2. import Parse from 'parse';
  3. import { Router } from '@angular/router';
  4. import { textbookServer } from './textbook';
  5. import { catchError } from 'rxjs/operators';
  6. import { HttpClient } from '@angular/common/http';
  7. import { NzMessageService } from 'ng-zorro-antd/message';
  8. import { NzModalService } from 'ng-zorro-antd/modal';
  9. @Injectable({
  10. providedIn: 'root',
  11. })
  12. export class AuthServr {
  13. isLoggedIn = false;
  14. countdown: number = 60; //登录时验证码倒计时
  15. regcountdown: number = 60; //注册验证码倒计时
  16. resetcountdown: number = 60; //重置密码验证码倒计时
  17. redirectUrl: string = '';
  18. roterPath: any = {
  19. "国家级管理员": '/nav-admin',
  20. "工作联系人": '/nav-province-contact',
  21. "高校联系人": '/nav-province-school-contact',
  22. "评审专家": '/nav-review',
  23. "教师": '/nav-author/manage/space',
  24. };
  25. constructor(
  26. public router: Router,
  27. private textbook: textbookServer,
  28. private http: HttpClient,
  29. private message: NzMessageService
  30. ) {}
  31. //登录
  32. login(id: string) {
  33. return new Promise((resolve, reject) => {
  34. let host =
  35. (Parse as any).serverURL?.split('parse')?.[0] ||
  36. 'https://server.fmode.cn/';
  37. this.http
  38. .post(host + 'api/textbook/login', {
  39. id: id,
  40. })
  41. .pipe(
  42. catchError(async (e) => {
  43. reject(e);
  44. })
  45. )
  46. .subscribe((res: any) => {
  47. console.log(res);
  48. if (res?.code == 200) {
  49. let token = res.data.token;
  50. Parse.User.become(token)
  51. .then(async (data: any) => {
  52. console.log(data);
  53. await this.profileVerify();
  54. resolve(data);
  55. })
  56. .catch((err: any) => {
  57. console.log(err.message);
  58. if (err.message.indexOf('Invalid username/password.') != -1) {
  59. reject({ message: '用户名或密码不正确' });
  60. } else {
  61. reject({ message: '用户名或密码不正确' });
  62. }
  63. });
  64. }
  65. });
  66. });
  67. // return new Promise(async (resolve, reject) => {
  68. // let a = /^1[3456789]\d{9}$/;
  69. // if (String(username).match(a)) {
  70. // let query = new Parse.Query('_User');
  71. // query.equalTo('company', company);
  72. // query.equalTo('mobile', username);
  73. // let res = await query.first();
  74. // if (res?.id) {
  75. // username = res.get('username');
  76. // } else {
  77. // reject({ message: '用户不存在' });
  78. // return;
  79. // }
  80. // }
  81. // Parse.User.logIn(username, password)
  82. // .then(async (data: any) => {
  83. // console.log(data);
  84. // await this.profileVerify()
  85. // resolve(data);
  86. // })
  87. // .catch((err: any) => {
  88. // console.log(err.message);
  89. // if (err.message.indexOf('Invalid username/password.') != -1) {
  90. // reject({ message: '用户名或密码不正确' });
  91. // } else {
  92. // reject({ message: '用户名或密码不正确' });
  93. // }
  94. // });
  95. // });
  96. }
  97. //手机号注册/登录
  98. register(mobile: any, code: any, company: string) {
  99. return new Promise((resolve, reject) => {
  100. let host =
  101. (Parse as any).serverURL?.split('parse')?.[0] ||
  102. 'https://server.fmode.cn/';
  103. this.http
  104. .post(host + 'api/textbook/register', {
  105. company: company,
  106. mobile: mobile,
  107. code: code,
  108. })
  109. .pipe(
  110. catchError(async (e) => {
  111. reject(e);
  112. })
  113. )
  114. .subscribe((res: any) => {
  115. console.log(res);
  116. if (res?.code == 200) {
  117. let token = res.data.token;
  118. Parse.User.become(token)
  119. .then(async (data: any) => {
  120. console.log(data);
  121. await this.profileVerify();
  122. resolve(data);
  123. })
  124. .catch((err: any) => {
  125. console.log(err.message);
  126. if (err.message.indexOf('Invalid username/password.') != -1) {
  127. reject({ message: '用户名或密码不正确' });
  128. } else {
  129. reject({ message: '用户名或密码不正确' });
  130. }
  131. });
  132. }
  133. });
  134. });
  135. }
  136. //验证用户类型
  137. async profileVerify(modal?: NzModalService, manage?:boolean) {
  138. if(!Parse.User.current()?.id) return
  139. let query = new Parse.Query('Profile');
  140. query.equalTo('user', Parse.User.current()?.id);
  141. query.notEqualTo('isDeleted', true);
  142. query.include('user', 'user.department');
  143. let r = await query.first();
  144. if (r?.id) {
  145. let user = Parse.User.current();
  146. if (user?.get('isDeleted')) {
  147. !modal && this.message.error('该账号已被封禁');
  148. modal?.error({
  149. nzTitle: '该账号已被封禁',
  150. nzContent: '',
  151. });
  152. return;
  153. }
  154. switch (user?.get('accountState')) {
  155. case '已认证':
  156. if (r.get('identity')) {
  157. let profile = r.toJSON();
  158. localStorage.setItem('profile', JSON.stringify(profile));
  159. this.textbook.profile = profile;
  160. let url = this.roterPath[r.get('identity')]
  161. // if(!manage) url = '/home'
  162. this.router.navigate([url]);
  163. // this.message.success('登录成功');
  164. return;
  165. }
  166. this.message.warning('已认证暂无身份');
  167. break;
  168. case '待认证':
  169. !modal && this.message.warning('账号待认证,请等待审核完成');
  170. modal?.warning({
  171. nzTitle: '账号待认证,请等待审核完成',
  172. nzContent: '',
  173. });
  174. break;
  175. case '已禁用':
  176. !modal && this.message.error('账号已禁用');
  177. modal?.error({
  178. nzTitle: '账号已禁用',
  179. nzContent: '',
  180. });
  181. break;
  182. default:
  183. !modal && this.message.error('异常账号');
  184. modal?.error({
  185. nzTitle: '异常账号',
  186. nzContent: '',
  187. });
  188. // this.router.navigate(['/user/account_info']);
  189. break;
  190. }
  191. return;
  192. }
  193. this.router.navigate(['/user/account_info']);
  194. }
  195. logout(): void {
  196. Parse.User.logOut().then((user) => {
  197. // this.developerSer.companyId = ''
  198. window.localStorage.clear();
  199. this.router.navigate(['/user/login']);
  200. });
  201. }
  202. }