Department.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 type { DepartmentItem } from '@/api/department/types'
  15. import { useTable } from '@/hooks/web/useTable'
  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: 'departmentName',
  79. label: t('userDemo.departmentName'),
  80. search: {
  81. hidden: true
  82. },
  83. form: {
  84. component: 'Input'
  85. }
  86. },
  87. {
  88. field: 'superiorDepartment',
  89. label: t('userDemo.superiorDepartment'),
  90. table: {
  91. hidden: true
  92. },
  93. form: {
  94. component: 'TreeSelect',
  95. componentProps: {
  96. nodeKey: 'id',
  97. checkStrictly: true,
  98. props: {
  99. label: 'departmentName'
  100. }
  101. },
  102. optionApi: async () => {
  103. const res = await getDepartmentApi()
  104. return res.data.list
  105. }
  106. },
  107. detail: {
  108. slots: {
  109. default: (data: any) => {
  110. return <>{data.superiorDepartmentName}</>
  111. }
  112. }
  113. }
  114. },
  115. {
  116. field: 'status',
  117. label: t('userDemo.status'),
  118. search: {
  119. hidden: true
  120. },
  121. table: {
  122. slots: {
  123. default: (data: any) => {
  124. const status = data.row.status
  125. return (
  126. <>
  127. <ElTag type={status === 0 ? 'danger' : 'success'}>
  128. {status === 1 ? t('userDemo.enable') : t('userDemo.disable')}
  129. </ElTag>
  130. </>
  131. )
  132. }
  133. }
  134. },
  135. form: {
  136. component: 'Select',
  137. componentProps: {
  138. options: [
  139. {
  140. value: 0,
  141. label: t('userDemo.disable')
  142. },
  143. {
  144. value: 1,
  145. label: t('userDemo.enable')
  146. }
  147. ]
  148. }
  149. },
  150. detail: {
  151. slots: {
  152. default: (data: any) => {
  153. return (
  154. <>
  155. <ElTag type={data.status === 0 ? 'danger' : 'success'}>
  156. {data.status === 1 ? t('userDemo.enable') : t('userDemo.disable')}
  157. </ElTag>
  158. </>
  159. )
  160. }
  161. }
  162. }
  163. },
  164. {
  165. field: 'createTime',
  166. label: t('tableDemo.displayTime'),
  167. search: {
  168. hidden: true
  169. },
  170. form: {
  171. component: 'DatePicker',
  172. componentProps: {
  173. type: 'datetime',
  174. valueFormat: 'YYYY-MM-DD HH:mm:ss'
  175. }
  176. }
  177. },
  178. {
  179. field: 'remark',
  180. label: t('userDemo.remark'),
  181. search: {
  182. hidden: true
  183. },
  184. form: {
  185. component: 'Input',
  186. componentProps: {
  187. type: 'textarea',
  188. rows: 5
  189. },
  190. colProps: {
  191. span: 24
  192. }
  193. },
  194. detail: {
  195. slots: {
  196. default: (data: any) => {
  197. return <>{data.remark}</>
  198. }
  199. }
  200. }
  201. },
  202. {
  203. field: 'action',
  204. width: '260px',
  205. label: t('tableDemo.action'),
  206. search: {
  207. hidden: true
  208. },
  209. form: {
  210. hidden: true
  211. },
  212. detail: {
  213. hidden: true
  214. },
  215. table: {
  216. slots: {
  217. default: (data: any) => {
  218. return (
  219. <>
  220. <ElButton type="primary" onClick={() => action(data.row, 'edit')}>
  221. {t('exampleDemo.edit')}
  222. </ElButton>
  223. <ElButton type="success" onClick={() => action(data.row, 'detail')}>
  224. {t('exampleDemo.detail')}
  225. </ElButton>
  226. <ElButton type="danger" onClick={() => delData(data.row)}>
  227. {t('exampleDemo.del')}
  228. </ElButton>
  229. </>
  230. )
  231. }
  232. }
  233. }
  234. }
  235. ])
  236. // @ts-ignore
  237. const { allSchemas } = useCrudSchemas(crudSchemas)
  238. const dialogVisible = ref(false)
  239. const dialogTitle = ref('')
  240. const currentRow = ref<DepartmentItem | null>(null)
  241. const actionType = ref('')
  242. const AddAction = () => {
  243. dialogTitle.value = t('exampleDemo.add')
  244. currentRow.value = null
  245. dialogVisible.value = true
  246. actionType.value = ''
  247. }
  248. const delLoading = ref(false)
  249. const delData = async (row: DepartmentItem | null) => {
  250. const elTableExpose = await getElTableExpose()
  251. ids.value = row
  252. ? [row.id]
  253. : elTableExpose?.getSelectionRows().map((v: DepartmentItem) => v.id) || []
  254. delLoading.value = true
  255. await delList(unref(ids).length).finally(() => {
  256. delLoading.value = false
  257. })
  258. }
  259. const action = (row: DepartmentItem, type: string) => {
  260. dialogTitle.value = t(type === 'edit' ? 'exampleDemo.edit' : 'exampleDemo.detail')
  261. actionType.value = type
  262. currentRow.value = row
  263. dialogVisible.value = true
  264. }
  265. const writeRef = ref<ComponentRef<typeof Write>>()
  266. const saveLoading = ref(false)
  267. const save = async () => {
  268. const write = unref(writeRef)
  269. const formData = await write?.submit()
  270. if (formData) {
  271. saveLoading.value = true
  272. const res = await saveDepartmentApi(formData)
  273. .catch(() => {})
  274. .finally(() => {
  275. saveLoading.value = false
  276. })
  277. if (res) {
  278. dialogVisible.value = false
  279. currentPage.value = 1
  280. getList()
  281. }
  282. }
  283. }
  284. </script>
  285. <template>
  286. <ContentWrap>
  287. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  288. <div class="mb-10px">
  289. <ElButton type="primary" @click="AddAction">{{ t('exampleDemo.add') }}</ElButton>
  290. <ElButton :loading="delLoading" type="danger" @click="delData(null)">
  291. {{ t('exampleDemo.del') }}
  292. </ElButton>
  293. </div>
  294. <Table
  295. v-model:pageSize="pageSize"
  296. v-model:currentPage="currentPage"
  297. :columns="allSchemas.tableColumns"
  298. :data="dataList"
  299. :loading="loading"
  300. :pagination="{
  301. total: total
  302. }"
  303. @register="tableRegister"
  304. />
  305. </ContentWrap>
  306. <Dialog v-model="dialogVisible" :title="dialogTitle">
  307. <Write
  308. v-if="actionType !== 'detail'"
  309. ref="writeRef"
  310. :form-schema="allSchemas.formSchema"
  311. :current-row="currentRow"
  312. />
  313. <Detail
  314. v-if="actionType === 'detail'"
  315. :detail-schema="allSchemas.detailSchema"
  316. :current-row="currentRow"
  317. />
  318. <template #footer>
  319. <ElButton v-if="actionType !== 'detail'" type="primary" :loading="saveLoading" @click="save">
  320. {{ t('exampleDemo.save') }}
  321. </ElButton>
  322. <ElButton @click="dialogVisible = false">{{ t('dialogDemo.close') }}</ElButton>
  323. </template>
  324. </Dialog>
  325. </template>