161 lines
4.2 KiB
TypeScript
161 lines
4.2 KiB
TypeScript
import { defineStore } from 'pinia';
|
|
import {login as apiLogin, logout as apiLogout, getInfo as apiGetInfo, } from '@/api/user';
|
|
import { getToken, setToken, removeToken } from '@/utils/auth';
|
|
import router, { resetRouter } from '@/router';
|
|
import tagsViewStore from './tagsView';
|
|
import permissionStore from './permission';
|
|
|
|
export interface IUserState {
|
|
token: string;
|
|
userId: string,
|
|
name: string;
|
|
avatar: string;
|
|
introduction: string;
|
|
roles: string[];
|
|
}
|
|
|
|
export default defineStore({
|
|
id: 'user',
|
|
state: ():{
|
|
address: string;
|
|
signature: string;
|
|
sex: string;
|
|
roles: any[];
|
|
description: string;
|
|
avatar: string;
|
|
token: any;
|
|
phone: string;
|
|
nickname: string;
|
|
id: string;
|
|
email: string;
|
|
age: string;
|
|
username: string
|
|
} => ({
|
|
token: getToken(),
|
|
id:'',
|
|
username:'',
|
|
address:'',
|
|
phone:'',
|
|
email:'',
|
|
age:'',
|
|
signature:'',
|
|
avatar:'',
|
|
nickname:'',
|
|
sex:'',
|
|
description:'',
|
|
roles:['admin']
|
|
}),
|
|
getters: {},
|
|
actions: {
|
|
// user login
|
|
|
|
|
|
login(userInfo):Promise<void> {
|
|
const { username, password } = userInfo;
|
|
return new Promise((resolve, reject) => {
|
|
apiLogin({ user: username.trim(), password: password }).then(response => {
|
|
const { data } = response;
|
|
this.token = data.token;
|
|
setToken(data.token);
|
|
console.log("token", this.token)
|
|
router.push('/')
|
|
resolve();
|
|
console.log('2222',response)
|
|
}).catch(error => {
|
|
console.log('3333',error)
|
|
reject(error);
|
|
});
|
|
});
|
|
},
|
|
|
|
// get user info
|
|
getInfo() {
|
|
return new Promise((resolve, reject) => {
|
|
apiGetInfo(this.token).then(response => {
|
|
const { data } = response;
|
|
console.log('66666',data)
|
|
if (!data) {
|
|
reject('Verification failed, please Login again.');
|
|
}
|
|
const { id,username,address,phone,email,age,signature,avatar,nickname,sex,description } = data;
|
|
// roles must be a non-empty array
|
|
// if (!roles || roles.length <= 0) {
|
|
// reject('getInfo: roles must be a non-null array!');
|
|
// }
|
|
this.id = id;
|
|
// this.roles = roles;
|
|
// this.roles =['admin'];
|
|
this.username = username;
|
|
this.avatar = avatar;
|
|
this.description = description;
|
|
this.sex = sex;
|
|
this.age = age;
|
|
this.address = address;
|
|
this.phone = phone;
|
|
this.email = email;
|
|
this.signature = signature;
|
|
this.nickname = nickname;
|
|
resolve(data);
|
|
}).catch(error => {
|
|
reject(error);
|
|
});
|
|
});
|
|
},
|
|
|
|
// user logout
|
|
logout():Promise<void> {
|
|
console.log("token",this.token)
|
|
return new Promise((resolve, reject) => {
|
|
apiLogout(this.token).then(() => {
|
|
this.token = '';
|
|
this.roles = [];
|
|
console.log('退出',this.token)
|
|
removeToken();
|
|
resetRouter();
|
|
// reset visited views and cached views
|
|
// to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
|
|
tagsViewStore().delAllViews();
|
|
resolve();
|
|
}).catch(error => {
|
|
reject(error);
|
|
});
|
|
});
|
|
},
|
|
|
|
// remove token
|
|
resetToken() {
|
|
this.token = '';
|
|
this.roles = [];
|
|
removeToken();
|
|
},
|
|
|
|
// dynamically modify permissions
|
|
async changeRoles(role) {
|
|
const token = role + '-token';
|
|
|
|
this.token = token;
|
|
setToken(token);
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const infoRes = await this.getInfo() as any;
|
|
let roles : any[] = [];
|
|
if (infoRes.roles) {
|
|
roles = infoRes.roles;
|
|
}
|
|
roles = ['admin']
|
|
resetRouter();
|
|
|
|
// generate accessible routes map based on roles
|
|
const accessRoutes = await permissionStore().generateRoutes(roles);
|
|
// dynamically add accessible routes
|
|
// router.addRoutes(accessRoutes);
|
|
accessRoutes.forEach(item => {
|
|
router.addRoute(item);
|
|
});
|
|
|
|
// reset visited views and cached views
|
|
tagsViewStore().delAllViews();
|
|
}
|
|
}
|
|
});
|