Browse Source

修改字典表

zs 1 năm trước cách đây
mục cha
commit
0cb7e4039e

+ 5 - 0
src/router/index.ts

@@ -45,6 +45,11 @@ const router = createRouter({
           meta: { title: '字典管理' },
           component: () => import('@/views/system/dict/index.vue')
         },
+        {
+          path: '/system/dictData',
+          meta: { title: '字典数据管理' },
+          component: () => import('@/views/system/dictData/index.vue')
+        },
         {
           path: '/user/user',
           meta: { title: '普通用户' },

+ 52 - 0
src/stores/basic/dictData.ts

@@ -0,0 +1,52 @@
+import { ref, computed } from 'vue';
+import { defineStore } from 'pinia';
+import { AxiosWrapper } from '@/util/axios-wrapper';
+import _ from 'lodash';
+
+import type { IQueryType, IQueryResult, IQueryParams } from '@/util/types.util';
+const axios = new AxiosWrapper();
+const api = {
+  url: `/travel/v1/api/dictData`
+};
+export const DictDataStore = defineStore('dictData', () => {
+  const count = ref(0);
+  const doubleCount = computed(() => count.value * 2);
+  function increment() {
+    count.value++;
+  }
+  const query = async ({ skip = 0, limit = undefined, ...info }: IQueryParams = {}): Promise<IQueryResult> => {
+    let cond: IQueryType = {};
+    if (skip) cond.skip = skip;
+    if (limit) cond.limit = limit;
+    cond = { ...cond, ...info };
+    const res = await axios.$get(`${api.url}`, cond);
+    return res;
+  };
+  const fetch = async (payload: any): Promise<IQueryResult> => {
+    const res = await axios.$get(`${api.url}/${payload}`);
+    return res;
+  };
+  const create = async (payload: any): Promise<IQueryResult> => {
+    const res = await axios.$post(`${api.url}`, payload);
+    return res;
+  };
+  const update = async (payload: any): Promise<IQueryResult> => {
+    const id = _.get(payload, 'id', _.get(payload, '_id'));
+    const res = await axios.$post(`${api.url}/${id}`, payload);
+    return res;
+  };
+  const del = async (payload: any): Promise<IQueryResult> => {
+    const res = await axios.$delete(`${api.url}/${payload}`);
+    return res;
+  };
+  return {
+    count,
+    doubleCount,
+    increment,
+    query,
+    fetch,
+    create,
+    update,
+    del
+  };
+});

+ 52 - 0
src/stores/basic/dictType.ts

@@ -0,0 +1,52 @@
+import { ref, computed } from 'vue';
+import { defineStore } from 'pinia';
+import { AxiosWrapper } from '@/util/axios-wrapper';
+import _ from 'lodash';
+
+import type { IQueryType, IQueryResult, IQueryParams } from '@/util/types.util';
+const axios = new AxiosWrapper();
+const api = {
+  url: `/travel/v1/api/dictType`
+}
+export const DictTypeStore = defineStore('dictType', () => {
+  const count = ref(0);
+  const doubleCount = computed(() => count.value * 2);
+  function increment() {
+    count.value++;
+  }
+  const query = async ({ skip = 0, limit = undefined, ...info }: IQueryParams = {}): Promise<IQueryResult> => {
+    let cond: IQueryType = {};
+    if (skip) cond.skip = skip;
+    if (limit) cond.limit = limit;
+    cond = { ...cond, ...info };
+    const res = await axios.$get(`${api.url}`, cond);
+    return res;
+  };
+  const fetch = async (payload: any): Promise<IQueryResult> => {
+    const res = await axios.$get(`${api.url}/${payload}`);
+    return res;
+  };
+  const create = async (payload: any): Promise<IQueryResult> => {
+    const res = await axios.$post(`${api.url}`, payload);
+    return res;
+  };
+  const update = async (payload: any): Promise<IQueryResult> => {
+    const id = _.get(payload, 'id', _.get(payload, '_id'));
+    const res = await axios.$post(`${api.url}/${id}`, payload);
+    return res;
+  };
+  const del = async (payload: any): Promise<IQueryResult> => {
+    const res = await axios.$delete(`${api.url}/${payload}`);
+    return res;
+  };
+  return {
+    count,
+    doubleCount,
+    increment,
+    query,
+    fetch,
+    create,
+    update,
+    del
+  };
+});

+ 174 - 19
src/views/system/dict/index.vue

@@ -2,40 +2,195 @@
   <div id="index">
     <el-row>
       <el-col :span="24" class="main animate__animated animate__backInRight" v-loading="loading">
-        <el-col :span="24" class="one"> 系统首页 </el-col>
+        <el-col :span="24" class="one">
+          <cSearch :is_title="false" :is_search="true" :fields="fields" @search="toSearch"> </cSearch>
+        </el-col>
+        <el-col :span="24" class="two">
+          <cButton @toAdd="toAdd()"> </cButton>
+        </el-col>
+        <el-col :span="24" class="thr">
+          <cTable :fields="fields" :opera="opera" :list="list" @query="search" :total="total" @edit="toEdit" @del="toDel">
+            <template #type="{ item, row }">
+              <template v-if="item.model === 'type'">
+                <el-link size="small" type="primary" @click="toType(row)">{{ getProps(row, item.model) }}</el-link>
+              </template>
+            </template>
+            <template #is_use="{ row }">
+              <el-switch
+                v-model="row.is_use"
+                inline-prompt
+                active-text="是"
+                inactive-text="否"
+                active-value="0"
+                inactive-value="1"
+                @click="handleChange(row)"
+              ></el-switch>
+            </template>
+          </cTable>
+        </el-col>
       </el-col>
     </el-row>
+    <cDialog :dialog="dialog" @toClose="toClose">
+      <template v-slot:info>
+        <el-col :span="24" class="dialog_one" v-if="dialog.type == '1'">
+          <cForm :span="24" :fields="formFields" :form="form" :rules="rules" @save="onSubmit">
+            <template #is_use>
+              <el-radio v-for="(i, index) in is_useList" :key="index" :label="i.value">{{ i.label }}</el-radio>
+            </template>
+          </cForm>
+        </el-col>
+      </template>
+    </cDialog>
   </div>
 </template>
-
-<script setup lang="ts">
+<script lang="ts" setup>
 // 基础
+import _ from 'lodash';
+import type { FormRules } from 'element-plus';
 import type { Ref } from 'vue';
-import { onMounted, ref } from 'vue';
+import { ref, onMounted, getCurrentInstance, reactive } from 'vue';
+import { ElMessage, ElMessageBox } from 'element-plus';
+import { useRouter } from 'vue-router';
 
 // 接口
-// import { ToolsStore } from '@/stores/tool';
-// import type { IQueryResult } from '@/util/types.util';
-// const toolsAxios = ToolsStore();
-
+import { DictDataStore } from '@/stores/basic/dictData'; //
+import { DictTypeStore } from '@/stores/basic/dictType'; //
+import type { IQueryResult } from '@/util/types.util';
+const dictType = DictTypeStore();
+const dictData = DictDataStore();
+// 路由
+const router = useRouter();
+const { proxy } = getCurrentInstance() as any;
 // 加载中
-const loading: Ref<any> = ref(false);
-
-// 请求
+const loading = ref(false);
+// 列表数据
+let list: Ref<any> = ref([]);
+let total: Ref<number> = ref(0);
+let skip = 0;
+let limit: number = proxy.$limit;
+let fields: Ref<any[]> = ref([
+  { label: '字典名称', model: 'title', isSearch: true },
+  { label: '字典类型', model: 'type', custom: true },
+  { label: '是否启用', model: 'is_use', custom: true },
+  { label: '备注', model: 'remark' }
+]);
+// 操作
+let opera: Ref<any[]> = ref([
+  { label: '修改', method: 'edit' },
+  { label: '删除', method: 'del', confirm: true, type: 'danger' }
+]);
+// 弹框
+const dialog: Ref<{ type: string; show: boolean; title: string }> = ref({ type: '1', show: false, title: '信息管理' });
+let form: Ref<{}> = ref({});
+// 表单
+let formFields: Ref<any[]> = ref([
+  { label: '字典名称', model: 'title' },
+  { label: '字典类型', model: 'type' },
+  { label: '是否启用', model: 'is_use', type: 'radio' },
+  { label: '备注', model: 'remark', type: 'textarea' }
+]);
+const rules = reactive<FormRules>({
+  name: [{ required: true, message: '字典名称', trigger: 'blur' }],
+  type: [{ required: true, message: '字典类型', trigger: 'blur' }]
+});
+// 查询数据
+let searchForm: Ref<any> = ref({});
+// 字典表
+let is_useList: Ref<any> = ref([]);
 onMounted(async () => {
   loading.value = true;
-  search();
+  await searchOther();
+  await search({ skip, limit });
   loading.value = false;
 });
-const search = async () => {
-  // let res: IQueryResult = await toolsAxios.dataCount();
-  // if (res.errcode == '0') {
-  //   info.value = res.data;
-  // }
+// 查询
+const search = async (e: { skip: number; limit: number }) => {
+  const { skip, limit } = e;
+  const condition = _.cloneDeep(searchForm.value);
+  let info = { limit: limit, skip: skip, ...condition };
+  let res: IQueryResult = await dictType.query(info);
+  if (res.errcode == 0) {
+    list.value = res.data;
+    total.value = res.total;
+  }
+};
+const toSearch = (query: any) => {
+  searchForm.value = query;
+  search({ skip, limit });
+};
+const getProps = (data: any, prop: any) => {
+  return _.get(data, prop);
+};
+// 修改是否启用
+const handleChange = (row: any) => {
+  const text = row.is_use === '0' ? '启用' : '停用';
+  ElMessageBox.confirm('确认要"' + text + '""' + row.type + '"吗?', {
+    confirmButtonText: '确定',
+    cancelButtonText: '取消',
+    type: 'warning'
+  })
+    .then(async () => {
+      let res: IQueryResult;
+      if (row._id) res = await dictType.update(row);
+      if (res.errcode == 0) {
+        ElMessage({ type: `success`, message: `修改成功` });
+      }
+    })
+    .catch(() => {
+      row.is_use = row.is_use === '0' ? '1' : '0';
+    });
+};
+// 新增
+const toAdd = () => {
+  dialog.value = { title: '信息管理', show: true, type: '1' };
+};
+// 修改
+const toEdit = async (data: any) => {
+  let res: IQueryResult = await dictType.fetch(data._id);
+  if (res.errcode == 0) {
+    form.value = res.data as {};
+    dialog.value = { title: '信息管理', show: true, type: '1' };
+  }
+};
+// 提交
+const onSubmit = async (data: any) => {
+  let res: IQueryResult;
+  if (data._id) res = await dictType.update(data);
+  else res = await dictType.create(data);
+  if (res.errcode == 0) {
+    ElMessage({ type: `success`, message: `维护信息成功` });
+    toClose();
+  }
+};
+// 删除
+const toDel = async (data: any) => {
+  let res: IQueryResult = await dictType.del(data._id);
+  if (res.errcode == 0) {
+    ElMessage({ type: `success`, message: `刪除信息成功` });
+    search({ skip, limit });
+  }
+};
+// 弹框关闭
+const toClose = () => {
+  form.value = {};
+  searchForm.value = {};
+  dialog.value = { title: '信息管理', show: false, type: '1' };
+  search({ skip, limit });
+};
+// 字典类型跳转
+const toType = (data: any) => {
+  router.push({ path: '/system/dictData', query: { id: data._id, type: data.type } });
+};
+// 查询其他信息
+const searchOther = async () => {
+  let res: IQueryResult = await dictData.query({ type: 'is_use' });
+  if (res.errcode == 0) is_useList.value = res.data;
 };
 </script>
-<style scoped lang="scss">
+<style lang="scss" scoped>
 .main {
-  padding: 2px;
+  .two {
+    margin: 0 0 10px 0;
+  }
 }
 </style>

+ 194 - 0
src/views/system/dictData/index.vue

@@ -0,0 +1,194 @@
+<template>
+  <div id="index">
+    <el-row>
+      <el-col :span="24" class="main animate__animated animate__backInRight" v-loading="loading">
+        <el-col :span="24" class="one">
+          <cSearch :is_title="false" :is_search="true" :fields="fields" @search="toSearch"> </cSearch>
+        </el-col>
+        <el-col :span="24" class="two">
+          <cButton @toAdd="toAdd()">
+            <template v-slot:custom>
+              <el-button type="primary" @click="toBack">返回</el-button>
+            </template>
+          </cButton>
+        </el-col>
+        <el-col :span="24" class="thr">
+          <cTable :fields="fields" :opera="opera" :list="list" @query="search" :total="total" @edit="toEdit" @del="toDel">
+            <template #is_use="{ row }">
+              <el-switch
+                v-model="row.is_use"
+                inline-prompt
+                active-text="是"
+                inactive-text="否"
+                active-value="0"
+                inactive-value="1"
+                @click="handleChange(row)"
+              ></el-switch>
+            </template>
+          </cTable>
+        </el-col>
+      </el-col>
+    </el-row>
+    <cDialog :dialog="dialog" @toClose="toClose">
+      <template v-slot:info>
+        <el-col :span="24" class="dialog_one" v-if="dialog.type == '1'">
+          <cForm :span="24" :fields="formFields" :form="form" :rules="rules" @save="toSave">
+            <template #is_use>
+              <el-radio v-for="i in is_useList" :key="i._id" :label="i.value">{{ i.label }}</el-radio>
+            </template>
+          </cForm>
+        </el-col>
+      </template>
+    </cDialog>
+  </div>
+</template>
+
+<script setup lang="ts">
+// 基础
+import _ from 'lodash';
+import type { FormRules } from 'element-plus';
+import type { Ref } from 'vue';
+import { ref, onMounted, getCurrentInstance, reactive } from 'vue';
+import { ElMessage, ElMessageBox } from 'element-plus';
+import { useRoute, useRouter } from 'vue-router';
+// 接口
+import { DictDataStore } from '@/stores/basic/dictData';
+import type { IQueryResult } from '@/util/types.util';
+const dictData = DictDataStore();
+// 路由
+const route = useRoute();
+const router = useRouter();
+const { proxy } = getCurrentInstance() as any;
+// 加载中
+const loading: Ref<any> = ref(false);
+// 列表数据
+let list: Ref<any> = ref([]);
+let total: Ref<number> = ref(0);
+let skip = 0;
+let limit: number = proxy.$limit;
+let fields: Ref<any[]> = ref([
+  { label: '类型', model: 'type' },
+  { label: '标签', model: 'label', isSearch: true },
+  { label: '键值', model: 'value', isSearch: true },
+  { label: '排序', model: 'sort', type: 'number' },
+  { label: '是否启用', model: 'is_use', custom: true }
+]);
+// 操作
+let opera: Ref<any[]> = ref([
+  { label: '修改', method: 'edit' },
+  { label: '删除', method: 'del', confirm: true, type: 'danger' }
+]);
+// 弹框
+const dialog: Ref<{ type: string; show: boolean; title: string }> = ref({ type: '1', show: false, title: '信息管理' });
+let form: Ref<{}> = ref({});
+// 表单
+let formFields: Ref<any[]> = ref([
+  { label: '类型', model: 'type', options: { disabled: true } },
+  { label: '标签', model: 'label' },
+  { label: '键值', model: 'value' },
+  { label: '排序', model: 'sort', type: 'number' },
+  { label: '是否启用', model: 'is_use', type: 'radio' }
+]);
+const rules = reactive<FormRules>({
+  label: [{ required: true, message: '标签', trigger: 'blur' }],
+  value: [{ required: true, message: '键值', trigger: 'blur' }]
+});
+// 查询数据
+let searchForm: Ref<any> = ref({});
+// 字典表
+let is_useList: Ref<any> = ref([]);
+// 请求
+onMounted(async () => {
+  loading.value = true;
+  await searchOther();
+  await search({ skip, limit });
+  loading.value = false;
+});
+const search = async (e: { skip: number; limit: number }) => {
+  const { skip, limit } = e;
+  const condition = _.cloneDeep(searchForm.value);
+  let type = route.query.type;
+  let info = { limit: limit, skip: skip, ...condition, type: type };
+  let res: IQueryResult = await dictData.query(info);
+  if (res.errcode == 0) {
+    list.value = res.data;
+    total.value = res.total;
+  }
+};
+const toSearch = (query: any) => {
+  searchForm.value = query;
+  search({ skip, limit });
+};
+// 新增
+const toAdd = () => {
+  let type = route.query.type;
+  form.value = { type: type };
+  dialog.value = { title: '信息管理', show: true, type: '1' };
+};
+// 修改
+const toEdit = async (data: any) => {
+  form.value = data;
+  dialog.value = { title: '信息管理', show: true, type: '1' };
+};
+// 修改是否启用
+const handleChange = (row: any) => {
+  const text = row.is_use === '0' ? '启用' : '停用';
+  ElMessageBox.confirm('确认要"' + text + '""' + row.type + '"吗?', {
+    confirmButtonText: '确定',
+    cancelButtonText: '取消',
+    type: 'warning'
+  })
+    .then(async () => {
+      let res: IQueryResult;
+      if (row._id) res = await dictData.update(row);
+      if (res.errcode == 0) {
+        ElMessage({ type: `success`, message: `修改成功` });
+      }
+    })
+    .catch(() => {
+      row.is_use = row.is_use === '0' ? '1' : '0';
+    });
+};
+// 提交保存
+const toSave = async (data: any) => {
+  let res: IQueryResult;
+  if (data._id) res = await dictData.update(data);
+  else res = await dictData.create(data);
+  if (res.errcode == 0) {
+    ElMessage({ type: `success`, message: `维护信息成功` });
+    toClose();
+  }
+};
+// 删除
+const toDel = async (data: any) => {
+  let res: IQueryResult = await dictData.del(data._id);
+  if (res.errcode == 0) {
+    ElMessage({ type: `success`, message: `刪除信息成功` });
+    search({ skip, limit });
+  }
+};
+// 弹框关闭
+const toClose = () => {
+  form.value = {};
+  dialog.value = { title: '信息管理', show: false, type: '1' };
+  search({ skip, limit });
+};
+// 查询其他信息
+const searchOther = async () => {
+  let res: IQueryResult;
+  // 是否启用
+  res = await dictData.query({ type: 'is_use' });
+  if (res.errcode == '0') is_useList.value = res.data;
+};
+// 返回上一页
+const toBack = () => {
+  router.push({ path: '/system/dict' });
+};
+</script>
+<style scoped lang="scss">
+.main {
+  .two {
+    margin: 0 0 10px 0;
+  }
+}
+</style>