feat: 重新整理目录结构,mock请求
This commit is contained in:
parent
c8400abd9f
commit
179ab2672f
|
@ -1,4 +1,5 @@
|
|||
import { defineMock } from 'vite-plugin-mock-dev-server'
|
||||
import { SUCCESS_CODE } from '@/constants'
|
||||
|
||||
const delay = 1000
|
||||
|
||||
|
@ -10,7 +11,7 @@ export default defineMock([
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: {
|
||||
users: 102400,
|
||||
messages: 81212,
|
||||
|
@ -27,7 +28,7 @@ export default defineMock([
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: [
|
||||
{ value: 1000, name: 'analysis.directAccess' },
|
||||
{ value: 310, name: 'analysis.mailMarketing' },
|
||||
|
@ -45,7 +46,7 @@ export default defineMock([
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: [
|
||||
{ value: 13253, name: 'analysis.monday' },
|
||||
{ value: 34235, name: 'analysis.tuesday' },
|
||||
|
@ -65,7 +66,7 @@ export default defineMock([
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: [
|
||||
{ estimate: 100, actual: 120, name: 'analysis.january' },
|
||||
{ estimate: 120, actual: 82, name: 'analysis.february' },
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { toAnyString } from '@/utils'
|
||||
import { faker } from '@faker-js/faker'
|
||||
import { SUCCESS_CODE } from '@/constants'
|
||||
|
||||
const departmentList: any = []
|
||||
|
||||
|
@ -80,7 +81,7 @@ export default [
|
|||
method: 'GET',
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: {
|
||||
list: departmentList
|
||||
}
|
||||
|
@ -92,7 +93,7 @@ export default [
|
|||
method: 'GET',
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: {
|
||||
list: departmentList,
|
||||
total: 5
|
||||
|
@ -123,7 +124,7 @@ export default [
|
|||
})
|
||||
}
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: {
|
||||
total: 100,
|
||||
list: mockList
|
||||
|
@ -138,7 +139,7 @@ export default [
|
|||
delay: 1000,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: 'success'
|
||||
}
|
||||
}
|
||||
|
@ -156,7 +157,7 @@ export default [
|
|||
}
|
||||
} else {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: 'success'
|
||||
}
|
||||
}
|
||||
|
@ -169,7 +170,7 @@ export default [
|
|||
delay: 1000,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: 'success'
|
||||
}
|
||||
}
|
||||
|
@ -187,7 +188,7 @@ export default [
|
|||
}
|
||||
} else {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: 'success'
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import { SUCCESS_CODE } from '@/constants'
|
||||
|
||||
const delay = 1000
|
||||
|
||||
const dictObj: Recordable = {
|
||||
|
@ -25,7 +27,7 @@ export default [
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: dictObj
|
||||
}
|
||||
}
|
||||
|
@ -37,7 +39,7 @@ export default [
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: [
|
||||
{
|
||||
label: 'test1',
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { faker } from '@faker-js/faker'
|
||||
import { SUCCESS_CODE } from '@/constants'
|
||||
|
||||
const delay = 1000
|
||||
|
||||
|
@ -10,7 +11,7 @@ export default [
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: {
|
||||
list: [
|
||||
{
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import { SUCCESS_CODE } from '@/constants'
|
||||
|
||||
const delay = 600000
|
||||
|
||||
export default [
|
||||
|
@ -7,7 +9,7 @@ export default [
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: 'request-1'
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +20,7 @@ export default [
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: 'request-2'
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +31,7 @@ export default [
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: 'request-3'
|
||||
}
|
||||
}
|
||||
|
@ -40,7 +42,7 @@ export default [
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: 'request-4'
|
||||
}
|
||||
}
|
||||
|
@ -51,7 +53,7 @@ export default [
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: 'request-5'
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { faker } from '@faker-js/faker'
|
||||
|
||||
import { SUCCESS_CODE } from '@/constants'
|
||||
import { toAnyString } from '@/utils'
|
||||
|
||||
const delay = 1000
|
||||
|
@ -1141,7 +1141,7 @@ export default [
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: adminList
|
||||
}
|
||||
}
|
||||
|
@ -1152,7 +1152,7 @@ export default [
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: {
|
||||
list: List,
|
||||
total: 4
|
||||
|
@ -1167,7 +1167,7 @@ export default [
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: testList
|
||||
}
|
||||
}
|
||||
|
@ -1178,7 +1178,7 @@ export default [
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: {
|
||||
list: List,
|
||||
total: 4
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
import { faker } from '@faker-js/faker'
|
||||
|
||||
import { SUCCESS_CODE } from '@/constants'
|
||||
import { toAnyString } from '@/utils'
|
||||
|
||||
const delay = 1000
|
||||
|
||||
const count = 100
|
||||
|
||||
const baseContent =
|
||||
|
@ -193,7 +192,7 @@ export default [
|
|||
(_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)
|
||||
)
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: {
|
||||
total: mockList.length,
|
||||
list: pageList
|
||||
|
@ -216,7 +215,7 @@ export default [
|
|||
(_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)
|
||||
)
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: {
|
||||
total: mockList.length,
|
||||
list: pageList
|
||||
|
@ -237,7 +236,7 @@ export default [
|
|||
})
|
||||
].concat(List)
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: 'success'
|
||||
}
|
||||
} else {
|
||||
|
@ -249,7 +248,7 @@ export default [
|
|||
}
|
||||
})
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: 'success'
|
||||
}
|
||||
}
|
||||
|
@ -264,7 +263,7 @@ export default [
|
|||
for (const example of List) {
|
||||
if (example.id === id) {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: example
|
||||
}
|
||||
}
|
||||
|
@ -290,7 +289,7 @@ export default [
|
|||
}
|
||||
}
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: 'success'
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import { SUCCESS_CODE } from '@/constants'
|
||||
|
||||
const delay = 1000
|
||||
|
||||
const List: {
|
||||
|
@ -40,7 +42,7 @@ export default [
|
|||
)
|
||||
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: {
|
||||
total: mockList.length,
|
||||
list: pageList
|
||||
|
@ -60,7 +62,7 @@ export default [
|
|||
if (user.username === data.username && user.password === data.password) {
|
||||
hasUser = true
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: user
|
||||
}
|
||||
}
|
||||
|
@ -80,7 +82,7 @@ export default [
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: null
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import { SUCCESS_CODE } from '@/constants'
|
||||
|
||||
const delay = 1000
|
||||
|
||||
export default [
|
||||
|
@ -8,7 +10,7 @@ export default [
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: {
|
||||
project: 40,
|
||||
access: 2340,
|
||||
|
@ -24,7 +26,7 @@ export default [
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: [
|
||||
{
|
||||
name: 'Github',
|
||||
|
@ -79,7 +81,7 @@ export default [
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: [
|
||||
{
|
||||
keys: ['workplace.push', 'Github'],
|
||||
|
@ -116,7 +118,7 @@ export default [
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: [
|
||||
{
|
||||
name: 'Github',
|
||||
|
@ -153,7 +155,7 @@ export default [
|
|||
delay,
|
||||
body: () => {
|
||||
return {
|
||||
code: 0,
|
||||
code: SUCCESS_CODE,
|
||||
data: [
|
||||
{ name: 'workplace.quote', max: 65, personal: 42, team: 50 },
|
||||
{ name: 'workplace.contribution', max: 160, personal: 30, team: 140 },
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import request from '@/config/axios'
|
||||
import request from '@/axios'
|
||||
|
||||
// 获取所有字典
|
||||
export const getDictApi = () => {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import request from '@/config/axios'
|
||||
import request from '@/axios'
|
||||
import type {
|
||||
AnalysisTotalTypes,
|
||||
UserAccessSource,
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import request from '@/config/axios'
|
||||
import request from '@/axios'
|
||||
import type { WorkplaceTotal, Project, Dynamic, Team, RadarData } from './types'
|
||||
|
||||
export const getCountApi = (): Promise<IResponse<WorkplaceTotal>> => {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import request from '@/config/axios'
|
||||
import request from '@/axios'
|
||||
import { DepartmentListResponse, DepartmentUserParams, DepartmentUserResponse } from './types'
|
||||
|
||||
export const getDepartmentApi = () => {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import request from '@/config/axios'
|
||||
import request from '@/axios'
|
||||
import type { UserType } from './types'
|
||||
|
||||
interface RoleParams {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import request from '@/config/axios'
|
||||
import request from '@/axios'
|
||||
|
||||
export const getMenuListApi = () => {
|
||||
return request.get({ url: '/mock/menu/list' })
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import request from '@/config/axios'
|
||||
import request from '@/axios'
|
||||
import { RequestResponse } from './types'
|
||||
|
||||
export const request1 = () => {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import request from '@/config/axios'
|
||||
import request from '@/axios'
|
||||
|
||||
export const getRoleListApi = () => {
|
||||
return request.get({ url: '/mock/role/table' })
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import request from '@/config/axios'
|
||||
import request from '@/axios'
|
||||
import type { TableData } from './types'
|
||||
|
||||
export const getTableListApi = (params: any) => {
|
||||
|
|
|
@ -1,46 +1,13 @@
|
|||
import {
|
||||
AxiosConfig,
|
||||
AxiosResponse,
|
||||
AxiosRequestHeaders,
|
||||
InternalAxiosRequestConfig
|
||||
} from './types'
|
||||
import { AxiosResponse, AxiosRequestHeaders, InternalAxiosRequestConfig } from './types'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import qs from 'qs'
|
||||
import router from '@/router'
|
||||
import { SUCCESS_CODE } from '@/constants'
|
||||
|
||||
import { useStorage } from '@/hooks/web/useStorage'
|
||||
|
||||
const { clear } = useStorage()
|
||||
|
||||
const config: AxiosConfig = {
|
||||
/**
|
||||
* 接口成功返回状态码
|
||||
*/
|
||||
code: 0,
|
||||
|
||||
/**
|
||||
* 接口请求超时时间
|
||||
*/
|
||||
timeout: 60000,
|
||||
|
||||
/**
|
||||
* 默认接口请求类型
|
||||
* 可选值:application/x-www-form-urlencoded multipart/form-data
|
||||
*/
|
||||
defaultHeaders: 'application/json',
|
||||
|
||||
interceptors: {
|
||||
//请求拦截
|
||||
// requestInterceptors: (config) => {
|
||||
// return config
|
||||
// },
|
||||
// 响应拦截器
|
||||
// responseInterceptors: (result: AxiosResponse) => {
|
||||
// return result
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
const defaultRequestInterceptors = (config: InternalAxiosRequestConfig) => {
|
||||
if (
|
||||
config.method === 'post' &&
|
||||
|
@ -64,11 +31,11 @@ const defaultRequestInterceptors = (config: InternalAxiosRequestConfig) => {
|
|||
return config
|
||||
}
|
||||
|
||||
const defaultResponseInterceptors = (response: AxiosResponse<any>) => {
|
||||
const defaultResponseInterceptors = (response: AxiosResponse) => {
|
||||
if (response?.config?.responseType === 'blob') {
|
||||
// 如果是文件流,直接过
|
||||
return response
|
||||
} else if (response.data.code === config.code) {
|
||||
} else if (response.data.code === SUCCESS_CODE) {
|
||||
return response.data
|
||||
} else {
|
||||
ElMessage.error(response?.data?.message)
|
||||
|
@ -81,4 +48,3 @@ const defaultResponseInterceptors = (response: AxiosResponse<any>) => {
|
|||
}
|
||||
|
||||
export { defaultResponseInterceptors, defaultRequestInterceptors }
|
||||
export default config
|
|
@ -1,11 +1,8 @@
|
|||
import service from './service'
|
||||
|
||||
import config from './config'
|
||||
|
||||
const { defaultHeaders } = config
|
||||
import { CONTENT_TYPE } from '@/constants'
|
||||
|
||||
const request = (option: AxiosConfig) => {
|
||||
const { url, method, params, data, headersType, responseType } = option
|
||||
const { url, method, params, data, headers, responseType } = option
|
||||
return service.request({
|
||||
url: url,
|
||||
method,
|
||||
|
@ -13,7 +10,8 @@ const request = (option: AxiosConfig) => {
|
|||
data,
|
||||
responseType: responseType,
|
||||
headers: {
|
||||
'Content-Type': headersType || defaultHeaders
|
||||
'Content-Type': CONTENT_TYPE,
|
||||
...headers
|
||||
}
|
||||
})
|
||||
}
|
|
@ -1,18 +1,16 @@
|
|||
import axios, { AxiosError } from 'axios'
|
||||
import config, { defaultRequestInterceptors, defaultResponseInterceptors } from './config'
|
||||
import { defaultRequestInterceptors, defaultResponseInterceptors } from './config'
|
||||
|
||||
import { AxiosInstance, InternalAxiosRequestConfig, RequestConfig, AxiosResponse } from './types'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { REQUEST_TIMEOUT } from '@/constants'
|
||||
|
||||
const { interceptors } = config
|
||||
export const PATH_URL = import.meta.env.VITE_API_BASE_PATH
|
||||
|
||||
const { requestInterceptors, responseInterceptors } = interceptors
|
||||
|
||||
const abortControllerMap: Map<string, AbortController> = new Map()
|
||||
|
||||
const axiosInstance: AxiosInstance = axios.create({
|
||||
...config,
|
||||
timeout: REQUEST_TIMEOUT,
|
||||
baseURL: PATH_URL
|
||||
})
|
||||
|
||||
|
@ -38,8 +36,8 @@ axiosInstance.interceptors.response.use(
|
|||
}
|
||||
)
|
||||
|
||||
axiosInstance.interceptors.request.use(requestInterceptors || defaultRequestInterceptors)
|
||||
axiosInstance.interceptors.response.use(responseInterceptors || defaultResponseInterceptors)
|
||||
axiosInstance.interceptors.request.use(defaultRequestInterceptors)
|
||||
axiosInstance.interceptors.response.use(defaultResponseInterceptors)
|
||||
|
||||
const service = {
|
||||
request: (config: RequestConfig) => {
|
|
@ -15,12 +15,6 @@ interface RequestInterceptors<T> {
|
|||
responseInterceptors?: (config: T) => T
|
||||
responseInterceptorsCatch?: (err: any) => any
|
||||
}
|
||||
interface AxiosConfig<T = AxiosResponse> {
|
||||
code: number
|
||||
defaultHeaders: AxiosHeaders
|
||||
timeout: number
|
||||
interceptors: RequestInterceptors<T>
|
||||
}
|
||||
|
||||
interface RequestConfig<T = AxiosResponse> extends AxiosRequestConfig {
|
||||
interceptors?: RequestInterceptors<T>
|
||||
|
@ -30,7 +24,6 @@ export {
|
|||
AxiosResponse,
|
||||
RequestInterceptors,
|
||||
RequestConfig,
|
||||
AxiosConfig,
|
||||
AxiosInstance,
|
||||
InternalAxiosRequestConfig,
|
||||
AxiosRequestHeaders,
|
|
@ -0,0 +1,14 @@
|
|||
/**
|
||||
* 请求成功状态码
|
||||
*/
|
||||
export const SUCCESS_CODE = 0
|
||||
|
||||
/**
|
||||
* 请求contentType
|
||||
*/
|
||||
export const CONTENT_TYPE = 'application/json'
|
||||
|
||||
/**
|
||||
* 请求超时时间
|
||||
*/
|
||||
export const REQUEST_TIMEOUT = 60000
|
|
@ -4,7 +4,7 @@ import { useI18n } from '@/hooks/web/useI18n'
|
|||
import { ElButton, ElDivider } from 'element-plus'
|
||||
import { request1, request2, request3, request4, request5, expired } from '@/api/request'
|
||||
import { ref } from 'vue'
|
||||
import request from '@/config/axios'
|
||||
import request from '@/axios'
|
||||
|
||||
const { t } = useI18n()
|
||||
|
||||
|
@ -171,3 +171,4 @@ const tokenExpired = () => {
|
|||
<ElButton type="primary" @click="tokenExpired">token过期</ElButton>
|
||||
</ContentWrap>
|
||||
</template>
|
||||
@/axios
|
||||
|
|
|
@ -15,6 +15,9 @@ interface ImportMetaEnv {
|
|||
readonly VITE_DROP_CONSOLE: string
|
||||
readonly VITE_SOURCEMAP: string
|
||||
readonly VITE_OUT_DIR: string
|
||||
readonly VITE_AXIOS_CONTENT_TYPE: string
|
||||
readonly VITE_AXIOS_TIMEOUT: number
|
||||
readonly VITE_AXIOS_SUCCESS_CODE: number
|
||||
}
|
||||
|
||||
declare global {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import type { CSSProperties } from 'vue'
|
||||
import { AxiosRequestHeaders } from 'axios'
|
||||
declare global {
|
||||
declare interface Fn<T = any> {
|
||||
(...arg: T[]): T
|
||||
|
@ -25,7 +26,7 @@ declare global {
|
|||
|
||||
declare type LayoutType = 'classic' | 'topLeft' | 'top' | 'cutMenu'
|
||||
|
||||
declare type AxiosHeaders =
|
||||
declare type AxiosContentType =
|
||||
| 'application/json'
|
||||
| 'application/x-www-form-urlencoded'
|
||||
| 'multipart/form-data'
|
||||
|
@ -39,12 +40,12 @@ declare global {
|
|||
data?: any
|
||||
url?: string
|
||||
method?: AxiosMethod
|
||||
headersType?: string
|
||||
headers?: AxiosRequestHeaders
|
||||
responseType?: AxiosResponseType
|
||||
}
|
||||
|
||||
declare interface IResponse<T = any> {
|
||||
code: string
|
||||
code: number
|
||||
data: T extends any ? T : T & any
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue