修改用户授权逻辑先静默授权,点击我的时才非静默授权
Uni-app H5 Deploy (Prod + Staging) / deploy (release) Successful in 51s

This commit is contained in:
kk
2026-07-10 18:34:07 +08:00
parent 20649c67a9
commit 0fc854cd33
4 changed files with 159 additions and 97 deletions
+72 -19
View File
@@ -2,11 +2,6 @@
<view class="app-container">
<!-- ========== 首页内容 ========== -->
<view v-if="currentTab === 'home'" class="page-content home-page">
<!-- 顶部标题栏 -->
<view class="page-header">
<text class="page-title">智能货柜{{ deviceId ? '-' + deviceId : '' }}</text>
</view>
<scroll-view
class="product-area"
scroll-y
@@ -168,7 +163,7 @@
<script>
import PageNavBar from '@/components/page-nav-bar/page-nav-bar.vue';
import config from '@/config/env.js';
import { get } from '@/utils/request.js';
import { get, gatewayGet } from '@/utils/request.js';
import { checkLoginWithPrompt, getCachedUserInfo } from '@/utils/auth-guard.js';
import { isVConsoleEnabled, showVConsole, hideVConsole } from '@/utils/vconsole';
@@ -236,10 +231,14 @@ export default {
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 跳转,提示并跳至扫码页
@@ -272,6 +271,8 @@ export default {
// 记录来源:从 tab 切换的
if (tab === 'mine') {
this.fromPage = 'tab';
// 获取用户信息
this.fetchUserInfo();
}
},
switchCategory(cat) { this.activeCategory = cat; },
@@ -291,6 +292,71 @@ 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 模式下,从缓存读取用户信息
if (config.mockWechatLogin) {
console.log('[index] Mock 模式,从缓存读取用户信息');
const cached = getCachedUserInfo();
if (cached) {
// 有缓存(注册后存储的),直接使用
this.userInfo = { ...this.userInfo, ...cached };
} else {
// 无缓存,使用 mock 数据(已注册用户)
this.userInfo = {
nickname: config.mockUser.user_info.nickname || '',
phone: config.mockUser.user_info.phone || '',
avatar: config.mockUser.user_info.avatar || '',
};
uni.setStorageSync('user_info', this.userInfo);
}
return;
}
try {
// 调用接口获取用户信息
const res = await gatewayGet('/api/v1/user/info', {
app_no: appNo,
});
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 });
} else {
console.warn('[index] fetchUserInfo failed:', data.message);
}
} catch (e) {
console.error('[index] fetchUserInfo error:', e);
}
},
async fetchProducts() {
this.loading = true;
@@ -405,19 +471,6 @@ page { height: 100%; background-color: #fff; }
/* ====== 首页样式 ====== */
.home-page { display: flex; flex-direction: column; height: 100%; }
/* 顶部标题栏 */
.page-header {
padding: 24rpx 32rpx;
background-color: #fff;
border-bottom: 1rpx solid #f0f0f0;
}
.page-title {
font-size: 32rpx;
font-weight: bold;
color: #333;
}
.product-area { flex: 1; overflow-y: auto; height: 0; }
/* 热销商品标题 + 装饰 */