| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- import FetchRequest from "@/api/FetchRequest";
- import { logout } from '@/api/Login'
- import { useWsStore } from '@/store/WsStore'
- import {useUserStore} from "@/store/userStore";
- import {useChatStore} from "@/store/chatStore";
- import MessageUtils from "@/utils/MessageUtils";
- class Auth {
- static getToken = () : string => {
- return uni.getStorageSync("access_token") ?? "";
- };
- static setToken = (token : string) : void => {
- uni.setStorageSync("access_token", token);
- };
-
- static getUserName = () : string => {
- return uni.getStorageSync("userName") ?? "";
- };
-
- static setUserName = (userName : string) : void => {
- uni.setStorageSync("userName", userName);
- };
-
- static getregisterID = () : string => {
- return uni.getStorageSync("jiguang_registerID") ?? "";
- };
-
- static setregisterID = (token : string) : void => {
- uni.setStorageSync("jiguang_registerID", token);
- };
-
- static getfontSize = () :number => {
- let fontSize= uni.getStorageSync("APP_fontSize")
- if(fontSize>0){
- return fontSize;
- }
- return 20;
- };
-
- static setfontSize = (fontSize : number) : void => {
- uni.setStorageSync("APP_fontSize", fontSize);
- };
-
- static getvoipUUID = () : string => {
- return uni.getStorageSync("voip_UUID") ?? "";
- };
-
- static setvoipUUID = (token : string) : void => {
- uni.setStorageSync("voip_UUID", token);
- };
-
- static getvoipTk = () : string => {
- return uni.getStorageSync("voipTk") ?? "";
- };
-
- static setvoipTk = (token : string) : void => {
- uni.setStorageSync("voipTk", token);
- };
- static setRefreshToken = (token : string) : void => {
- uni.setStorageSync("refresh_token", token);
- };
- static getRefreshToken = () : string => {
- return uni.getStorageSync("refresh_token") ?? "";
- };
- static clearToken = () : void => {
- uni.removeStorageSync("access_token");
- };
- static isLogin = () => {
- return new Promise((resolve, reject) => {
- const header : HeadersInit = {
- "Accept": "application/json",
- "Content-Type": "application/json",
- "Authorization": "Bearer " + Auth.getToken()
- };
- const config : RequestInit = {
- method: 'GET',
- mode: "cors",
- headers: header,
- };
- FetchRequest.fetch(`${FetchRequest.getHost()}/api/sys/users/my`, config)
- .then(res => {
- if (res.data.code === 200) {
- uni.setStorageSync('userId',res.data.id)
- uni.setStorageSync('userName', res.data.name);
- resolve(true)
- }else{
- reject(false)
- }
- }).catch(err => {
- console.error(FetchRequest.getHost())
- console.error(err)
- reject(false)
- })
- })
- }
- static logout = () => {
- logout()
- .finally(() => {
- console.error('logout')
- uni.setStorageSync('userId','')
- uni.setStorageSync('userName','');
- useChatStore().clearMessage();
- this.clearToken();
- useWsStore().close(true)
- uni.redirectTo({
- url:"/pages/login/login",
- fail(err) {
- MessageUtils.error('无法跳转到登录界面');
- }
- })
- })
- }
- }
- export default Auth;
|