提交
This commit is contained in:
@@ -0,0 +1,272 @@
|
||||
<template>
|
||||
<view class="auth-page">
|
||||
<view class="auth-card">
|
||||
<view v-if="status === 'loading'">
|
||||
<view class="spinner"></view>
|
||||
<view class="text-wrap">
|
||||
<text class="title">{{ title }}</text>
|
||||
<text class="desc">{{ desc }}<text class="dots">{{ dots }}</text></text>
|
||||
</view>
|
||||
<view class="progress">
|
||||
<view class="bar"></view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view v-else class="error-wrap">
|
||||
<uni-icons type="clear" size="44" color="#ff4d4f"></uni-icons>
|
||||
<text class="title">登录失败</text>
|
||||
<text class="desc">{{ errorMsg }}</text>
|
||||
<button class="retry-btn" @click="retry">重试</button>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { request } from '@/api/apiT.js'
|
||||
import { saveRoles } from '@/common/role.js'
|
||||
import { getVisibleTabBar } from '@/common/tabbar.js'
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
status: 'loading',
|
||||
title: '正在完成登录',
|
||||
desc: '处理中',
|
||||
dots: '',
|
||||
errorMsg: '请稍后重试',
|
||||
_dotTimer: null,
|
||||
minStayMs: 600,
|
||||
_startedAt: 0,
|
||||
_code: ''
|
||||
}
|
||||
},
|
||||
onLoad(options) {
|
||||
this._startedAt = Date.now()
|
||||
this.startDots()
|
||||
// #ifdef H5
|
||||
this._code = options.code || ''
|
||||
// 支持从回调参数里带 redirect(有些场景会拼到回调地址上)
|
||||
const redirect = options.redirect || options.r || ''
|
||||
if (redirect) {
|
||||
try {
|
||||
uni.setStorageSync('post_login_redirect', decodeURIComponent(redirect))
|
||||
} catch (e) {
|
||||
uni.setStorageSync('post_login_redirect', redirect)
|
||||
}
|
||||
}
|
||||
// #endif
|
||||
|
||||
if (!this._code) {
|
||||
this.fail('缺少 code 参数')
|
||||
return
|
||||
}
|
||||
this.logins(this._code)
|
||||
},
|
||||
onUnload() {
|
||||
this.stopDots()
|
||||
},
|
||||
methods: {
|
||||
startDots() {
|
||||
this.stopDots()
|
||||
let n = 0
|
||||
this._dotTimer = setInterval(() => {
|
||||
n = (n + 1) % 4
|
||||
this.dots = '.'.repeat(n)
|
||||
}, 350)
|
||||
},
|
||||
stopDots() {
|
||||
if (this._dotTimer) {
|
||||
clearInterval(this._dotTimer)
|
||||
this._dotTimer = null
|
||||
}
|
||||
},
|
||||
fail(msg) {
|
||||
this.status = 'error'
|
||||
this.errorMsg = msg || '请稍后重试'
|
||||
uni.showToast({ title: msg || '登录失败', icon: 'none' })
|
||||
},
|
||||
async logins(code) {
|
||||
try {
|
||||
this.title = '正在交换登录凭证'
|
||||
this.desc = '请稍候'
|
||||
|
||||
const res = await request({
|
||||
url: 'common/auth/login',
|
||||
method: 'POST',
|
||||
loginType: 'feishu', // 飞书登录
|
||||
data: { feishu_code: code, app_id: uni.getStorageSync('fsappid'), is_pc: 0 }
|
||||
})
|
||||
|
||||
if (res.data.code !== 200) {
|
||||
throw new Error(res.data.message || '登录失败')
|
||||
}
|
||||
|
||||
const data = res.data.data
|
||||
|
||||
// 保存登录类型
|
||||
uni.setStorageSync('loginType', 'feishu')
|
||||
|
||||
// 统一使用 union_id 作为 token(与PC端保持一致)
|
||||
const token = data.union_id || data.token
|
||||
if (!token) {
|
||||
throw new Error('未获取到 token')
|
||||
}
|
||||
|
||||
uni.setStorageSync('token', token)
|
||||
|
||||
// 保存用户信息
|
||||
const userInfo = {
|
||||
...data,
|
||||
token: token
|
||||
}
|
||||
uni.setStorageSync('userInfo', userInfo)
|
||||
|
||||
// 处理角色信息(统一使用 role.js 工具)
|
||||
if (data.role) {
|
||||
saveRoles(data.role, data.role_wp)
|
||||
|
||||
// 通知自定义 TabBar 刷新
|
||||
uni.$emit && uni.$emit('userRoleChanged')
|
||||
}
|
||||
|
||||
this.title = '登录成功'
|
||||
this.desc = '正在进入页面'
|
||||
|
||||
this.goRedirectSmooth()
|
||||
} catch (err) {
|
||||
console.error('[feishu callback] error:', err)
|
||||
this.fail(err?.message || '登录失败')
|
||||
}
|
||||
},
|
||||
goRedirectSmooth() {
|
||||
const elapsed = Date.now() - this._startedAt
|
||||
const wait = Math.max(0, this.minStayMs - elapsed)
|
||||
setTimeout(() => {
|
||||
// 获取用户可见的TabBar列表
|
||||
const visibleTabs = getVisibleTabBar()
|
||||
// 检查是否包含首页Tab
|
||||
const hasIndexTab = visibleTabs.some(tab => tab.pagePath === 'pages/index/index')
|
||||
// 默认跳转目标:有首页进入首页,否则进入我的页面
|
||||
const defaultTarget = hasIndexTab ? '/pages/index/index' : '/pages/index/mine'
|
||||
// 优先使用存储的跳转地址
|
||||
const target = uni.getStorageSync('post_login_redirect') || defaultTarget
|
||||
uni.removeStorageSync('post_login_redirect')
|
||||
uni.reLaunch({ url: target })
|
||||
}, wait)
|
||||
},
|
||||
retry() {
|
||||
this.status = 'loading'
|
||||
this.title = '正在完成登录'
|
||||
this.desc = '处理中'
|
||||
this.startDots()
|
||||
if (this._code) this.logins(this._code)
|
||||
else this.fail('缺少 code 参数')
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.auth-page {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 40rpx;
|
||||
box-sizing: border-box;
|
||||
background: linear-gradient(180deg, #FF972F 0%, #F4F6F8 38%, #F4F6F8 100%);
|
||||
}
|
||||
|
||||
.auth-card {
|
||||
width: 100%;
|
||||
max-width: 640rpx;
|
||||
background: rgba(255, 255, 255, 0.92);
|
||||
border-radius: 20rpx;
|
||||
padding: 44rpx 36rpx 32rpx;
|
||||
box-shadow: 0 10rpx 40rpx rgba(0, 0, 0, 0.08);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
backdrop-filter: blur(10px);
|
||||
}
|
||||
|
||||
.spinner {
|
||||
width: 88rpx;
|
||||
height: 88rpx;
|
||||
border-radius: 50%;
|
||||
border: 8rpx solid rgba(255,151,47, 0.18);
|
||||
border-top-color: #FF972F;
|
||||
animation: spin 0.9s linear infinite;
|
||||
margin-bottom: 24rpx;
|
||||
}
|
||||
|
||||
.text-wrap {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.title {
|
||||
display: block;
|
||||
font-size: 34rpx;
|
||||
color: #1f2937;
|
||||
font-weight: 600;
|
||||
margin-bottom: 8rpx;
|
||||
}
|
||||
|
||||
.desc {
|
||||
display: block;
|
||||
font-size: 26rpx;
|
||||
color: #6b7280;
|
||||
}
|
||||
|
||||
.dots {
|
||||
display: inline-block;
|
||||
width: 40rpx;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.progress {
|
||||
width: 100%;
|
||||
height: 10rpx;
|
||||
border-radius: 999rpx;
|
||||
background: rgba(255,151,47, 0.10);
|
||||
overflow: hidden;
|
||||
margin-top: 26rpx;
|
||||
}
|
||||
|
||||
.progress .bar {
|
||||
height: 100%;
|
||||
width: 40%;
|
||||
border-radius: 999rpx;
|
||||
background: rgba(255,151,47, 0.55);
|
||||
animation: move 1.1s ease-in-out infinite;
|
||||
}
|
||||
|
||||
.error-wrap {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.retry-btn {
|
||||
margin-top: 24rpx;
|
||||
width: 240rpx;
|
||||
height: 72rpx;
|
||||
line-height: 72rpx;
|
||||
border-radius: 999rpx;
|
||||
background: #FF972F;
|
||||
color: #fff;
|
||||
font-size: 28rpx;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
to { transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
@keyframes move {
|
||||
0% { transform: translateX(-60%); }
|
||||
50% { transform: translateX(140%); }
|
||||
100% { transform: translateX(-60%); }
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user