feat: 账套模式全量接入 — 12个API entity_id统一走token解析链 + 前端登录选公司/切换器重签token
- 后端: kpis/bsc_layers/cash/predict/growth_quality/tax_compliance/data 的 entity_id 参数统一改为 Depends(get_entity_id) - 前端: 拦截器删除自动附加X-Entity-Id/entity_id; 登录页公司选择器(按用户名授权过滤); 切换器改POST /auth/switch-entity重新签发token+整页刷新; ReportCenter同步改造 - 修复前后端不匹配: login-entities路由→/auth/entities; login响应entity_id取user.entity_id
This commit is contained in:
@@ -4,7 +4,17 @@
|
||||
<h1>管理会计OS</h1>
|
||||
<p class="subtitle">博海网络科技</p>
|
||||
<el-form ref="formRef" :model="form" label-width="0" @keyup.enter="handleLogin">
|
||||
<el-form-item><el-input v-model="form.username" placeholder="用户名" size="large" /></el-form-item>
|
||||
<el-form-item>
|
||||
<el-input v-model="form.username" placeholder="用户名" size="large" @input="onUsernameChange" />
|
||||
</el-form-item>
|
||||
<el-form-item>
|
||||
<el-select v-model="form.entity_id" placeholder="选择登录公司" size="large" style="width:100%" :disabled="entities.length === 0">
|
||||
<el-option v-for="e in entities" :key="e.id" :label="e.short_name || e.name" :value="e.id">
|
||||
<span>{{ e.short_name || e.name }}</span>
|
||||
<span style="float:right;color:#999;font-size:12px;">{{ e.name }}</span>
|
||||
</el-option>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
<el-form-item><el-input v-model="form.password" type="password" placeholder="密码" size="large" show-password /></el-form-item>
|
||||
<el-form-item><el-button type="primary" size="large" style="width:100%" :loading="loading" @click="handleLogin">登 录</el-button></el-form-item>
|
||||
</el-form>
|
||||
@@ -13,27 +23,53 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, reactive } from 'vue'
|
||||
import { ref, reactive, onMounted } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { authApi } from '../api/index'
|
||||
|
||||
const router = useRouter()
|
||||
const loading = ref(false)
|
||||
const form = reactive({ username: 'admin', password: 'admin123' })
|
||||
const entities = ref<any[]>([])
|
||||
const form = reactive({ username: 'admin', password: 'admin123', entity_id: undefined as number | undefined })
|
||||
|
||||
async function loadEntities(username: string) {
|
||||
try {
|
||||
const r: any = await authApi.loginEntities(username)
|
||||
entities.value = r.data || []
|
||||
// 默认选中第一个授权企业
|
||||
if (entities.value.length > 0 && (form.entity_id === undefined || !entities.value.some((e: any) => e.id === form.entity_id))) {
|
||||
form.entity_id = entities.value[0].id
|
||||
}
|
||||
} catch (_) {
|
||||
entities.value = []
|
||||
}
|
||||
}
|
||||
|
||||
function onUsernameChange(val: string) {
|
||||
if (val && val.trim()) loadEntities(val.trim())
|
||||
}
|
||||
|
||||
async function handleLogin() {
|
||||
if (form.entity_id === undefined) {
|
||||
ElMessage.warning('请选择登录公司')
|
||||
return
|
||||
}
|
||||
loading.value = true
|
||||
try {
|
||||
const res: any = await authApi.login(form)
|
||||
const res: any = await authApi.login({ ...form, entity_id: Number(form.entity_id) })
|
||||
localStorage.setItem('cma_token', res.token)
|
||||
localStorage.setItem('cma_user', JSON.stringify(res.user))
|
||||
localStorage.setItem('cma_entity_id', String(res.user?.entity_id ?? 1))
|
||||
localStorage.setItem('cma_entity_name', res.user?.entity_short_name || res.user?.entity_name || '')
|
||||
router.push('/dashboard')
|
||||
} catch (e: any) {
|
||||
ElMessage.error(e?.response?.data?.detail || '登录失败')
|
||||
}
|
||||
loading.value = false
|
||||
}
|
||||
|
||||
onMounted(() => loadEntities(form.username))
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
@@ -587,7 +587,7 @@ function getEntityId() {
|
||||
|
||||
async function loadEntities() {
|
||||
try {
|
||||
const r: any = await api.get('/entities')
|
||||
const r: any = await api.get('/auth/my-entities')
|
||||
entities.value = r.data || r || []
|
||||
const e = entities.value.find((x: any) => x.id === currentEntityId.value)
|
||||
if (e) localStorage.setItem('cma_entity_name', e.short_name || e.name || '')
|
||||
@@ -596,13 +596,19 @@ async function loadEntities() {
|
||||
}
|
||||
}
|
||||
|
||||
function onEntityChange(id: number) {
|
||||
localStorage.setItem('cma_entity_id', String(id))
|
||||
const e = entities.value.find((x: any) => x.id === id)
|
||||
if (e) localStorage.setItem('cma_entity_name', e.short_name || e.name || '')
|
||||
ElMessage.success(`已切换企业:${e ? e.short_name + ' / ' + e.name : id}`)
|
||||
loadAll()
|
||||
loadStatutory()
|
||||
async function onEntityChange(id: number) {
|
||||
// 账套模式:切换走 switch-entity 重新签发token + 整页刷新
|
||||
if (id === Number(localStorage.getItem('cma_entity_id'))) return
|
||||
try {
|
||||
const r: any = await api.post('/auth/switch-entity', { entity_id: id })
|
||||
localStorage.setItem('cma_token', r.token)
|
||||
localStorage.setItem('cma_entity_id', String(r.entity_id))
|
||||
localStorage.setItem('cma_entity_name', r.entity_short_name || r.entity_name || '')
|
||||
ElMessage.success(`已切换企业:${r.entity_short_name || r.entity_name || id}`)
|
||||
location.reload()
|
||||
} catch (e: any) {
|
||||
ElMessage.error(e?.response?.data?.detail || '切换失败')
|
||||
}
|
||||
}
|
||||
|
||||
// 跨页面同步:其他页面切换企业时联动刷新
|
||||
|
||||
Reference in New Issue
Block a user