Skip to content

Commit b2acb41

Browse files
author
白唯
committed
refactor(api): 重构网络请求模块以及完善 vuex 模块示例
1 parent 003bccf commit b2acb41

File tree

12 files changed

+661
-100
lines changed

12 files changed

+661
-100
lines changed

@types/index.d.ts

+112-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { AppStateType } from '@/store/modules/app/state'
22
import { ConsoleStateType } from '@/store/modules/console/state'
3+
import { UserStateType } from '@/store/modules/user/state'
34

45
// vuex模块的类型
5-
type ModuleType = { app: AppStateType; console: ConsoleStateType }
6+
type ModuleType = {
7+
app: AppStateType
8+
console: ConsoleStateType
9+
user: UserStateType
10+
}
611

712
// 所有的StateType
813
export type StateType = ModuleType
@@ -30,8 +35,111 @@ export type IconType = 'icon' | 'iconfont'
3035
export type ModalOpenMode = 'edit' | 'add' | 'other'
3136

3237
export interface BasicUserType {
38+
id: number
39+
name?: string
40+
avatar?: string
41+
role?: string
42+
department?: string
43+
code?: string
44+
createTime?: string
45+
description?: string
46+
email?: string
47+
lastLoginTime?: string
48+
modifyTime?: string
49+
modifyUser?: number
50+
nickName?: string
51+
phone?: string
52+
roleId?: number
53+
roleName?: string
54+
status?: number
55+
tenantId?: number
56+
type?: string
57+
userId?: number
58+
username?: string
59+
}
60+
61+
export interface ListParamType {
62+
id: number
63+
pageSize: number
64+
pageNum: number
65+
}
66+
67+
// 接口响应通过格式
68+
export interface HttpResponse {
69+
status: number
70+
statusText: string
71+
data: {
72+
code: number
73+
desc: string
74+
[key: string]: any
75+
}
76+
}
77+
78+
// 接口请求列表通用参数配置
79+
export interface HttpListQuery {
80+
pageNum?: number
81+
pageSize?: number
82+
orderNum?: number
83+
[key: string]: any
84+
}
85+
86+
// 团队列表类型
87+
export interface TeamListType {
88+
createTime?: string
89+
description?: string
90+
id?: number
91+
memberNum?: number
92+
name?: string
93+
orderNum?: number
94+
projectNum?: number
95+
tenantId?: number
96+
}
97+
98+
// 批量添加团队成员列表
99+
export interface TeamMemberType {
100+
id?: number
101+
roleId?: number // 0 成员 1管理员 2 访客
102+
status?: number
103+
teamId?: number
104+
tenantId?: number
105+
toolRole?: string
106+
userId?: number
107+
userTenantId?: number
108+
}
109+
110+
export enum RoleType {
111+
'超级管理员' = 1,
112+
'子账号',
113+
'团队管理员',
114+
'团队成员',
115+
'团队访客',
116+
'项目管理员',
117+
'项目成员',
118+
'项目访客'
119+
}
120+
121+
// 权限列表类型
122+
export interface RoleItemType {
123+
createTime: string
124+
id: number
125+
roleId: number
126+
modifyTime: string
127+
parentId: number
128+
remark: string
129+
roleName: keyof RoleType
130+
type: number
131+
menuIds: string
132+
}
133+
134+
export interface AddTeamGroupParams {
135+
description?: string
136+
id?: number
33137
name: string
34-
avatar: unknown
35-
role: string
36-
department: string
138+
tenantId: number
139+
}
140+
141+
export interface AddTeamGroupMemberParams {
142+
groupId: number
143+
id?: number
144+
userId: number
37145
}

src/api/apiList.ts

-53
This file was deleted.

src/api/axios.ts

+45-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import Axios, { AxiosResponse, AxiosRequestConfig, AxiosError } from 'axios'
2+
import router from '@/router'
3+
import { message } from 'ant-design-vue'
4+
import Store from '@/store'
25

36
/**
47
* get status code
@@ -57,30 +60,50 @@ const getErrorCode2text = (response: AxiosResponse): string => {
5760
*/
5861
const service = Axios.create({
5962
baseURL: process.env.VUE_APP_BASE_URL,
60-
timeout: 10000
63+
timeout: 10000,
64+
headers: {
65+
'User-Type': 'bus'
66+
}
6167
})
6268

6369
/**
6470
* @description 请求发起前的拦截器
6571
* @returns {AxiosRequestConfig} config
6672
*/
67-
service.interceptors.request.use(
68-
async <AxiosRequestConfig>(config: AxiosRequestConfig) => {
69-
// check network
70-
/* TODO add http headers
71-
const token = window.localStorage.getItem('token')
72-
config.headers = {
73-
...config.headers,
74-
Authorization: token
75-
} */
73+
service.interceptors.request.use(async (config: AxiosRequestConfig) => {
74+
// 如果是获取token接口:
75+
if (config.url === '/auth/oauth/token') {
76+
let userAccount = ''
77+
// 若存在username,则为登录情况,判断user-account
78+
if (config.params.username) {
79+
userAccount = config.params.username.includes('-')
80+
? 'ACCOUNT_USER'
81+
: 'ADMIN_USER'
82+
} else {
83+
// 刷新token情况,通过用户信息email是否有值判断
84+
userAccount = Store.state.user.userDetail.email
85+
? 'ADMIN_USER'
86+
: 'ACCOUNT_USER'
87+
}
7688

77-
return config
89+
config.headers['User-Account'] = userAccount
90+
config.headers.Authorization = 'Basic ZmViczoxMjM0NTY='
91+
} else {
92+
// 如果保存有token,则取,否则不添加Authorization
93+
if (localStorage.vuex && JSON.parse(localStorage.vuex).user?.token) {
94+
const token = Store.state.user?.token
95+
const tokenType = token.token_type
96+
const accessToken = token.access_token
97+
config.headers.Authorization = `${tokenType} ${accessToken}`
98+
}
7899
}
79-
)
100+
101+
return config
102+
})
80103

81104
/**
82-
* @description 请求发起前的拦截器
83-
* @returns {} config
105+
* @description 响应收到后的拦截器
106+
* @returns {}
84107
*/
85108
service.interceptors.response.use(
86109
/** 请求有响应 */
@@ -101,14 +124,20 @@ service.interceptors.response.use(
101124
}
102125

103126
if (error.response) {
104-
__emsg = error.response.data.msg
127+
__emsg = error.response.data.message
128+
? error.response.data.message
129+
: error.response.data.data
105130
}
106131
// timeout
107132
if (__emsg.indexOf('timeout') >= 0) {
108133
__emsg = 'timeout'
109134
}
110135

111-
if (error?.response?.data?.code === 401) {
136+
if (error?.response?.status === 401) {
137+
if (router.currentRoute.value.path !== '/entry/login') {
138+
message.info('登录凭证已过期,请重新登录')
139+
router.push('/entry/login')
140+
}
112141
return Promise.reject(new Error('401'))
113142
}
114143
return Promise.reject(new Error(__emsg))

src/api/common.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Axios from './axios'
2+
import { HttpResponse } from '../../@types/index'
3+
4+
/**
5+
* @description 公共模块的的网络请求,所有通用 api 放在此处
6+
*/
7+
8+
class CommonService {
9+
// 添加团队
10+
static getRoleInfoList(): Promise<HttpResponse> {
11+
return Axios.get('/bus/common/getRoleInfo', {
12+
responseType: 'json'
13+
})
14+
}
15+
}
16+
17+
export default CommonService

0 commit comments

Comments
 (0)