index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <ContentWrap>
  3. <!-- 列表 -->
  4. <XTable @register="registerTable">
  5. <template #toolbar_buttons>
  6. <!-- 操作:新增 -->
  7. <XButton
  8. type="primary"
  9. preIcon="ep:zoom-in"
  10. :title="t('action.add')"
  11. v-hasPermi="['system:tenant:create']"
  12. @click="handleCreate()"
  13. />
  14. <XButton
  15. type="warning"
  16. preIcon="ep:download"
  17. :title="t('action.export')"
  18. v-hasPermi="['system:tenant:export']"
  19. @click="exportList('租户列表.xls')"
  20. />
  21. </template>
  22. <template #accountCount_default="{ row }">
  23. <el-tag> {{ row.accountCount }} </el-tag>
  24. </template>
  25. <template #packageId_default="{ row }">
  26. <el-tag v-if="row.packageId === 0" type="danger">系统租户</el-tag>
  27. <el-tag v-else type="success"> {{ getPackageName(row.packageId) }} </el-tag>
  28. </template>
  29. <template #actionbtns_default="{ row }">
  30. <!-- 操作:修改 -->
  31. <XTextButton
  32. preIcon="ep:edit"
  33. :title="t('action.edit')"
  34. v-hasPermi="['system:tenant:update']"
  35. @click="handleUpdate(row.id)"
  36. />
  37. <!-- 操作:详情 -->
  38. <XTextButton
  39. preIcon="ep:view"
  40. :title="t('action.detail')"
  41. v-hasPermi="['system:tenant:update']"
  42. @click="handleDetail(row.id)"
  43. />
  44. <!-- 操作:删除 -->
  45. <XTextButton
  46. preIcon="ep:delete"
  47. :title="t('action.del')"
  48. v-hasPermi="['system:tenant:delete']"
  49. @click="deleteData(row.id)"
  50. />
  51. </template>
  52. </XTable>
  53. </ContentWrap>
  54. <XModal v-model="dialogVisible" :title="dialogTitle">
  55. <!-- 对话框(添加 / 修改) -->
  56. <Form
  57. v-if="['create', 'update'].includes(actionType)"
  58. :schema="allSchemas.formSchema"
  59. :rules="rules"
  60. ref="formRef"
  61. />
  62. <!-- 对话框(详情) -->
  63. <Descriptions
  64. v-if="actionType === 'detail'"
  65. :schema="allSchemas.detailSchema"
  66. :data="detailData"
  67. >
  68. <template #packageId="{ row }">
  69. <el-tag v-if="row.packageId === 0" type="danger">系统租户</el-tag>
  70. <el-tag v-else type="success"> {{ getPackageName(row.packageId) }} </el-tag>
  71. </template>
  72. </Descriptions>
  73. <!-- 操作按钮 -->
  74. <template #footer>
  75. <!-- 按钮:保存 -->
  76. <XButton
  77. v-if="['create', 'update'].includes(actionType)"
  78. type="primary"
  79. :title="t('action.save')"
  80. :loading="actionLoading"
  81. @click="submitForm()"
  82. />
  83. <!-- 按钮:关闭 -->
  84. <XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
  85. </template>
  86. </XModal>
  87. </template>
  88. <script setup lang="ts" name="Tenant">
  89. import type { FormExpose } from '@/components/Form'
  90. import * as TenantApi from '@/api/system/tenant'
  91. import { rules, allSchemas, tenantPackageOption } from './tenant.data'
  92. const { t } = useI18n() // 国际化
  93. const message = useMessage() // 消息弹窗
  94. // 列表相关的变量
  95. const [registerTable, { reload, deleteData, exportList }] = useXTable({
  96. allSchemas: allSchemas,
  97. getListApi: TenantApi.getTenantPageApi,
  98. deleteApi: TenantApi.deleteTenantApi,
  99. exportListApi: TenantApi.exportTenantApi
  100. })
  101. const actionLoading = ref(false) // 遮罩层
  102. const actionType = ref('') // 操作按钮的类型
  103. const dialogVisible = ref(false) // 是否显示弹出层
  104. const dialogTitle = ref('edit') // 弹出层标题
  105. const formRef = ref<FormExpose>() // 表单 Ref
  106. const detailData = ref() // 详情 Ref
  107. const getPackageName = (packageId: number) => {
  108. for (let item of tenantPackageOption) {
  109. if (item.value === packageId) {
  110. return item.label
  111. }
  112. }
  113. return '未知套餐'
  114. }
  115. // 设置标题
  116. const setDialogTile = (type: string) => {
  117. dialogTitle.value = t('action.' + type)
  118. actionType.value = type
  119. dialogVisible.value = true
  120. }
  121. // 新增操作
  122. const handleCreate = async () => {
  123. // 重置表单
  124. setDialogTile('create')
  125. await nextTick()
  126. console.log(allSchemas.formSchema, 'allSchemas.formSchema')
  127. if (allSchemas.formSchema[4].field !== 'username') {
  128. unref(formRef)?.addSchema(
  129. {
  130. field: 'username',
  131. label: '用户名称',
  132. component: 'Input'
  133. },
  134. 0
  135. )
  136. unref(formRef)?.addSchema(
  137. {
  138. field: 'password',
  139. label: '用户密码',
  140. component: 'InputPassword'
  141. },
  142. 1
  143. )
  144. }
  145. }
  146. // 修改操作
  147. const handleUpdate = async (rowId: number) => {
  148. setDialogTile('update')
  149. await nextTick()
  150. unref(formRef)?.delSchema('username')
  151. unref(formRef)?.delSchema('password')
  152. // 设置数据
  153. const res = await TenantApi.getTenantApi(rowId)
  154. unref(formRef)?.setValues(res)
  155. }
  156. // 详情操作
  157. const handleDetail = async (rowId: number) => {
  158. // 设置数据
  159. const res = await TenantApi.getTenantApi(rowId)
  160. detailData.value = res
  161. setDialogTile('detail')
  162. }
  163. // 提交按钮
  164. const submitForm = async () => {
  165. const elForm = unref(formRef)?.getElFormRef()
  166. if (!elForm) return
  167. elForm.validate(async (valid) => {
  168. if (valid) {
  169. actionLoading.value = true
  170. // 提交请求
  171. try {
  172. const data = unref(formRef)?.formModel as TenantApi.TenantVO
  173. if (actionType.value === 'create') {
  174. await TenantApi.createTenantApi(data)
  175. message.success(t('common.createSuccess'))
  176. } else {
  177. await TenantApi.updateTenantApi(data)
  178. message.success(t('common.updateSuccess'))
  179. }
  180. // 操作成功,重新加载列表
  181. dialogVisible.value = false
  182. } finally {
  183. actionLoading.value = false
  184. // 刷新列表
  185. await reload()
  186. }
  187. }
  188. })
  189. }
  190. </script>