修改授权逻辑
Uni-app H5 Deploy (Prod + Staging) / deploy (release) Successful in 48s

This commit is contained in:
kk
2026-07-11 10:28:43 +08:00
parent 0fc854cd33
commit 7b2e4dc72e
4 changed files with 226 additions and 79 deletions
+49 -54
View File
@@ -164,7 +164,7 @@
import PageNavBar from '@/components/page-nav-bar/page-nav-bar.vue';
import config from '@/config/env.js';
import { get, gatewayGet } from '@/utils/request.js';
import { checkLoginWithPrompt, getCachedUserInfo } from '@/utils/auth-guard.js';
import { checkLoginWithPrompt, getCachedUserInfo, isLoggedIn } from '@/utils/auth-guard.js';
import { isVConsoleEnabled, showVConsole, hideVConsole } from '@/utils/vconsole';
export default {
@@ -226,22 +226,24 @@ export default {
this.isWechat = /MicroMessenger/i.test(navigator.userAgent);
// #endif
// 处理"我的"用户信息授权回调(从 loading 页跳回)
if (options.action === 'mine') {
this.currentTab = 'mine';
this.fromPage = 'navigate';
}
// 支持通过 tab 参数切换到"我的"页面
if (options.tab === 'mine') {
this.currentTab = 'mine';
// 记录来源:从其他页面跳转过来的
this.fromPage = 'navigate';
// 获取用户信息
this.fetchUserInfo();
}
if (options.device_id) {
this.deviceId = options.device_id;
// 动态设置导航栏标题
uni.setNavigationBarTitle({ title: `智能货柜-${this.deviceId}` });
this.fetchProducts();
} else if (!options.tab) {
// 无设备编号且非 tab 跳转,提示并跳至扫码页
} else if (!options.tab && options.action !== 'mine') {
// 无设备编号且非 tab/action 跳转,提示并跳至扫码页
uni.showToast({ title: '请扫描设备二维码', icon: 'none' });
setTimeout(() => {
uni.redirectTo({ url: '/pages/index/scan' });
@@ -265,14 +267,20 @@ export default {
},
methods: {
switchTab(tab) {
// 切换到"我的"Tab 时检查登录
if (tab === 'mine' && !checkLoginWithPrompt('mine')) return;
this.currentTab = tab;
// 记录来源:从 tab 切换的
if (tab === 'mine') {
// 切换到"我的"Tab 时检查登录
if (!checkLoginWithPrompt('mine')) return;
this.currentTab = tab;
this.fromPage = 'tab';
// 获取用户信息
this.fetchUserInfo();
// 已有缓存的用户信息则直接显示,否则发起非静默授权获取
const cached = getCachedUserInfo();
if (cached && cached.nickname) {
this.userInfo = { ...this.userInfo, ...cached };
} else {
this.startUserInfoAuth();
}
} else {
this.currentTab = tab;
}
},
switchCategory(cat) { this.activeCategory = cat; },
@@ -292,24 +300,14 @@ export default {
}
console.log('[index] this.userInfo:', JSON.stringify(this.userInfo, null, 2));
},
// 从接口获取用户信息(点击"我的"时调用
async fetchUserInfo() {
const appNo = uni.getStorageSync('app_no') || config.mockAppNo;
if (!appNo) {
console.warn('[index] 无法获取应用信息');
return;
}
// Mock 模式下,从缓存读取用户信息
// 发起非静默授权获取用户信息(snsapi_userinfo
async startUserInfoAuth() {
// Mock 模式下,直接使用 mock 数据
if (config.mockWechatLogin) {
console.log('[index] Mock 模式,从缓存读取用户信息');
const cached = getCachedUserInfo();
if (cached) {
// 有缓存(注册后存储的),直接使用
if (cached && cached.nickname) {
this.userInfo = { ...this.userInfo, ...cached };
} else {
// 无缓存,使用 mock 数据(已注册用户)
this.userInfo = {
nickname: config.mockUser.user_info.nickname || '',
phone: config.mockUser.user_info.phone || '',
@@ -320,41 +318,38 @@ export default {
return;
}
const appNo = uni.getStorageSync('app_no');
if (!appNo) {
console.warn('[index] 无法获取应用信息');
return;
}
try {
// 调用接口获取用户信息
const res = await gatewayGet('/api/v1/user/info', {
// 回调地址:loading 页处理后跳回首页(带 action 和 device_id
const baseUri = config.authRedirectUri || window.location.origin + '/pages/auth/loading';
const redirectUriObj = new URL(baseUri);
redirectUriObj.searchParams.set('action', 'userinfo');
if (this.deviceId) {
redirectUriObj.searchParams.set('device_id', this.deviceId);
}
const redirectUri = redirectUriObj.toString();
// 获取非静默授权链接(会弹窗确认)
const res = await gatewayGet('/api/v1/user/auth/url', {
app_no: appNo,
scopes: 'snsapi_userinfo',
redirect_uri: redirectUri,
});
const data = res.data || {};
console.log('[index] fetchUserInfo response:', JSON.stringify(data, null, 2));
if (data.code === 0 && data.data) {
const userInfo = data.data;
this.userInfo = {
nickname: userInfo.nickname || '',
phone: userInfo.phone || '',
avatar: userInfo.avatar || '',
};
// 存入缓存
uni.setStorageSync('user_info', this.userInfo);
console.log('[index] fetchUserInfo success:', JSON.stringify(this.userInfo, null, 2));
} else if (data.code === 1010) {
// 用户未注册,需要重新走授权流程获取 temp_token
console.warn('[index] 用户未注册,重新授权');
uni.removeStorageSync('token');
uni.removeStorageSync('user_info');
// 跳转到 loading 页重新授权(会获取 temp_token 后跳注册页)
const deviceId = this.deviceId || '';
const loadingUrl = deviceId
? `/pages/auth/loading?device_id=${deviceId}`
: '/pages/auth/loading';
uni.redirectTo({ url: loadingUrl });
if (data.code === 0 && data.data && data.data.auth_url) {
// 跳转微信授权页(弹窗确认)
window.location.href = data.data.auth_url;
} else {
console.warn('[index] fetchUserInfo failed:', data.message);
console.warn('[index] 获取授权链接失败:', data.message);
}
} catch (e) {
console.error('[index] fetchUserInfo error:', e);
console.error('[index] startUserInfoAuth error:', e);
}
},
async fetchProducts() {