Skip to content

Commit

Permalink
feat: refactoring API
Browse files Browse the repository at this point in the history
  • Loading branch information
kailong321200875 committed Jun 25, 2022
1 parent 4351853 commit 37b7583
Show file tree
Hide file tree
Showing 29 changed files with 199 additions and 340 deletions.
4 changes: 1 addition & 3 deletions mock/role/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,7 @@ export default [
const { roleName } = query
return {
code: result_code,
data: {
list: roleName === 'admin' ? adminList : testList
}
data: roleName === 'admin' ? adminList : testList
}
}
}
Expand Down
9 changes: 0 additions & 9 deletions src/api-types/user.ts

This file was deleted.

10 changes: 6 additions & 4 deletions src/api/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { useAxios } from '@/hooks/web/useAxios'
const request = useAxios()

// 获取所有字典
export const getDictApi = () => {
return request.get({ url: '/dict/list' })
export const getDictApi = async (): Promise<IResponse> => {
const res = await request.get({ url: '/dict/list' })
return res && res.data
}

// 模拟获取某个字典
export const getDictOneApi = () => {
return request.get({ url: '/dict/one' })
export const getDictOneApi = async (): Promise<IResponse> => {
const res = await request.get({ url: '/dict/one' })
return res && res.data
}
20 changes: 12 additions & 8 deletions src/api/dashboard/analysis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ import type {

const request = useAxios()

export const getCountApi = () => {
return request.get<AnalysisTotalTypes>({ url: '/analysis/total' })
export const getCountApi = async (): Promise<IResponse<AnalysisTotalTypes[]>> => {
const res = await request.get({ url: '/analysis/total' })
return res && res.data
}

export const getUserAccessSourceApi = () => {
return request.get<UserAccessSource[]>({ url: '/analysis/userAccessSource' })
export const getUserAccessSourceApi = async (): Promise<IResponse<UserAccessSource[]>> => {
const res = await request.get({ url: '/analysis/userAccessSource' })
return res && res.data
}

export const getWeeklyUserActivityApi = () => {
return request.get<WeeklyUserActivity[]>({ url: '/analysis/weeklyUserActivity' })
export const getWeeklyUserActivityApi = async (): Promise<IResponse<WeeklyUserActivity[]>> => {
const res = await request.get({ url: '/analysis/weeklyUserActivity' })
return res && res.data
}

export const getMonthlySalesApi = () => {
return request.get<MonthlySales[]>({ url: '/analysis/monthlySales' })
export const getMonthlySalesApi = async (): Promise<IResponse<MonthlySales[]>> => {
const res = await request.get({ url: '/analysis/monthlySales' })
return res && res.data
}
25 changes: 15 additions & 10 deletions src/api/dashboard/workplace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@ import type { WorkplaceTotal, Project, Dynamic, Team, RadarData } from './types'

const request = useAxios()

export const getCountApi = () => {
return request.get<WorkplaceTotal>({ url: '/workplace/total' })
export const getCountApi = async (): Promise<IResponse<WorkplaceTotal>> => {
const res = await request.get({ url: '/workplace/total' })
return res && res.data
}

export const getProjectApi = () => {
return request.get<Project[]>({ url: '/workplace/project' })
export const getProjectApi = async (): Promise<IResponse<Project>> => {
const res = await request.get({ url: '/workplace/project' })
return res && res.data
}

export const getDynamicApi = () => {
return request.get<Dynamic[]>({ url: '/workplace/dynamic' })
export const getDynamicApi = async (): Promise<IResponse<Dynamic[]>> => {
const res = await request.get({ url: '/workplace/dynamic' })
return res && res.data
}

export const getTeamApi = () => {
return request.get<Team[]>({ url: '/workplace/team' })
export const getTeamApi = async (): Promise<IResponse<Team[]>> => {
const res = await request.get({ url: '/workplace/team' })
return res && res.data
}

export const getRadarApi = () => {
return request.get<RadarData[]>({ url: '/workplace/radar' })
export const getRadarApi = async (): Promise<IResponse<RadarData[]>> => {
const res = await request.get({ url: '/workplace/radar' })
return res && res.data
}
33 changes: 17 additions & 16 deletions src/api/login/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { useAxios } from '@/hooks/web/useAxios'
import type { UserType } from './types'
import { IUserModel } from '@/api-types/user'

interface RoleParams {
roleName: string
}

const request = useAxios()

export const loginApi = async (data: Pick<IUserModel, 'user_name' | 'password'>) => {
const res = await request.post<IResponse<IUserModel>>({
url: '/user/login',
data
})
export const loginApi = async (data: UserType): Promise<IResponse<UserType>> => {
const res = await request.post({ url: '/user/login', data })
return res && res.data
}

export const loginOutApi = () => {
return request.get({ url: '/user/loginOut' })
export const loginOutApi = async (): Promise<IResponse> => {
const res = await request.get({ url: '/user/loginOut' })
return res && res.data
}

export const getUserListApi = ({ params }: AxiosConfig) => {
Expand All @@ -23,14 +24,14 @@ export const getUserListApi = ({ params }: AxiosConfig) => {
}>({ url: '/user/list', params })
}

export const getAdminRoleApi = ({ params }) => {
return request.get<{
list: AppCustomRouteRecordRaw[]
}>({ url: '/role/list', params })
export const getAdminRoleApi = async (
params: RoleParams
): Promise<IResponse<AppCustomRouteRecordRaw[]>> => {
const res = await request.get({ url: '/role/list', params })
return res && res.data
}

export const getTestRoleApi = ({ params }) => {
return request.get<{
list: string[]
}>({ url: '/role/list', params })
export const getTestRoleApi = async (params: RoleParams): Promise<IResponse<string[]>> => {
const res = await request.get({ url: '/role/list', params })
return res && res.data
}
21 changes: 0 additions & 21 deletions src/api/register/index.ts

This file was deleted.

23 changes: 12 additions & 11 deletions src/api/table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ import type { TableData } from './types'

const request = useAxios()

export const getTableListApi = ({ params }) => {
return request.get<{
total: number
list: TableData[]
}>({ url: '/example/list', params })
export const getTableListApi = async (params: any): Promise<IResponse> => {
const res = await request.get({ url: '/example/list', params })
return res && res.data
}

export const saveTableApi = ({ data }) => {
return request.post<TableData>({ url: '/example/save', data })
export const saveTableApi = async (data: Partial<TableData>): Promise<IResponse> => {
const res = await request.post({ url: '/example/save', data })
return res && res.data
}

export const getTableDetApi = ({ params }) => {
return request.get<TableData>({ url: '/example/detail', params })
export const getTableDetApi = async (id: string): Promise<IResponse<TableData>> => {
const res = await request.get({ url: '/example/detail', params: { id } })
return res && res.data
}

export const delTableListApi = ({ data }) => {
return request.post({ url: '/example/delete', data })
export const delTableListApi = async (ids: string[] | number[]): Promise<IResponse> => {
const res = await request.post({ url: '/example/delete', data: { ids } })
return res && res.data
}
2 changes: 1 addition & 1 deletion src/config/axios/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const config: {
*/
base_url: {
// 开发环境接口前缀
base: '/api',
base: '',

// 打包开发环境接口前缀
dev: '',
Expand Down
85 changes: 34 additions & 51 deletions src/hooks/web/useTable.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { Table, TableExpose } from '@/components/Table'
import { ElTable, ElMessageBox, ElMessage } from 'element-plus'
import { ref, reactive, watch, computed, unref, nextTick } from 'vue'
import { AxiosPromise } from 'axios'
import { get } from 'lodash-es'
import type { TableProps } from '@/components/Table/src/types'
import { useI18n } from '@/hooks/web/useI18n'

const { t } = useI18n()

interface UseTableConfig<T, L> {
getListApi: (option: L) => AxiosPromise<T>
delListApi?: (option: AxiosConfig) => AxiosPromise<unknown>
interface TableResponse<T = any> {
total: number
list: T[]
pageNumber: number
pageSize: number
}

interface UseTableConfig<T = any> {
getListApi: (option: any) => Promise<IResponse<TableResponse<T>>>
delListApi?: (option: any) => Promise<IResponse>
// 返回数据格式配置
response: {
list: string
Expand All @@ -19,20 +25,18 @@ interface UseTableConfig<T, L> {
props?: TableProps
}

interface TableObject<K, L> {
interface TableObject<T = any> {
pageSize: number
currentPage: number
total: number
tableList: K[]
paramsObj: L
tableList: T[]
params: any
loading: boolean
currentRow: Nullable<K>
currentRow: Nullable<T>
}

export const useTable = <T, K, L extends AxiosConfig = AxiosConfig>(
config?: UseTableConfig<T, L>
) => {
const tableObject = reactive<TableObject<K, L>>({
export const useTable = <T = any>(config?: UseTableConfig<T>) => {
const tableObject = reactive<TableObject<T>>({
// 页数
pageSize: 10,
// 当前页
Expand All @@ -42,7 +46,7 @@ export const useTable = <T, K, L extends AxiosConfig = AxiosConfig>(
// 表格数据
tableList: [],
// AxiosConfig 配置
paramsObj: {} as L,
params: {},
// 加载中
loading: true,
// 当前行的数据
Expand All @@ -51,11 +55,9 @@ export const useTable = <T, K, L extends AxiosConfig = AxiosConfig>(

const paramsObj = computed(() => {
return {
params: {
...tableObject.paramsObj.params,
pageSize: tableObject.pageSize,
pageIndex: tableObject.currentPage
}
...tableObject.params,
pageSize: tableObject.pageSize,
pageIndex: tableObject.currentPage
}
})

Expand Down Expand Up @@ -99,8 +101,8 @@ export const useTable = <T, K, L extends AxiosConfig = AxiosConfig>(
return table
}

const delData = async (paramsObj: AxiosConfig, ids: string[] | number[]) => {
const res = await (config?.delListApi && config?.delListApi(paramsObj))
const delData = async (ids: string[] | number[]) => {
const res = await (config?.delListApi && config?.delListApi(ids))
if (res) {
ElMessage.success(t('common.delSuccess'))

Expand All @@ -117,22 +119,12 @@ export const useTable = <T, K, L extends AxiosConfig = AxiosConfig>(
}
}

const methods: {
setProps: (props: Recordable) => void
getList: () => Promise<void>
setColumn: (columnProps: TableSetPropsType[]) => void
setSearchParams: (data: Recordable) => void
getSelections: () => Promise<K[]>
delList: (ids: string[] | number[], multiple: boolean, message?: boolean) => Promise<void>
} = {
const methods = {
getList: async () => {
tableObject.loading = true
const res = await config
?.getListApi(unref(paramsObj) as unknown as L)
.catch(() => {})
.finally(() => {
tableObject.loading = false
})
const res = await config?.getListApi(unref(paramsObj)).finally(() => {
tableObject.loading = false
})
if (res) {
tableObject.tableList = get(res.data || {}, config?.response.list as string)
tableObject.total = get(res.data || {}, config?.response?.total as string) || 0
Expand All @@ -148,17 +140,15 @@ export const useTable = <T, K, L extends AxiosConfig = AxiosConfig>(
},
getSelections: async () => {
const table = await getTable()
return (table?.selections || []) as K[]
return (table?.selections || []) as T[]
},
// 与Search组件结合
setSearchParams: (data: Recordable) => {
tableObject.currentPage = 1
tableObject.paramsObj = Object.assign(tableObject.paramsObj, {
params: {
pageSize: tableObject.pageSize,
pageIndex: tableObject.currentPage,
...data
}
tableObject.params = Object.assign(tableObject.params, {
pageSize: tableObject.pageSize,
pageIndex: tableObject.currentPage,
...data
})
methods.getList()
},
Expand All @@ -176,23 +166,16 @@ export const useTable = <T, K, L extends AxiosConfig = AxiosConfig>(
return
}
}
const paramsObj: AxiosConfig = {
data: {
ids
}
}
if (message) {
ElMessageBox.confirm(t('common.delMessage'), t('common.delWarning'), {
confirmButtonText: t('common.delOk'),
cancelButtonText: t('common.delCancel'),
type: 'warning'
}).then(async () => {
await delData(ids)
})
.then(async () => {
await delData(paramsObj, ids)
})
.catch(() => {})
} else {
await delData(paramsObj, ids)
await delData(ids)
}
}
}
Expand Down
Loading

0 comments on commit 37b7583

Please sign in to comment.