MyMappings.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <template>
  2. <div class="my-mappings-container">
  3. <div class="header">
  4. <h2>平台账号管理</h2>
  5. <div class="header-actions">
  6. <el-input
  7. v-model="searchQuery"
  8. placeholder="搜索应用名称"
  9. style="width: 250px; margin-right: 10px;"
  10. clearable
  11. @clear="fetchMappings"
  12. @keyup.enter="handleSearch"
  13. >
  14. <template #append>
  15. <el-button icon="Search" @click="handleSearch" />
  16. </template>
  17. </el-input>
  18. <el-button type="primary" @click="fetchMappings">
  19. <el-icon style="margin-right: 4px"><Refresh /></el-icon> 刷新
  20. </el-button>
  21. </div>
  22. </div>
  23. <el-table :data="mappings" v-loading="loading" stripe border style="width: 100%">
  24. <el-table-column prop="app_name" label="应用名称" min-width="150" />
  25. <el-table-column prop="mapped_key" label="映射账号" min-width="150" />
  26. <el-table-column prop="mapped_email" label="映射邮箱" min-width="180">
  27. <template #default="scope">
  28. {{ scope.row.mapped_email || '-' }}
  29. </template>
  30. </el-table-column>
  31. <el-table-column prop="is_active" label="状态" width="100">
  32. <template #default="scope">
  33. <el-tag :type="scope.row.is_active ? 'success' : 'danger'">
  34. {{ scope.row.is_active ? '正常' : '已禁用' }}
  35. </el-tag>
  36. </template>
  37. </el-table-column>
  38. <el-table-column label="操作" width="120" fixed="right">
  39. <template #default="scope">
  40. <el-button
  41. type="primary"
  42. size="small"
  43. :disabled="!scope.row.is_active"
  44. :loading="loginLoading[scope.row.app_id]"
  45. @click="handleSsoLogin(scope.row)"
  46. >
  47. 进入
  48. </el-button>
  49. </template>
  50. </el-table-column>
  51. </el-table>
  52. <div class="pagination">
  53. <el-pagination
  54. v-model:current-page="currentPage"
  55. v-model:page-size="pageSize"
  56. :page-sizes="[10, 20, 50]"
  57. layout="total, sizes, prev, pager, next, jumper"
  58. :total="total"
  59. @size-change="handleSizeChange"
  60. @current-change="handleCurrentChange"
  61. />
  62. </div>
  63. </div>
  64. </template>
  65. <script setup lang="ts">
  66. import { ref, onMounted, reactive } from 'vue'
  67. import { Refresh, Search } from '@element-plus/icons-vue'
  68. import { ElMessage } from 'element-plus'
  69. import api from '../utils/request'
  70. import { ssoLogin } from '../api/public'
  71. interface Mapping {
  72. app_name: string
  73. app_id: string
  74. protocol_type: string
  75. mapped_key: string
  76. mapped_email: string
  77. is_active: boolean
  78. }
  79. const mappings = ref<Mapping[]>([])
  80. const loading = ref(false)
  81. const searchQuery = ref('')
  82. const loginLoading = reactive<Record<string, boolean>>({})
  83. const currentPage = ref(1)
  84. const pageSize = ref(10)
  85. const total = ref(0)
  86. const fetchMappings = async () => {
  87. loading.value = true
  88. try {
  89. const params: any = {
  90. skip: (currentPage.value - 1) * pageSize.value,
  91. limit: pageSize.value
  92. }
  93. if (searchQuery.value) params.app_name = searchQuery.value
  94. const res = await api.get('/simple/me/mappings', { params })
  95. if (res.data) {
  96. mappings.value = res.data.items
  97. total.value = res.data.total
  98. }
  99. } catch (e) {
  100. // handled
  101. } finally {
  102. loading.value = false
  103. }
  104. }
  105. const handleSearch = () => {
  106. currentPage.value = 1
  107. fetchMappings()
  108. }
  109. const handleSizeChange = (val: number) => {
  110. pageSize.value = val
  111. fetchMappings()
  112. }
  113. const handleCurrentChange = (val: number) => {
  114. currentPage.value = val
  115. fetchMappings()
  116. }
  117. const handleSsoLogin = async (mapping: Mapping) => {
  118. if (!mapping.is_active) {
  119. ElMessage.warning('该账号已禁用,无法登录')
  120. return
  121. }
  122. loginLoading[mapping.app_id] = true
  123. try {
  124. // 统一通过后端 /simple/sso-login 获取 redirect_url
  125. // SIMPLE_API: 返回带 ticket 的业务系统 URL
  126. // OIDC: 返回 Hydra 授权 URL
  127. const res = await ssoLogin(
  128. { app_id: mapping.app_id, username: '', password: '' },
  129. {
  130. // 发送认证 token(如果已登录 UAP 平台则走会话模式)
  131. headers: {
  132. 'Authorization': `Bearer ${localStorage.getItem('access_token')}`
  133. }
  134. }
  135. )
  136. if (res.data && res.data.redirect_url) {
  137. // 在新标签页中打开目标应用
  138. window.open(res.data.redirect_url, '_blank')
  139. ElMessage.success('正在新标签页中打开应用...')
  140. } else {
  141. ElMessage.error('SSO 登录失败:未返回跳转地址')
  142. }
  143. } catch (error: any) {
  144. console.error('SSO 登录失败:', error)
  145. ElMessage.error(error.response?.data?.detail || 'SSO 登录失败')
  146. } finally {
  147. loginLoading[mapping.app_id] = false
  148. }
  149. }
  150. onMounted(() => {
  151. fetchMappings()
  152. })
  153. </script>
  154. <style scoped>
  155. .my-mappings-container {
  156. padding: 20px;
  157. background: #fff;
  158. border-radius: 4px;
  159. }
  160. .header {
  161. display: flex;
  162. justify-content: space-between;
  163. align-items: center;
  164. margin-bottom: 20px;
  165. }
  166. .header-actions {
  167. display: flex;
  168. align-items: center;
  169. }
  170. .pagination {
  171. margin-top: 20px;
  172. display: flex;
  173. justify-content: flex-end;
  174. }
  175. </style>