This commit is contained in:
妖姐 2024-04-13 14:47:41 +08:00
parent 1172832d9b
commit 66f597f514
11 changed files with 98 additions and 52 deletions

View File

@ -3,4 +3,5 @@ NODE_ENV = 'development'
# 开发模式连生产服
VUE_APP_BASE_API = '/api-prod'
VUE_APP_COOKIE = ''
VUE_APP_COOKIE = ''
VUE_APP_INNER='http://nbxt.oa.x-lf.cn'

8
components.d.ts vendored
View File

@ -1,9 +1,9 @@
// generated by unplugin-vue-components
// We suggest you to commit this file into source control
// Read more: https://github.com/vuejs/core/pull/3399
import '@vue/runtime-core';
import '@vue/runtime-core'
export {};
export {}
declare module '@vue/runtime-core' {
export interface GlobalComponents {
@ -32,13 +32,9 @@ declare module '@vue/runtime-core' {
ElMenuItem: typeof import('element-plus/es')['ElMenuItem']
ElOption: typeof import('element-plus/es')['ElOption']
ElProgress: typeof import('element-plus/es')['ElProgress']
ElRadio: typeof import('element-plus/es')['ElRadio']
ElRadioButton: typeof import('element-plus/es')['ElRadioButton']
ElRadioGroup: typeof import('element-plus/es')['ElRadioGroup']
ElRow: typeof import('element-plus/es')['ElRow']
ElScrollbar: typeof import('element-plus/es')['ElScrollbar']
ElSelect: typeof import('element-plus/es')['ElSelect']
ElSlider: typeof import('element-plus/es')['ElSlider']
ElSubMenu: typeof import('element-plus/es')['ElSubMenu']
ElSwitch: typeof import('element-plus/es')['ElSwitch']
ElTable: typeof import('element-plus/es')['ElTable']

View File

@ -1,25 +1,39 @@
import request from '@/utils/request';
function getCurrentTimestamp() {
return new Date().getTime();
}
export function login(data) {
return request({
url: '/vue-element-admin/user/login',
url: '/auth/login',
method: 'post',
data
data,
headers: {
'Timestamp':getCurrentTimestamp()
}
});
}
export function getInfo(token) {
return request({
url: '/vue-element-admin/user/info',
method: 'get',
params: { token }
url: '/user/profile/get',
method: 'post',
params: {token},
headers: {
'Timestamp': getCurrentTimestamp()
}
});
}
export function logout(token) {
return request({
url: '/vue-element-admin/user/logout',
method: 'post',
params: { token }
url: '/auth/logout',
method: 'get',
params: { token },
headers: {
'Timestamp': getCurrentTimestamp()
}
});
}

View File

@ -20,7 +20,7 @@ router.beforeEach(async (to, from, next) => {
// determine whether the user has logged in
const hasToken = getToken();
console.log('88888',hasToken)
if (hasToken) {
if (to.path === '/login') {
// if is logged in, redirect to the home page
@ -28,7 +28,9 @@ router.beforeEach(async (to, from, next) => {
next({ path: '/' });
} else {
// determine whether the user has obtained his permission roles through getInfo
const hasRoles = userStore().roles && userStore().roles.length > 0;
// const hasRoles = userStore().roles && userStore().roles.length > 0;
const hasRoles = true;
// console.log('hasRoles=', hasRoles);
if (hasRoles) {
next();
@ -38,6 +40,7 @@ router.beforeEach(async (to, from, next) => {
// note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const infoRes = await userStore().getInfo() as any;
console.log('permissionuserinfor:',infoRes)
let roles = [];
if (infoRes.roles) {
roles = infoRes.roles;

View File

@ -60,7 +60,7 @@ export const constantRoutes:RouteRecordRaw[] = [
path: 'dashboard',
component: () => import('@/views/dashboard/index.vue'),
name: 'Dashboard',
meta: { title: 'Dashboard', icon: 'dashboard', affix: true }
meta: { title: '我管理的', icon: 'dashboard', affix: true }
}
]
},
@ -121,7 +121,7 @@ export const asyncRoutes:RouteRecordRaw[] = [
alwaysShow: true, // will always show the root menu
title: 'Permission',
icon: 'lock',
roles: ['admin', 'editor'] // you can set roles in root nav
roles: ['admin', 'editor','teacher_user'] // you can set roles in root nav
},
children: [
{
@ -130,7 +130,7 @@ export const asyncRoutes:RouteRecordRaw[] = [
name: 'PagePermission',
meta: {
title: 'Page Permission',
roles: ['admin'] // or you can only set roles in sub nav
roles: ['admin','teacher_user'] // or you can only set roles in sub nav
}
},
{
@ -148,7 +148,7 @@ export const asyncRoutes:RouteRecordRaw[] = [
name: 'RolePermission',
meta: {
title: 'Role Permission',
roles: ['admin']
roles: ['admin','teacher']
}
}
]

View File

@ -56,7 +56,7 @@ export default defineStore({
},
generateRoutes(roles: string[]) {
let accessedRoutes;
if (roles.includes('admin')) {
if (roles.includes('teacher_user')) {
accessedRoutes = asyncRoutes || [];
} else {
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles);

View File

@ -1,5 +1,5 @@
import { defineStore } from 'pinia';
import { login as apiLogin, logout as apiLogout, getInfo as apiGetInfo } from '@/api/user';
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';
@ -16,26 +16,52 @@ export interface IUserState {
export default defineStore({
id: 'user',
state: ():IUserState => ({
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(),
userId: '',
name: '',
avatar: '',
introduction: '',
roles: []
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({ username: username.trim(), password: password }).then(response => {
apiLogin({ user: username.trim(), password: password }).then(response => {
const { data } = response;
this.token = data.token;
setToken(data.token);
router.push('/')
resolve();
console.log('2222',response)
}).catch(error => {
console.log('3333',error)
reject(error);
});
});
@ -46,22 +72,28 @@ export default defineStore({
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 { roles, name, avatar, introduction } = data;
const { id,username,address,phone,email,age,signature,avatar,nickname,sex,description,roles } = data;
// roles must be a non-empty array
if (!roles || roles.length <= 0) {
reject('getInfo: roles must be a non-null array!');
}
this.roles = roles;
this.name = name;
this.id = id;
// this.roles = roles;
// this.roles =['admin'];
this.username = username;
this.avatar = avatar;
this.introduction = introduction;
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);

View File

@ -2,11 +2,12 @@ import axios from 'axios';
import store from '@/store';
import { getToken } from '@/utils/auth';
const BaseURL ='https://nbxt.oa.x-lf.cn'
// create an axios instance
const service = axios.create({
baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
baseURL: BaseURL, // url = base url + request url
// withCredentials: true, // send cookies when cross-domain requests
timeout: 5000 // request timeout
timeout: 10000 // request timeout
});
// request interceptor
@ -18,7 +19,7 @@ service.interceptors.request.use(
// let each request carry token
// ['X-Token'] is a custom headers key
// please modify it according to the actual situation
config.headers['X-Token'] = getToken();
config.headers['token'] = getToken();
}
return config;
},
@ -43,9 +44,9 @@ service.interceptors.response.use(
*/
response => {
const res = response.data;
console.log('999999',res)
// if the custom code is not 20000, it is judged as an error.
if (res.code !== 20000) {
if (res.code !== 20000 && res.code !== 200) {
ElMessage({
message: res.message || 'Error',
type: 'error',

View File

@ -15,7 +15,7 @@ export function isExternal(path) {
* @returns {Boolean}
*/
export function validUsername(str) {
const valid_map = ['admin', 'editor'];
const valid_map = ['admin', 'editor', 'teacher_user'];
return valid_map.indexOf(str.trim()) >= 0;
}

View File

@ -86,8 +86,8 @@ export default defineComponent({
};
return {
loginForm: {
username: 'admin',
password: '111111'
username: 'teacher_user',
password: '123456'
},
loginRules: {
username: [{ required: true, trigger: 'blur', validator: validateUsername }],

View File

@ -65,13 +65,12 @@ export default defineConfig(({ command, mode }) => {
host: 'localhost',
port: 8001,
proxy: {
'/api-test': {
target: 'https://api.midfar.com/dspt_test/api',
'/api-prod': {
target: 'https://nbxt.oa.x-lf.cn',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api-test/, '')
},
headers: {
Cookie: env.VUE_APP_COOKIE
pathRewrite: {
'^/api-prod': ''
}
}
}
}