|
@@ -3,10 +3,14 @@ import { PageOption } from "@smallwei/avue";
|
|
|
import { ElMessageBox, ElMessage } from "element-plus";
|
|
|
import { useUserStoreHook } from "@/store/modules/user";
|
|
|
import { checkPerm } from "@/directive/permission";
|
|
|
+import { configs } from "@typescript-eslint/eslint-plugin";
|
|
|
|
|
|
interface UseCrudConfig {
|
|
|
// 模块的url,用来进行增删改查
|
|
|
src?: string;
|
|
|
+
|
|
|
+ dataListUrl?: string;
|
|
|
+
|
|
|
// 需要操作的数据
|
|
|
row?: any;
|
|
|
// done用于结束操作
|
|
@@ -15,7 +19,7 @@ interface UseCrudConfig {
|
|
|
index?: number;
|
|
|
// 用于中断操作
|
|
|
loading?: () => void;
|
|
|
- // 查询参数 一般用search的值就可以了
|
|
|
+ // 额外查询参数 一般用search的值就可以了
|
|
|
params?: object;
|
|
|
// 是否是编辑,如果是编辑调用更新,否则调用新增
|
|
|
isEdit?: boolean;
|
|
@@ -23,15 +27,19 @@ interface UseCrudConfig {
|
|
|
|
|
|
export const useCrud = (config?: UseCrudConfig) => {
|
|
|
const url = ref(config?.src);
|
|
|
-
|
|
|
+ const commonConfig = ref(config);
|
|
|
/** 表格配置属性 */
|
|
|
const option = ref({
|
|
|
searchIcon: true,
|
|
|
+ // searchSpan: 4,
|
|
|
searchIndex: 3, //searchIcon是否启用功能按钮, searchIndex配置收缩展示的个数,默认为2个
|
|
|
index: true, //是否显示第几项
|
|
|
+ indexLabel: "序号",
|
|
|
+ indexWidth: "55px",
|
|
|
refreshBtn: false,
|
|
|
border: true,
|
|
|
viewBtn: true,
|
|
|
+ tip: false, //选中的提示
|
|
|
});
|
|
|
const data = ref<any>([]); //表格数据
|
|
|
const form = ref({}); //新增或者编辑弹出的表单绑定值
|
|
@@ -40,7 +48,7 @@ export const useCrud = (config?: UseCrudConfig) => {
|
|
|
|
|
|
/** 表格的分页数据 v-model */
|
|
|
const page = ref<PageOption>({
|
|
|
- total: 12220,
|
|
|
+ total: 0,
|
|
|
currentPage: 1,
|
|
|
pageSize: 10,
|
|
|
});
|
|
@@ -50,25 +58,24 @@ export const useCrud = (config?: UseCrudConfig) => {
|
|
|
const toDeleteIds = ref<Array<string>>([]);
|
|
|
|
|
|
const save = async (config?: UseCrudConfig) => {
|
|
|
- const path = config?.isEdit ? "/update" : "/add";
|
|
|
try {
|
|
|
- const res = await request({
|
|
|
+ const path = config?.isEdit ? "/update" : "/add";
|
|
|
+
|
|
|
+ const res = (await request({
|
|
|
url: `${url.value}${path}`,
|
|
|
method: "post",
|
|
|
data: form.value,
|
|
|
- });
|
|
|
- if (res?.data?.code == 200) {
|
|
|
+ })) as any;
|
|
|
+ if (res?.code == 200) {
|
|
|
Methords.dataList();
|
|
|
config?.done && config?.done();
|
|
|
+ ElMessage.success(res?.msg ?? "");
|
|
|
} else {
|
|
|
- ElMessage.error(res?.data?.msg ?? "");
|
|
|
+ config?.loading && config?.loading();
|
|
|
+ ElMessage.error(res?.msg ?? "");
|
|
|
}
|
|
|
} catch (err) {
|
|
|
- ElMessage.error("Oops, this is a error message.");
|
|
|
- // config?.loading && config?.loading();
|
|
|
- } finally {
|
|
|
- ElMessage.error("Oops, this is a error message.");
|
|
|
- // config?.done && config?.done();
|
|
|
+ config?.loading && config?.loading();
|
|
|
}
|
|
|
};
|
|
|
|
|
@@ -95,17 +102,61 @@ export const useCrud = (config?: UseCrudConfig) => {
|
|
|
handleSearchData();
|
|
|
try {
|
|
|
const res = await request({
|
|
|
+ url: commonConfig.value.dataListUrl ?? `${url.value}/page`,
|
|
|
+ method: "post",
|
|
|
+ data: {
|
|
|
+ pageNo: page.value.currentPage,
|
|
|
+ pageSize: page.value.pageSize,
|
|
|
+ ...search.value,
|
|
|
+ ...(commonConfig.value?.params ?? {}),
|
|
|
+ },
|
|
|
+ });
|
|
|
+ if (res?.data) {
|
|
|
+ if (res?.data instanceof Array) {
|
|
|
+ data.value = res?.data || [];
|
|
|
+ page.value.total = res?.data?.length || 0;
|
|
|
+ } else {
|
|
|
+ data.value = res?.data?.records || [];
|
|
|
+ page.value.total = res?.data?.totalCount || 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ config?.done && config?.done();
|
|
|
+ } catch (err) {
|
|
|
+ config?.loading && config?.loading();
|
|
|
+ } finally {
|
|
|
+ config?.done && config?.done();
|
|
|
+ }
|
|
|
+ },
|
|
|
+ dataEditList: async (config?: UseCrudConfig) => {
|
|
|
+ handleSearchData();
|
|
|
+ try {
|
|
|
+ const res = await request({
|
|
|
url: `${url.value}/page`,
|
|
|
method: "post",
|
|
|
data: {
|
|
|
pageNo: page.value.currentPage,
|
|
|
pageSize: page.value.pageSize,
|
|
|
...search.value,
|
|
|
+ ...(commonConfig.value?.params ?? {}),
|
|
|
},
|
|
|
});
|
|
|
if (res?.data) {
|
|
|
- data.value = res?.data?.records || [];
|
|
|
- page.value.total = res?.data?.totalCount || 0;
|
|
|
+ if(res?.data instanceof Array){
|
|
|
+ data.value = res?.data || []
|
|
|
+ page.value.total = res?.data?.length || 0
|
|
|
+ }else{
|
|
|
+ data.value = res?.data?.records || [];
|
|
|
+ for (let i = 0; i < data.value.length; i++) {
|
|
|
+ data.value[i].$cellEdit = true;
|
|
|
+ if(data.value[i].children!=undefined&&data.value[i].children!=null&&data.value[i].children.length > 0 ){
|
|
|
+ for(let j=0;j < data.value[i].children.length; j++){
|
|
|
+ data.value[i].children[j].$cellEdit = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ page.value.total = res?.data?.totalCount || 0;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
config?.done && config?.done();
|
|
|
} catch (err) {
|
|
@@ -114,7 +165,27 @@ export const useCrud = (config?: UseCrudConfig) => {
|
|
|
config?.done && config?.done();
|
|
|
}
|
|
|
},
|
|
|
+ dataNoPageList: async (config?: UseCrudConfig) => {
|
|
|
+ handleSearchData();
|
|
|
+ try {
|
|
|
+ const res = await request({
|
|
|
+ url: `${url.value}/list`,
|
|
|
+ method: "post",
|
|
|
+ data: {
|
|
|
+ ...search.value,
|
|
|
+ },
|
|
|
+ });
|
|
|
|
|
|
+ if (res?.data) {
|
|
|
+ data.value = res?.data || [];
|
|
|
+ }
|
|
|
+ config?.done && config?.done();
|
|
|
+ } catch (err) {
|
|
|
+ config?.loading && config?.loading();
|
|
|
+ } finally {
|
|
|
+ config?.done && config?.done();
|
|
|
+ }
|
|
|
+ },
|
|
|
createRow: (row: any, done: () => void, loading: () => void) => {
|
|
|
save({ row: row, done: done, loading: loading });
|
|
|
},
|
|
@@ -140,11 +211,15 @@ export const useCrud = (config?: UseCrudConfig) => {
|
|
|
cancelButtonText: "取消",
|
|
|
type: "warning",
|
|
|
}).then(async () => {
|
|
|
+ if(row.children && row.children.length > 0 ){
|
|
|
+ ElMessage.error("请先解绑下级关系")
|
|
|
+ return
|
|
|
+ }
|
|
|
try {
|
|
|
const res = await request({
|
|
|
url: `${url.value}/del`,
|
|
|
method: "post",
|
|
|
- data: { id: row.id ?? "" },
|
|
|
+ data: row,
|
|
|
});
|
|
|
Methords.dataList();
|
|
|
config?.done && config?.done();
|
|
@@ -190,6 +265,35 @@ export const useCrud = (config?: UseCrudConfig) => {
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
+ * 表格拖拽后批量保存
|
|
|
+ * */
|
|
|
+ multipleUpdate: async () => {
|
|
|
+ try {
|
|
|
+ // 由于数据带有$开头的属性,所以需要处理下,改为只传id和sortNum。
|
|
|
+ const dtosArray: { id: string; sortNum: number }[] = [];
|
|
|
+ for (let i = 0; i < data.value.length; i++) {
|
|
|
+ let cur = page.value.currentPage ?? 1;
|
|
|
+ cur = cur - 1;
|
|
|
+ const size = page.value.pageSize ?? 10;
|
|
|
+ let sortNum = cur * size;
|
|
|
+ sortNum = sortNum + i;
|
|
|
+
|
|
|
+ dtosArray.push({ id: data.value[i].id, sortNum: sortNum });
|
|
|
+ }
|
|
|
+ const res = await request({
|
|
|
+ url: `${url.value}/batch-update`,
|
|
|
+ method: "post",
|
|
|
+ data: dtosArray,
|
|
|
+ });
|
|
|
+ Methords.dataList();
|
|
|
+ config?.done && config?.done();
|
|
|
+ } catch (err) {
|
|
|
+ config?.loading && config?.loading();
|
|
|
+ } finally {
|
|
|
+ config?.done && config?.done();
|
|
|
+ }
|
|
|
+ },
|
|
|
+ /**
|
|
|
* 点击搜索按钮触发
|
|
|
*/
|
|
|
searchChange: async (params: any, done: () => void) => {
|
|
@@ -261,6 +365,7 @@ export const useCrud = (config?: UseCrudConfig) => {
|
|
|
* 根据搜索项导出数据
|
|
|
*/
|
|
|
exportData: async (urlStr: string) => {
|
|
|
+ handleSearchData();
|
|
|
const response = await request({
|
|
|
url: urlStr,
|
|
|
method: "post",
|
|
@@ -281,5 +386,6 @@ export const useCrud = (config?: UseCrudConfig) => {
|
|
|
toDeleteIds,
|
|
|
Methords,
|
|
|
Utils,
|
|
|
+ commonConfig,
|
|
|
};
|
|
|
};
|