Skip to content

Commit

Permalink
perf: perf axios config
Browse files Browse the repository at this point in the history
  • Loading branch information
kailong321200875 committed Jun 25, 2022
1 parent 1878c2e commit 39edd84
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 33 deletions.
4 changes: 3 additions & 1 deletion src/api-types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ export interface IUserModel {
password: string
check_password: string
is_admin: number
code: string | number
code?: string | number
token?: string
refreshToken?: string
}
8 changes: 5 additions & 3 deletions src/api/login/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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()

export const loginApi = (data: UserLoginType) => {
return request.post({
export const loginApi = async (data: Pick<IUserModel, 'user_name' | 'password'>) => {
const res = await request.post<IResponse<IUserModel>>({
url: '/user/login',
data
})
return res && res.data
}

export const loginOutApi = () => {
Expand Down
10 changes: 6 additions & 4 deletions src/api/register/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ interface ICodeModel {
uuid: string
}

export const getCodeApi = async () => {
const res = await request.get<IResponse<ICodeModel>>({ url: 'user/captcha' })
export const getCodeApi = async (): Promise<IResponse<ICodeModel>> => {
const res = await request.get({ url: 'user/captcha' })
return res && res.data
}

export const registerApi = async (data: Omit<IUserModel, 'is_admin'>) => {
const res = await request.post<IResponse<IUserModel>>({ url: 'user/register', data })
export const registerApi = async (
data: Omit<IUserModel, 'is_admin'>
): Promise<IResponse<IUserModel>> => {
const res = await request.post({ url: 'user/register', data })
return res && res.data
}
44 changes: 26 additions & 18 deletions src/views/Login/components/LoginForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { ElButton, ElCheckbox, ElLink } from 'element-plus'
import { required } from '@/utils/formRules'
import { useForm } from '@/hooks/web/useForm'
import { loginApi, getTestRoleApi, getAdminRoleApi } from '@/api/login'
import type { UserLoginType } from '@/api/login/types'
import { useCache } from '@/hooks/web/useCache'
import { useAppStore } from '@/store/modules/app'
import { usePermissionStore } from '@/store/modules/permission'
import { useRouter } 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'])
Expand All @@ -21,10 +23,12 @@ const permissionStore = usePermissionStore()
const { currentRoute, addRoute, push } = useRouter()
const { wsCache } = useCache()
const { t } = useI18n()
const rules = {
username: [required],
user_name: [required],
password: [required]
}
Expand All @@ -36,7 +40,7 @@ const schema = reactive<FormSchema[]>([
}
},
{
field: 'username',
field: 'user_name',
label: t('login.username'),
value: 'admin',
component: 'Input',
Expand Down Expand Up @@ -119,17 +123,21 @@ const signIn = async () => {
if (isValid) {
loading.value = true
const { getFormData } = methods
const formData = await getFormData<UserLoginType>()
const res = await loginApi(formData)
.catch(() => {})
.finally(() => (loading.value = false))
if (res) {
const { wsCache } = useCache()
wsCache.set(appStore.getUserInfo, res.data)
getRole()
const formData = await getFormData<IUserModel>()
try {
const { result } = await loginApi(
Object.assign(cloneDeep(formData), {
password: md5(formData.password)
})
)
if (result) {
wsCache.set(appStore.getUserInfo, result)
getRole()
}
} finally {
loading.value = false
}
}
})
Expand All @@ -138,22 +146,22 @@ const signIn = async () => {
// 获取角色信息
const getRole = async () => {
const { getFormData } = methods
const formData = await getFormData<UserLoginType>()
const formData = await getFormData<IUserModel>()
const params = {
roleName: formData.username
roleName: formData.user_name
}
// admin - 模拟后端过滤菜单
// test - 模拟前端过滤菜单
const res =
formData.username === 'admin'
formData.user_name === 'admin'
? await getAdminRoleApi({ params })
: await getTestRoleApi({ params })
if (res) {
const { wsCache } = useCache()
const routers = res.data.list || []
wsCache.set('roleRouters', routers)
formData.username === 'admin'
formData.user_name === 'admin'
? await permissionStore.generateRoutes('admin', routers).catch(() => {})
: await permissionStore.generateRoutes('test', routers).catch(() => {})
Expand Down
18 changes: 11 additions & 7 deletions src/views/Login/components/RegisterForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ const toLogin = () => {
const codeUrl = ref('')
const codeUuid = ref('')
const getCode = async () => {
const res = await getCodeApi()
if (res) {
const { url, uuid } = res.result
const { result } = await getCodeApi()
if (result) {
const { url, uuid } = result
codeUrl.value = url
codeUuid.value = uuid
}
Expand All @@ -152,14 +152,14 @@ const loginRegister = async () => {
try {
loading.value = true
const formData = await getFormData<Omit<IUserModel, 'is_admin'>>()
const res = await registerApi(
const { result } = await registerApi(
Object.assign(cloneDeep(formData), {
uuid: codeUuid.value,
password: md5(formData.password),
check_password: md5(formData.check_password)
})
)
if (res) {
if (result) {
ElMessage.success('注册成功')
toLogin()
}
Expand Down Expand Up @@ -187,8 +187,12 @@ const loginRegister = async () => {

<template #code="form">
<div class="w-[100%] flex">
<ElInput v-model="form['code']" :placeholder="t('login.codePlaceholder')" class="flex-2" />
<div v-html="codeUrl" class="h-38px flex-1 cursor-pointer w-150px" @click="getCode"></div>
<ElInput
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>
</template>

Expand Down

0 comments on commit 39edd84

Please sign in to comment.