perf: perf axios config

This commit is contained in:
kailong321200875 2022-06-25 20:04:58 +08:00
parent 1878c2ede7
commit 39edd84023
5 changed files with 49 additions and 31 deletions

View File

@ -3,5 +3,7 @@ export interface IUserModel {
password: string password: string
check_password: string check_password: string
is_admin: number is_admin: number
code: string | number code?: string | number
token?: string
refreshToken?: string
} }

View File

@ -1,13 +1,15 @@
import { useAxios } from '@/hooks/web/useAxios' import { useAxios } from '@/hooks/web/useAxios'
import type { UserLoginType, UserType } from './types' import type { UserType } from './types'
import { IUserModel } from '@/api-types/user'
const request = useAxios() const request = useAxios()
export const loginApi = (data: UserLoginType) => { export const loginApi = async (data: Pick<IUserModel, 'user_name' | 'password'>) => {
return request.post({ const res = await request.post<IResponse<IUserModel>>({
url: '/user/login', url: '/user/login',
data data
}) })
return res && res.data
} }
export const loginOutApi = () => { export const loginOutApi = () => {

View File

@ -8,12 +8,14 @@ interface ICodeModel {
uuid: string uuid: string
} }
export const getCodeApi = async () => { export const getCodeApi = async (): Promise<IResponse<ICodeModel>> => {
const res = await request.get<IResponse<ICodeModel>>({ url: 'user/captcha' }) const res = await request.get({ url: 'user/captcha' })
return res && res.data return res && res.data
} }
export const registerApi = async (data: Omit<IUserModel, 'is_admin'>) => { export const registerApi = async (
const res = await request.post<IResponse<IUserModel>>({ url: 'user/register', data }) data: Omit<IUserModel, 'is_admin'>
): Promise<IResponse<IUserModel>> => {
const res = await request.post({ url: 'user/register', data })
return res && res.data return res && res.data
} }

View File

@ -6,12 +6,14 @@ import { ElButton, ElCheckbox, ElLink } from 'element-plus'
import { required } from '@/utils/formRules' import { required } from '@/utils/formRules'
import { useForm } from '@/hooks/web/useForm' import { useForm } from '@/hooks/web/useForm'
import { loginApi, getTestRoleApi, getAdminRoleApi } from '@/api/login' import { loginApi, getTestRoleApi, getAdminRoleApi } from '@/api/login'
import type { UserLoginType } from '@/api/login/types'
import { useCache } from '@/hooks/web/useCache' import { useCache } from '@/hooks/web/useCache'
import { useAppStore } from '@/store/modules/app' import { useAppStore } from '@/store/modules/app'
import { usePermissionStore } from '@/store/modules/permission' import { usePermissionStore } from '@/store/modules/permission'
import { useRouter } from 'vue-router' import { useRouter } from 'vue-router'
import type { RouteLocationNormalizedLoaded, RouteRecordRaw } from 'vue-router' import type { RouteLocationNormalizedLoaded, RouteRecordRaw } from 'vue-router'
import { IUserModel } from '@/api-types/user'
import md5 from 'js-md5'
import { cloneDeep } from 'lodash-es'
const emit = defineEmits(['to-register']) const emit = defineEmits(['to-register'])
@ -21,10 +23,12 @@ const permissionStore = usePermissionStore()
const { currentRoute, addRoute, push } = useRouter() const { currentRoute, addRoute, push } = useRouter()
const { wsCache } = useCache()
const { t } = useI18n() const { t } = useI18n()
const rules = { const rules = {
username: [required], user_name: [required],
password: [required] password: [required]
} }
@ -36,7 +40,7 @@ const schema = reactive<FormSchema[]>([
} }
}, },
{ {
field: 'username', field: 'user_name',
label: t('login.username'), label: t('login.username'),
value: 'admin', value: 'admin',
component: 'Input', component: 'Input',
@ -119,17 +123,21 @@ const signIn = async () => {
if (isValid) { if (isValid) {
loading.value = true loading.value = true
const { getFormData } = methods const { getFormData } = methods
const formData = await getFormData<UserLoginType>() const formData = await getFormData<IUserModel>()
const res = await loginApi(formData) try {
.catch(() => {}) const { result } = await loginApi(
.finally(() => (loading.value = false)) Object.assign(cloneDeep(formData), {
password: md5(formData.password)
})
)
if (res) { if (result) {
const { wsCache } = useCache() wsCache.set(appStore.getUserInfo, result)
wsCache.set(appStore.getUserInfo, res.data) getRole()
}
getRole() } finally {
loading.value = false
} }
} }
}) })
@ -138,14 +146,14 @@ const signIn = async () => {
// //
const getRole = async () => { const getRole = async () => {
const { getFormData } = methods const { getFormData } = methods
const formData = await getFormData<UserLoginType>() const formData = await getFormData<IUserModel>()
const params = { const params = {
roleName: formData.username roleName: formData.user_name
} }
// admin - // admin -
// test - // test -
const res = const res =
formData.username === 'admin' formData.user_name === 'admin'
? await getAdminRoleApi({ params }) ? await getAdminRoleApi({ params })
: await getTestRoleApi({ params }) : await getTestRoleApi({ params })
if (res) { if (res) {
@ -153,7 +161,7 @@ const getRole = async () => {
const routers = res.data.list || [] const routers = res.data.list || []
wsCache.set('roleRouters', routers) wsCache.set('roleRouters', routers)
formData.username === 'admin' formData.user_name === 'admin'
? await permissionStore.generateRoutes('admin', routers).catch(() => {}) ? await permissionStore.generateRoutes('admin', routers).catch(() => {})
: await permissionStore.generateRoutes('test', routers).catch(() => {}) : await permissionStore.generateRoutes('test', routers).catch(() => {})

View File

@ -134,9 +134,9 @@ const toLogin = () => {
const codeUrl = ref('') const codeUrl = ref('')
const codeUuid = ref('') const codeUuid = ref('')
const getCode = async () => { const getCode = async () => {
const res = await getCodeApi() const { result } = await getCodeApi()
if (res) { if (result) {
const { url, uuid } = res.result const { url, uuid } = result
codeUrl.value = url codeUrl.value = url
codeUuid.value = uuid codeUuid.value = uuid
} }
@ -152,14 +152,14 @@ const loginRegister = async () => {
try { try {
loading.value = true loading.value = true
const formData = await getFormData<Omit<IUserModel, 'is_admin'>>() const formData = await getFormData<Omit<IUserModel, 'is_admin'>>()
const res = await registerApi( const { result } = await registerApi(
Object.assign(cloneDeep(formData), { Object.assign(cloneDeep(formData), {
uuid: codeUuid.value, uuid: codeUuid.value,
password: md5(formData.password), password: md5(formData.password),
check_password: md5(formData.check_password) check_password: md5(formData.check_password)
}) })
) )
if (res) { if (result) {
ElMessage.success('注册成功') ElMessage.success('注册成功')
toLogin() toLogin()
} }
@ -187,8 +187,12 @@ const loginRegister = async () => {
<template #code="form"> <template #code="form">
<div class="w-[100%] flex"> <div class="w-[100%] flex">
<ElInput v-model="form['code']" :placeholder="t('login.codePlaceholder')" class="flex-2" /> <ElInput
<div v-html="codeUrl" class="h-38px flex-1 cursor-pointer w-150px" @click="getCode"></div> v-model="form['code']"
:placeholder="t('login.codePlaceholder')"
class="!w-[calc(100%-150px)]"
/>
<div v-html="codeUrl" class="h-38px cursor-pointer w-150px" @click="getCode"></div>
</div> </div>
</template> </template>