Files
financial_system_frontend/src/components/KingdeeAccountBindingPanel.vue
T

326 lines
15 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="kingdee-bind-panel" :class="{ embedded: embedded }">
<div v-if="loadError" class="inline-alert danger" role="alert">
<b>!</b><span>{{ loadError }}</span>
<button class="btn small" type="button" @click="load">重试</button>
</div>
<div class="card fill-list-card primary-list-card" :class="{ 'panel-card': embedded }">
<div class="filter-bar list-toolbar">
<div class="list-toolbar__filters">
<label v-if="canViewAll" class="filter-search">
<span aria-hidden="true">&#x1F50D;</span>
<input v-model="search" type="search" placeholder="搜索姓名或金蝶账号" aria-label="搜索财务人员">
</label>
<span v-else class="filter-hint">当前权限:仅查看本人</span>
</div>
<div class="list-toolbar__meta">
<button
v-if="canCreateBinding"
class="btn small primary"
type="button"
:disabled="loading || !addCandidates.length"
:title="addButtonTitle"
@click="openCreate"
>新增绑定</button>
<span class="tag info">{{ filtered.length }} 人</span>
<span v-if="uiRead('ui.kingdee.binding.column.status')" class="tag" :class="boundCount ? 'success' : 'neutral'">已绑定 {{ boundCount }}</span>
</div>
</div>
<div v-if="loading" class="load-wrap" role="status">
<span class="loader" aria-hidden="true"></span><span>正在加载绑定列表…</span>
</div>
<div v-else-if="!loadError && filtered.length" class="table-wrap" tabindex="0" role="region" aria-label="金蝶账号绑定列表">
<table class="data-table responsive-table">
<thead>
<tr>
<th v-if="uiRead('ui.kingdee.binding.column.employee')">财务人员</th>
<th v-if="uiRead('ui.kingdee.binding.column.username')">金蝶账号</th>
<th v-if="uiRead('ui.kingdee.binding.column.status')">绑定状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="item in paginatedItems" :key="item.employeeOpenId">
<td v-if="uiRead('ui.kingdee.binding.column.employee')" data-label="财务人员">
<strong>{{ item.realName }}</strong>
<small v-if="item.employeeOpenId === meId" class="self-mark">本人</small>
</td>
<td v-if="uiRead('ui.kingdee.binding.column.username')" data-label="金蝶账号"><small>{{ item.kingdeeUsername || '—' }}</small></td>
<td v-if="uiRead('ui.kingdee.binding.column.status')" data-label="绑定状态">
<span class="tag" :class="item.bound ? 'success' : 'neutral'">{{ item.bound ? '已绑定' : '未绑定' }}</span>
</td>
<td data-label="操作" class="actions-cell">
<button v-if="canBindRow(item)" class="table-action" type="button" @click="openBind(item)">
{{ item.bound ? '重新绑定' : '绑定' }}
</button>
<button v-if="canTestRow(item) && item.bound" class="table-action" type="button" :disabled="testingId === item.employeeOpenId" @click="testBinding(item)">
{{ testingId === item.employeeOpenId ? '验证中…' : '验证' }}
</button>
<button v-if="canUnbindRow(item) && item.bound" class="table-action danger-action" type="button" @click="unbind(item)">解绑</button>
</td>
</tr>
</tbody>
</table>
</div>
<div v-else-if="!loading && !loadError" class="empty-state">
<strong>{{ canViewAll ? '暂无财务人员' : '暂无本人记录' }}</strong>
<span>{{ canViewAll ? '请先为用户分配「财务」角色,或授予「变更金蝶财务单据」权限。' : '当前账号不在可绑定范围内,或尚无权限。' }}</span>
</div>
<AppListPagination
v-if="!loading && !loadError && filtered.length && canViewAll"
v-model:page="page"
v-model:page-size="pageSize"
:total="filtered.length"
aria-label="金蝶账号绑定列表分页"
/>
</div>
<dialog v-if="canOpenDialog" ref="bindDialog" v-dialog-drag class="app-dialog" aria-labelledby="kingdee-bind-title" @cancel.prevent="closeBind">
<form @submit.prevent="saveBind">
<div class="modal-head" data-dialog-drag-handle>
<div>
<span class="eyebrow">BIND KINGDEE ACCOUNT</span>
<h2 id="kingdee-bind-title">{{ adding ? '新增金蝶账号绑定' : '绑定金蝶账号' }} · {{ activeBinding?.realName || '请选择人员' }}</h2>
<p>填写该人员在金蝶云星空的登录用户名与密码;保存前默认验证登录。</p>
</div>
<button class="modal-close" type="button" aria-label="关闭" :disabled="saving" @click="closeBind">×</button>
</div>
<div class="modal-body">
<div class="form-grid">
<div v-if="adding" class="field full">
<label for="kingdee-binding-user">绑定人员 <em>*</em></label>
<select id="kingdee-binding-user" v-model="selectedEmployeeOpenId" class="select" required :disabled="saving">
<option :value="null" disabled>请选择未绑定人员</option>
<option v-for="item in addCandidates" :key="item.employeeOpenId" :value="item.employeeOpenId">{{ addCandidateLabel(item) }}</option>
</select>
</div>
<div v-if="uiRead('dialog.kingdee.account.username')" class="field">
<label for="kingdee-username">金蝶用户名 <em>*</em></label>
<input id="kingdee-username" v-model.trim="form.kingdeeUsername" required autocomplete="username" :disabled="saving || !uiEdit('dialog.kingdee.account.username')">
</div>
<div v-if="uiRead('dialog.kingdee.account.password')" class="field">
<label for="kingdee-password">金蝶密码 <em>*</em></label>
<input id="kingdee-password" v-model="form.kingdeePassword" type="password" required autocomplete="new-password" :disabled="saving || !uiEdit('dialog.kingdee.account.password')" :placeholder="editing?.bound ? '已安全保存;编辑时请重新输入' : '请输入金蝶密码'">
</div>
<label class="check-option full">
<input v-model="form.verifyLogin" type="checkbox" :disabled="saving || !canEditCredentials">
<span>保存前验证金蝶登录</span>
</label>
</div>
</div>
<div class="modal-actions">
<button class="btn" type="button" :disabled="saving" @click="closeBind">取消</button>
<button v-if="canEditCredentials" class="btn primary" type="submit" :disabled="saving">{{ saving ? '保存中…' : '保存绑定' }}</button>
</div>
</form>
</dialog>
</div>
</template>
<script setup lang="ts">
import { computed, nextTick, onMounted, reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from '@/utils/uiFeedback'
import AppListPagination from '@/components/common/AppListPagination.vue'
import { matchesReadableSearch } from '@/authorization/searchProjection'
import { useListPagination } from '@/composables/useListPagination'
import { kingdeeAccountsApi, type KingdeeAccountBinding } from '@/api/kingdeeAccounts'
import { useAuthorizationStore } from '@/store/authorization'
import { useUserStore } from '@/store/user'
defineProps<{ embedded?: boolean }>()
const userStore = useUserStore()
const authorizationStore = useAuthorizationStore()
const uiRead = (code: string) => userStore.canRead(code)
const uiEdit = (code: string) => userStore.canEdit(code)
const meId = computed(() => userStore.userInfo?.feishuOpenId || null)
const canViewAll = computed(() => userStore.canRead('finance.kingdee.account.view-all') && authorizationStore.can('integration.kingdee.account:read-all'))
const canViewOwn = computed(() => userStore.canRead('finance.kingdee.account.view-own') && authorizationStore.can('integration.kingdee.account:read-own'))
const canViewAny = computed(() => canViewAll.value || canViewOwn.value)
const canBindAll = computed(() => userStore.canEdit('finance.kingdee.account.bind') && authorizationStore.can('integration.kingdee.account:manage-all'))
const canBindOwn = computed(() => userStore.canEdit('finance.kingdee.account.bind-own') && authorizationStore.can('integration.kingdee.account:manage-own'))
const canUnbindAll = computed(() => userStore.canEdit('finance.kingdee.account.unbind') && authorizationStore.can('integration.kingdee.account:manage-all'))
const canUnbindOwn = computed(() => userStore.canEdit('finance.kingdee.account.unbind-own') && authorizationStore.can('integration.kingdee.account:manage-own'))
const canOpenDialog = computed(() => uiRead('dialog.kingdee.account'))
const canEditCredentials = computed(() => uiEdit('dialog.kingdee.account.username') && uiEdit('dialog.kingdee.account.password'))
const canCreateBinding = computed(() => canOpenDialog.value && canEditCredentials.value && (canBindAll.value || canBindOwn.value))
const canBindRow = (item: KingdeeAccountBinding) =>
canOpenDialog.value && (canBindAll.value || (canBindOwn.value && item.employeeOpenId === meId.value))
const canUnbindRow = (item: KingdeeAccountBinding) =>
canUnbindAll.value || (canUnbindOwn.value && item.employeeOpenId === meId.value)
const canTestRow = (item: KingdeeAccountBinding) =>
(item.employeeOpenId === meId.value ? uiEdit('dialog.kingdee.account.test-own') : uiEdit('dialog.kingdee.account.test-all')) && canBindRow(item)
const items = ref<KingdeeAccountBinding[]>([])
const loading = ref(false)
const loadError = ref('')
const search = ref('')
const saving = ref(false)
const testingId = ref<string | null>(null)
const bindDialog = ref<HTMLDialogElement | null>(null)
const editing = ref<KingdeeAccountBinding | null>(null)
const adding = ref(false)
const selectedEmployeeOpenId = ref<string | null>(null)
const form = reactive({ kingdeeUsername: '', kingdeePassword: '', verifyLogin: true })
const errorText = (error: unknown) => {
const responseMessage = (error as { response?: { data?: { message?: unknown } } })?.response?.data?.message
if (typeof responseMessage === 'string' && responseMessage.trim()) return responseMessage
return error instanceof Error && error.message ? error.message : '请求失败,请稍后重试。'
}
const filtered = computed(() => {
return items.value.filter(item => matchesReadableSearch(search.value, [
{ readable: uiRead('ui.kingdee.binding.column.employee'), values: [item.realName] },
{ readable: uiRead('ui.kingdee.binding.column.username'), values: [item.kingdeeUsername] },
]))
})
const boundCount = computed(() => items.value.filter(item => item.bound).length)
const canReadEmployeeIdentity = computed(() => uiRead('ui.kingdee.binding.column.employee'))
const addCandidates = computed(() => items.value.filter(item =>
!item.bound && canBindRow(item) && (item.employeeOpenId === meId.value || canReadEmployeeIdentity.value),
))
const activeBinding = computed(() => adding.value
? addCandidates.value.find(item => item.employeeOpenId === selectedEmployeeOpenId.value) || null
: editing.value)
const addButtonTitle = computed(() => {
if (addCandidates.value.length) return '为未绑定人员新增金蝶账号绑定'
if (canBindAll.value && !canReadEmployeeIdentity.value && !canBindOwn.value) return '需要“财务人员”字段查看权限才能选择绑定人员'
return '当前没有可新增绑定的人员'
})
const addCandidateLabel = (item: KingdeeAccountBinding) => item.realName
const { page, pageSize, paginatedItems } = useListPagination(filtered, { resetSources: [search] })
const load = async () => {
if (!canViewAny.value) {
items.value = []
loadError.value = '当前账号没有金蝶账号绑定查看权限。'
return
}
loading.value = true
loadError.value = ''
try {
await userStore.loadPermissions()
items.value = await kingdeeAccountsApi.list() || []
} catch (error) {
loadError.value = errorText(error)
} finally {
loading.value = false
}
}
const openBind = async (item: KingdeeAccountBinding) => {
if (!canBindRow(item)) return
adding.value = false
selectedEmployeeOpenId.value = item.employeeOpenId
editing.value = item
form.kingdeeUsername = item.kingdeeUsername || ''
form.kingdeePassword = ''
form.verifyLogin = true
await nextTick()
bindDialog.value?.showModal()
}
const openCreate = async () => {
if (!canCreateBinding.value || !addCandidates.value.length) return
adding.value = true
editing.value = null
selectedEmployeeOpenId.value = addCandidates.value[0].employeeOpenId
form.kingdeeUsername = ''
form.kingdeePassword = ''
form.verifyLogin = true
await nextTick()
bindDialog.value?.showModal()
}
const closeBind = () => {
if (saving.value) return
bindDialog.value?.close()
adding.value = false
selectedEmployeeOpenId.value = null
editing.value = null
}
const saveBind = async () => {
const target = activeBinding.value
if (!target || !canBindRow(target) || !canEditCredentials.value || saving.value) return
saving.value = true
let savedSuccessfully = false
try {
const saved = await kingdeeAccountsApi.bind(target.employeeOpenId, {
kingdeeUsername: form.kingdeeUsername,
kingdeePassword: form.kingdeePassword,
verifyLogin: form.verifyLogin,
})
const index = items.value.findIndex(item => item.employeeOpenId === saved.employeeOpenId)
if (index >= 0) items.value[index] = saved
else items.value.push(saved)
savedSuccessfully = true
ElMessage.success('金蝶账号已绑定')
} catch (error) {
ElMessage.error(`绑定失败:${errorText(error)}`)
} finally {
saving.value = false
if (savedSuccessfully) closeBind()
}
}
const testBinding = async (item: KingdeeAccountBinding) => {
if (!canTestRow(item) || testingId.value != null) return
testingId.value = item.employeeOpenId
try {
const result = await kingdeeAccountsApi.test(item.employeeOpenId)
if (result?.success) ElMessage.success(result.message || '验证成功')
else ElMessage.error(result?.message || '验证失败')
} catch (error) {
ElMessage.error(`验证失败:${errorText(error)}`)
} finally {
testingId.value = null
}
}
const unbind = async (item: KingdeeAccountBinding) => {
if (!canUnbindRow(item)) return
try {
await ElMessageBox.confirm(`确认解绑 ${item.realName} 的金蝶账号?解绑后该员工将无法变更金蝶财务单据。`, '解绑确认', {
type: 'warning',
confirmButtonText: '解绑',
cancelButtonText: '取消',
})
} catch {
return
}
try {
await kingdeeAccountsApi.unbind(item.employeeOpenId)
const index = items.value.findIndex(row => row.employeeOpenId === item.employeeOpenId)
if (index >= 0) {
items.value[index] = {
...items.value[index],
bound: false,
kingdeeUsername: null,
boundAt: null,
updatedAt: null,
boundBy: null,
}
}
ElMessage.success('已解绑')
} catch (error) {
ElMessage.error(`解绑失败:${errorText(error)}`)
}
}
onMounted(() => { if (canViewAny.value) void load() })
defineExpose({ reload: load })
</script>
<style scoped>
.kingdee-bind-panel.embedded .panel-card {
box-shadow: none;
border: 1px solid var(--line);
}
.filter-hint { color: var(--muted); font-size: 12px; }
.self-mark { margin-left: 6px; color: var(--muted); font-size: 11px; }
.check-option.full { grid-column: 1 / -1; display: flex; align-items: center; gap: 8px; margin-top: 4px; }
</style>