Files
smt-used-car-pc/src/views/auth/feishu-callback.vue
T
2026-03-12 16:48:21 +08:00

97 lines
2.5 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="feishu-callback">
<div v-if="loading">正在登录,请稍候...</div>
<div v-else-if="error">{{ error }}</div>
<div v-else>登录成功,正在跳转...</div>
</div>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/store/modules/user'
import { UserService } from '@/api/usersApi'
defineOptions({ name: 'FeishuCallback' })
// 角色设置辅助函数
const getRoles = (role: number, role_wp: number | null) => {
const roles: string[] = []
// 基础角色
if (role == 1) {
roles.push('business')
} else if (role == 2) {
roles.push('procurement')
}
// 根据 role_wp 添加额外角色
if (role_wp == 1) {
roles.push('businesspro')
} else if (role_wp == 2) {
roles.push('supplier')
} else if (role_wp == 3) {
roles.push('adminpro')
}
return roles
}
const loading = ref(true)
const error = ref('')
const userStore = useUserStore()
const router = useRouter()
onMounted(async () => {
const urlParams = new URLSearchParams(window.location.search)
const code = urlParams.get('code')
const app_id = localStorage.getItem('app_id')
if (!code) {
error.value = '未获取到飞书授权 code'
loading.value = false
return
}
if (!app_id) {
error.value = '未找到 app_id,请重新登录'
loading.value = false
setTimeout(() => router.replace('/'), 2000)
return
}
try {
const result = await UserService.login({
feishu_code: code,
app_id: app_id,
is_pc: 1
})
const data = result.data
// 存储token和用户信息
userStore.setToken(data.union_id)
// 设置角色(支持多角色数组)
const roles = getRoles(data.role, data.role_wp)
localStorage.setItem('role', JSON.stringify(roles))
const userInfo = await UserService.getUserInfo()
userStore.setUserInfo(userInfo)
userStore.setLoginStatus(true)
router.push(data.home)
} catch (e: any) {
error.value = e?.response?.data?.message || e?.message || '登录失败,请重试'
setTimeout(() => router.replace('/'), 2000)
} finally {
loading.value = false
}
})
</script>
<style scoped>
.feishu-callback {
min-height: 200px;
display: flex;
align-items: center;
justify-content: center;
font-size: 18px;
color: #333;
}
</style>