index.vue 4.5 KB

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