格式化代码,websocket功能完善
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import { useI18n as useVueI18n } from 'vue-i18n'
|
||||
import { useI18nStore } from '@/stores/modules/i18n'
|
||||
import { useI18n as useVueI18n } from "vue-i18n";
|
||||
import { useI18nStore } from "@/stores/modules/i18n";
|
||||
|
||||
export function useI18n() {
|
||||
const { t, locale, availableLocales } = useVueI18n()
|
||||
const i18nStore = useI18nStore()
|
||||
const { t, locale, availableLocales } = useVueI18n();
|
||||
const i18nStore = useI18nStore();
|
||||
|
||||
return {
|
||||
t,
|
||||
@@ -11,6 +11,6 @@ export function useI18n() {
|
||||
availableLocales,
|
||||
setLocale: i18nStore.setLocale,
|
||||
currentLocale: i18nStore.currentLocale,
|
||||
localeLabel: i18nStore.localeLabel
|
||||
}
|
||||
localeLabel: i18nStore.localeLabel,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ref, reactive, computed, onMounted } from 'vue'
|
||||
import { message } from 'ant-design-vue'
|
||||
import { ref, reactive, computed, onMounted } from "vue";
|
||||
import { message } from "ant-design-vue";
|
||||
|
||||
/**
|
||||
* 表格通用hooks
|
||||
@@ -19,30 +19,32 @@ export function useTable(options = {}) {
|
||||
api,
|
||||
searchForm: initialSearchForm = {},
|
||||
columns = [],
|
||||
rowKey = 'id',
|
||||
rowKey = "id",
|
||||
needPagination = true,
|
||||
paginationConfig = {},
|
||||
needSelection = false,
|
||||
immediateLoad = true
|
||||
} = options
|
||||
immediateLoad = true,
|
||||
} = options;
|
||||
|
||||
// 表格引用
|
||||
const tableRef = ref(null)
|
||||
const tableRef = ref(null);
|
||||
|
||||
// 搜索表单
|
||||
const searchForm = reactive({ ...initialSearchForm })
|
||||
const searchForm = reactive({ ...initialSearchForm });
|
||||
|
||||
// 表格数据
|
||||
const tableData = ref([])
|
||||
const tableData = ref([]);
|
||||
|
||||
// 加载状态
|
||||
const loading = ref(false)
|
||||
const loading = ref(false);
|
||||
|
||||
// 选中的行数据
|
||||
const selectedRows = ref([])
|
||||
const selectedRows = ref([]);
|
||||
|
||||
// 选中的行keys
|
||||
const selectedRowKeys = computed(() => selectedRows.value.map(item => item[rowKey]))
|
||||
const selectedRowKeys = computed(() =>
|
||||
selectedRows.value.map((item) => item[rowKey]),
|
||||
);
|
||||
|
||||
// 分页配置
|
||||
const defaultPaginationConfig = {
|
||||
@@ -51,171 +53,180 @@ export function useTable(options = {}) {
|
||||
total: 0,
|
||||
showSizeChanger: true,
|
||||
showTotal: (total) => `共 ${total} 条`,
|
||||
pageSizeOptions: ['20', '50', '100', '200']
|
||||
}
|
||||
pageSizeOptions: ["20", "50", "100", "200"],
|
||||
};
|
||||
|
||||
const pagination = reactive({
|
||||
...defaultPaginationConfig,
|
||||
...paginationConfig
|
||||
})
|
||||
...paginationConfig,
|
||||
});
|
||||
|
||||
// 行选择配置
|
||||
const rowSelection = computed(() => {
|
||||
if (!needSelection) return null
|
||||
if (!needSelection) return null;
|
||||
return {
|
||||
selectedRowKeys: selectedRowKeys.value,
|
||||
onChange: (keys, rows) => {
|
||||
selectedRows.value = rows
|
||||
}
|
||||
}
|
||||
})
|
||||
selectedRows.value = rows;
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
// 行选择事件处理(用于scTable的@select事件)
|
||||
const handleSelectChange = (record, selected, selectedRows) => {
|
||||
if (!needSelection) return
|
||||
if (!needSelection) return;
|
||||
if (selected) {
|
||||
selectedRows.value.push(record)
|
||||
selectedRows.value.push(record);
|
||||
} else {
|
||||
const index = selectedRows.value.findIndex(item => item[rowKey] === record[rowKey])
|
||||
const index = selectedRows.value.findIndex(
|
||||
(item) => item[rowKey] === record[rowKey],
|
||||
);
|
||||
if (index > -1) {
|
||||
selectedRows.value.splice(index, 1)
|
||||
selectedRows.value.splice(index, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 全选/取消全选处理(用于scTable的@selectAll事件)
|
||||
const handleSelectAll = (selected, selectedRows, changeRows) => {
|
||||
if (!needSelection) return
|
||||
if (!needSelection) return;
|
||||
if (selected) {
|
||||
changeRows.forEach(record => {
|
||||
if (!selectedRows.value.find(item => item[rowKey] === record[rowKey])) {
|
||||
selectedRows.value.push(record)
|
||||
changeRows.forEach((record) => {
|
||||
if (
|
||||
!selectedRows.value.find(
|
||||
(item) => item[rowKey] === record[rowKey],
|
||||
)
|
||||
) {
|
||||
selectedRows.value.push(record);
|
||||
}
|
||||
})
|
||||
});
|
||||
} else {
|
||||
changeRows.forEach(record => {
|
||||
const index = selectedRows.value.findIndex(item => item[rowKey] === record[rowKey])
|
||||
changeRows.forEach((record) => {
|
||||
const index = selectedRows.value.findIndex(
|
||||
(item) => item[rowKey] === record[rowKey],
|
||||
);
|
||||
if (index > -1) {
|
||||
selectedRows.value.splice(index, 1)
|
||||
selectedRows.value.splice(index, 1);
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 加载数据
|
||||
const loadData = async (params = {}) => {
|
||||
if (!api) {
|
||||
console.warn('useTable: 未提供api函数,无法加载数据')
|
||||
return
|
||||
console.warn("useTable: 未提供api函数,无法加载数据");
|
||||
return;
|
||||
}
|
||||
|
||||
loading.value = true
|
||||
loading.value = true;
|
||||
try {
|
||||
const requestParams = {
|
||||
...searchForm,
|
||||
...params
|
||||
}
|
||||
...params,
|
||||
};
|
||||
|
||||
// 如果需要分页,添加分页参数
|
||||
if (needPagination) {
|
||||
requestParams.page = pagination.current
|
||||
requestParams.limit = pagination.pageSize
|
||||
requestParams.page = pagination.current;
|
||||
requestParams.limit = pagination.pageSize;
|
||||
}
|
||||
|
||||
// 调用API函数,确保this上下文正确
|
||||
const res = await api(requestParams)
|
||||
const res = await api(requestParams);
|
||||
|
||||
if (res.code === 200) {
|
||||
// 如果是分页数据
|
||||
if (needPagination) {
|
||||
tableData.value = res.data?.list || []
|
||||
pagination.total = res.data?.total || 0
|
||||
tableData.value = res.data?.list || [];
|
||||
pagination.total = res.data?.total || 0;
|
||||
} else {
|
||||
// 非分页数据(如树形数据)
|
||||
// 确保数据是数组,如果不是数组则包装成数组
|
||||
const data = res.data
|
||||
const data = res.data;
|
||||
if (Array.isArray(data)) {
|
||||
tableData.value = data
|
||||
} else if (data && typeof data === 'object') {
|
||||
tableData.value = data;
|
||||
} else if (data && typeof data === "object") {
|
||||
// 如果返回的是对象,可能包含 list 或 items 等字段
|
||||
tableData.value = data.list || data.items || data.data || []
|
||||
tableData.value =
|
||||
data.list || data.items || data.data || [];
|
||||
} else {
|
||||
tableData.value = []
|
||||
tableData.value = [];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
message.error(res.message || '加载数据失败')
|
||||
message.error(res.message || "加载数据失败");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载数据失败:', error)
|
||||
message.error('加载数据失败')
|
||||
console.error("加载数据失败:", error);
|
||||
message.error("加载数据失败");
|
||||
} finally {
|
||||
loading.value = false
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 分页变化处理
|
||||
const handlePaginationChange = ({ page, pageSize }) => {
|
||||
if (!needPagination) return
|
||||
pagination.current = page
|
||||
pagination.pageSize = pageSize
|
||||
loadData()
|
||||
}
|
||||
if (!needPagination) return;
|
||||
pagination.current = page;
|
||||
pagination.pageSize = pageSize;
|
||||
loadData();
|
||||
};
|
||||
|
||||
// 搜索
|
||||
const handleSearch = () => {
|
||||
if (needPagination) {
|
||||
pagination.current = 1
|
||||
pagination.current = 1;
|
||||
}
|
||||
loadData()
|
||||
}
|
||||
loadData();
|
||||
};
|
||||
|
||||
// 重置
|
||||
const handleReset = () => {
|
||||
// 重置搜索表单为初始值
|
||||
Object.keys(searchForm).forEach(key => {
|
||||
searchForm[key] = initialSearchForm[key]
|
||||
})
|
||||
Object.keys(searchForm).forEach((key) => {
|
||||
searchForm[key] = initialSearchForm[key];
|
||||
});
|
||||
// 清空选择
|
||||
selectedRows.value = []
|
||||
selectedRows.value = [];
|
||||
// 重置分页
|
||||
if (needPagination) {
|
||||
pagination.current = 1
|
||||
pagination.current = 1;
|
||||
}
|
||||
// 重新加载数据
|
||||
loadData()
|
||||
}
|
||||
loadData();
|
||||
};
|
||||
|
||||
// 刷新表格
|
||||
const refreshTable = () => {
|
||||
loadData()
|
||||
}
|
||||
loadData();
|
||||
};
|
||||
|
||||
// 清空选择
|
||||
const clearSelection = () => {
|
||||
selectedRows.value = []
|
||||
}
|
||||
selectedRows.value = [];
|
||||
};
|
||||
|
||||
// 设置选中行
|
||||
const setSelectedRows = (rows) => {
|
||||
selectedRows.value = rows
|
||||
}
|
||||
selectedRows.value = rows;
|
||||
};
|
||||
|
||||
// 更新搜索表单
|
||||
const setSearchForm = (data) => {
|
||||
Object.assign(searchForm, data)
|
||||
}
|
||||
Object.assign(searchForm, data);
|
||||
};
|
||||
|
||||
// 直接设置表格数据(用于特殊场景)
|
||||
const setTableData = (data) => {
|
||||
tableData.value = data
|
||||
}
|
||||
tableData.value = data;
|
||||
};
|
||||
|
||||
// 组件挂载时自动加载数据
|
||||
if (immediateLoad) {
|
||||
onMounted(() => {
|
||||
loadData()
|
||||
})
|
||||
loadData();
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -243,6 +254,6 @@ export function useTable(options = {}) {
|
||||
clearSelection,
|
||||
setSelectedRows,
|
||||
setSearchForm,
|
||||
setTableData
|
||||
}
|
||||
setTableData,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user