Auth.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import FetchRequest from "@/api/FetchRequest";
  2. import { logout } from '@/api/Login'
  3. import { useWsStore } from '@/store/WsStore'
  4. import {useUserStore} from "@/store/userStore";
  5. import {useChatStore} from "@/store/chatStore";
  6. import MessageUtils from "@/utils/MessageUtils";
  7. class Auth {
  8. static getToken = () : string => {
  9. return uni.getStorageSync("access_token") ?? "";
  10. };
  11. static setToken = (token : string) : void => {
  12. uni.setStorageSync("access_token", token);
  13. };
  14. static getUserName = () : string => {
  15. return uni.getStorageSync("userName") ?? "";
  16. };
  17. static setUserName = (userName : string) : void => {
  18. uni.setStorageSync("userName", userName);
  19. };
  20. static getregisterID = () : string => {
  21. return uni.getStorageSync("jiguang_registerID") ?? "";
  22. };
  23. static setregisterID = (token : string) : void => {
  24. uni.setStorageSync("jiguang_registerID", token);
  25. };
  26. static getfontSize = () :number => {
  27. let fontSize= uni.getStorageSync("APP_fontSize")
  28. if(fontSize>0){
  29. return fontSize;
  30. }
  31. return 20;
  32. };
  33. static setfontSize = (fontSize : number) : void => {
  34. uni.setStorageSync("APP_fontSize", fontSize);
  35. };
  36. static getvoipUUID = () : string => {
  37. return uni.getStorageSync("voip_UUID") ?? "";
  38. };
  39. static setvoipUUID = (token : string) : void => {
  40. uni.setStorageSync("voip_UUID", token);
  41. };
  42. static getvoipTk = () : string => {
  43. return uni.getStorageSync("voipTk") ?? "";
  44. };
  45. static setvoipTk = (token : string) : void => {
  46. uni.setStorageSync("voipTk", token);
  47. };
  48. static setRefreshToken = (token : string) : void => {
  49. uni.setStorageSync("refresh_token", token);
  50. };
  51. static getRefreshToken = () : string => {
  52. return uni.getStorageSync("refresh_token") ?? "";
  53. };
  54. static clearToken = () : void => {
  55. uni.removeStorageSync("access_token");
  56. };
  57. static isLogin = () => {
  58. return new Promise((resolve, reject) => {
  59. const header : HeadersInit = {
  60. "Accept": "application/json",
  61. "Content-Type": "application/json",
  62. "Authorization": "Bearer " + Auth.getToken()
  63. };
  64. const config : RequestInit = {
  65. method: 'GET',
  66. mode: "cors",
  67. headers: header,
  68. };
  69. FetchRequest.fetch(`${FetchRequest.getHost()}/api/sys/users/my`, config)
  70. .then(res => {
  71. if (res.data.code === 200) {
  72. uni.setStorageSync('userId',res.data.id)
  73. uni.setStorageSync('userName', res.data.name);
  74. resolve(true)
  75. }else{
  76. reject(false)
  77. }
  78. }).catch(err => {
  79. console.error(FetchRequest.getHost())
  80. console.error(err)
  81. reject(false)
  82. })
  83. })
  84. }
  85. static logout = () => {
  86. logout()
  87. .finally(() => {
  88. console.error('logout')
  89. uni.setStorageSync('userId','')
  90. uni.setStorageSync('userName','');
  91. useChatStore().clearMessage();
  92. this.clearToken();
  93. useWsStore().close(true)
  94. uni.redirectTo({
  95. url:"/pages/login/login",
  96. fail(err) {
  97. MessageUtils.error('无法跳转到登录界面');
  98. }
  99. })
  100. })
  101. }
  102. }
  103. export default Auth;