index.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. const elForm = unref(formRef)?.getElFormRef()
  68. if (!elForm) return
  69. elForm.validate(async (valid) => {
  70. if (valid) {
  71. actionLoading.value = true
  72. // 提交请求
  73. try {
  74. const data = unref(formRef)?.formModel as TenantVO
  75. data.packageId = tenantPackageId.value
  76. if (actionType.value === 'create') {
  77. data.expireTime = dayjs(data.expireTime).valueOf().toString()
  78. await TenantApi.createTenantApi(data)
  79. ElMessage.success(t('common.createSuccess'))
  80. } else {
  81. data.expireTime = dayjs(data.expireTime).valueOf().toString()
  82. await TenantApi.updateTenantApi(data)
  83. ElMessage.success(t('common.updateSuccess'))
  84. }
  85. // 操作成功,重新加载列表
  86. dialogVisible.value = false
  87. await getList()
  88. } finally {
  89. actionLoading.value = false
  90. }
  91. }
  92. })
  93. }
  94. // ========== 详情相关 ==========
  95. const detailRef = ref() // 详情 Ref
  96. // 详情操作
  97. const handleDetail = async (row: any) => {
  98. // 设置数据
  99. detailRef.value = row
  100. setDialogTile('detail')
  101. }
  102. // ========== 初始化 ==========
  103. onMounted(async () => {
  104. await getList()
  105. await getTenantPackageOptions()
  106. })
  107. </script>
  108. <template>
  109. <!-- 搜索工作区 -->
  110. <ContentWrap>
  111. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  112. </ContentWrap>
  113. <ContentWrap>
  114. <!-- 操作工具栏 -->
  115. <div class="mb-10px">
  116. <el-button type="primary" v-hasPermi="['system:tenant:create']" @click="handleCreate">
  117. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  118. </el-button>
  119. <el-button
  120. type="warning"
  121. v-hasPermi="['system:tenant:export']"
  122. :loading="tableObject.exportLoading"
  123. @click="exportList('租户数据.xls')"
  124. >
  125. <Icon icon="ep:download" class="mr-5px" /> {{ t('action.export') }}
  126. </el-button>
  127. </div>
  128. <!-- 列表 -->
  129. <Table
  130. :columns="allSchemas.tableColumns"
  131. :selection="false"
  132. :data="tableObject.tableList"
  133. :loading="tableObject.loading"
  134. :pagination="{
  135. total: tableObject.total
  136. }"
  137. v-model:pageSize="tableObject.pageSize"
  138. v-model:currentPage="tableObject.currentPage"
  139. @register="register"
  140. >
  141. <template #accountCount="{ row }">
  142. <el-tag> {{ row.accountCount }} </el-tag>
  143. </template>
  144. <template #status="{ row }">
  145. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  146. </template>
  147. <template #packageId="{ row }">
  148. <el-tag v-if="row.packageId === 0" type="danger">系统租户</el-tag>
  149. <el-tag v-else type="success"> {{ getPackageName(row.packageId) }} </el-tag>
  150. </template>
  151. <template #expireTime="{ row }">
  152. <span>{{ dayjs(row.expireTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  153. </template>
  154. <template #createTime="{ row }">
  155. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  156. </template>
  157. <template #action="{ row }">
  158. <el-button
  159. link
  160. type="primary"
  161. v-hasPermi="['system:tenant:update']"
  162. @click="handleUpdate(row)"
  163. >
  164. <Icon icon="ep:edit" class="mr-1px" /> {{ t('action.edit') }}
  165. </el-button>
  166. <el-button
  167. link
  168. type="primary"
  169. v-hasPermi="['system:tenant:update']"
  170. @click="handleDetail(row)"
  171. >
  172. <Icon icon="ep:view" class="mr-1px" /> {{ t('action.detail') }}
  173. </el-button>
  174. <el-button
  175. link
  176. type="primary"
  177. v-hasPermi="['system:tenant:delete']"
  178. @click="delList(row.id, false)"
  179. >
  180. <Icon icon="ep:delete" class="mr-1px" /> {{ t('action.del') }}
  181. </el-button>
  182. </template>
  183. </Table>
  184. </ContentWrap>
  185. <Dialog v-model="dialogVisible" :title="dialogTitle">
  186. <!-- 对话框(添加 / 修改) -->
  187. <Form
  188. v-if="['create', 'update'].includes(actionType)"
  189. :schema="allSchemas.formSchema"
  190. :rules="rules"
  191. ref="formRef"
  192. >
  193. <template #packageId>
  194. <el-select v-model="tenantPackageId">
  195. <el-option
  196. v-for="item in tenantPackageOptions"
  197. :key="item.id"
  198. :label="item.name"
  199. :value="item.id"
  200. />
  201. </el-select>
  202. </template>
  203. </Form>
  204. <!-- 对话框(详情) -->
  205. <Descriptions
  206. v-if="actionType === 'detail'"
  207. :schema="allSchemas.detailSchema"
  208. :data="detailRef"
  209. >
  210. <template #status="{ row }">
  211. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  212. </template>
  213. <template #packageId="{ row }">
  214. <el-tag v-if="row.packageId === 0" type="danger">系统租户</el-tag>
  215. <el-tag v-else type="success"> {{ getPackageName(row.packageId) }} </el-tag>
  216. </template>
  217. <template #expireTime="{ row }">
  218. <span>{{ dayjs(row.expireTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  219. </template>
  220. <template #createTime="{ row }">
  221. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  222. </template>
  223. </Descriptions>
  224. <!-- 操作按钮 -->
  225. <template #footer>
  226. <el-button
  227. v-if="['create', 'update'].includes(actionType)"
  228. type="primary"
  229. :loading="actionLoading"
  230. @click="submitForm"
  231. >
  232. {{ t('action.save') }}
  233. </el-button>
  234. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  235. </template>
  236. </Dialog>
  237. </template>