Skip to content

Commit

Permalink
fead:optimize login registration and request network optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghang committed Nov 13, 2023
1 parent 3c84edd commit ab5f205
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 66 deletions.
13 changes: 13 additions & 0 deletions mock/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ export default [
}
}
},
{
url: "/register",
method: "post",
response: () => {
return {
code: 1,
data: {
id: Random.id(),
expiresTime: Random.now(),
}
}
}
},
{
url: "/userInfo",
method: "post",
Expand Down
1 change: 1 addition & 0 deletions src/packages/api/apiMap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export default {
login:"/login",
register:"/register",
menus:"/menus",
userInfo:"/userInfo"
}
17 changes: 13 additions & 4 deletions src/packages/api/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@ import {post} from "@/packages/http/request.ts"
import apiMap from "@/packages/api/apiMap.ts"
import {resetApiInstanceUrl} from "@/packages/global"

const login = () => {
return post(resetApiInstanceUrl(apiMap.login))
const login = (params) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return post(resetApiInstanceUrl(apiMap.login), params, {hint: true})
}

const register = (params) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return post(resetApiInstanceUrl(apiMap.register), params, {hint: true})
}
const menus = () => {
return post(resetApiInstanceUrl(apiMap.menus))
}

const userInfo = ()=>{
const userInfo = () => {
return post(resetApiInstanceUrl(apiMap.userInfo))
}

export {
login,
menus,
userInfo
userInfo,
register
}
5 changes: 4 additions & 1 deletion src/packages/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logo from "@/packages/assets/logo-min.png"
import maxLogo from "@/packages/assets/logo.png"

export default {
whiteList: ["/login"],
Expand All @@ -11,10 +12,12 @@ export default {
},
website: {
logo,
maxLogo,
title: "bag-admin"
},
components: {
headerUserSet: null
},
apis:{}
apis:{},
events:{}
}
13 changes: 10 additions & 3 deletions src/packages/http/request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios, {AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig} from "axios"
import axiosRetry from "axios-retry"
import locaStore from "@/packages/utils/locaStore.ts"

const http: AxiosInstance = axios.create({
withCredentials: true,
Expand All @@ -12,22 +13,28 @@ axiosRetry(http, {
})

http.interceptors.request.use((config: InternalAxiosRequestConfig) => {
config.headers["authorization"] = locaStore.get("access_token")
return config
}, (error: AxiosError) => {
return Promise.reject(error)
})

http.interceptors.response.use((response: AxiosResponse) => {
const {code,msg} = response.data
const {code, msg} = response.data
if (code === 1) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if(response.config.hint && msg){
if (response.config.hint && msg) {
window.$message.success(msg)
}
return response.data
}
return response
if(code === 1003){
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
http.$router.push(http.$configOptions.resetPath)
}
return Promise.reject(response)
}, (error: AxiosError) => {
return Promise.reject(error)
})
Expand Down
3 changes: 2 additions & 1 deletion src/packages/install.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ const install = (app: App, options?: any) => {
app.config.globalProperties["configOptions"] = configOptions
app.provide("configOptions", readonly(configOptions))
app.provide("$mitt", emitter)
axios.configOptions = configOptions
axios.$configOptions = configOptions
axios.$router = router
setupPinia(app)
setupIcons(app)
setupGlobal()
Expand Down
1 change: 1 addition & 0 deletions src/packages/layout/components/UserSet.vue
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ export default defineComponent({
},
handleSelect(key: string | number) {
if (key === "stmt4") {
app.$reset()
locaStore.clearAll()
router.push("/login")
}
Expand Down
3 changes: 1 addition & 2 deletions src/packages/router/beforeEach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,14 @@ function updateRouterAll(to: RouteLocationNormalized, from: RouteLocationNormali
})
appStore.treeMenus = toTree({arr: appStore.allMenus.filter((item: any) => item.shows)})
createRouterComponent(allMenus)
next(to.fullPath)
}).finally(() => {
hasRoles = true
next(to.fullPath)
})
}
}

const beforeEach = (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
const appStore = appPinia()
if (hasWhiteRouter(to.path)) {
return next()
}
Expand Down
Loading

0 comments on commit ab5f205

Please sign in to comment.