-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
33 changed files
with
311 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
"Attributify", | ||
"ctitle", | ||
"devapi", | ||
"ERRMESSAGE", | ||
"mockjs", | ||
"unocss", | ||
"unplugin", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { message } from 'antd' | ||
import axios from 'axios' | ||
import { getToken } from '@/utils' | ||
import type { AxiosError, AxiosResponse } from 'axios' | ||
import { API_BASE_URL } from '@/constant' | ||
|
||
/** | ||
* @description: 响应结果 | ||
* @argument SUCCESS 请求成功 | ||
* @argument EXPIRE token请求失效或校验失败 | ||
* @argument ERROR 请求错误 | ||
* @argument TIMEOUT 请求超时 | ||
* @argument TYPE 请求类型 | ||
*/ | ||
enum ResultEnum { | ||
SUCCESS = 200, | ||
EXPIRE = 203, | ||
ERROR = -1, | ||
ERRMESSAGE = '请求失败', | ||
TIMEOUT = 25000, | ||
TYPE = 'success', | ||
} | ||
console.log('API_BASE_URL', API_BASE_URL) | ||
|
||
const instance = axios.create({ | ||
timeout: 10 * 1000, | ||
baseURL: API_BASE_URL, | ||
}) | ||
|
||
/** | ||
* @description: 请求拦截器 | ||
* @returns {*} | ||
*/ | ||
instance.interceptors.request.use( | ||
(config) => { | ||
config.headers['Authorization'] = `Bearer ${getToken()}` // JWT 的固定格式 | ||
return config | ||
}, | ||
(error: AxiosError) => { | ||
message.error(error.message) | ||
return Promise.reject(error) | ||
}, | ||
) | ||
|
||
/** | ||
* @description: 响应拦截器 | ||
* @returns {*} | ||
*/ | ||
instance.interceptors.response.use( | ||
(response: AxiosResponse) => { | ||
const resData = (response.data || {}) as ResType | ||
const { errno, data, msg } = resData | ||
|
||
// * 登陆失效(code == 203) | ||
if (errno === ResultEnum.EXPIRE) { | ||
// TODO: 清除本地缓存 | ||
message.error(msg || ResultEnum.ERRMESSAGE) | ||
// 返回登录页 | ||
return Promise.reject(data) | ||
} | ||
|
||
if (errno !== ResultEnum.SUCCESS) { | ||
message.error(msg || ResultEnum.ERRMESSAGE) | ||
return Promise.reject(data) | ||
} | ||
return data as any | ||
}, | ||
(error: AxiosError) => { | ||
// 处理 HTTP 网络错误 | ||
let messageStr = '' | ||
// HTTP 状态码 | ||
const status = error.response?.status | ||
switch (status) { | ||
case 401: | ||
messageStr = 'token 失效,请重新登录' | ||
break | ||
case 403: | ||
messageStr = '拒绝访问' | ||
break | ||
case 404: | ||
messageStr = '请求地址错误' | ||
break | ||
case 500: | ||
messageStr = '服务器故障' | ||
break | ||
default: | ||
messageStr = '网络连接故障' | ||
} | ||
|
||
message.error(messageStr) | ||
return Promise.reject(error) | ||
}, | ||
) | ||
|
||
export default instance | ||
|
||
export type ResType = { | ||
errno: number | ||
data?: ResDataType | ||
msg?: string | ||
} | ||
|
||
export type ResDataType = { | ||
[key: string]: any | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './question' | ||
export * from './stat' | ||
export * from './user' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import axios, { ResDataType } from './ajax' | ||
|
||
// 获取问卷的统计列表 | ||
export async function getQuestionStatListService( | ||
questionId: string, | ||
opt: { page: number; pageSize: number }, | ||
): Promise<ResDataType> { | ||
const url = `/api/stat/${questionId}` | ||
const data = (await axios.get(url, { params: opt })) as ResDataType | ||
return data | ||
} | ||
|
||
// 获取组件统计数据汇总 | ||
export async function getComponentStatService( | ||
questionId: string, | ||
componentId: string, | ||
): Promise<ResDataType> { | ||
const url = `/api/stat/${questionId}/${componentId}` | ||
const data = (await axios.get(url)) as ResDataType | ||
return data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import axios, { ResDataType } from './ajax' | ||
|
||
// 获取用户信息 | ||
export async function getUserInfoService(): Promise<ResDataType> { | ||
const url = '/api/user/info' | ||
const data = (await axios.get(url)) as ResDataType | ||
return data | ||
} | ||
|
||
// 注册用户 | ||
export async function registerService( | ||
username: string, | ||
password: string, | ||
nickname?: string, | ||
): Promise<ResDataType> { | ||
const url = '/api/user/register' | ||
const body = { username, password, nickname: nickname || username } | ||
const data = (await axios.post(url, body)) as ResDataType | ||
return data | ||
} | ||
|
||
// 登录 | ||
export async function loginService( | ||
username: string, | ||
password: string, | ||
): Promise<ResDataType> { | ||
const url = '/api/user/login' | ||
const body = { username, password } | ||
const data = (await axios.post(url, body)) as ResDataType | ||
return data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.