mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-11-12 20:23:52 +08:00
This commit is contained in:
@@ -1,9 +1,22 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { createStorage, storage } from '@/utils/Storage';
|
||||
import { store } from '@/store';
|
||||
import { ACCESS_TOKEN, CURRENT_CONFIG, CURRENT_USER, IS_LOCKSCREEN } from '@/store/mutation-types';
|
||||
import {
|
||||
ACCESS_TOKEN,
|
||||
CURRENT_CONFIG,
|
||||
CURRENT_LOGIN_CONFIG,
|
||||
CURRENT_USER,
|
||||
IS_LOCKSCREEN,
|
||||
} from '@/store/mutation-types';
|
||||
import { ResultEnum } from '@/enums/httpEnum';
|
||||
import { getConfig, getUserInfo, login, logout } from '@/api/system/user';
|
||||
import {
|
||||
getConfig,
|
||||
getLoginConfig,
|
||||
getUserInfo,
|
||||
login,
|
||||
logout,
|
||||
mobileLogin,
|
||||
} from '@/api/system/user';
|
||||
const Storage = createStorage({ storage: localStorage });
|
||||
|
||||
export interface UserInfoState {
|
||||
@@ -34,6 +47,7 @@ export interface UserInfoState {
|
||||
lastLoginAt: string;
|
||||
lastLoginIp: string;
|
||||
openId: string;
|
||||
inviteCode: string;
|
||||
}
|
||||
|
||||
export interface ConfigState {
|
||||
@@ -42,6 +56,13 @@ export interface ConfigState {
|
||||
wsAddr: string;
|
||||
}
|
||||
|
||||
export interface LoginConfigState {
|
||||
loginRegisterSwitch: number;
|
||||
loginCaptchaSwitch: number;
|
||||
loginProtocol: string;
|
||||
loginPolicy: string;
|
||||
}
|
||||
|
||||
export interface IUserState {
|
||||
token: string;
|
||||
username: string;
|
||||
@@ -50,6 +71,7 @@ export interface IUserState {
|
||||
permissions: any[];
|
||||
info: UserInfoState | null;
|
||||
config: ConfigState | null;
|
||||
loginConfig: LoginConfigState | null;
|
||||
}
|
||||
|
||||
export const useUserStore = defineStore({
|
||||
@@ -62,6 +84,7 @@ export const useUserStore = defineStore({
|
||||
permissions: [],
|
||||
info: Storage.get(CURRENT_USER, null),
|
||||
config: Storage.get(CURRENT_CONFIG, null),
|
||||
loginConfig: Storage.get(CURRENT_LOGIN_CONFIG, null),
|
||||
}),
|
||||
getters: {
|
||||
getToken(): string {
|
||||
@@ -85,6 +108,9 @@ export const useUserStore = defineStore({
|
||||
getConfig(): ConfigState | null {
|
||||
return this.config;
|
||||
},
|
||||
getLoginConfig(): LoginConfigState | null {
|
||||
return this.loginConfig;
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
setToken(token: string) {
|
||||
@@ -108,10 +134,20 @@ export const useUserStore = defineStore({
|
||||
setConfig(config: ConfigState | null) {
|
||||
this.config = config;
|
||||
},
|
||||
// 登录
|
||||
setLoginConfig(config: LoginConfigState | null) {
|
||||
this.loginConfig = config;
|
||||
},
|
||||
// 账号登录
|
||||
async login(userInfo) {
|
||||
return await this.handleLogin(login(userInfo));
|
||||
},
|
||||
// 手机号登录
|
||||
async mobileLogin(userInfo) {
|
||||
return await this.handleLogin(mobileLogin(userInfo));
|
||||
},
|
||||
async handleLogin(request: Promise<any>) {
|
||||
try {
|
||||
const response = await login(userInfo);
|
||||
const response = await request;
|
||||
const { data, code } = response;
|
||||
if (code === ResultEnum.SUCCESS) {
|
||||
const ex = 30 * 24 * 60 * 60 * 1000;
|
||||
@@ -150,7 +186,7 @@ export const useUserStore = defineStore({
|
||||
});
|
||||
});
|
||||
},
|
||||
// 获取用户配置
|
||||
// 获取基础配置
|
||||
GetConfig() {
|
||||
const that = this;
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -166,6 +202,22 @@ export const useUserStore = defineStore({
|
||||
});
|
||||
});
|
||||
},
|
||||
// 获取登录配置
|
||||
LoadLoginConfig: function () {
|
||||
const that = this;
|
||||
return new Promise((resolve, reject) => {
|
||||
getLoginConfig()
|
||||
.then((res) => {
|
||||
const result = res as unknown as LoginConfigState;
|
||||
that.setLoginConfig(result);
|
||||
storage.set(CURRENT_LOGIN_CONFIG, result);
|
||||
resolve(res);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
},
|
||||
// 登出
|
||||
async logout() {
|
||||
try {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
export const ACCESS_TOKEN = 'ACCESS-TOKEN'; // 用户token
|
||||
export const CURRENT_USER = 'CURRENT-USER'; // 当前用户信息
|
||||
export const CURRENT_CONFIG = 'CURRENT-CONFIG'; // 当前用户信息
|
||||
export const CURRENT_CONFIG = 'CURRENT-CONFIG'; // 当前基础配置
|
||||
export const CURRENT_LOGIN_CONFIG = 'CURRENT-LOGIN-CONFIG'; // 当前登录配置
|
||||
export const IS_LOCKSCREEN = 'IS-LOCKSCREEN'; // 是否锁屏
|
||||
export const TABS_ROUTES = 'TABS-ROUTES'; // 标签页
|
||||
|
||||
Reference in New Issue
Block a user