This commit is contained in:
+38
-41
@@ -1,6 +1,5 @@
|
||||
<script>
|
||||
import { initEnv } from '@/utils/env.js';
|
||||
import { handleAuthCallback, onAuthSuccess } from '@/utils/auth-guard.js';
|
||||
|
||||
export default {
|
||||
onLaunch: function(options) {
|
||||
@@ -10,22 +9,51 @@
|
||||
initEnv();
|
||||
|
||||
// #ifdef H5
|
||||
// 2. 处理授权回调(URL 中带 code/auth_code 参数)
|
||||
this.handleAuthRedirect();
|
||||
|
||||
// 3. 解析 URL 路径中的设备编号(如 /A1036)
|
||||
// 2. 解析 URL 路径中的设备编号(如 /A1036)
|
||||
let deviceId = '';
|
||||
const path = window.location.pathname;
|
||||
const segments = path.split('/').filter(Boolean);
|
||||
if (segments.length > 0) {
|
||||
const last = segments[segments.length - 1];
|
||||
// 匹配设备编号格式:大写字母开头 + 大写字母/数字
|
||||
if (/^[A-Z][A-Z0-9]+$/.test(last)) {
|
||||
uni.redirectTo({
|
||||
url: '/pages/index/index?device_id=' + last
|
||||
});
|
||||
return;
|
||||
deviceId = last;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 检查 URL 中是否有授权回调的 code
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const code = urlParams.get('code');
|
||||
|
||||
if (code) {
|
||||
// 有 code → 进入授权 Loading 页处理
|
||||
const loadingUrl = deviceId
|
||||
? `/pages/auth/loading?device_id=${deviceId}`
|
||||
: '/pages/auth/loading';
|
||||
uni.redirectTo({ url: loadingUrl });
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. 检查 JWT
|
||||
const token = uni.getStorageSync('token');
|
||||
|
||||
if (token) {
|
||||
// 有 JWT → 直接进入首页
|
||||
const homeUrl = deviceId
|
||||
? `/pages/index/index?device_id=${deviceId}`
|
||||
: '/pages/index/scan';
|
||||
uni.redirectTo({ url: homeUrl });
|
||||
} else {
|
||||
// 无 JWT → 进入授权 Loading 页
|
||||
const loadingUrl = deviceId
|
||||
? `/pages/auth/loading?device_id=${deviceId}`
|
||||
: '/pages/auth/loading';
|
||||
uni.redirectTo({ url: loadingUrl });
|
||||
}
|
||||
// #endif
|
||||
|
||||
// #ifndef H5
|
||||
// 小程序环境:直接进入扫码页
|
||||
uni.redirectTo({ url: '/pages/index/scan' });
|
||||
// #endif
|
||||
},
|
||||
onShow: function() {
|
||||
@@ -33,37 +61,6 @@
|
||||
},
|
||||
onHide: function() {
|
||||
console.log('App Hide')
|
||||
},
|
||||
methods: {
|
||||
// #ifdef H5
|
||||
async handleAuthRedirect() {
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const code = urlParams.get('code') || urlParams.get('auth_code') || '';
|
||||
|
||||
if (!code) return;
|
||||
|
||||
// 有授权码,调用后端回调接口
|
||||
const result = await handleAuthCallback();
|
||||
if (result) {
|
||||
onAuthSuccess(result);
|
||||
|
||||
// 清除 URL 中的 code 参数,避免刷新重复处理
|
||||
const url = new URL(window.location.href);
|
||||
url.searchParams.delete('code');
|
||||
url.searchParams.delete('auth_code');
|
||||
url.searchParams.delete('state');
|
||||
window.history.replaceState({}, '', url.toString());
|
||||
|
||||
// 如果需要绑定手机号
|
||||
if (result.need_bindphone) {
|
||||
uni.setStorageSync('temp_token', result.temp_token);
|
||||
uni.showToast({ title: '请绑定手机号完成注册', icon: 'none' });
|
||||
} else {
|
||||
uni.showToast({ title: '登录成功', icon: 'success' });
|
||||
}
|
||||
}
|
||||
}
|
||||
// #endif
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user