index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <div class="main animate__animated animate__backInRight" v-loading="loading">
  3. <custom-search-bar :fields="fields.filter((f) => f.isSearch)" v-model="searchForm" @search="search" @reset="toReset"></custom-search-bar>
  4. <custom-button-bar :fields="buttonFields" @create="toAdd"></custom-button-bar>
  5. <custom-table :data="data" :fields="fields" @query="search" :total="total" :opera="opera" @dict="toDict" @update="toEdit" @delete="toDelete">
  6. <template #is_use="{ row }">
  7. <el-tag v-if="row.is_use == '0'" type="success">{{ $t('common.is_use_abled') }}</el-tag>
  8. <el-tag v-else type="info">{{ $t('common.is_use_disabled') }}</el-tag>
  9. </template>
  10. </custom-table>
  11. <el-dialog v-model="dialog.show" :title="dialog.title" :destroy-on-close="false" @close="toClose" :top="dialog.top">
  12. <el-row>
  13. <el-col :span="24" v-if="dialog.type == '1'">
  14. <custom-form v-model="form" :fields="formFields" :rules="rules" @save="toSave">
  15. <template #is_use>
  16. <el-radio v-for="i in isUseList" :key="i.id" :label="i.value">{{ i.label }}</el-radio>
  17. </template>
  18. </custom-form>
  19. </el-col>
  20. <el-col :span="24" v-if="dialog.type == '2'">
  21. <dictData></dictData>
  22. </el-col>
  23. </el-row>
  24. </el-dialog>
  25. </div>
  26. </template>
  27. <script setup>
  28. import dictData from '@/views/system/dictData/index.vue'
  29. const $checkRes = inject('$checkRes')
  30. import { cloneDeep, get } from 'lodash-es'
  31. const { t } = useI18n()
  32. // 接口
  33. import { DictTypeStore } from '@/store/api/system/dictType'
  34. import { DictDataStore } from '@/store/api/system/dictData'
  35. const store = DictTypeStore()
  36. const dictDataStore = DictDataStore()
  37. const data = ref([])
  38. const searchForm = ref({})
  39. const fields = [
  40. { label: t('pages.dict.title'), model: 'title', isSearch: true },
  41. { label: t('pages.dict.code'), model: 'code', isSearch: true },
  42. { label: t('pages.dict.is_use'), model: 'is_use', format: (i) => getDict(i), custom: true },
  43. { label: t('pages.dict.remark'), model: 'remark' }
  44. ]
  45. const opera = [
  46. { label: t('common.dict'), method: 'dict' },
  47. { label: t('common.update'), method: 'update' },
  48. { label: t('common.delete'), method: 'delete', confirm: true, type: 'danger' }
  49. ]
  50. const buttonFields = [{ label: t('common.create'), method: 'create' }]
  51. let skip = 0
  52. let limit = inject('limit')
  53. const total = ref(0)
  54. const isUseList = ref([])
  55. // 加载中
  56. const loading = ref(false)
  57. const formFields = [
  58. { label: t('pages.dict.title'), model: 'title' },
  59. { label: t('pages.dict.code'), model: 'code' },
  60. { label: t('pages.dict.is_use'), model: 'is_use', type: 'radio' },
  61. { label: t('pages.dict.remark'), model: 'remark', type: 'textarea' }
  62. ]
  63. const rules = reactive({ title: [{ required: true, message: t('pages.dict.titleMessage'), trigger: 'blur' }], code: [{ required: true, message: t('pages.dict.codeMessage'), trigger: 'blur' }] })
  64. const dialog = ref({ type: '1', show: false, title: t('pages.dict.dialogTitle'), top: '15vh' })
  65. const form = ref({})
  66. // 请求
  67. onMounted(async () => {
  68. loading.value = true
  69. await searchOther()
  70. await search({ skip, limit })
  71. loading.value = false
  72. })
  73. const searchOther = async () => {
  74. const result = await dictDataStore.query({ code: 'isUse', is_use: '0' })
  75. if ($checkRes(result)) isUseList.value = result.data
  76. }
  77. const search = async (query = { skip, limit }) => {
  78. skip = query.skip
  79. limit = query.limit
  80. const info = { skip: query.skip, limit: query.limit, ...searchForm.value }
  81. const res = await store.query(info)
  82. if (res.errcode == '0') {
  83. data.value = res.data
  84. total.value = res.total
  85. }
  86. }
  87. // 字典数据转换
  88. const getDict = (data) => {
  89. const res = isUseList.value.find((f) => f.value == data)
  90. return get(res, 'label')
  91. }
  92. // 添加
  93. const toAdd = () => {
  94. dialog.value = { type: '1', show: true, title: t('pages.dict.addDialogTitle'), top: '15vh' }
  95. }
  96. // 字典数据
  97. const toDict = (data) => {
  98. form.value = data
  99. dialog.value = { type: '2', show: true, title: `【${data.title}】 ` + t('pages.dictData.codeDialogTitle'), top: '5vh' }
  100. }
  101. // 修改
  102. const toEdit = (data) => {
  103. form.value = data
  104. dialog.value = { type: '1', show: true, title: t('pages.dict.upDialogTitle'), top: '15vh' }
  105. }
  106. // 删除
  107. const toDelete = async (data) => {
  108. const res = await store.del(data.id)
  109. if ($checkRes(res, true)) {
  110. search({ skip, limit })
  111. }
  112. }
  113. const toSave = async () => {
  114. const data = cloneDeep(form.value)
  115. let res
  116. if (get(data, 'id')) res = await store.update(data)
  117. else res = await store.create(data)
  118. if ($checkRes(res, true)) {
  119. search({ skip, limit })
  120. toClose()
  121. }
  122. }
  123. // 重置
  124. const toReset = async () => {
  125. searchForm.value = {}
  126. await search({ skip, limit })
  127. }
  128. const toClose = () => {
  129. form.value = {}
  130. dialog.value = { show: false }
  131. }
  132. // provide
  133. provide('isUseList', isUseList)
  134. provide('codeInfo', form)
  135. </script>
  136. <style scoped lang="scss"></style>