Browse Source

增加远程调用api,完善service的类型定义

main
dark 7 months ago
parent
commit
dd8637e66b
  1. 6
      src/request.ts
  2. 31
      src/service/base.ts
  3. 25
      src/service/system.ts
  4. 2
      src/store/department.ts
  5. 10
      src/store/system.ts
  6. 9
      src/store/types/department.d.ts
  7. 15
      src/types/department.d.ts
  8. 23
      src/types/login.d.ts
  9. 50
      src/types/menus.d.ts
  10. 30
      src/types/roles.d.ts
  11. 25
      src/types/user.d.ts
  12. 9
      vite.config.ts

6
src/request.ts

@ -1,7 +1,9 @@
import axios from 'axios'
import axios, { AxiosRequestConfig } from 'axios'
export type { AxiosRequestConfig }
export const request = axios.create({ export const request = axios.create({
baseURL: '/api',
baseURL: '/api/v1',
timeout: 1000, timeout: 1000,
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',

31
src/service/base.ts

@ -0,0 +1,31 @@
import { request, AxiosRequestConfig } from '@/request.ts'
type TPage = {
page: number
pageSize: number
}
export const createCURD = <TParams, TResult>(api: string, options?: AxiosRequestConfig) => {
return {
list: (params?: TParams & TPage) => {
return request.get<TResult>(api, { ...options, params })
},
add: (data: TParams) => {
return request.post<TResult>(`${api}/add`, data, options)
},
update: (id: number, data: TParams) => {
return request.put(`${api}/${id}`, data, options)
},
delete: (id: number) => {
return request.delete(`${api}/delete`, { ...options, params: { id } })
},
batchDelete: (ids: number[]) => {
return request.delete(`${api}/deletes`, { ...options, params: { ids } })
},
info: (id: number) => {
return request.get<TResult>(`${api}/${id}`, options)
}
}
}

25
src/service/system.ts

@ -1,23 +1,16 @@
import request from '../request.ts' import request from '../request.ts'
import { LoginRequest, LoginResponse } from '@/types/login'
import { createCURD } from '@/service/base.ts'
const systemServ = { const systemServ = {
dept: {
...createCURD('/sys/dept')
},
menus: { menus: {
list: () => {
return request.get('/menus')
},
add: (data: any) => {
return request.post('/menus', data)
},
update: (id: number| string, data: any) => {
return request.put(`/menus/${id}`, data)
},
delete: (id: number| string) => {
return request.delete(`/menus/${id}`)
},
info: (id: number| string) => {
return request.get(`/menus/${id}`)
}
...createCURD('/sys/menu')
},
login: (data: LoginRequest) => {
return request.post<LoginResponse>('/sys/login', data)
} }
} }

2
src/store/department.ts

@ -1,5 +1,5 @@
import { atom } from 'jotai' import { atom } from 'jotai'
import { IDepartment } from './types/department'
import { IDepartment } from '../types/department'
import { QueryClient } from '@tanstack/query-core' import { QueryClient } from '@tanstack/query-core'
import { atomWithMutation, atomWithQuery } from 'jotai-tanstack-query' import { atomWithMutation, atomWithQuery } from 'jotai-tanstack-query'
import { IApiResult } from '../types' import { IApiResult } from '../types'

10
src/store/system.ts

@ -1,7 +1,7 @@
import { atomWithQuery } from 'jotai-tanstack-query' import { atomWithQuery } from 'jotai-tanstack-query'
import systemServ from '../service/system.ts' import systemServ from '../service/system.ts'
import { MenuItem } from '../types'
import { getIcon } from '../components/icon'
import { MenuItem } from '@/types'
import { getIcon } from '@/components/icon'
import { atom } from 'jotai/index' import { atom } from 'jotai/index'
// 格式化菜单数据, 把children转换成routes // 格式化菜单数据, 把children转换成routes
@ -34,13 +34,13 @@ export const menuDataAtom = atomWithQuery(() => ({
select: data => formatMenuData(data.data ?? []), select: data => formatMenuData(data.data ?? []),
})) }))
export const selectedMenuIdAtom = atom<string | number>(0)
export const selectedMenuAtom = atom<MenuItem>({})
export const selectedMenuIdAtom = atom<number>(0)
export const selectedMenuAtom = atom<MenuItem | unknown>(undefined)
export const byIdMenuAtom = atomWithQuery((get) => ({ export const byIdMenuAtom = atomWithQuery((get) => ({
queryKey: [ 'selectedMenu', get(selectedMenuIdAtom) ], queryKey: [ 'selectedMenu', get(selectedMenuIdAtom) ],
queryFn: async ({ queryKey: [ , id ] }) => { queryFn: async ({ queryKey: [ , id ] }) => {
return await systemServ.menus.info(id)
return await systemServ.menus.info(id as number)
}, },
select: data => data.data, select: data => data.data,
})) }))

9
src/store/types/department.d.ts

@ -1,9 +0,0 @@
export interface IDepartment {
id: string
name: string
parentId: number
order: number
createAt: string
updateAt: string
}

15
src/types/department.d.ts

@ -0,0 +1,15 @@
export interface IDepartment {
id: number,
parent_id: number,
manager_user_id: number,
phone: string,
name: string,
sort: number,
status: string,
remark: string
}
export interface DepartmentRequest extends IDepartment {
}

23
src/types/login.d.ts

@ -0,0 +1,23 @@
export interface LoginLogRequest {
key: string,
start: string,
end: string,
order: string,
prop: string,
page: number,
pageSize: number
}
export interface LoginRequest {
'mfa_status': boolean;
'username': string;
'password': string;
'code': string;
}
export interface LoginResponse {
'token': string;
'mfa_status': boolean;
}

50
src/types/menus.d.ts

@ -0,0 +1,50 @@
export interface MenuButton {
code: string,
label: string
}
export interface IMenu {
parent_id: number,
parent_path: string,
sort: number,
code: string,
name: string,
description: string,
sequence: number,
type: string,
path: string,
properties: string,
status: string
buttons: MenuButton[]
}
export interface MenuRequest extends IMenu {
}
export interface MenuResponse {
id: number,
}
export interface MenuDropRequest {
source: number,
target: number,
action: string
}
export interface MenuListRequest {
order: string,
prop: string,
page: number,
pageSize: number
}
export interface MenuListResponse {
page: number,
pageSize: number,
total: number,
rows: IMenu[]
}

30
src/types/roles.d.ts

@ -0,0 +1,30 @@
export interface IRole {
sort: number,
code: string,
name: string,
description: string,
sequence: number,
status: string,
menu_ids: number[]
}
export interface RoleRequest extends IRole {
}
export interface RoleListResponse {
key: string,
order: string,
prop: string,
page: number,
pageSize: number
}
export interface RoleListResponse {
page: number,
pageSize: number,
total: number,
rows: IRole[]
}

25
src/types/user.d.ts

@ -0,0 +1,25 @@
export interface IUser {
id: number,
created_at: string,
created_by: number,
updated_at: string,
updated_by: number,
username: string,
dept_id: number,
dept_name: string,
name: string,
phone: string,
email: string,
remark: string,
status: string,
mfa_status: boolean,
}
export interface IUserInfo {
info: IUser,
buttons: string[],
nickname: string,
avatar: string
}

9
vite.config.ts

@ -12,6 +12,15 @@ export default defineConfig({
'@': '/src', '@': '/src',
}, },
}, },
server:{
proxy:{
'/api': {
target: 'https://proapi.devwork.top',
changeOrigin: true,
// rewrite: (path) => path.replace(/^\/api/, '')
}
}
},
plugins: [ plugins: [
react(), react(),
viteMockServe({ viteMockServe({

Loading…
Cancel
Save