auth.service.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { Injectable } from '@angular/core';
  2. import * as Parse from 'parse';
  3. import { Observable, of } from 'rxjs';
  4. import { tap, delay } from 'rxjs/operators';
  5. import { Router } from '@angular/router';
  6. @Injectable({
  7. providedIn: 'root',
  8. })
  9. export class AuthService {
  10. isLoggedIn = false;
  11. company:string = localStorage.getItem('company') || 'Qje9D4bqol'
  12. redirectUrl: string = 'tabs';
  13. constructor(private router: Router) {}
  14. authMobile(
  15. mobile: string,
  16. password?: string,
  17. nickname?: string,
  18. code?: string,
  19. register?: boolean
  20. ) {
  21. return new Promise((resolve, reject) => {
  22. Parse.Cloud.run('auth_mobile', {
  23. c: this.company,
  24. mobile: mobile,
  25. password: password,
  26. code: code,
  27. register: register,
  28. nickname: nickname,
  29. })
  30. .then((authData) => {
  31. console.log(authData);
  32. if (authData) {
  33. // let sessionToken = authData.get('sessionToken');
  34. Parse.User.become(authData)
  35. .then(async (data) => {
  36. console.log(data);
  37. this.redirectUrl = this.redirectUrl ? this.redirectUrl : 'tabs';
  38. this.router.navigate([this.redirectUrl])
  39. resolve(data);
  40. })
  41. .catch((err) => {
  42. console.error(err);
  43. reject('登录失败,token已过期');
  44. });
  45. }else{
  46. reject(authData.message);
  47. }
  48. })
  49. .catch((err) => {
  50. console.error(err.message);
  51. reject(err.message);
  52. });
  53. });
  54. }
  55. clearReWechat(cid: string) {
  56. // 清楚过期登录状态,重新登陆
  57. localStorage.clear(); // 清楚过期登录状态,重新登陆
  58. window.location.search = `c=${cid}`; // TODOLIST 此处需要通过循环清楚CODE,STATE等微信后缀,重新拼接用户的QueryParams
  59. }
  60. logout(): void {
  61. localStorage.clear(); // 清楚过期登录状态,重新登陆
  62. Parse.User.logOut().then((user) => {
  63. this.router.navigate(['login']);
  64. });
  65. }
  66. }