index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <template>
  2. <div class="flex">
  3. <!-- ====== 字典分类 ====== -->
  4. <el-card class="w-1/2 dict" :gutter="12" shadow="always">
  5. <template #header>
  6. <div class="card-header">
  7. <span>字典分类</span>
  8. </div>
  9. </template>
  10. <vxe-grid
  11. ref="xTypeGrid"
  12. v-bind="typeGridOptions"
  13. @cell-click="cellClickEvent"
  14. class="xtable-scrollbar"
  15. >
  16. <!-- 操作:新增类型 -->
  17. <template #toolbar_buttons>
  18. <XButton
  19. type="primary"
  20. preIcon="ep:zoom-in"
  21. :title="t('action.add')"
  22. v-hasPermi="['system:dict:create']"
  23. @click="handleTypeCreate()"
  24. />
  25. </template>
  26. <template #actionbtns_default="{ row }">
  27. <!-- 操作:编辑类型 -->
  28. <XTextButton
  29. preIcon="ep:edit"
  30. :title="t('action.edit')"
  31. v-hasPermi="['system:dict:update']"
  32. @click="handleTypeUpdate(row.id)"
  33. />
  34. <!-- 操作:删除类型 -->
  35. <XTextButton
  36. preIcon="ep:delete"
  37. :title="t('action.del')"
  38. v-hasPermi="['system:dict:delete']"
  39. @click="handleTypeDelete(row.id)"
  40. />
  41. </template>
  42. </vxe-grid>
  43. <!-- @星语:分页和列表重叠在一起了 -->
  44. </el-card>
  45. <!-- ====== 字典数据 ====== -->
  46. <el-card class="w-1/2 dict" style="margin-left: 10px" :gutter="12" shadow="hover">
  47. <template #header>
  48. <div class="card-header">
  49. <span>字典数据</span>
  50. </div>
  51. </template>
  52. <!-- 列表 -->
  53. <div v-if="!tableTypeSelect">
  54. <span>请从左侧选择</span>
  55. </div>
  56. <div v-if="tableTypeSelect">
  57. <!-- 列表 -->
  58. <vxe-grid ref="xDataGrid" v-bind="dataGridOptions" class="xtable-scrollbar">
  59. <!-- 操作:新增数据 -->
  60. <template #toolbar_buttons>
  61. <XButton
  62. type="primary"
  63. preIcon="ep:zoom-in"
  64. :title="t('action.add')"
  65. v-hasPermi="['system:dict:create']"
  66. @click="handleDataCreate()"
  67. />
  68. </template>
  69. <template #actionbtns_default="{ row }">
  70. <!-- 操作:修改数据 -->
  71. <XTextButton
  72. v-hasPermi="['system:dict:update']"
  73. preIcon="ep:edit"
  74. :title="t('action.edit')"
  75. @click="handleDataUpdate(row.id)"
  76. />
  77. <!-- 操作:删除数据 -->
  78. <XTextButton
  79. v-hasPermi="['system:dict:delete']"
  80. preIcon="ep:delete"
  81. :title="t('action.del')"
  82. @click="handleDataDelete(row.id)"
  83. />
  84. </template>
  85. </vxe-grid>
  86. </div>
  87. </el-card>
  88. <XModal id="dictModel" v-model="dialogVisible" :title="dialogTitle">
  89. <Form
  90. v-if="['typeCreate', 'typeUpdate'].includes(actionType)"
  91. :schema="DictTypeSchemas.allSchemas.formSchema"
  92. :rules="DictTypeSchemas.dictTypeRules"
  93. ref="typeFormRef"
  94. >
  95. <template #type>
  96. <template v-if="actionType == 'typeUpdate'">
  97. <el-tag>{{ dictTypeValue }}</el-tag>
  98. </template>
  99. <template v-else><el-input v-model="dictTypeValue" /> </template>
  100. </template>
  101. </Form>
  102. <Form
  103. v-if="['dataCreate', 'dataUpdate'].includes(actionType)"
  104. :schema="DictDataSchemas.allSchemas.formSchema"
  105. :rules="DictDataSchemas.dictDataRules"
  106. ref="dataFormRef"
  107. />
  108. <!-- 操作按钮 -->
  109. <template #footer>
  110. <XButton
  111. v-if="['typeCreate', 'typeUpdate'].includes(actionType)"
  112. type="primary"
  113. :title="t('action.save')"
  114. :loading="actionLoading"
  115. @click="submitTypeForm"
  116. />
  117. <XButton
  118. v-if="['dataCreate', 'dataUpdate'].includes(actionType)"
  119. type="primary"
  120. :title="t('action.save')"
  121. :loading="actionLoading"
  122. @click="submitDataForm"
  123. />
  124. <XButton :title="t('dialog.close')" @click="dialogVisible = false" />
  125. </template>
  126. </XModal>
  127. </div>
  128. </template>
  129. <script setup lang="ts" name="Dict">
  130. import { ref, unref, reactive } from 'vue'
  131. import { useI18n } from '@/hooks/web/useI18n'
  132. import { useMessage } from '@/hooks/web/useMessage'
  133. import { useVxeGrid } from '@/hooks/web/useVxeGrid'
  134. import { VxeGridInstance, VxeTableEvents } from 'vxe-table'
  135. import { FormExpose } from '@/components/Form'
  136. import { ElInput, ElTag, ElCard } from 'element-plus'
  137. import * as DictTypeSchemas from './dict.type'
  138. import * as DictDataSchemas from './dict.data'
  139. import * as DictTypeApi from '@/api/system/dict/dict.type'
  140. import * as DictDataApi from '@/api/system/dict/dict.data'
  141. import { DictDataVO, DictTypeVO } from '@/api/system/dict/types'
  142. const { t } = useI18n() // 国际化
  143. const message = useMessage() // 消息弹窗
  144. const xTypeGrid = ref<VxeGridInstance>() // 列表 Grid Ref
  145. const {
  146. gridOptions: typeGridOptions,
  147. getList: typeGetList,
  148. deleteData: typeDeleteData
  149. } = useVxeGrid<DictTypeVO>({
  150. allSchemas: DictTypeSchemas.allSchemas,
  151. getListApi: DictTypeApi.getDictTypePageApi,
  152. deleteApi: DictTypeApi.deleteDictTypeApi
  153. })
  154. const xDataGrid = ref<VxeGridInstance>() // 列表 Grid Ref
  155. const queryParams = reactive({
  156. dictType: null
  157. })
  158. const {
  159. gridOptions: dataGridOptions,
  160. getList: dataGetList,
  161. deleteData: dataDeleteData
  162. } = useVxeGrid<DictDataVO>({
  163. allSchemas: DictDataSchemas.allSchemas,
  164. queryParams: queryParams,
  165. getListApi: DictDataApi.getDictDataPageApi,
  166. deleteApi: DictDataApi.deleteDictDataApi
  167. })
  168. // ========== 字典分类列表相关 ==========
  169. const dictTypeValue = ref('')
  170. // 字典分类修改操作
  171. const handleTypeCreate = () => {
  172. dictTypeValue.value = ''
  173. setDialogTile('typeCreate')
  174. }
  175. const handleTypeUpdate = async (rowId: number) => {
  176. setDialogTile('typeUpdate')
  177. // 设置数据
  178. const res = await DictTypeApi.getDictTypeApi(rowId)
  179. dictTypeValue.value = res.type
  180. unref(typeFormRef)?.setValues(res)
  181. }
  182. // 字典数据修改操作
  183. const handleDataCreate = () => {
  184. setDialogTile('dataCreate')
  185. }
  186. const handleDataUpdate = async (rowId: number) => {
  187. setDialogTile('dataUpdate')
  188. // 设置数据
  189. const res = await DictDataApi.getDictDataApi(rowId)
  190. unref(dataFormRef)?.setValues(res)
  191. }
  192. // 字典分类点击行事件
  193. const parentType = ref('')
  194. const tableTypeSelect = ref(false)
  195. const cellClickEvent: VxeTableEvents.CellClick = async ({ row }) => {
  196. tableTypeSelect.value = true
  197. queryParams.dictType = row['type']
  198. await dataGetList(xDataGrid)
  199. parentType.value = row['type']
  200. }
  201. // 弹出框
  202. const dialogVisible = ref(false) // 是否显示弹出层
  203. const dialogTitle = ref('edit') // 弹出层标题
  204. const actionLoading = ref(false) // 遮罩层
  205. const typeFormRef = ref<FormExpose>() // 分类表单 Ref
  206. const dataFormRef = ref<FormExpose>() // 数据表单 Ref
  207. const actionType = ref('') // 操作按钮的类型
  208. // 设置标题
  209. const setDialogTile = (type: string) => {
  210. dialogTitle.value = t('action.' + type)
  211. actionType.value = type
  212. dialogVisible.value = true
  213. }
  214. // 删除操作
  215. const handleTypeDelete = async (rowId: number) => {
  216. await typeDeleteData(xTypeGrid, rowId)
  217. }
  218. const handleDataDelete = async (rowId: number) => {
  219. await dataDeleteData(xDataGrid, rowId)
  220. }
  221. // 提交按钮
  222. const submitTypeForm = async () => {
  223. const elForm = unref(typeFormRef)?.getElFormRef()
  224. if (!elForm) return
  225. elForm.validate(async (valid) => {
  226. if (valid && dictTypeValue.value != '') {
  227. actionLoading.value = true
  228. // 提交请求
  229. try {
  230. const data = unref(typeFormRef)?.formModel as DictTypeVO
  231. if (actionType.value === 'typeCreate') {
  232. data.type = dictTypeValue.value
  233. await DictTypeApi.createDictTypeApi(data)
  234. message.success(t('common.createSuccess'))
  235. } else if (actionType.value === 'typeUpdate') {
  236. await DictTypeApi.updateDictTypeApi(data)
  237. message.success(t('common.updateSuccess'))
  238. }
  239. dialogVisible.value = false
  240. } finally {
  241. actionLoading.value = false
  242. typeGetList(xTypeGrid)
  243. }
  244. }
  245. })
  246. }
  247. const submitDataForm = async () => {
  248. const elForm = unref(dataFormRef)?.getElFormRef()
  249. if (!elForm) return
  250. elForm.validate(async (valid) => {
  251. if (valid) {
  252. actionLoading.value = true
  253. // 提交请求
  254. try {
  255. const data = unref(dataFormRef)?.formModel as DictDataVO
  256. if (actionType.value === 'dataCreate') {
  257. data.dictType = parentType.value
  258. await DictDataApi.createDictDataApi(data)
  259. message.success(t('common.createSuccess'))
  260. } else if (actionType.value === 'dataUpdate') {
  261. await DictDataApi.updateDictDataApi(data)
  262. message.success(t('common.updateSuccess'))
  263. }
  264. dialogVisible.value = false
  265. } finally {
  266. actionLoading.value = false
  267. dataGetList(xDataGrid)
  268. }
  269. }
  270. })
  271. }
  272. </script>