index.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 handleExport = async () => {
  24. await exportList('租户数据.xls')
  25. }
  26. // ========== 套餐 ==========
  27. const tenantPackageId = ref() // 套餐
  28. const tenantPackageOptions = ref<TenantPackageVO[]>([]) //套餐列表
  29. const getTenantPackageOptions = async () => {
  30. const res = await getTenantPackageList()
  31. tenantPackageOptions.value.push(...res)
  32. }
  33. const getPackageName = (packageId: number) => {
  34. for (let item of tenantPackageOptions.value) {
  35. if (item.id === packageId) {
  36. return item.name
  37. }
  38. }
  39. return '未知套餐'
  40. }
  41. // ========== CRUD 相关 ==========
  42. const actionLoading = ref(false) // 遮罩层
  43. const actionType = ref('') // 操作按钮的类型
  44. const dialogVisible = ref(false) // 是否显示弹出层
  45. const dialogTitle = ref('edit') // 弹出层标题
  46. const formRef = ref<FormExpose>() // 表单 Ref
  47. // 设置标题
  48. const setDialogTile = (type: string) => {
  49. dialogTitle.value = t('action.' + type)
  50. actionType.value = type
  51. dialogVisible.value = true
  52. }
  53. // 新增操作
  54. const handleCreate = () => {
  55. setDialogTile('create')
  56. // 重置表单
  57. tenantPackageId.value = ''
  58. unref(formRef)?.getElFormRef()?.resetFields()
  59. }
  60. // 修改操作
  61. const handleUpdate = async (row: any) => {
  62. setDialogTile('update')
  63. // 设置数据
  64. const res = await TenantApi.getTenantApi(row.id)
  65. tenantPackageId.value = res.packageId
  66. res.expireTime = dayjs(res.expireTime).format('YYYY-MM-DD HH:mm:ss')
  67. unref(formRef)?.setValues(res)
  68. }
  69. // 提交按钮
  70. const submitForm = async () => {
  71. actionLoading.value = true
  72. // 提交请求 unix()
  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. const handleDelete = (row: TenantVO) => {
  94. delList(row.id, false)
  95. }
  96. // ========== 详情相关 ==========
  97. const detailRef = ref() // 详情 Ref
  98. // 详情操作
  99. const handleDetail = async (row: any) => {
  100. // 设置数据
  101. detailRef.value = row
  102. setDialogTile('detail')
  103. }
  104. // ========== 初始化 ==========
  105. onMounted(async () => {
  106. await getList()
  107. await getTenantPackageOptions()
  108. })
  109. </script>
  110. <template>
  111. <!-- 搜索工作区 -->
  112. <ContentWrap>
  113. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  114. </ContentWrap>
  115. <ContentWrap>
  116. <!-- 操作工具栏 -->
  117. <div class="mb-10px">
  118. <el-button type="primary" v-hasPermi="['system:tenant:create']" @click="handleCreate">
  119. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  120. </el-button>
  121. <el-button
  122. type="warning"
  123. v-hasPermi="['system:tenant:export']"
  124. :loading="tableObject.exportLoading"
  125. @click="handleExport"
  126. >
  127. <Icon icon="ep:download" class="mr-5px" /> {{ t('action.export') }}
  128. </el-button>
  129. </div>
  130. <!-- 列表 -->
  131. <Table
  132. :columns="allSchemas.tableColumns"
  133. :selection="false"
  134. :data="tableObject.tableList"
  135. :loading="tableObject.loading"
  136. :pagination="{
  137. total: tableObject.total
  138. }"
  139. v-model:pageSize="tableObject.pageSize"
  140. v-model:currentPage="tableObject.currentPage"
  141. @register="register"
  142. >
  143. <template #accountCount="{ row }">
  144. <el-tag> {{ row.accountCount }} </el-tag>
  145. </template>
  146. <template #status="{ row }">
  147. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  148. </template>
  149. <template #packageId="{ row }">
  150. <!-- <DictTag :type="DICT_TYPE.SYSTEM_TENANT_PACKAGE_ID" :value="row.packageId" />-->
  151. <el-tag v-if="row.packageId === 0" type="danger">系统租户</el-tag>
  152. <el-tag v-else type="success"> {{ getPackageName(row.packageId) }} </el-tag>
  153. </template>
  154. <template #expireTime="{ row }">
  155. <span>{{ dayjs(row.expireTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  156. </template>
  157. <template #createTime="{ row }">
  158. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  159. </template>
  160. <template #action="{ row }">
  161. <!-- <el-button type="text" v-hasPermi="['system:tenant:update']" @click="handleUpdate(row)">-->
  162. <el-button
  163. link
  164. type="primary"
  165. v-hasPermi="['system:tenant:update']"
  166. @click="handleUpdate(row)"
  167. >
  168. <Icon icon="ep:edit" class="mr-1px" /> {{ t('action.edit') }}
  169. </el-button>
  170. <el-button
  171. link
  172. type="primary"
  173. v-hasPermi="['system:tenant:update']"
  174. @click="handleDetail(row)"
  175. >
  176. <Icon icon="ep:view" class="mr-1px" /> {{ t('action.detail') }}
  177. </el-button>
  178. <el-button
  179. link
  180. type="primary"
  181. v-hasPermi="['system:tenant:delete']"
  182. @click="handleDelete(row)"
  183. >
  184. <Icon icon="ep:delete" class="mr-1px" /> {{ t('action.del') }}
  185. </el-button>
  186. </template>
  187. </Table>
  188. </ContentWrap>
  189. <Dialog v-model="dialogVisible" :title="dialogTitle">
  190. <!-- 对话框(添加 / 修改) -->
  191. <Form
  192. v-if="['create', 'update'].includes(actionType)"
  193. :schema="allSchemas.formSchema"
  194. :rules="rules"
  195. ref="formRef"
  196. >
  197. <template #packageId>
  198. <el-select v-model="tenantPackageId" placeholder="Select">
  199. <el-option
  200. v-for="item in tenantPackageOptions"
  201. :key="item.id"
  202. :label="item.name"
  203. :value="item.id"
  204. />
  205. </el-select>
  206. </template>
  207. </Form>
  208. <!-- 对话框(详情) -->
  209. <Descriptions
  210. v-if="actionType === 'detail'"
  211. :schema="allSchemas.detailSchema"
  212. :data="detailRef"
  213. >
  214. <template #status="{ row }">
  215. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  216. </template>
  217. <template #packageId="{ row }">
  218. <el-tag v-if="row.packageId === 0" type="danger">系统租户</el-tag>
  219. <el-tag v-else type="success"> {{ getPackageName(row.packageId) }} </el-tag>
  220. </template>
  221. <template #expireTime="{ row }">
  222. <span>{{ dayjs(row.expireTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  223. </template>
  224. <template #createTime="{ row }">
  225. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  226. </template>
  227. </Descriptions>
  228. <!-- 操作按钮 -->
  229. <template #footer>
  230. <el-button
  231. v-if="['create', 'update'].includes(actionType)"
  232. type="primary"
  233. :loading="actionLoading"
  234. @click="submitForm"
  235. >
  236. {{ t('action.save') }}
  237. </el-button>
  238. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  239. </template>
  240. </Dialog>
  241. </template>