feat:增加权限控制
This commit is contained in:
@@ -1,3 +1,3 @@
|
|||||||
import createService from 'vue3-common/utils/axiosUtil'
|
import createService from '@/utils/axiosUtil'
|
||||||
|
|
||||||
export const cloudService = createService()
|
export const cloudService = createService()
|
||||||
|
|||||||
@@ -1,16 +1,15 @@
|
|||||||
import { cloudService } from './index'
|
import { cloudService } from './index'
|
||||||
import { ISession } from '@/types/auth'
|
import { ISession } from '@/types/auth'
|
||||||
|
|
||||||
export const loginHomeApi = (name: string, password: string): Promise<ISession> =>
|
export const loginAdminApi = (username: string, password: string): Promise<ISession> => {
|
||||||
cloudService({
|
const data = new FormData()
|
||||||
url: '/home-service/session',
|
data.append('username', username)
|
||||||
method: 'post',
|
data.append('password', password)
|
||||||
params: { name, password }
|
|
||||||
})
|
|
||||||
|
|
||||||
export const loginAdminApi = (name: string, password: string): Promise<ISession> =>
|
return cloudService({
|
||||||
cloudService({
|
url: '/blog-api/session',
|
||||||
url: '/admin-service/session',
|
|
||||||
method: 'post',
|
method: 'post',
|
||||||
params: { name, password }
|
data
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ import { onMounted, reactive, ref } from 'vue'
|
|||||||
import { loginRules } from '@/utils/element/elRules'
|
import { loginRules } from '@/utils/element/elRules'
|
||||||
import { getLocalStorage, setLocalStorage } from 'vue3-common/utils/storageUtil'
|
import { getLocalStorage, setLocalStorage } from 'vue3-common/utils/storageUtil'
|
||||||
import { isMobile } from 'vue3-common/utils/layoutUtil'
|
import { isMobile } from 'vue3-common/utils/layoutUtil'
|
||||||
|
import { loginAdminApi } from '@/apis/session'
|
||||||
|
|
||||||
const rules = reactive<FormRules>(loginRules)
|
const rules = reactive<FormRules>(loginRules)
|
||||||
const authStore = useAuthStore()
|
const authStore = useAuthStore()
|
||||||
@@ -84,18 +85,13 @@ const handleLogin = async () => {
|
|||||||
authStore.loginLoading = true
|
authStore.loginLoading = true
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (authStore.loginForm.username === 'admin' && authStore.loginForm.password === '19940822Cxx') {
|
await authStore.handleLogin()
|
||||||
await router.push('/dashboard')
|
userStore.handleSession(authStore.session)
|
||||||
} else {
|
await router.push('/dashboard')
|
||||||
ElMessage.error('账号或密码错误')
|
|
||||||
}
|
|
||||||
// await authStore.handleLogin(type)
|
|
||||||
// userStore.handleSession(authStore.homeSession)
|
|
||||||
|
|
||||||
// 2. 存储账户密码到localStorage中
|
if (authStore.loginForm.isRemember) {
|
||||||
// if (authStore.loginForm.isRemember) {
|
setLocalStorage('loginForm', JSON.stringify(authStore.loginForm))
|
||||||
// setLocalStorage('loginForm', JSON.stringify(authStore.loginForm))
|
}
|
||||||
// }
|
|
||||||
} finally {
|
} finally {
|
||||||
authStore.loginLoading = false
|
authStore.loginLoading = false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,12 +12,12 @@ export const useAuthStore = defineStore('auth', {
|
|||||||
},
|
},
|
||||||
loginLoading: false,
|
loginLoading: false,
|
||||||
isVerified: false,
|
isVerified: false,
|
||||||
homeSession: {} as ISession
|
session: {} as ISession
|
||||||
}),
|
}),
|
||||||
actions: {
|
actions: {
|
||||||
async handleLogin() {
|
async handleLogin() {
|
||||||
const { username, password } = this.loginForm
|
const { username, password } = this.loginForm
|
||||||
this.homeSession = await loginAdminApi(username, password)
|
this.session = await loginAdminApi(username, password)
|
||||||
ElMessage.success('登录成功')
|
ElMessage.success('登录成功')
|
||||||
},
|
},
|
||||||
resetLoginForm() {
|
resetLoginForm() {
|
||||||
|
|||||||
@@ -38,9 +38,7 @@ export const useUserStore = defineStore('user', {
|
|||||||
removeLocalStorage('loginForm')
|
removeLocalStorage('loginForm')
|
||||||
},
|
},
|
||||||
handleSession(session: ISession) {
|
handleSession(session: ISession) {
|
||||||
this.setToken(session.saToken.tokenValue)
|
this.setToken(`${session.token_type} ${session.access_token}`)
|
||||||
this.setUserId(session.userInfo.id)
|
|
||||||
this.userInfo = session.userInfo
|
|
||||||
},
|
},
|
||||||
async queryUser() {
|
async queryUser() {
|
||||||
this.userInfo = await queryUserApi(Number(this.getUserId()))
|
this.userInfo = await queryUserApi(Number(this.getUserId()))
|
||||||
@@ -48,10 +46,6 @@ export const useUserStore = defineStore('user', {
|
|||||||
async updateUser() {
|
async updateUser() {
|
||||||
await updateUserApi(this.userInfo.id, this.userInfo)
|
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) {
|
setToken(token: string) {
|
||||||
setLocalStorage('token', token)
|
setLocalStorage('token', token)
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -21,20 +21,8 @@ export interface IUser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface ISession {
|
export interface ISession {
|
||||||
saToken: {
|
access_token: string;
|
||||||
tokenName: string;
|
token_type: string;
|
||||||
tokenValue: string;
|
|
||||||
isLogin: boolean;
|
|
||||||
loginId: string;
|
|
||||||
loginType: string;
|
|
||||||
tokenTimeout: number;
|
|
||||||
sessionTimeout: number;
|
|
||||||
tokenSessionTimeout: number;
|
|
||||||
tokenActiveTimeout: number;
|
|
||||||
loginDevice: string;
|
|
||||||
tag: string;
|
|
||||||
};
|
|
||||||
userInfo: IUser;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IAccount {
|
export interface IAccount {
|
||||||
|
|||||||
89
src/utils/axiosUtil.ts
Normal file
89
src/utils/axiosUtil.ts
Normal 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
|
||||||
@@ -17,7 +17,13 @@
|
|||||||
"src/*"
|
"src/*"
|
||||||
],
|
],
|
||||||
"vue3-common/*": ["node_modules/vue3-common/dist/*"]
|
"vue3-common/*": ["node_modules/vue3-common/dist/*"]
|
||||||
}
|
},
|
||||||
|
"lib": [
|
||||||
|
"decorators",
|
||||||
|
"dom",
|
||||||
|
"es2015",
|
||||||
|
"scripthost"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"]
|
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user