| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- <script setup lang="tsx">
- import { reactive, ref, unref } from 'vue'
- import {
- getUserRole,
- addUserRole,
- putUserRole,
- delUserRole,
- putUserRoleStatus
- } from '@/api/system/department'
- import { AddAdmins } from '@/api/system/department/types'
- import { useTable } from '@/hooks/web/useTable'
- import { useI18n } from '@/hooks/web/useI18n'
- import { Table, TableColumn } from '@/components/Table'
- import { ElMessage, ElMessageBox, ElSwitch, ElTag, ElDivider } from 'element-plus'
- import { Search } from '@/components/Search'
- import { FormSchema } from '@/components/Form'
- import { ContentWrap } from '@/components/ContentWrap'
- import Write from './components/Write.vue'
- import Detail from './components/Detail.vue'
- import { Dialog } from '@/components/Dialog'
- import { BaseButton } from '@/components/Button'
- const { t } = useI18n()
- const { tableRegister, tableState, tableMethods } = useTable({
- fetchDataApi: async () => {
- const res = await getUserRole({
- ...searchParams.value,
- page: unref(currentPage) || 1,
- limit: unref(pageSize) || 10
- })
- return {
- list:
- res.data.list.map((ee) => {
- ee.isStatus = ee.status === 1 ? true : false
- ee.roles = ee.roles.split(',')
- return ee
- }) || [],
- total: res.data.count || 0
- }
- }
- })
- const { dataList, loading, total, currentPage, pageSize } = tableState
- const { getList } = tableMethods
- const tableColumns = reactive<TableColumn[]>([
- {
- field: 'id',
- label: 'ID',
- width: 80
- },
- {
- field: 'account',
- label: '账号',
- width: 100
- },
- {
- field: 'phone',
- label: '电话',
- width: 120
- },
- {
- field: 'real_name',
- label: '姓名',
- width: 120
- },
- {
- field: 'roles',
- label: '权限',
- minWidth: 80,
- slots: {
- default: (data: any) => {
- return (
- <>
- {data.row.roles.map((item) => (
- <ElTag class={'mr-2'}>{item}</ElTag>
- ))}
- </>
- )
- }
- }
- },
- {
- field: '_add_time',
- label: '创建时间',
- width: 160
- },
- {
- field: 'status',
- label: t('menu.status'),
- align: 'center',
- headerAlign: 'center',
- width: 80,
- slots: {
- default: (data: any) => {
- return (
- <>
- <ElSwitch
- inlinePrompt
- activeText="启用"
- inactiveText="禁用"
- v-model={data.row.isStatus}
- onChange={() => putUserStatus(data.row)}
- ></ElSwitch>
- </>
- )
- }
- }
- },
- {
- field: 'action',
- label: t('userDemo.action'),
- width: 160,
- fixed: 'right',
- align: 'center',
- headerAlign: 'center',
- slots: {
- default: (data: any) => {
- const row = data.row
- return (
- <>
- <BaseButton link size="small" type="primary" onClick={() => action(row, 'edit')}>
- {t('exampleDemo.edit')}
- </BaseButton>
- <ElDivider direction="vertical" />
- <BaseButton link size="small" type="success" onClick={() => action(row, 'detail')}>
- {t('exampleDemo.detail')}
- </BaseButton>
- <ElDivider direction="vertical" />
- <BaseButton link size="small" type="danger" onClick={() => delAction(row)}>
- {t('exampleDemo.del')}
- </BaseButton>
- </>
- )
- }
- }
- }
- ])
- const searchSchema = reactive<FormSchema[]>([
- {
- field: 'status',
- label: t('menu.status'),
- component: 'Select',
- componentProps: {
- options: [
- {
- label: '全部',
- value: ''
- },
- {
- label: t('userDemo.disable'),
- value: 0
- },
- {
- label: t('userDemo.enable'),
- value: 1
- }
- ]
- }
- },
- {
- field: 'name',
- label: '搜索',
- component: 'Input',
- value: '',
- componentProps: {
- placeholder: '请输入姓名或者账号'
- }
- }
- ])
- const searchParams = ref<{ status: number | string; name?: string }>({ status: '', name: '' })
- const setSearchParams = (data: any) => {
- searchParams.value = data
- getList()
- }
- const dialogVisible = ref(false)
- const dialogTitle = ref('')
- const currentRow = ref()
- const actionType = ref('')
- const writeRef = ref<ComponentRef<typeof Write>>()
- const saveLoading = ref(false)
- const action = (row: any, type: string) => {
- dialogTitle.value = t(type === 'edit' ? 'exampleDemo.edit' : 'exampleDemo.detail')
- actionType.value = type
- currentRow.value = {
- id: row.id,
- account: row.account, //账号
- real_name: row.real_name, //名字
- pwd: '',
- conf_pwd: '',
- phone: row.phone, //手机号
- roles: row.roles, //角色数组
- status: row.status === 1 ? true : false //是否启用
- }
- dialogVisible.value = true
- }
- const putUserStatus = (row: any) => {
- putUserRoleStatus({ id: row.id, status: row.status === 1 ? 0 : 1 }).then(() => {
- getList()
- })
- }
- const AddAction = () => {
- dialogTitle.value = t('exampleDemo.add')
- currentRow.value = undefined
- dialogVisible.value = true
- actionType.value = ''
- }
- const delAction = async (row: any) => {
- ElMessageBox.confirm('删除后无法恢复,是否删除?', {
- confirmButtonText: '删除',
- cancelButtonText: '取消',
- type: 'warning'
- })
- .then(async () => {
- await delUserRole({ id: row.id })
- await getList()
- ElMessage({
- showClose: true,
- message: '删除',
- type: 'success'
- })
- })
- .catch(() => {})
- }
- const save = async () => {
- const write = unref(writeRef)
- const formData = await write?.submit()
- if (formData) {
- saveLoading.value = true
- const data: AddAdmins = {
- id: formData.id || '',
- account: formData.account, //账号
- pwd: formData.pwd, //密码
- conf_pwd: formData.conf_pwd, //确认密码
- real_name: formData.real_name, //名字
- phone: formData.phone, //手机号
- roles: formData.roles, //角色数组
- status: formData.status ? 1 : 0 //是否启用
- }
- try {
- let res: any = {}
- if (actionType.value === 'edit') {
- res = await putUserRole(data)
- } else if (actionType.value === '') {
- res = await addUserRole(data)
- }
- if (res?.status == 200) {
- ElMessage({
- showClose: true,
- message: '保存成功',
- type: 'success'
- })
- getList()
- dialogVisible.value = false
- }
- } catch (error) {
- console.log(error)
- } finally {
- saveLoading.value = false
- }
- }
- }
- </script>
- <template>
- <ContentWrap>
- <Search :schema="searchSchema" @reset="setSearchParams" @search="setSearchParams" />
- <div class="mb-10px">
- <BaseButton type="primary" @click="AddAction">{{ t('exampleDemo.add') }}</BaseButton>
- </div>
- <Table
- v-model:current-page="currentPage"
- v-model:page-size="pageSize"
- :columns="tableColumns"
- default-expand-all
- node-key="id"
- stripe
- :data="dataList"
- :loading="loading"
- @register="tableRegister"
- :pagination="{
- total
- }"
- />
- </ContentWrap>
- <Dialog v-model="dialogVisible" :title="dialogTitle" width="60%">
- <Write v-if="actionType !== 'detail'" ref="writeRef" :current-row="currentRow" />
- <Detail v-if="actionType === 'detail'" :current-row="currentRow" />
- <template #footer>
- <BaseButton
- v-if="actionType !== 'detail'"
- type="primary"
- :loading="saveLoading"
- @click="save"
- >
- {{ t('exampleDemo.save') }}
- </BaseButton>
- <BaseButton @click="dialogVisible = false">{{ t('dialogDemo.close') }}</BaseButton>
- </template>
- </Dialog>
- </template>
|