user.service.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { Injectable, Inject } from '@angular/core';
  2. import { DOCUMENT } from '@angular/common';
  3. import { ConfigService, RequsetData } from "./config.service";
  4. import { from, Observable } from "rxjs";
  5. @Injectable({ providedIn: 'root' })
  6. export class UserService {
  7. win: any;
  8. lStorage: any;
  9. userInfo: any;
  10. userExt: any;
  11. openIDName = "userOpenID";
  12. constructor(@Inject(DOCUMENT) private document: any) {
  13. this.win = this.document.defaultView;
  14. this.lStorage = this.win.localStorage;
  15. }
  16. SetUser(user: any): void {
  17. this.userInfo = user;
  18. //this.document.cookie=`username=${user.username};path=/`;
  19. this.lStorage['userinfo'] = JSON.stringify(this.userInfo);
  20. }
  21. GetUser(): any {
  22. if (this.lStorage['userinfo']) {
  23. this.userInfo = JSON.parse(this.lStorage['userinfo']);
  24. }
  25. return this.userInfo;
  26. }
  27. SetFunctionCode(functionCode: string): void {
  28. this.lStorage['functionCodes'] = functionCode;
  29. }
  30. GetFunctionCode(): string {
  31. return this.lStorage['functionCodes'];
  32. }
  33. SetExt(ext: any) {
  34. this.lStorage['userExt'] = JSON.stringify(ext);
  35. }
  36. GetExt() {
  37. return JSON.parse(this.lStorage['userExt']);
  38. }
  39. SetToken(token: string) {
  40. this.lStorage['token'] = token;
  41. }
  42. GetToken() {
  43. return this.lStorage['token'];
  44. }
  45. Remember(loginId: string, pwd: string) {
  46. this.lStorage['loginId'] = loginId;
  47. this.lStorage['pwd'] = pwd;
  48. }
  49. GetRemember() {
  50. if (this.lStorage['loginId'] && this.lStorage['pwd']) {
  51. return {
  52. loginId: this.lStorage['loginId'],
  53. pwd: this.lStorage['pwd']
  54. }
  55. } else {
  56. return null;
  57. }
  58. }
  59. ClearRemember() {
  60. this.lStorage.removeItem('loginId');
  61. this.lStorage.removeItem('pwd');
  62. }
  63. ClearUser(): void {
  64. this.lStorage.removeItem('userinfo');
  65. this.lStorage.removeItem('token');
  66. this.lStorage.removeItem(this.openIDName);
  67. this.ClearRemember();
  68. }
  69. setOpenID(openID: string) {
  70. this.lStorage[this.openIDName] = openID;
  71. }
  72. getOpenID() {
  73. return this.lStorage[this.openIDName] || "";
  74. }
  75. }