diff --git a/src/service/api/category.ts b/src/service/api/category.ts index 286a137..6ff461b 100644 --- a/src/service/api/category.ts +++ b/src/service/api/category.ts @@ -1,14 +1,21 @@ import { request } from '../request'; /** get category list */ -export function fetchGetCategoryList(name?: string | null, type?: string | null) { +// export function fetchGetCategoryList(name?: string | null, type?: string | null) { +// return request({ +// url: '/category/categoryTree', +// method: 'post', +// data: { +// categoryName: name, +// categoryType: type +// } +// }); +// } +export function fetchGetCategoryList(data?: Api.Category.CategorySearchParams) { return request({ url: '/category/categoryTree', method: 'post', - data: { - categoryName: name, - categoryType: type - } + data }); } diff --git a/src/typings/api.d.ts b/src/typings/api.d.ts index 3e33f18..b2b8d29 100644 --- a/src/typings/api.d.ts +++ b/src/typings/api.d.ts @@ -1368,6 +1368,11 @@ declare namespace Api { namespace Category { type CategoryType = '网站' | '文献'; + type CategorySearchParams = { + categoryName?: string | null, + categoryType?: string | null + } + type RowData = Category & { children?: RowData[]; }; diff --git a/src/views/category/index.vue b/src/views/category/index.vue index 1f1e9a5..cc7e91a 100644 --- a/src/views/category/index.vue +++ b/src/views/category/index.vue @@ -1,25 +1,19 @@ diff --git a/src/views/category/modules/category.vue b/src/views/category/modules/category.vue index d7f2968..8c15d3b 100644 --- a/src/views/category/modules/category.vue +++ b/src/views/category/modules/category.vue @@ -1,12 +1,8 @@ diff --git a/src/views/category/modules/table.ts b/src/views/category/modules/table.ts index 7cf7af2..1ffe282 100644 --- a/src/views/category/modules/table.ts +++ b/src/views/category/modules/table.ts @@ -1,12 +1,47 @@ -import { ref, Ref, Reactive } from 'vue'; -import { jsonClone } from '@sa/utils'; +import { ref, Ref } from 'vue'; import { useBoolean } from '@sa/hooks'; import { $t } from '@/locales'; +type CategoryApiFn = ( + params?: R +) => Promise>; // data/msg + +export function useCategoryTable(apiFn: CategoryApiFn) { + const loading = ref(false); + const tableData = ref([]); + const params = ref({ + categoryName: null, + categoryType: null + }); + + const rowKey = (row: Api.Category.RowData) => row.id + + const getData = async () => { // 无参 + loading.value = true; + try { + const { data } = await apiFn(params.value); + tableData.value = data as Api.Category.RowData[]; + } catch (error) { + console.error(error); + } finally { + loading.value = false; + } + }; + + return { + loading, + tableData, + params, + getData, + rowKey + }; +} + export function useFormOperate>(config: { getData: () => Promise; + immediate?: boolean; // 添加 immediate 参数,默认为 false }) { - const { getData } = config; + const { getData, immediate } = config; const { bool: drawerVisible, setTrue: openDrawer, setFalse: closeDrawer } = useBoolean(); const operateType: Ref = ref('add'); @@ -38,6 +73,21 @@ export function useFormOperate>(config: { await getData(); } + async function onUpdate() { + console.log('搜索'); + // 在这里添加搜索逻辑 + try { + await getData(); + } catch (error) { + window.$message?.error($t('common.loadFail')); + } + } + + // 如果 immediate 为 true,则在初始化时调用 getData + if (immediate) { + getData(); + } + return { drawerVisible, // 抽屉是否可见 operateType, // 操作类型 @@ -46,6 +96,7 @@ export function useFormOperate>(config: { closeDrawer, // 关闭抽屉 handleAdd, // 添加操作 handleEdit, // 编辑操作 - onDeleted + onDeleted, // 删除操作 + onUpdate // 搜索操作 }; } \ No newline at end of file