feat: 重新整理目录结构,mock请求

This commit is contained in:
kailong321200875 2023-11-30 11:34:39 +08:00
parent c8400abd9f
commit 179ab2672f
27 changed files with 96 additions and 112 deletions

View File

@ -1,4 +1,5 @@
import { defineMock } from 'vite-plugin-mock-dev-server' import { defineMock } from 'vite-plugin-mock-dev-server'
import { SUCCESS_CODE } from '@/constants'
const delay = 1000 const delay = 1000
@ -10,7 +11,7 @@ export default defineMock([
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: { data: {
users: 102400, users: 102400,
messages: 81212, messages: 81212,
@ -27,7 +28,7 @@ export default defineMock([
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: [ data: [
{ value: 1000, name: 'analysis.directAccess' }, { value: 1000, name: 'analysis.directAccess' },
{ value: 310, name: 'analysis.mailMarketing' }, { value: 310, name: 'analysis.mailMarketing' },
@ -45,7 +46,7 @@ export default defineMock([
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: [ data: [
{ value: 13253, name: 'analysis.monday' }, { value: 13253, name: 'analysis.monday' },
{ value: 34235, name: 'analysis.tuesday' }, { value: 34235, name: 'analysis.tuesday' },
@ -65,7 +66,7 @@ export default defineMock([
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: [ data: [
{ estimate: 100, actual: 120, name: 'analysis.january' }, { estimate: 100, actual: 120, name: 'analysis.january' },
{ estimate: 120, actual: 82, name: 'analysis.february' }, { estimate: 120, actual: 82, name: 'analysis.february' },

View File

@ -1,5 +1,6 @@
import { toAnyString } from '@/utils' import { toAnyString } from '@/utils'
import { faker } from '@faker-js/faker' import { faker } from '@faker-js/faker'
import { SUCCESS_CODE } from '@/constants'
const departmentList: any = [] const departmentList: any = []
@ -80,7 +81,7 @@ export default [
method: 'GET', method: 'GET',
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: { data: {
list: departmentList list: departmentList
} }
@ -92,7 +93,7 @@ export default [
method: 'GET', method: 'GET',
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: { data: {
list: departmentList, list: departmentList,
total: 5 total: 5
@ -123,7 +124,7 @@ export default [
}) })
} }
return { return {
code: 0, code: SUCCESS_CODE,
data: { data: {
total: 100, total: 100,
list: mockList list: mockList
@ -138,7 +139,7 @@ export default [
delay: 1000, delay: 1000,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: 'success' data: 'success'
} }
} }
@ -156,7 +157,7 @@ export default [
} }
} else { } else {
return { return {
code: 0, code: SUCCESS_CODE,
data: 'success' data: 'success'
} }
} }
@ -169,7 +170,7 @@ export default [
delay: 1000, delay: 1000,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: 'success' data: 'success'
} }
} }
@ -187,7 +188,7 @@ export default [
} }
} else { } else {
return { return {
code: 0, code: SUCCESS_CODE,
data: 'success' data: 'success'
} }
} }

View File

@ -1,3 +1,5 @@
import { SUCCESS_CODE } from '@/constants'
const delay = 1000 const delay = 1000
const dictObj: Recordable = { const dictObj: Recordable = {
@ -25,7 +27,7 @@ export default [
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: dictObj data: dictObj
} }
} }
@ -37,7 +39,7 @@ export default [
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: [ data: [
{ {
label: 'test1', label: 'test1',

View File

@ -1,4 +1,5 @@
import { faker } from '@faker-js/faker' import { faker } from '@faker-js/faker'
import { SUCCESS_CODE } from '@/constants'
const delay = 1000 const delay = 1000
@ -10,7 +11,7 @@ export default [
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: { data: {
list: [ list: [
{ {

View File

@ -1,3 +1,5 @@
import { SUCCESS_CODE } from '@/constants'
const delay = 600000 const delay = 600000
export default [ export default [
@ -7,7 +9,7 @@ export default [
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: 'request-1' data: 'request-1'
} }
} }
@ -18,7 +20,7 @@ export default [
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: 'request-2' data: 'request-2'
} }
} }
@ -29,7 +31,7 @@ export default [
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: 'request-3' data: 'request-3'
} }
} }
@ -40,7 +42,7 @@ export default [
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: 'request-4' data: 'request-4'
} }
} }
@ -51,7 +53,7 @@ export default [
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: 'request-5' data: 'request-5'
} }
} }

View File

@ -1,5 +1,5 @@
import { faker } from '@faker-js/faker' import { faker } from '@faker-js/faker'
import { SUCCESS_CODE } from '@/constants'
import { toAnyString } from '@/utils' import { toAnyString } from '@/utils'
const delay = 1000 const delay = 1000
@ -1141,7 +1141,7 @@ export default [
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: adminList data: adminList
} }
} }
@ -1152,7 +1152,7 @@ export default [
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: { data: {
list: List, list: List,
total: 4 total: 4
@ -1167,7 +1167,7 @@ export default [
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: testList data: testList
} }
} }
@ -1178,7 +1178,7 @@ export default [
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: { data: {
list: List, list: List,
total: 4 total: 4

View File

@ -1,9 +1,8 @@
import { faker } from '@faker-js/faker' import { faker } from '@faker-js/faker'
import { SUCCESS_CODE } from '@/constants'
import { toAnyString } from '@/utils' import { toAnyString } from '@/utils'
const delay = 1000 const delay = 1000
const count = 100 const count = 100
const baseContent = const baseContent =
@ -193,7 +192,7 @@ export default [
(_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1) (_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)
) )
return { return {
code: 0, code: SUCCESS_CODE,
data: { data: {
total: mockList.length, total: mockList.length,
list: pageList list: pageList
@ -216,7 +215,7 @@ export default [
(_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1) (_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)
) )
return { return {
code: 0, code: SUCCESS_CODE,
data: { data: {
total: mockList.length, total: mockList.length,
list: pageList list: pageList
@ -237,7 +236,7 @@ export default [
}) })
].concat(List) ].concat(List)
return { return {
code: 0, code: SUCCESS_CODE,
data: 'success' data: 'success'
} }
} else { } else {
@ -249,7 +248,7 @@ export default [
} }
}) })
return { return {
code: 0, code: SUCCESS_CODE,
data: 'success' data: 'success'
} }
} }
@ -264,7 +263,7 @@ export default [
for (const example of List) { for (const example of List) {
if (example.id === id) { if (example.id === id) {
return { return {
code: 0, code: SUCCESS_CODE,
data: example data: example
} }
} }
@ -290,7 +289,7 @@ export default [
} }
} }
return { return {
code: 0, code: SUCCESS_CODE,
data: 'success' data: 'success'
} }
} }

View File

@ -1,3 +1,5 @@
import { SUCCESS_CODE } from '@/constants'
const delay = 1000 const delay = 1000
const List: { const List: {
@ -40,7 +42,7 @@ export default [
) )
return { return {
code: 0, code: SUCCESS_CODE,
data: { data: {
total: mockList.length, total: mockList.length,
list: pageList list: pageList
@ -60,7 +62,7 @@ export default [
if (user.username === data.username && user.password === data.password) { if (user.username === data.username && user.password === data.password) {
hasUser = true hasUser = true
return { return {
code: 0, code: SUCCESS_CODE,
data: user data: user
} }
} }
@ -80,7 +82,7 @@ export default [
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: null data: null
} }
} }

View File

@ -1,3 +1,5 @@
import { SUCCESS_CODE } from '@/constants'
const delay = 1000 const delay = 1000
export default [ export default [
@ -8,7 +10,7 @@ export default [
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: { data: {
project: 40, project: 40,
access: 2340, access: 2340,
@ -24,7 +26,7 @@ export default [
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: [ data: [
{ {
name: 'Github', name: 'Github',
@ -79,7 +81,7 @@ export default [
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: [ data: [
{ {
keys: ['workplace.push', 'Github'], keys: ['workplace.push', 'Github'],
@ -116,7 +118,7 @@ export default [
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: [ data: [
{ {
name: 'Github', name: 'Github',
@ -153,7 +155,7 @@ export default [
delay, delay,
body: () => { body: () => {
return { return {
code: 0, code: SUCCESS_CODE,
data: [ data: [
{ name: 'workplace.quote', max: 65, personal: 42, team: 50 }, { name: 'workplace.quote', max: 65, personal: 42, team: 50 },
{ name: 'workplace.contribution', max: 160, personal: 30, team: 140 }, { name: 'workplace.contribution', max: 160, personal: 30, team: 140 },

View File

@ -1,4 +1,4 @@
import request from '@/config/axios' import request from '@/axios'
// 获取所有字典 // 获取所有字典
export const getDictApi = () => { export const getDictApi = () => {

View File

@ -1,4 +1,4 @@
import request from '@/config/axios' import request from '@/axios'
import type { import type {
AnalysisTotalTypes, AnalysisTotalTypes,
UserAccessSource, UserAccessSource,

View File

@ -1,4 +1,4 @@
import request from '@/config/axios' import request from '@/axios'
import type { WorkplaceTotal, Project, Dynamic, Team, RadarData } from './types' import type { WorkplaceTotal, Project, Dynamic, Team, RadarData } from './types'
export const getCountApi = (): Promise<IResponse<WorkplaceTotal>> => { export const getCountApi = (): Promise<IResponse<WorkplaceTotal>> => {

View File

@ -1,4 +1,4 @@
import request from '@/config/axios' import request from '@/axios'
import { DepartmentListResponse, DepartmentUserParams, DepartmentUserResponse } from './types' import { DepartmentListResponse, DepartmentUserParams, DepartmentUserResponse } from './types'
export const getDepartmentApi = () => { export const getDepartmentApi = () => {

View File

@ -1,4 +1,4 @@
import request from '@/config/axios' import request from '@/axios'
import type { UserType } from './types' import type { UserType } from './types'
interface RoleParams { interface RoleParams {

View File

@ -1,4 +1,4 @@
import request from '@/config/axios' import request from '@/axios'
export const getMenuListApi = () => { export const getMenuListApi = () => {
return request.get({ url: '/mock/menu/list' }) return request.get({ url: '/mock/menu/list' })

View File

@ -1,4 +1,4 @@
import request from '@/config/axios' import request from '@/axios'
import { RequestResponse } from './types' import { RequestResponse } from './types'
export const request1 = () => { export const request1 = () => {

View File

@ -1,4 +1,4 @@
import request from '@/config/axios' import request from '@/axios'
export const getRoleListApi = () => { export const getRoleListApi = () => {
return request.get({ url: '/mock/role/table' }) return request.get({ url: '/mock/role/table' })

View File

@ -1,4 +1,4 @@
import request from '@/config/axios' import request from '@/axios'
import type { TableData } from './types' import type { TableData } from './types'
export const getTableListApi = (params: any) => { export const getTableListApi = (params: any) => {

View File

@ -1,46 +1,13 @@
import { import { AxiosResponse, AxiosRequestHeaders, InternalAxiosRequestConfig } from './types'
AxiosConfig,
AxiosResponse,
AxiosRequestHeaders,
InternalAxiosRequestConfig
} from './types'
import { ElMessage } from 'element-plus' import { ElMessage } from 'element-plus'
import qs from 'qs' import qs from 'qs'
import router from '@/router' import router from '@/router'
import { SUCCESS_CODE } from '@/constants'
import { useStorage } from '@/hooks/web/useStorage' import { useStorage } from '@/hooks/web/useStorage'
const { clear } = 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) => { const defaultRequestInterceptors = (config: InternalAxiosRequestConfig) => {
if ( if (
config.method === 'post' && config.method === 'post' &&
@ -64,11 +31,11 @@ const defaultRequestInterceptors = (config: InternalAxiosRequestConfig) => {
return config return config
} }
const defaultResponseInterceptors = (response: AxiosResponse<any>) => { const defaultResponseInterceptors = (response: AxiosResponse) => {
if (response?.config?.responseType === 'blob') { if (response?.config?.responseType === 'blob') {
// 如果是文件流,直接过 // 如果是文件流,直接过
return response return response
} else if (response.data.code === config.code) { } else if (response.data.code === SUCCESS_CODE) {
return response.data return response.data
} else { } else {
ElMessage.error(response?.data?.message) ElMessage.error(response?.data?.message)
@ -81,4 +48,3 @@ const defaultResponseInterceptors = (response: AxiosResponse<any>) => {
} }
export { defaultResponseInterceptors, defaultRequestInterceptors } export { defaultResponseInterceptors, defaultRequestInterceptors }
export default config

View File

@ -1,11 +1,8 @@
import service from './service' import service from './service'
import { CONTENT_TYPE } from '@/constants'
import config from './config'
const { defaultHeaders } = config
const request = (option: AxiosConfig) => { const request = (option: AxiosConfig) => {
const { url, method, params, data, headersType, responseType } = option const { url, method, params, data, headers, responseType } = option
return service.request({ return service.request({
url: url, url: url,
method, method,
@ -13,7 +10,8 @@ const request = (option: AxiosConfig) => {
data, data,
responseType: responseType, responseType: responseType,
headers: { headers: {
'Content-Type': headersType || defaultHeaders 'Content-Type': CONTENT_TYPE,
...headers
} }
}) })
} }

View File

@ -1,18 +1,16 @@
import axios, { AxiosError } from 'axios' import axios, { AxiosError } from 'axios'
import config, { defaultRequestInterceptors, defaultResponseInterceptors } from './config' import { defaultRequestInterceptors, defaultResponseInterceptors } from './config'
import { AxiosInstance, InternalAxiosRequestConfig, RequestConfig, AxiosResponse } from './types' import { AxiosInstance, InternalAxiosRequestConfig, RequestConfig, AxiosResponse } from './types'
import { ElMessage } from 'element-plus' import { ElMessage } from 'element-plus'
import { REQUEST_TIMEOUT } from '@/constants'
const { interceptors } = config
export const PATH_URL = import.meta.env.VITE_API_BASE_PATH export const PATH_URL = import.meta.env.VITE_API_BASE_PATH
const { requestInterceptors, responseInterceptors } = interceptors
const abortControllerMap: Map<string, AbortController> = new Map() const abortControllerMap: Map<string, AbortController> = new Map()
const axiosInstance: AxiosInstance = axios.create({ const axiosInstance: AxiosInstance = axios.create({
...config, timeout: REQUEST_TIMEOUT,
baseURL: PATH_URL baseURL: PATH_URL
}) })
@ -38,8 +36,8 @@ axiosInstance.interceptors.response.use(
} }
) )
axiosInstance.interceptors.request.use(requestInterceptors || defaultRequestInterceptors) axiosInstance.interceptors.request.use(defaultRequestInterceptors)
axiosInstance.interceptors.response.use(responseInterceptors || defaultResponseInterceptors) axiosInstance.interceptors.response.use(defaultResponseInterceptors)
const service = { const service = {
request: (config: RequestConfig) => { request: (config: RequestConfig) => {

View File

@ -15,12 +15,6 @@ interface RequestInterceptors<T> {
responseInterceptors?: (config: T) => T responseInterceptors?: (config: T) => T
responseInterceptorsCatch?: (err: any) => any responseInterceptorsCatch?: (err: any) => any
} }
interface AxiosConfig<T = AxiosResponse> {
code: number
defaultHeaders: AxiosHeaders
timeout: number
interceptors: RequestInterceptors<T>
}
interface RequestConfig<T = AxiosResponse> extends AxiosRequestConfig { interface RequestConfig<T = AxiosResponse> extends AxiosRequestConfig {
interceptors?: RequestInterceptors<T> interceptors?: RequestInterceptors<T>
@ -30,7 +24,6 @@ export {
AxiosResponse, AxiosResponse,
RequestInterceptors, RequestInterceptors,
RequestConfig, RequestConfig,
AxiosConfig,
AxiosInstance, AxiosInstance,
InternalAxiosRequestConfig, InternalAxiosRequestConfig,
AxiosRequestHeaders, AxiosRequestHeaders,

14
src/constants/index.ts Normal file
View File

@ -0,0 +1,14 @@
/**
*
*/
export const SUCCESS_CODE = 0
/**
* contentType
*/
export const CONTENT_TYPE = 'application/json'
/**
*
*/
export const REQUEST_TIMEOUT = 60000

View File

@ -4,7 +4,7 @@ import { useI18n } from '@/hooks/web/useI18n'
import { ElButton, ElDivider } from 'element-plus' import { ElButton, ElDivider } from 'element-plus'
import { request1, request2, request3, request4, request5, expired } from '@/api/request' import { request1, request2, request3, request4, request5, expired } from '@/api/request'
import { ref } from 'vue' import { ref } from 'vue'
import request from '@/config/axios' import request from '@/axios'
const { t } = useI18n() const { t } = useI18n()
@ -171,3 +171,4 @@ const tokenExpired = () => {
<ElButton type="primary" @click="tokenExpired">token过期</ElButton> <ElButton type="primary" @click="tokenExpired">token过期</ElButton>
</ContentWrap> </ContentWrap>
</template> </template>
@/axios

3
types/env.d.ts vendored
View File

@ -15,6 +15,9 @@ interface ImportMetaEnv {
readonly VITE_DROP_CONSOLE: string readonly VITE_DROP_CONSOLE: string
readonly VITE_SOURCEMAP: string readonly VITE_SOURCEMAP: string
readonly VITE_OUT_DIR: 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 { declare global {

7
types/global.d.ts vendored
View File

@ -1,4 +1,5 @@
import type { CSSProperties } from 'vue' import type { CSSProperties } from 'vue'
import { AxiosRequestHeaders } from 'axios'
declare global { declare global {
declare interface Fn<T = any> { declare interface Fn<T = any> {
(...arg: T[]): T (...arg: T[]): T
@ -25,7 +26,7 @@ declare global {
declare type LayoutType = 'classic' | 'topLeft' | 'top' | 'cutMenu' declare type LayoutType = 'classic' | 'topLeft' | 'top' | 'cutMenu'
declare type AxiosHeaders = declare type AxiosContentType =
| 'application/json' | 'application/json'
| 'application/x-www-form-urlencoded' | 'application/x-www-form-urlencoded'
| 'multipart/form-data' | 'multipart/form-data'
@ -39,12 +40,12 @@ declare global {
data?: any data?: any
url?: string url?: string
method?: AxiosMethod method?: AxiosMethod
headersType?: string headers?: AxiosRequestHeaders
responseType?: AxiosResponseType responseType?: AxiosResponseType
} }
declare interface IResponse<T = any> { declare interface IResponse<T = any> {
code: string code: number
data: T extends any ? T : T & any data: T extends any ? T : T & any
} }