Department.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <script setup lang="tsx">
  2. import { ContentWrap } from '@/components/ContentWrap'
  3. import { Search } from '@/components/Search'
  4. import { Dialog } from '@/components/Dialog'
  5. import { useI18n } from '@/hooks/web/useI18n'
  6. import { ElButton, ElTag } from 'element-plus'
  7. import { Table } from '@/components/Table'
  8. import {
  9. getDepartmentApi,
  10. getDepartmentTableApi,
  11. saveDepartmentApi,
  12. deleteDepartmentApi
  13. } from '@/api/department'
  14. import { useTable } from '@/hooks/web/useTable'
  15. import { TableData } from '@/api/table/types'
  16. import { ref, unref, reactive } from 'vue'
  17. import Write from './components/Write.vue'
  18. import Detail from './components/Detail.vue'
  19. import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
  20. const ids = ref<string[]>([])
  21. const { tableRegister, tableState, tableMethods } = useTable({
  22. fetchDataApi: async () => {
  23. const { currentPage, pageSize } = tableState
  24. const res = await getDepartmentTableApi({
  25. pageIndex: unref(currentPage),
  26. pageSize: unref(pageSize),
  27. ...unref(searchParams)
  28. })
  29. return {
  30. list: res.data.list,
  31. total: res.data.total
  32. }
  33. },
  34. fetchDelApi: async () => {
  35. const res = await deleteDepartmentApi(unref(ids))
  36. return !!res
  37. }
  38. })
  39. const { loading, dataList, total, currentPage, pageSize } = tableState
  40. const { getList, getElTableExpose, delList } = tableMethods
  41. const searchParams = ref({})
  42. const setSearchParams = (params: any) => {
  43. searchParams.value = params
  44. getList()
  45. }
  46. const { t } = useI18n()
  47. const crudSchemas = reactive<CrudSchema[]>([
  48. {
  49. field: 'selection',
  50. search: {
  51. hidden: true
  52. },
  53. form: {
  54. hidden: true
  55. },
  56. detail: {
  57. hidden: true
  58. },
  59. table: {
  60. type: 'selection'
  61. }
  62. },
  63. {
  64. field: 'index',
  65. label: t('tableDemo.index'),
  66. type: 'index',
  67. search: {
  68. hidden: true
  69. },
  70. form: {
  71. hidden: true
  72. },
  73. detail: {
  74. hidden: true
  75. }
  76. },
  77. {
  78. field: 'id',
  79. label: t('userDemo.departmentName'),
  80. table: {
  81. slots: {
  82. default: (data: any) => {
  83. return <>{data[0].row.departmentName}</>
  84. }
  85. }
  86. },
  87. form: {
  88. component: 'TreeSelect',
  89. componentProps: {
  90. nodeKey: 'id',
  91. props: {
  92. label: 'departmentName'
  93. }
  94. },
  95. optionApi: async () => {
  96. const res = await getDepartmentApi()
  97. return res.data.list
  98. }
  99. },
  100. detail: {
  101. slots: {
  102. default: (data: any) => {
  103. return <>{data.departmentName}</>
  104. }
  105. }
  106. }
  107. },
  108. {
  109. field: 'status',
  110. label: t('userDemo.status'),
  111. search: {
  112. hidden: true
  113. },
  114. table: {
  115. slots: {
  116. default: (data: any) => {
  117. const status = data[0].row.status
  118. return (
  119. <>
  120. <ElTag type={status === 0 ? 'danger' : 'success'}>
  121. {status === 1 ? t('userDemo.enable') : t('userDemo.disable')}
  122. </ElTag>
  123. </>
  124. )
  125. }
  126. }
  127. },
  128. form: {
  129. component: 'Select',
  130. componentProps: {
  131. options: [
  132. {
  133. value: 0,
  134. label: t('userDemo.disable')
  135. },
  136. {
  137. value: 1,
  138. label: t('userDemo.enable')
  139. }
  140. ]
  141. }
  142. },
  143. detail: {
  144. slots: {
  145. default: (data: any) => {
  146. return (
  147. <>
  148. <ElTag type={data.status === 0 ? 'danger' : 'success'}>
  149. {data.status === 1 ? t('userDemo.enable') : t('userDemo.disable')}
  150. </ElTag>
  151. </>
  152. )
  153. }
  154. }
  155. }
  156. },
  157. {
  158. field: 'createTime',
  159. label: t('tableDemo.displayTime'),
  160. search: {
  161. hidden: true
  162. },
  163. form: {
  164. component: 'DatePicker',
  165. componentProps: {
  166. type: 'datetime',
  167. valueFormat: 'YYYY-MM-DD HH:mm:ss'
  168. }
  169. }
  170. },
  171. {
  172. field: 'remark',
  173. label: t('userDemo.remark'),
  174. search: {
  175. hidden: true
  176. },
  177. form: {
  178. component: 'Input',
  179. componentProps: {
  180. type: 'textarea',
  181. rows: 5
  182. },
  183. colProps: {
  184. span: 24
  185. }
  186. },
  187. detail: {
  188. slots: {
  189. default: (data: any) => {
  190. return <>{data.remark}</>
  191. }
  192. }
  193. }
  194. },
  195. {
  196. field: 'action',
  197. width: '260px',
  198. label: t('tableDemo.action'),
  199. search: {
  200. hidden: true
  201. },
  202. form: {
  203. hidden: true
  204. },
  205. detail: {
  206. hidden: true
  207. },
  208. table: {
  209. slots: {
  210. default: (data: any) => {
  211. return (
  212. <>
  213. <ElButton type="primary" onClick={() => action(data[0].row, 'edit')}>
  214. {t('exampleDemo.edit')}
  215. </ElButton>
  216. <ElButton type="success" onClick={() => action(data[0].row, 'detail')}>
  217. {t('exampleDemo.detail')}
  218. </ElButton>
  219. <ElButton type="danger" onClick={() => delData(data[0].row)}>
  220. {t('exampleDemo.del')}
  221. </ElButton>
  222. </>
  223. )
  224. }
  225. }
  226. }
  227. }
  228. ])
  229. // @ts-ignore
  230. const { allSchemas } = useCrudSchemas(crudSchemas)
  231. const dialogVisible = ref(false)
  232. const dialogTitle = ref('')
  233. const currentRow = ref<TableData | null>(null)
  234. const actionType = ref('')
  235. const AddAction = () => {
  236. dialogTitle.value = t('exampleDemo.add')
  237. currentRow.value = null
  238. dialogVisible.value = true
  239. actionType.value = ''
  240. }
  241. const delLoading = ref(false)
  242. const delData = async (row: TableData | null) => {
  243. const elTableExpose = await getElTableExpose()
  244. ids.value = row ? [row.id] : elTableExpose?.getSelectionRows().map((v: TableData) => v.id) || []
  245. delLoading.value = true
  246. await delList(unref(ids).length).finally(() => {
  247. delLoading.value = false
  248. })
  249. }
  250. const action = (row: TableData, type: string) => {
  251. dialogTitle.value = t(type === 'edit' ? 'exampleDemo.edit' : 'exampleDemo.detail')
  252. actionType.value = type
  253. currentRow.value = row
  254. dialogVisible.value = true
  255. }
  256. const writeRef = ref<ComponentRef<typeof Write>>()
  257. const saveLoading = ref(false)
  258. const save = async () => {
  259. const write = unref(writeRef)
  260. const formData = await write?.submit()
  261. if (formData) {
  262. saveLoading.value = true
  263. const res = await saveDepartmentApi(formData)
  264. .catch(() => {})
  265. .finally(() => {
  266. saveLoading.value = false
  267. })
  268. if (res) {
  269. dialogVisible.value = false
  270. currentPage.value = 1
  271. getList()
  272. }
  273. }
  274. }
  275. </script>
  276. <template>
  277. <ContentWrap>
  278. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  279. <div class="mb-10px">
  280. <ElButton type="primary" @click="AddAction">{{ t('exampleDemo.add') }}</ElButton>
  281. <ElButton :loading="delLoading" type="danger" @click="delData(null)">
  282. {{ t('exampleDemo.del') }}
  283. </ElButton>
  284. </div>
  285. <Table
  286. v-model:pageSize="pageSize"
  287. v-model:currentPage="currentPage"
  288. :columns="allSchemas.tableColumns"
  289. :data="dataList"
  290. :loading="loading"
  291. :pagination="{
  292. total: total
  293. }"
  294. @register="tableRegister"
  295. />
  296. </ContentWrap>
  297. <Dialog v-model="dialogVisible" :title="dialogTitle">
  298. <Write
  299. v-if="actionType !== 'detail'"
  300. ref="writeRef"
  301. :form-schema="allSchemas.formSchema"
  302. :current-row="currentRow"
  303. />
  304. <Detail
  305. v-if="actionType === 'detail'"
  306. :detail-schema="allSchemas.detailSchema"
  307. :current-row="currentRow"
  308. />
  309. <template #footer>
  310. <ElButton v-if="actionType !== 'detail'" type="primary" :loading="saveLoading" @click="save">
  311. {{ t('exampleDemo.save') }}
  312. </ElButton>
  313. <ElButton @click="dialogVisible = false">{{ t('dialogDemo.close') }}</ElButton>
  314. </template>
  315. </Dialog>
  316. </template>