From a123c47042d01c9ad3956ea442bf760f9e024999 Mon Sep 17 00:00:00 2001
From: xingyu <xingyu4j@vip.qq.com>
Date: Tue, 15 Nov 2022 20:12:22 +0800
Subject: [PATCH] refactor: role

---
 .../src/api/system/role/index.ts              |  20 ++-
 .../src/api/system/role/types.ts              |   9 --
 .../src/views/system/role/index.vue           | 134 ++++++++----------
 .../src/views/system/role/role.data.ts        | 120 ++++++----------
 4 files changed, 124 insertions(+), 159 deletions(-)
 delete mode 100644 yudao-ui-admin-vue3/src/api/system/role/types.ts

diff --git a/yudao-ui-admin-vue3/src/api/system/role/index.ts b/yudao-ui-admin-vue3/src/api/system/role/index.ts
index efbb75d48..83292c72a 100644
--- a/yudao-ui-admin-vue3/src/api/system/role/index.ts
+++ b/yudao-ui-admin-vue3/src/api/system/role/index.ts
@@ -1,8 +1,24 @@
 import request from '@/config/axios'
-import type { RoleVO } from './types'
+
+export type RoleVO = {
+  id: number
+  name: string
+  code: string
+  sort: number
+  status: number
+  type: number
+  createTime: string
+}
+
+export interface RolePageReqVO extends PageParam {
+  name?: string
+  code?: string
+  status?: number
+  createTime?: string[]
+}
 
 // 查询角色列表
-export const getRolePageApi = async (params) => {
+export const getRolePageApi = async (params: RolePageReqVO) => {
   return await request.get({ url: '/system/role/page', params })
 }
 
diff --git a/yudao-ui-admin-vue3/src/api/system/role/types.ts b/yudao-ui-admin-vue3/src/api/system/role/types.ts
deleted file mode 100644
index f5e17ea99..000000000
--- a/yudao-ui-admin-vue3/src/api/system/role/types.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-export type RoleVO = {
-  id: number
-  name: string
-  code: string
-  sort: number
-  status: number
-  type: number
-  createTime: string
-}
diff --git a/yudao-ui-admin-vue3/src/views/system/role/index.vue b/yudao-ui-admin-vue3/src/views/system/role/index.vue
index a082d4973..666cd8908 100644
--- a/yudao-ui-admin-vue3/src/views/system/role/index.vue
+++ b/yudao-ui-admin-vue3/src/views/system/role/index.vue
@@ -1,42 +1,18 @@
 <template>
-  <!-- 搜索工作区 -->
   <ContentWrap>
-    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
-  </ContentWrap>
-  <ContentWrap>
-    <!-- 操作工具栏 -->
-    <div class="mb-10px">
-      <XButton
-        type="primary"
-        preIcon="ep:zoom-in"
-        :title="t('action.add')"
-        v-hasPermi="['system:role:create']"
-        @click="handleCreate()"
-      />
-    </div>
     <!-- 列表 -->
-    <Table
-      :columns="allSchemas.tableColumns"
-      :selection="false"
-      :data="tableObject.tableList"
-      :loading="tableObject.loading"
-      :pagination="{
-        total: tableObject.total
-      }"
-      v-model:pageSize="tableObject.pageSize"
-      v-model:currentPage="tableObject.currentPage"
-      @register="register"
-    >
-      <template #type="{ row }">
-        <DictTag :type="DICT_TYPE.SYSTEM_ROLE_TYPE" :value="row.type" />
+    <vxe-grid ref="xGrid" v-bind="gridOptions" class="xtable-scrollbar">
+      <!-- 操作:新增 -->
+      <template #toolbar_buttons>
+        <XButton
+          type="primary"
+          preIcon="ep:zoom-in"
+          :title="t('action.add')"
+          v-hasPermi="['system:role:create']"
+          @click="handleCreate()"
+        />
       </template>
-      <template #status="{ row }">
-        <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
-      </template>
-      <template #createTime="{ row }">
-        <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
-      </template>
-      <template #action="{ row }">
+      <template #actionbtns_default="{ row }">
         <XTextButton
           preIcon="ep:edit"
           :title="t('action.edit')"
@@ -47,7 +23,7 @@
           preIcon="ep:view"
           :title="t('action.detail')"
           v-hasPermi="['system:role:update']"
-          @click="handleDetail(row)"
+          @click="handleDetail(row.id)"
         />
         <XTextButton
           preIcon="ep:basketball"
@@ -65,10 +41,10 @@
           preIcon="ep:delete"
           :title="t('action.del')"
           v-hasPermi="['system:role:delete']"
-          @click="delList(row.id, false)"
+          @click="handleDelete(row.id)"
         />
       </template>
-    </Table>
+    </vxe-grid>
   </ContentWrap>
 
   <XModal v-model="dialogVisible" :title="dialogTitle">
@@ -91,10 +67,10 @@
         v-if="['create', 'update'].includes(actionType)"
         type="primary"
         :title="t('action.save')"
-        :loading="loading"
+        :loading="actionLoading"
         @click="submitForm()"
       />
-      <XButton :loading="loading" :title="t('dialog.close')" @click="dialogVisible = false" />
+      <XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
     </template>
   </XModal>
   <XModal v-model="dialogScopeVisible" :title="dialogScopeTitle">
@@ -151,56 +127,66 @@
     </el-form>
     <!-- 操作按钮 -->
     <template #footer>
-      <XButton type="primary" :title="t('action.save')" :loading="loading" @click="submitScope()" />
-      <XButton :loading="loading" :title="t('dialog.close')" @click="dialogScopeVisible = false" />
+      <XButton
+        type="primary"
+        :title="t('action.save')"
+        :loading="actionLoading"
+        @click="submitScope()"
+      />
+      <XButton
+        :loading="actionLoading"
+        :title="t('dialog.close')"
+        @click="dialogScopeVisible = false"
+      />
     </template>
   </XModal>
 </template>
 <script setup lang="ts">
 import { onMounted, reactive, ref, unref } from 'vue'
-import dayjs from 'dayjs'
 import {
   ElForm,
   ElFormItem,
   ElInput,
   ElSelect,
   ElOption,
-  ElMessage,
   ElTree,
   ElCard,
   ElSwitch
 } from 'element-plus'
 import { DICT_TYPE, getDictOptions } from '@/utils/dict'
-import { useTable } from '@/hooks/web/useTable'
 import { useI18n } from '@/hooks/web/useI18n'
+import { useMessage } from '@/hooks/web/useMessage'
 import { FormExpose } from '@/components/Form'
 import { rules, allSchemas } from './role.data'
-import type { RoleVO } from '@/api/system/role/types'
+import { handleTree } from '@/utils/tree'
+import { SystemDataScopeEnum } from '@/utils/constants'
+import { useVxeGrid } from '@/hooks/web/useVxeGrid'
+import { VxeGridInstance } from 'vxe-table'
+import * as RoleApi from '@/api/system/role'
+import { listSimpleMenusApi } from '@/api/system/menu'
+import { listSimpleDeptApi } from '@/api/system/dept'
+import * as PermissionApi from '@/api/system/permission'
 import type {
   PermissionAssignRoleDataScopeReqVO,
   PermissionAssignRoleMenuReqVO
 } from '@/api/system/permission/types'
-import * as RoleApi from '@/api/system/role'
-import * as PermissionApi from '@/api/system/permission'
-import { listSimpleMenusApi } from '@/api/system/menu'
-import { listSimpleDeptApi } from '@/api/system/dept'
-import { handleTree } from '@/utils/tree'
-import { SystemDataScopeEnum } from '@/utils/constants'
 const { t } = useI18n() // 国际化
-
-// ========== 列表相关 ==========
-const { register, tableObject, methods } = useTable<RoleVO>({
+const message = useMessage() // 消息弹窗
+// 列表相关的变量
+const xGrid = ref<VxeGridInstance>() // 列表 Grid Ref
+const { gridOptions, reloadList, delList } = useVxeGrid<RoleApi.RoleVO>({
+  allSchemas: allSchemas,
   getListApi: RoleApi.getRolePageApi,
   delListApi: RoleApi.deleteRoleApi
 })
-const { getList, setSearchParams, delList } = methods
 
 // ========== CRUD 相关 ==========
-const loading = ref(false) // 遮罩层
+const actionLoading = ref(false) // 遮罩层
 const actionType = ref('') // 操作按钮的类型
 const dialogVisible = ref(false) // 是否显示弹出层
 const dialogTitle = ref('edit') // 弹出层标题
 const formRef = ref<FormExpose>() // 表单 Ref
+const detailRef = ref() // 详情 Ref
 
 // 设置标题
 const setDialogTile = (type: string) => {
@@ -222,41 +208,44 @@ const handleUpdate = async (rowId: number) => {
   unref(formRef)?.setValues(res)
 }
 
+// 删除操作
+const handleDelete = async (rowId: number) => {
+  await delList(xGrid, rowId)
+}
+
 // 提交按钮
 const submitForm = async () => {
   const elForm = unref(formRef)?.getElFormRef()
   if (!elForm) return
   elForm.validate(async (valid) => {
     if (valid) {
-      loading.value = true
+      actionLoading.value = true
       // 提交请求
       try {
-        const data = unref(formRef)?.formModel as RoleVO
+        const data = unref(formRef)?.formModel as RoleApi.RoleVO
         if (actionType.value === 'create') {
           await RoleApi.createRoleApi(data)
-          ElMessage.success(t('common.createSuccess'))
+          message.success(t('common.createSuccess'))
         } else {
           await RoleApi.updateRoleApi(data)
-          ElMessage.success(t('common.updateSuccess'))
+          message.success(t('common.updateSuccess'))
         }
-        // 操作成功,重新加载列表
         dialogVisible.value = false
-        await getList()
       } finally {
-        loading.value = false
+        actionLoading.value = false
+        // 刷新列表
+        reloadList(xGrid)
       }
     }
   })
 }
 
-// ========== 详情相关 ==========
-const detailRef = ref() // 详情 Ref
-
 // 详情操作
-const handleDetail = async (row: RoleVO) => {
-  // 设置数据
-  detailRef.value = row
+const handleDetail = async (rowId: number) => {
   setDialogTile('detail')
+  // 设置数据
+  const res = await RoleApi.getRoleApi(rowId)
+  detailRef.value = res
 }
 
 // ========== 数据权限 ==========
@@ -287,7 +276,7 @@ const handleCheckedTreeNodeAll = () => {
   treeRef.value!.setCheckedNodes(treeNodeAll.value ? treeOptions.value : [])
 }
 // 权限操作
-const handleScope = async (type: string, row: RoleVO) => {
+const handleScope = async (type: string, row: RoleApi.RoleVO) => {
   dataScopeForm.id = row.id
   dataScopeForm.name = row.name
   dataScopeForm.code = row.code
@@ -329,7 +318,7 @@ const submitScope = async () => {
     })
     await PermissionApi.assignRoleMenuApi(data.value)
   }
-  ElMessage.success(t('common.updateSuccess'))
+  message.success(t('common.updateSuccess'))
   dialogScopeVisible.value = false
 }
 const init = () => {
@@ -337,7 +326,6 @@ const init = () => {
 }
 // ========== 初始化 ==========
 onMounted(() => {
-  getList()
   init()
 })
 </script>
diff --git a/yudao-ui-admin-vue3/src/views/system/role/role.data.ts b/yudao-ui-admin-vue3/src/views/system/role/role.data.ts
index 5bfc4f04f..5043cc22a 100644
--- a/yudao-ui-admin-vue3/src/views/system/role/role.data.ts
+++ b/yudao-ui-admin-vue3/src/views/system/role/role.data.ts
@@ -2,7 +2,7 @@ import { reactive } from 'vue'
 import { useI18n } from '@/hooks/web/useI18n'
 import { required } from '@/utils/formRules'
 import { DICT_TYPE } from '@/utils/dict'
-import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
+import { VxeCrudSchema, useVxeCrudSchemas } from '@/hooks/web/useVxeCrudSchemas'
 // 国际化
 const { t } = useI18n()
 // 表单校验
@@ -12,82 +12,52 @@ export const rules = reactive({
   sort: [required]
 })
 // CrudSchema
-const crudSchemas = reactive<CrudSchema[]>([
-  {
-    label: t('common.index'),
-    field: 'id',
-    type: 'index',
-    form: {
-      show: false
+const crudSchemas = reactive<VxeCrudSchema>({
+  primaryKey: 'id',
+  primaryType: 'seq',
+  action: true,
+  actionWidth: '400px',
+  columns: [
+    {
+      title: '角色名称',
+      field: 'name',
+      isSearch: true
     },
-    detail: {
-      show: false
-    }
-  },
-  {
-    label: '角色名称',
-    field: 'name',
-    search: {
-      show: true
-    }
-  },
-  {
-    label: '角色类型',
-    field: 'type',
-    dictType: DICT_TYPE.SYSTEM_ROLE_TYPE
-  },
-  {
-    label: '角色标识',
-    field: 'code',
-    search: {
-      show: true
-    }
-  },
-  {
-    label: '显示顺序',
-    field: 'sort',
-    form: {
-      component: 'InputNumber',
-      value: 0
-    }
-  },
-  {
-    label: t('common.status'),
-    field: 'status',
-    dictType: DICT_TYPE.COMMON_STATUS,
-    search: {
-      show: true
-    }
-  },
-  {
-    label: t('common.createTime'),
-    field: 'createTime',
-    form: {
-      show: false
+    {
+      title: '角色类型',
+      field: 'type',
+      dictType: DICT_TYPE.SYSTEM_ROLE_TYPE
     },
-    detail: {
-      show: false
+    {
+      title: '角色标识',
+      field: 'code',
+      isSearch: true
     },
-    search: {
-      show: true,
-      component: 'DatePicker',
-      componentProps: {
-        type: 'daterange',
-        valueFormat: 'YYYY-MM-DD HH:mm:ss',
-        defaultTime: [new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)]
+    {
+      title: '显示顺序',
+      field: 'sort',
+      form: {
+        component: 'InputNumber'
+      }
+    },
+    {
+      title: t('common.status'),
+      field: 'status',
+      dictType: DICT_TYPE.COMMON_STATUS,
+      isSearch: true
+    },
+    {
+      title: t('common.createTime'),
+      field: 'createTime',
+      formatter: 'formatDate',
+      isForm: false,
+      search: {
+        show: true,
+        itemRender: {
+          name: 'XDataTimePicker'
+        }
       }
     }
-  },
-  {
-    field: 'action',
-    width: '450px',
-    label: t('table.action'),
-    form: {
-      show: false
-    },
-    detail: {
-      show: false
-    }
-  }
-])
-export const { allSchemas } = useCrudSchemas(crudSchemas)
+  ]
+})
+export const { allSchemas } = useVxeCrudSchemas(crudSchemas)