Files
37duAutomation/desktop/electron-shell/frontend/src/components/PlatformAccounts.vue
T

251 lines
9.9 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div class="plat-page">
<PageHead eyebrow="平台账号" title="登录状态" :sub="headSub">
<template #actions>
<button class="btn-ghost" :disabled="loading" @click="refresh">
{{ loading ? '校验中…' : '重新校验' }}
</button>
</template>
</PageHead>
<div class="page-body">
<div class="paccs">
<div v-if="!platStatus.length" class="pacc pacc-loading">
<span class="spin"></span>
<b>{{ loading ? '正在读取平台清单…' : '暂无可用平台' }}</b>
<span>{{ loading ? '首次启动需校验平台登录态,请稍候' : '请稍后重试或联系管理员' }}</span>
</div>
<div v-for="p in platStatus" :key="p.id" class="pacc" :class="{ on: p.online }">
<div class="p-top">
<span class="p-dot" :class="{ on: p.online }"></span>
<span class="p-name">{{ p.label }}</span>
<span class="p-state" :class="{ on: p.online, waiting: p.has_creds === null }">
{{ p.has_creds === null ? (failed ? '校验失败' : '校验中') : p.online ? '已登录' : '未登录' }}
</span>
</div>
<div class="p-user" :class="{ none: p.has_creds === false, waiting: p.has_creds === null }">
{{ p.has_creds === null ? (failed ? '登录态校验失败' : '正在校验登录态…') : p.has_creds ? p.user : '未配置个人账号' }}
</div>
<div class="p-acts">
<el-button v-if="!p.online" size="small" :loading="busy === p.id" @click="login(p.id)">
{{ busy === p.id ? '登录中…' : '登录' }}
</el-button>
<el-button v-else size="small" plain type="danger" :disabled="!!busy" @click="logout(p.id)">
登出
</el-button>
<el-button size="small" text :disabled="p.has_creds === null" @click="openEdit(p)">配置账号</el-button>
</div>
</div>
</div>
<p v-if="failed" class="pacc-note">
登录态校验未完成(平台无响应或网络异常)
<button type="button" class="pacc-retry" :disabled="loading" @click="refresh">重新校验</button>
</p>
</div>
<!-- 账号配置弹窗(必须 append-to-body:避免被容器包含块劫持 fixed 定位) -->
<el-dialog v-model="dlg" :title="`配置 ${editLabel} 账号`" width="380px" align-center
append-to-body class="pacc-dlg">
<el-form label-width="72px" @submit.prevent>
<el-form-item label="登录账号">
<el-input v-model="form.user" placeholder="平台登录账号" maxlength="64" />
</el-form-item>
<el-form-item label="登录密码">
<el-input v-model="form.pass" type="password" show-password
:placeholder="hasCreds ? '新密码(留空表示不修改)' : '平台登录密码'" maxlength="64" />
</el-form-item>
</el-form>
<template #footer>
<el-button @click="dlg = false">取消</el-button>
<el-button type="primary" :loading="saving" @click="save">保存</el-button>
</template>
</el-dialog>
</div>
</template>
<script setup>
import { reactive, ref, onMounted, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { store, log } from '../store'
import { loginPlatform, logoutPlatform, getPlatformStatus, savePlatformCreds } from '../api'
import PageHead from './PageHead.vue'
// 当前账号的平台状态(按账号隔离:各自 HR/OA 账号密码 + 各自会话)
// 先用 meta 下发的平台清单即时铺出卡片(has_creds=null 表示"校验中"),
// 状态接口返回后再补齐登录态与已存账号,避免等待外网校验时整页空白。
const platStatus = ref(
store.platforms.map((p) => ({ id: p.id, label: p.label, online: !!p.online, has_creds: null, user: '' }))
)
const loading = ref(true) // 状态接口是否仍在进行
const failed = ref(false) // 状态接口失败(网络/平台无响应)
const dlg = ref(false) // 配置弹窗开关
const editing = ref(null) // 正在配置的平台对象
const form = reactive({ user: '', pass: '' })
const busy = ref('')
const saving = ref(false)
const editLabel = computed(() => (editing.value ? editing.value.label : ''))
const hasCreds = computed(() => !!(editing.value && editing.value.has_creds))
// 页头副标题:在线数 + 一句凭证说明(与其它页统一的"统计 + 说明"格式)
const headSub = computed(() => {
const total = platStatus.value.length
const on = platStatus.value.filter((p) => p.online).length
return `${on} / ${total} 个平台已登录 · 账号密码仅保存在本机;登录后场景按当前账号身份执行`
})
async function refresh() {
loading.value = true
failed.value = false
try {
const r = await getPlatformStatus()
const list = (r && r.platforms) || []
if (list.length) platStatus.value = list
// 同步全局平台在线态(首页统计/场景徽章按当前账号展示)
for (const ps of platStatus.value) {
const p = store.platforms.find((x) => x.id === ps.id)
if (p) p.online = ps.online
}
} catch (e) {
failed.value = true
log('ERROR', `平台状态校验失败:${e.message || e}`)
} finally {
loading.value = false
}
}
onMounted(refresh)
function openEdit(p) {
editing.value = p
form.user = p.user || ''
form.pass = ''
dlg.value = true
}
async function save() {
saving.value = true
try {
await savePlatformCreds({ [editing.value.id]: { user: form.user, pass: form.pass } })
ElMessage.success(`已保存 ${editing.value.label} 账号配置`)
dlg.value = false
await refresh()
} catch (e) {
ElMessage.error('保存失败:' + (e.message || e))
} finally {
saving.value = false
}
}
async function login(id) {
if (busy.value) return
const p = platStatus.value.find((x) => x.id === id)
if (p?.online) { ElMessage.success(`${p.label} 已登录`); return }
busy.value = id
try {
const r = await loginPlatform(id)
const ok = !!(r && r.online)
if (ok) log('INFO', `平台 ${p?.label || id} 登录成功`)
else log('ERROR', `平台 ${p?.label || id} 登录未生效:${(r && r.message) || ''}`)
ElMessage[ok ? 'success' : 'error'](
`${p?.label || id} ${ok ? '登录成功' : '登录失败:' + ((r && r.message) || '登录态未生效')}`
)
await refresh()
} catch (e) {
log('ERROR', `平台 ${p?.label || id} 登录失败:${e.message || e}`)
ElMessage.error(`登录失败:${e.message || e}`)
} finally {
busy.value = ''
}
}
async function logout(id) {
busy.value = id
try {
const r = await logoutPlatform(id)
const p = platStatus.value.find((x) => x.id === id)
if (p) p.online = !!r.online
log('INFO', `平台 ${p?.label || id} 已登出`)
ElMessage.success(`平台 ${p?.label || id} 已登出`)
await refresh()
} catch (e) {
log('ERROR', `平台 ${(platStatus.value.find((x) => x.id === id) || {}).label || id} 登出失败:${e.message || e}`)
ElMessage.error(`登出失败:${e.message || e}`)
} finally {
busy.value = ''
}
}
</script>
<style scoped>
.plat-page { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.page-body {
flex: 1; min-height: 0; overflow-y: auto;
padding: 20px 24px 18px;
display: flex; flex-direction: column; gap: var(--space-3);
}
.btn-ghost {
height: 32px; padding: 0 var(--space-3);
border: 1px solid var(--line-btn); border-radius: var(--r-row);
background: var(--surface); color: var(--t2); cursor: pointer;
font: 500 12.5px/1 var(--f-cn);
transition: background var(--dur), color var(--dur);
}
.btn-ghost:hover:not(:disabled) { background: var(--field); color: var(--t1); }
.btn-ghost:disabled { opacity: .5; cursor: not-allowed; }
.paccs { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; }
.pacc {
padding: 16px; display: flex; flex-direction: column; gap: 10px;
background: var(--subtle); border: 1px solid var(--line); border-radius: var(--r-sub);
}
.pacc.on {
background: linear-gradient(160deg, var(--good-soft) 0%, var(--surface) 72%);
border-color: var(--good-line);
}
.p-top { display: flex; align-items: center; gap: var(--space-2); }
.p-dot {
flex: none; width: 8px; height: 8px; border-radius: 50%;
background: var(--t5);
}
.p-dot.on { background: var(--good); box-shadow: 0 0 0 3px var(--good-soft); }
.p-name { font: 600 13px/18px var(--f-cn); color: var(--t1); }
.p-state {
margin-left: auto; font: 500 10.5px/16px var(--f-cn); color: var(--t3);
padding: 1px var(--space-2); border-radius: var(--r-pill); background: var(--field);
}
.p-state.on { color: var(--good-ink); background: var(--good-soft); }
.p-user {
font: 400 11.5px/16px var(--f-num); color: var(--t2);
white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.p-user.none { color: var(--t4); }
.p-user.waiting { color: var(--t4); }
.p-acts { display: flex; align-items: center; gap: var(--space-1-5); margin-top: 2px; }
.p-acts .el-button + .el-button { margin-left: 0; }
/* 校验中:避免空白页,给出明确的进度反馈 */
.pacc-loading {
grid-column: 1 / -1; min-height: 150px;
align-items: center; justify-content: center; text-align: center;
color: var(--t3); font-size: 12px;
}
.pacc-loading b { font: 600 13px/18px var(--f-cn); color: var(--t2); }
.pacc-loading span { font-size: 11.5px; color: var(--t4); }
.spin {
width: 14px; height: 14px; border-radius: 50%;
border: 2px solid var(--brand-track); border-top-color: var(--brand);
animation: pacc-spin .8s linear infinite;
}
@keyframes pacc-spin { to { transform: rotate(360deg); } }
/* 校验失败提示(可手动重试) */
.pacc-note {
display: flex; align-items: center; gap: var(--space-2);
margin: 0; font: 400 11.5px/16px var(--f-cn); color: var(--t4);
}
.pacc-retry {
border: 0; background: none; padding: 0; cursor: pointer;
font: 500 11.5px/16px var(--f-cn); color: var(--brand);
}
.pacc-retry:disabled { color: var(--t4); cursor: default; }
</style>