index.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <script setup lang="ts">
  2. import { ref, unref, onMounted } from 'vue'
  3. import dayjs from 'dayjs'
  4. import { ElMessage, ElTag, ElSelect, ElOption } from 'element-plus'
  5. import { DICT_TYPE } from '@/utils/dict'
  6. import { useTable } from '@/hooks/web/useTable'
  7. import { useI18n } from '@/hooks/web/useI18n'
  8. import { FormExpose } from '@/components/Form'
  9. import type { TenantVO } from '@/api/system/tenant/types'
  10. import { rules, allSchemas } from './tenant.data'
  11. import * as TenantApi from '@/api/system/tenant'
  12. import { getTenantPackageList } from '@/api/system/tenantPackage'
  13. import { TenantPackageVO } from '@/api/system/tenantPackage/types'
  14. const { t } = useI18n() // 国际化
  15. // ========== 列表相关 ==========
  16. const { register, tableObject, methods } = useTable<TenantVO>({
  17. getListApi: TenantApi.getTenantPageApi,
  18. delListApi: TenantApi.deleteTenantApi,
  19. exportListApi: TenantApi.exportTenantApi
  20. })
  21. const { getList, setSearchParams, delList, exportList } = methods
  22. // ========== 套餐 ==========
  23. const tenantPackageId = ref() // 套餐
  24. const tenantPackageOptions = ref<TenantPackageVO[]>([]) //套餐列表
  25. const getTenantPackageOptions = async () => {
  26. const res = await getTenantPackageList()
  27. tenantPackageOptions.value.push(...res)
  28. }
  29. const getPackageName = (packageId: number) => {
  30. for (let item of tenantPackageOptions.value) {
  31. if (item.id === packageId) {
  32. return item.name
  33. }
  34. }
  35. return '未知套餐'
  36. }
  37. // ========== CRUD 相关 ==========
  38. const actionLoading = ref(false) // 遮罩层
  39. const actionType = ref('') // 操作按钮的类型
  40. const dialogVisible = ref(false) // 是否显示弹出层
  41. const dialogTitle = ref('edit') // 弹出层标题
  42. const formRef = ref<FormExpose>() // 表单 Ref
  43. // 设置标题
  44. const setDialogTile = (type: string) => {
  45. dialogTitle.value = t('action.' + type)
  46. actionType.value = type
  47. dialogVisible.value = true
  48. }
  49. // 新增操作
  50. const handleCreate = () => {
  51. setDialogTile('create')
  52. // 重置表单
  53. tenantPackageId.value = ''
  54. unref(formRef)?.getElFormRef()?.resetFields()
  55. }
  56. // 修改操作
  57. const handleUpdate = async (row: any) => {
  58. setDialogTile('update')
  59. // 设置数据
  60. const res = await TenantApi.getTenantApi(row.id)
  61. tenantPackageId.value = res.packageId
  62. res.expireTime = dayjs(res.expireTime).format('YYYY-MM-DD HH:mm:ss')
  63. unref(formRef)?.setValues(res)
  64. }
  65. // 提交按钮
  66. const submitForm = async () => {
  67. actionLoading.value = true
  68. // 提交请求 unix()
  69. try {
  70. const data = unref(formRef)?.formModel as TenantVO
  71. data.packageId = tenantPackageId.value
  72. if (actionType.value === 'create') {
  73. data.expireTime = dayjs(data.expireTime).valueOf().toString()
  74. await TenantApi.createTenantApi(data)
  75. ElMessage.success(t('common.createSuccess'))
  76. } else {
  77. data.expireTime = dayjs(data.expireTime).valueOf().toString()
  78. await TenantApi.updateTenantApi(data)
  79. ElMessage.success(t('common.updateSuccess'))
  80. }
  81. // 操作成功,重新加载列表
  82. dialogVisible.value = false
  83. await getList()
  84. } finally {
  85. actionLoading.value = false
  86. }
  87. }
  88. // ========== 详情相关 ==========
  89. const detailRef = ref() // 详情 Ref
  90. // 详情操作
  91. const handleDetail = async (row: any) => {
  92. // 设置数据
  93. detailRef.value = row
  94. setDialogTile('detail')
  95. }
  96. // ========== 初始化 ==========
  97. onMounted(async () => {
  98. await getList()
  99. await getTenantPackageOptions()
  100. })
  101. </script>
  102. <template>
  103. <!-- 搜索工作区 -->
  104. <ContentWrap>
  105. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  106. </ContentWrap>
  107. <ContentWrap>
  108. <!-- 操作工具栏 -->
  109. <div class="mb-10px">
  110. <el-button type="primary" v-hasPermi="['system:tenant:create']" @click="handleCreate">
  111. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  112. </el-button>
  113. <el-button
  114. type="warning"
  115. v-hasPermi="['system:tenant:export']"
  116. :loading="tableObject.exportLoading"
  117. @click="exportList('租户数据.xls')"
  118. >
  119. <Icon icon="ep:download" class="mr-5px" /> {{ t('action.export') }}
  120. </el-button>
  121. </div>
  122. <!-- 列表 -->
  123. <Table
  124. :columns="allSchemas.tableColumns"
  125. :selection="false"
  126. :data="tableObject.tableList"
  127. :loading="tableObject.loading"
  128. :pagination="{
  129. total: tableObject.total
  130. }"
  131. v-model:pageSize="tableObject.pageSize"
  132. v-model:currentPage="tableObject.currentPage"
  133. @register="register"
  134. >
  135. <template #accountCount="{ row }">
  136. <el-tag> {{ row.accountCount }} </el-tag>
  137. </template>
  138. <template #status="{ row }">
  139. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  140. </template>
  141. <template #packageId="{ row }">
  142. <el-tag v-if="row.packageId === 0" type="danger">系统租户</el-tag>
  143. <el-tag v-else type="success"> {{ getPackageName(row.packageId) }} </el-tag>
  144. </template>
  145. <template #expireTime="{ row }">
  146. <span>{{ dayjs(row.expireTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  147. </template>
  148. <template #createTime="{ row }">
  149. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  150. </template>
  151. <template #action="{ row }">
  152. <el-button
  153. link
  154. type="primary"
  155. v-hasPermi="['system:tenant:update']"
  156. @click="handleUpdate(row)"
  157. >
  158. <Icon icon="ep:edit" class="mr-1px" /> {{ t('action.edit') }}
  159. </el-button>
  160. <el-button
  161. link
  162. type="primary"
  163. v-hasPermi="['system:tenant:update']"
  164. @click="handleDetail(row)"
  165. >
  166. <Icon icon="ep:view" class="mr-1px" /> {{ t('action.detail') }}
  167. </el-button>
  168. <el-button
  169. link
  170. type="primary"
  171. v-hasPermi="['system:tenant:delete']"
  172. @click="delList(row.id, false)"
  173. >
  174. <Icon icon="ep:delete" class="mr-1px" /> {{ t('action.del') }}
  175. </el-button>
  176. </template>
  177. </Table>
  178. </ContentWrap>
  179. <Dialog v-model="dialogVisible" :title="dialogTitle">
  180. <!-- 对话框(添加 / 修改) -->
  181. <Form
  182. v-if="['create', 'update'].includes(actionType)"
  183. :schema="allSchemas.formSchema"
  184. :rules="rules"
  185. ref="formRef"
  186. >
  187. <template #packageId>
  188. <el-select v-model="tenantPackageId">
  189. <el-option
  190. v-for="item in tenantPackageOptions"
  191. :key="item.id"
  192. :label="item.name"
  193. :value="item.id"
  194. />
  195. </el-select>
  196. </template>
  197. </Form>
  198. <!-- 对话框(详情) -->
  199. <Descriptions
  200. v-if="actionType === 'detail'"
  201. :schema="allSchemas.detailSchema"
  202. :data="detailRef"
  203. >
  204. <template #status="{ row }">
  205. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  206. </template>
  207. <template #packageId="{ row }">
  208. <el-tag v-if="row.packageId === 0" type="danger">系统租户</el-tag>
  209. <el-tag v-else type="success"> {{ getPackageName(row.packageId) }} </el-tag>
  210. </template>
  211. <template #expireTime="{ row }">
  212. <span>{{ dayjs(row.expireTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  213. </template>
  214. <template #createTime="{ row }">
  215. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  216. </template>
  217. </Descriptions>
  218. <!-- 操作按钮 -->
  219. <template #footer>
  220. <el-button
  221. v-if="['create', 'update'].includes(actionType)"
  222. type="primary"
  223. :loading="actionLoading"
  224. @click="submitForm"
  225. >
  226. {{ t('action.save') }}
  227. </el-button>
  228. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  229. </template>
  230. </Dialog>
  231. </template>