feat:增加权限控制

This commit is contained in:
2025-09-23 15:41:50 +08:00
parent e8a3339f43
commit 724561df55
8 changed files with 118 additions and 46 deletions

View File

@@ -1,3 +1,3 @@
import createService from 'vue3-common/utils/axiosUtil'
import createService from '@/utils/axiosUtil'
export const cloudService = createService()

View File

@@ -1,16 +1,15 @@
import { cloudService } from './index'
import { ISession } from '@/types/auth'
export const loginHomeApi = (name: string, password: string): Promise<ISession> =>
cloudService({
url: '/home-service/session',
method: 'post',
params: { name, password }
})
export const loginAdminApi = (username: string, password: string): Promise<ISession> => {
const data = new FormData()
data.append('username', username)
data.append('password', password)
export const loginAdminApi = (name: string, password: string): Promise<ISession> =>
cloudService({
url: '/admin-service/session',
return cloudService({
url: '/blog-api/session',
method: 'post',
params: { name, password }
data
})
}

View File

@@ -58,6 +58,7 @@ import { onMounted, reactive, ref } from 'vue'
import { loginRules } from '@/utils/element/elRules'
import { getLocalStorage, setLocalStorage } from 'vue3-common/utils/storageUtil'
import { isMobile } from 'vue3-common/utils/layoutUtil'
import { loginAdminApi } from '@/apis/session'
const rules = reactive<FormRules>(loginRules)
const authStore = useAuthStore()
@@ -84,18 +85,13 @@ const handleLogin = async () => {
authStore.loginLoading = true
try {
if (authStore.loginForm.username === 'admin' && authStore.loginForm.password === '19940822Cxx') {
await router.push('/dashboard')
} else {
ElMessage.error('账号或密码错误')
}
// await authStore.handleLogin(type)
// userStore.handleSession(authStore.homeSession)
await authStore.handleLogin()
userStore.handleSession(authStore.session)
await router.push('/dashboard')
// 2. 存储账户密码到localStorage中
// if (authStore.loginForm.isRemember) {
// setLocalStorage('loginForm', JSON.stringify(authStore.loginForm))
// }
if (authStore.loginForm.isRemember) {
setLocalStorage('loginForm', JSON.stringify(authStore.loginForm))
}
} finally {
authStore.loginLoading = false
}

View File

@@ -12,12 +12,12 @@ export const useAuthStore = defineStore('auth', {
},
loginLoading: false,
isVerified: false,
homeSession: {} as ISession
session: {} as ISession
}),
actions: {
async handleLogin() {
const { username, password } = this.loginForm
this.homeSession = await loginAdminApi(username, password)
this.session = await loginAdminApi(username, password)
ElMessage.success('登录成功')
},
resetLoginForm() {

View File

@@ -38,9 +38,7 @@ export const useUserStore = defineStore('user', {
removeLocalStorage('loginForm')
},
handleSession(session: ISession) {
this.setToken(session.saToken.tokenValue)
this.setUserId(session.userInfo.id)
this.userInfo = session.userInfo
this.setToken(`${session.token_type} ${session.access_token}`)
},
async queryUser() {
this.userInfo = await queryUserApi(Number(this.getUserId()))
@@ -48,10 +46,6 @@ export const useUserStore = defineStore('user', {
async updateUser() {
await updateUserApi(this.userInfo.id, this.userInfo)
},
async updateAccountPassword(oldPassword: string, newPassword: string) {
console.log(this.userInfo.accountId)
// await updateAccountPasswordApi(this.userInfo.accountId, oldPassword, newPassword)
},
setToken(token: string) {
setLocalStorage('token', token)
},

View File

@@ -21,20 +21,8 @@ export interface IUser {
}
export interface ISession {
saToken: {
tokenName: string;
tokenValue: string;
isLogin: boolean;
loginId: string;
loginType: string;
tokenTimeout: number;
sessionTimeout: number;
tokenSessionTimeout: number;
tokenActiveTimeout: number;
loginDevice: string;
tag: string;
};
userInfo: IUser;
access_token: string;
token_type: string;
}
export interface IAccount {

89
src/utils/axiosUtil.ts Normal file
View File

@@ -0,0 +1,89 @@
import axios, { InternalAxiosRequestConfig, AxiosResponse, AxiosError } from 'axios'
import { ElMessageErrorTip } from 'vue3-common/utils/elUtil'
const createService = (baseURL = '', timeout = 5000) => {
const service = axios.create({
baseURL,
timeout
})
// 请求拦截器
service.interceptors.request.use(
(config: InternalAxiosRequestConfig) => {
const token = localStorage.getItem('token')
if (token) {
config.headers.Authorization = token
}
return config
},
(error: AxiosError) => {
return Promise.reject(error)
}
)
// 响应拦截器
service.interceptors.response.use(
(response: AxiosResponse) => {
return response.data
},
(error: AxiosError) => {
const { code, message } = error
if (error.response) {
// 发送请求成功 并收到服务器的响应
handleServiceError(error.response, message)
return Promise.reject(error)
} else if (error.request) {
// 发送请求成功 未收到服务器的响应
handleHttpError(code, message)
return Promise.reject(error)
} else {
// 其他错误情况 如发送请求失败等
ElMessageErrorTip(`前端错误 请联系管理人员:${message}`)
}
}
)
return service
}
/**
* 处理服务器响应错误
* @param response 服务器响应
* @param message 错误消息
*/
const handleServiceError = (response: AxiosResponse, message: string) => {
const { status, data, statusText } = response
switch (status) {
case 401:
ElMessageErrorTip('账号已过期 请重新登录')
window.location.href = '/'
break
case 500:
ElMessageErrorTip(`${data.message || statusText}`)
break
default:
ElMessageErrorTip(`服务器错误 请联系管理人员:${status}:${message}`)
}
}
/**
* 处理Http错误
* @param code code码
* @param message 消息
*/
const handleHttpError = (code: string | undefined, message: string) => {
switch (code) {
case 'ECONNABORTED':
case 'ETIMEDOUT':
ElMessageErrorTip('网络连接错误 请联系管理人员')
break
default:
ElMessageErrorTip(`网络错误 请联系管理人员:${code}:${message}`)
break
}
}
export default createService

View File

@@ -17,7 +17,13 @@
"src/*"
],
"vue3-common/*": ["node_modules/vue3-common/dist/*"]
}
},
"lib": [
"decorators",
"dom",
"es2015",
"scripthost"
]
},
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"]
}