Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0fc854cd33 | |||
| 20649c67a9 | |||
| 3a154198e9 | |||
| c84c9df5d4 | |||
| 71606ebfe5 | |||
| a7e0366fb7 | |||
| 5bb31516a0 | |||
| 2560d9d529 | |||
| 6534723050 | |||
| f031501286 |
@@ -0,0 +1 @@
|
||||
2K94E8Rn63mn6Nys
|
||||
+39
-8
@@ -1,16 +1,37 @@
|
||||
<script>
|
||||
import { initEnv } from '@/utils/env.js';
|
||||
import config from '@/config/env.js';
|
||||
|
||||
export default {
|
||||
onLaunch: function(options) {
|
||||
console.log('App Launch')
|
||||
|
||||
// 0. Mock 模式:仅首次进入时清除登录状态(刷新时保留)
|
||||
if (config.mockWechatLogin) {
|
||||
const mockInitialized = uni.getStorageSync('__mock_initialized');
|
||||
if (!mockInitialized) {
|
||||
uni.removeStorageSync('token');
|
||||
uni.removeStorageSync('user_info');
|
||||
uni.removeStorageSync('temp_token');
|
||||
uni.removeStorageSync('auth_info');
|
||||
uni.setStorageSync('__mock_initialized', 'true');
|
||||
console.log('[Mock] 首次进入,清除登录状态');
|
||||
} else {
|
||||
console.log('[Mock] 刷新页面,保留登录状态');
|
||||
}
|
||||
}
|
||||
|
||||
// 1. 环境检测 + 缓存 app_no/platform
|
||||
initEnv();
|
||||
|
||||
// #ifdef H5
|
||||
// 2. 解析 URL 路径中的设备编号(如 /A1036)
|
||||
// 2. 解析设备编号:优先从 query 参数获取,其次从路径解析
|
||||
let deviceId = '';
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
deviceId = urlParams.get('device_id') || '';
|
||||
|
||||
// 兜底:从路径解析(如 /pages/index/index/N1154)
|
||||
if (!deviceId) {
|
||||
const path = window.location.pathname;
|
||||
const segments = path.split('/').filter(Boolean);
|
||||
if (segments.length > 0) {
|
||||
@@ -19,29 +40,39 @@
|
||||
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';
|
||||
// 有 code → 进入授权 Loading 页处理,保留 code 参数
|
||||
let loadingUrl = '/pages/auth/loading?code=' + encodeURIComponent(code);
|
||||
if (deviceId) {
|
||||
loadingUrl += '&device_id=' + deviceId;
|
||||
}
|
||||
uni.redirectTo({ url: loadingUrl });
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. 检查 JWT
|
||||
// 4. 判断是否为根路径或设备编号路径(如 / 或 /N1154)
|
||||
const currentPath = window.location.pathname;
|
||||
const isRootOrDevice = currentPath === '/' || /^\/[A-Z][A-Z0-9]+$/.test(currentPath);
|
||||
|
||||
// 5. 检查 JWT
|
||||
const token = uni.getStorageSync('token');
|
||||
|
||||
if (token) {
|
||||
// 有 JWT → 直接进入首页
|
||||
if (isRootOrDevice) {
|
||||
// 根路径/设备编号路径 → 跳转首页
|
||||
const homeUrl = deviceId
|
||||
? `/pages/index/index?device_id=${deviceId}`
|
||||
: '/pages/index/scan';
|
||||
uni.redirectTo({ url: homeUrl });
|
||||
} else {
|
||||
// 其他业务页面 → 留在当前页面
|
||||
console.log('[App] 已登录,保留在当前页面');
|
||||
}
|
||||
} else {
|
||||
// 无 JWT → 进入授权 Loading 页
|
||||
const loadingUrl = deviceId
|
||||
|
||||
+22
-3
@@ -6,16 +6,35 @@ const dev = {
|
||||
apiBaseUrl: 'http://192.168.0.23:4000',
|
||||
|
||||
// 网关地址(vms-gateway,端口 4001)— 用户授权、短信等接口走网关
|
||||
apiGatewayUrl: 'https://gateway.arklinksmart.cn',
|
||||
apiGatewayUrl: 'http://192.168.0.23:4001',
|
||||
|
||||
// 授权回调地址(支付宝/微信 OAuth redirect_uri)
|
||||
authRedirectUri: 'https://gateway.arklinksmart.cn/api/v1/user/auth/callback',
|
||||
// 授权回调地址(微信 OAuth redirect_uri,必须 HTTPS,指向前端页面)
|
||||
authRedirectUri: 'https://m.arklinksmart.cn/pages/auth/loading',
|
||||
|
||||
// 开发阶段硬编码 Token(生产环境应从登录流程获取)
|
||||
bearerToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImRlZXB2ZW5kaW5nIiwic3ViIjoiMTY0MDI0MTEyNiIsImZyb20iOiJwb3MiLCJyb2xlIjoicG9zIiwibWFpbmJvYXJkX2lkIjoiVDEwMDIiLCJwb3Nfc24iOiIxNjQwMjQxMTI2IiwiaWF0IjoxNzc4NDkxOTA3LCJleHAiOjE3NzkwOTY3MDd9.DjscZtrTCr30XOngToUifxoel4q2EGES_uqGQernkvg',
|
||||
|
||||
// 是否开启请求日志
|
||||
enableRequestLog: true,
|
||||
|
||||
// Mock 微信登录(跳过 OAuth 流程)
|
||||
// 'skip' → 直接登录进首页
|
||||
// 'register' → 模拟新用户,走注册流程
|
||||
// 'login' → 模拟已注册用户,走登录流程
|
||||
// false → 走真实微信 OAuth
|
||||
mockWechatLogin: 'register',
|
||||
mockAppNo: '60000001102001',
|
||||
mockUser: {
|
||||
token: 'mock_token_dev_2024',
|
||||
user_info: {
|
||||
user_id: 'mock_user_001',
|
||||
phone: '13264706088',
|
||||
open_id: 'mock_openid_002',
|
||||
union_id: 'test_union_id',
|
||||
nickname: '测试用户',
|
||||
avatar: 'https://img.wewemivending.com/vms/us/img/uploads/2025/12/18/6943d37c7d82b3439.jpeg',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default dev;
|
||||
|
||||
+2
-2
@@ -8,8 +8,8 @@ const prod = {
|
||||
// 网关地址(vms-gateway)— 用户授权、短信等接口走网关
|
||||
apiGatewayUrl: 'https://gateway.arklinksmart.cn',
|
||||
|
||||
// 授权回调地址(支付宝/微信 OAuth redirect_uri)
|
||||
authRedirectUri: 'https://gateway.arklinksmart.cn/api/v1/user/auth/callback',
|
||||
// 授权回调地址(微信 OAuth redirect_uri,必须 HTTPS,指向前端页面)
|
||||
authRedirectUri: 'https://m.arklinksmart.cn/pages/auth/loading',
|
||||
|
||||
// 生产环境 Token 从登录流程获取,此处为空
|
||||
bearerToken: '',
|
||||
|
||||
+94
-11
@@ -21,9 +21,19 @@ export default {
|
||||
this.deviceId = options.device_id || '';
|
||||
|
||||
// #ifdef H5
|
||||
// 检查 URL 中是否有授权回调的 code
|
||||
// Mock 模式:跳过微信 OAuth,直接登录
|
||||
if (config.mockWechatLogin) {
|
||||
this.mockLogin();
|
||||
return;
|
||||
}
|
||||
|
||||
// 优先从页面参数获取 code(App.vue 跳转时带过来)
|
||||
// 兜底从 URL search 参数获取(微信直接回调时)
|
||||
let code = options.code || '';
|
||||
if (!code) {
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const code = urlParams.get('code');
|
||||
code = urlParams.get('code') || '';
|
||||
}
|
||||
|
||||
if (code) {
|
||||
// 有 code → 调用后端登录
|
||||
@@ -41,7 +51,7 @@ export default {
|
||||
},
|
||||
methods: {
|
||||
/**
|
||||
* 发起 OAuth 授权
|
||||
* 发起 OAuth 授权(静默授权,只获取 openid)
|
||||
*/
|
||||
async startAuth() {
|
||||
const platform = detectPlatform();
|
||||
@@ -56,20 +66,21 @@ export default {
|
||||
try {
|
||||
this.statusText = '正在获取授权...';
|
||||
|
||||
// 构建回调地址(当前页面地址)
|
||||
const redirectUri = encodeURIComponent(
|
||||
window.location.origin + window.location.pathname
|
||||
// 回调地址必须是 HTTPS,使用配置文件中的地址
|
||||
const redirectUri = config.authRedirectUri || (
|
||||
window.location.origin.replace(/^http:/, 'https:') + window.location.pathname
|
||||
);
|
||||
|
||||
// 使用 snsapi_base 静默授权,不弹窗
|
||||
const res = await gatewayGet('/api/v1/user/auth/url', {
|
||||
app_no: appNo,
|
||||
scopes: 'snsapi_userinfo',
|
||||
scopes: 'snsapi_base',
|
||||
redirect_uri: redirectUri,
|
||||
});
|
||||
|
||||
const data = res.data || {};
|
||||
if (data.code === 0 && data.data && data.data.auth_url) {
|
||||
// 跳转微信授权页
|
||||
// 跳转微信授权页(静默,不弹窗)
|
||||
window.location.href = data.data.auth_url;
|
||||
} else {
|
||||
uni.showToast({ title: data.message || '获取授权失败', icon: 'none' });
|
||||
@@ -83,7 +94,7 @@ export default {
|
||||
},
|
||||
|
||||
/**
|
||||
* 处理授权回调(从 URL 中获取 code 后调用后端 /login)
|
||||
* 处理授权回调(从 URL 中获取 code 后换取 openid,再用 openid 登录)
|
||||
*/
|
||||
async handleCallback(code) {
|
||||
const appNo = uni.getStorageSync('app_no') || getAppNo(detectPlatform());
|
||||
@@ -97,14 +108,18 @@ export default {
|
||||
try {
|
||||
this.statusText = '正在验证身份...';
|
||||
|
||||
// 用 code 换取 openid(后端处理,前端只传 code)
|
||||
const res = await gatewayPost('/api/v1/user/login', {
|
||||
app_no: appNo,
|
||||
code: code,
|
||||
});
|
||||
|
||||
const data = res.data || {};
|
||||
console.log('[loading] login response:', JSON.stringify(data, null, 2));
|
||||
|
||||
if (data.code === 0 && data.data) {
|
||||
const result = data.data;
|
||||
console.log('[loading] login result:', JSON.stringify(result, null, 2));
|
||||
|
||||
// 清除 URL 中的 code 参数,避免刷新重复处理
|
||||
const url = new URL(window.location.href);
|
||||
@@ -112,15 +127,17 @@ export default {
|
||||
url.searchParams.delete('state');
|
||||
window.history.replaceState({}, '', url.toString());
|
||||
|
||||
// 存储 openid
|
||||
uni.setStorageSync('openid', result.openid);
|
||||
|
||||
if (result.register) {
|
||||
// 已注册 → 缓存 JWT → 跳转首页
|
||||
uni.setStorageSync('token', result.token);
|
||||
uni.setStorageSync('user_info', result.user_info);
|
||||
// 不再存储 user_info,点击"我的"时再获取
|
||||
this.goToHome();
|
||||
} else {
|
||||
// 未注册 → 缓存临时数据 → 跳转注册页
|
||||
uni.setStorageSync('temp_token', result.temp_token);
|
||||
uni.setStorageSync('auth_info', result.auth_info);
|
||||
this.goToRegister();
|
||||
}
|
||||
} else {
|
||||
@@ -142,6 +159,14 @@ export default {
|
||||
},
|
||||
|
||||
goToRegister() {
|
||||
// 记录扫码时的原始 URL,注册完成后跳回
|
||||
const scanUrl = this.deviceId
|
||||
? `/pages/index/index?device_id=${this.deviceId}`
|
||||
: '';
|
||||
if (scanUrl) {
|
||||
uni.setStorageSync('__scan_redirect_url', scanUrl);
|
||||
}
|
||||
|
||||
const url = this.deviceId
|
||||
? `/pages/register/register?device_id=${this.deviceId}`
|
||||
: '/pages/register/register';
|
||||
@@ -151,6 +176,64 @@ export default {
|
||||
goToScan() {
|
||||
uni.redirectTo({ url: '/pages/index/scan' });
|
||||
},
|
||||
|
||||
/**
|
||||
* Mock 登录(跳过微信授权,调用真实登录接口)
|
||||
*/
|
||||
async mockLogin() {
|
||||
const mode = config.mockWechatLogin;
|
||||
const mock = config.mockUser;
|
||||
const mockOpenid = mock.user_info.open_id;
|
||||
|
||||
// 'skip' 模式直接登录,不调接口
|
||||
if (mode === 'skip') {
|
||||
this.statusText = '模拟登录中...';
|
||||
uni.setStorageSync('token', mock.token);
|
||||
uni.setStorageSync('openid', mockOpenid);
|
||||
console.log('[Auth] Mock skip → home');
|
||||
setTimeout(() => this.goToHome(), 500);
|
||||
return;
|
||||
}
|
||||
|
||||
// 'register' / 'login' 模式:调用真实登录接口,传 mock openid
|
||||
this.statusText = '模拟:调用登录接口...';
|
||||
const appNo = uni.getStorageSync('app_no') || config.mockAppNo;
|
||||
|
||||
try {
|
||||
const res = await gatewayPost('/api/v1/user/login', {
|
||||
app_no: appNo,
|
||||
openid: mockOpenid,
|
||||
});
|
||||
|
||||
const data = res.data || {};
|
||||
console.log('[Auth] Mock login response:', JSON.stringify(data, null, 2));
|
||||
|
||||
if (data.code === 0 && data.data) {
|
||||
const result = data.data;
|
||||
|
||||
// 存储 openid
|
||||
uni.setStorageSync('openid', result.openid || mockOpenid);
|
||||
|
||||
if (result.register) {
|
||||
// 已注册 → 存 token → 跳首页
|
||||
uni.setStorageSync('token', result.token);
|
||||
console.log('[Auth] Mock: existing user → home');
|
||||
this.goToHome();
|
||||
} else {
|
||||
// 未注册 → 存 temp_token → 跳注册页
|
||||
uni.setStorageSync('temp_token', result.temp_token);
|
||||
console.log('[Auth] Mock: new user → register');
|
||||
this.goToRegister();
|
||||
}
|
||||
} else {
|
||||
uni.showToast({ title: data.message || '登录失败', icon: 'none' });
|
||||
console.error('[Auth] Mock login failed:', data);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[Auth] Mock login error:', e);
|
||||
uni.showToast({ title: '网络请求失败', icon: 'none' });
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
||||
+150
-5
@@ -103,7 +103,7 @@
|
||||
<!-- 蓝色个人信息区域 -->
|
||||
<view class="user-header">
|
||||
<view class="user-info">
|
||||
<view class="avatar-wrapper">
|
||||
<view class="avatar-wrapper" @click="handleAvatarClick">
|
||||
<image class="avatar" :src="userInfo.avatar || '/static/default-avatar.png'" mode="aspectFill" />
|
||||
</view>
|
||||
<view class="user-detail">
|
||||
@@ -122,6 +122,13 @@
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 退出登录 -->
|
||||
<view class="menu-list logout-btn" @click="handleLogout">
|
||||
<view class="menu-item">
|
||||
<text class="menu-text" style="color: #e4393c; text-align: center; width: 100%;">退出登录</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 开发调试工具(仅开发环境显示) -->
|
||||
<view v-if="isDev" class="dev-tools">
|
||||
<view class="dev-tools-title">开发工具</view>
|
||||
@@ -134,7 +141,7 @@
|
||||
|
||||
<!-- 底部左右箭头导航条 -->
|
||||
<page-nav-bar
|
||||
:hasPrev="true"
|
||||
:hasPrev="canGoBack"
|
||||
:hasNext="false"
|
||||
@prev="handleNavPrev"
|
||||
@next="handleNavNext"
|
||||
@@ -156,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';
|
||||
|
||||
@@ -171,6 +178,9 @@ export default {
|
||||
categoryList: [],
|
||||
activeCategory: 'all',
|
||||
userInfo: { nickname: '', phone: '', avatar: '' },
|
||||
avatarClickCount: 0,
|
||||
avatarClickTimer: null,
|
||||
fromPage: '', // 记录从哪个页面跳转过来的
|
||||
menuList: [
|
||||
{ title: '我的订单', action: 'order' },
|
||||
{ title: '常见问题', action: 'faq' },
|
||||
@@ -184,6 +194,10 @@ export default {
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
canGoBack() {
|
||||
// 有上一页,或者从 tab 切换过来的,都可以返回
|
||||
return getCurrentPages().length > 1 || this.fromPage === 'tab';
|
||||
},
|
||||
maskedPhone() {
|
||||
const p = this.userInfo.phone;
|
||||
return p ? p.substring(0,3) + '****' + p.substring(7) : '';
|
||||
@@ -215,14 +229,24 @@ export default {
|
||||
// 支持通过 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 跳转,跳回扫码页
|
||||
// 无设备编号且非 tab 跳转,提示并跳至扫码页
|
||||
uni.showToast({ title: '请扫描设备二维码', icon: 'none' });
|
||||
setTimeout(() => {
|
||||
uni.redirectTo({ url: '/pages/index/scan' });
|
||||
}, 1500);
|
||||
return;
|
||||
}
|
||||
|
||||
// 加载缓存的用户信息
|
||||
@@ -244,6 +268,12 @@ export default {
|
||||
// 切换到"我的"Tab 时检查登录
|
||||
if (tab === 'mine' && !checkLoginWithPrompt('mine')) return;
|
||||
this.currentTab = tab;
|
||||
// 记录来源:从 tab 切换的
|
||||
if (tab === 'mine') {
|
||||
this.fromPage = 'tab';
|
||||
// 获取用户信息
|
||||
this.fetchUserInfo();
|
||||
}
|
||||
},
|
||||
switchCategory(cat) { this.activeCategory = cat; },
|
||||
getDisplayPrice(item) {
|
||||
@@ -254,11 +284,78 @@ export default {
|
||||
// 从缓存加载用户信息
|
||||
loadUserInfo() {
|
||||
const cached = getCachedUserInfo();
|
||||
console.log('[index] getCachedUserInfo:', JSON.stringify(cached, null, 2));
|
||||
if (cached) {
|
||||
this.userInfo = { ...this.userInfo, ...cached };
|
||||
} else {
|
||||
this.userInfo = { nickname: '', phone: '', avatar: '' };
|
||||
}
|
||||
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;
|
||||
@@ -305,8 +402,54 @@ export default {
|
||||
break;
|
||||
}
|
||||
},
|
||||
handleNavPrev() { this.currentTab = 'home'; },
|
||||
handleNavPrev() {
|
||||
if (this.fromPage === 'navigate') {
|
||||
// 从其他页面 navigateTo 过来的,返回上一页
|
||||
uni.navigateBack();
|
||||
} else if (this.fromPage === 'tab') {
|
||||
// 从首页 tab 切换过来的,切换回首页
|
||||
this.currentTab = 'home';
|
||||
}
|
||||
// 其他情况不执行操作
|
||||
},
|
||||
handleNavNext() { uni.showToast({ title: '暂无下一页', icon: 'none' }); },
|
||||
handleAvatarClick() {
|
||||
this.avatarClickCount++;
|
||||
clearTimeout(this.avatarClickTimer);
|
||||
|
||||
if (this.avatarClickCount >= 10) {
|
||||
// 点击10次,清除缓存
|
||||
this.avatarClickCount = 0;
|
||||
this.clearCacheAndReload();
|
||||
} else {
|
||||
// 2秒后重置计数
|
||||
this.avatarClickTimer = setTimeout(() => {
|
||||
this.avatarClickCount = 0;
|
||||
}, 2000);
|
||||
}
|
||||
},
|
||||
handleLogout() {
|
||||
uni.showModal({
|
||||
title: '退出登录',
|
||||
content: '确定要退出登录吗?',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
this.clearCacheAndReload();
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
clearCacheAndReload() {
|
||||
uni.clearStorageSync();
|
||||
uni.showToast({ title: '已退出登录', icon: 'success' });
|
||||
setTimeout(() => {
|
||||
// 跳转到干净的URL,去掉code/state参数,避免重复使用旧code
|
||||
const url = new URL(window.location.href);
|
||||
url.searchParams.delete('code');
|
||||
url.searchParams.delete('state');
|
||||
window.location.href = url.toString();
|
||||
}, 1000);
|
||||
},
|
||||
onVConsoleChange(e) {
|
||||
const enabled = e.detail.value;
|
||||
if (enabled) {
|
||||
@@ -327,6 +470,7 @@ page { height: 100%; background-color: #fff; }
|
||||
|
||||
/* ====== 首页样式 ====== */
|
||||
.home-page { display: flex; flex-direction: column; height: 100%; }
|
||||
|
||||
.product-area { flex: 1; overflow-y: auto; height: 0; }
|
||||
|
||||
/* 热销商品标题 + 装饰 */
|
||||
@@ -455,6 +599,7 @@ page { height: 100%; background-color: #fff; }
|
||||
.menu-item:last-child { border-bottom: none; }
|
||||
.menu-text { font-size: 30rpx; color: #333; }
|
||||
.menu-arrow { font-size: 28rpx; color: #ccc; }
|
||||
.logout-btn { margin-top: 20rpx; }
|
||||
|
||||
/* 开发工具区域 */
|
||||
.dev-tools { margin-top: 20rpx; background-color: #fff; border-radius: 16rpx; overflow: hidden; }
|
||||
|
||||
@@ -101,7 +101,25 @@ export default {
|
||||
methods: {
|
||||
handleScan() {
|
||||
// #ifdef H5
|
||||
uni.showToast({ title: 'H5 环境暂不支持扫码', icon: 'none' });
|
||||
// 检测是否在微信环境
|
||||
const isWechat = /MicroMessenger/i.test(navigator.userAgent);
|
||||
if (isWechat) {
|
||||
// 微信环境:拉起微信扫一扫
|
||||
// 注意:需要后端配合引入微信 JS-SDK 并完成签名配置
|
||||
uni.showToast({ title: '请使用微信扫一扫功能', icon: 'none' });
|
||||
// 如果已配置微信 JS-SDK,可以使用:
|
||||
// wx.scanQRCode({
|
||||
// needResult: 1,
|
||||
// scanType: ['qrCode', 'barCode'],
|
||||
// success: (res) => {
|
||||
// const result = res.resultStr;
|
||||
// this.parseScanResult(result);
|
||||
// }
|
||||
// });
|
||||
} else {
|
||||
// 非微信环境
|
||||
uni.showToast({ title: '请使用微信扫码或在小程序中打开', icon: 'none' });
|
||||
}
|
||||
// #endif
|
||||
|
||||
// #ifdef MP-WEIXIN
|
||||
|
||||
@@ -69,6 +69,7 @@
|
||||
|
||||
<script>
|
||||
import { gatewayPost } from '@/utils/request.js';
|
||||
import config from '@/config/env.js';
|
||||
|
||||
export default {
|
||||
data() {
|
||||
@@ -153,7 +154,7 @@ export default {
|
||||
}, 1000);
|
||||
},
|
||||
|
||||
// 注册
|
||||
// 注册(Mock 模式也走真实接口)
|
||||
async handleRegister() {
|
||||
if (!this.canSubmit) return;
|
||||
|
||||
@@ -183,9 +184,21 @@ export default {
|
||||
|
||||
const ret = res.data || {};
|
||||
if (ret.code === 0 && ret.data) {
|
||||
// 保存 token 和用户信息
|
||||
// 保存 token
|
||||
uni.setStorageSync('token', ret.data.token);
|
||||
uni.setStorageSync('user_info', ret.data.user_info);
|
||||
|
||||
// Mock 模式下,合并真实注册信息和 mock 配置
|
||||
if (config.mockWechatLogin) {
|
||||
const mockInfo = config.mockUser.user_info;
|
||||
const userInfo = {
|
||||
user_id: ret.data.user_id || mockInfo.user_id,
|
||||
phone: this.phone, // 使用真实注册的手机号
|
||||
nickname: mockInfo.nickname,
|
||||
avatar: mockInfo.avatar,
|
||||
};
|
||||
uni.setStorageSync('user_info', userInfo);
|
||||
console.log('[register] Mock 模式,合并用户信息:', userInfo);
|
||||
}
|
||||
|
||||
// 清除临时数据
|
||||
uni.removeStorageSync('temp_token');
|
||||
@@ -193,14 +206,7 @@ export default {
|
||||
|
||||
uni.hideLoading();
|
||||
uni.showToast({ title: '注册成功', icon: 'success' });
|
||||
|
||||
// 跳转首页
|
||||
setTimeout(() => {
|
||||
const url = this.deviceId
|
||||
? `/pages/index/index?device_id=${this.deviceId}`
|
||||
: '/pages/index/index';
|
||||
uni.redirectTo({ url });
|
||||
}, 1500);
|
||||
setTimeout(() => this.navigateToTarget(), 1500);
|
||||
} else {
|
||||
uni.hideLoading();
|
||||
uni.showToast({ title: ret.message || '注册失败', icon: 'none' });
|
||||
@@ -211,6 +217,19 @@ export default {
|
||||
}
|
||||
},
|
||||
|
||||
// 跳转到目标页面
|
||||
navigateToTarget() {
|
||||
const scanRedirectUrl = uni.getStorageSync('__scan_redirect_url');
|
||||
uni.removeStorageSync('__scan_redirect_url');
|
||||
|
||||
const url = scanRedirectUrl
|
||||
? scanRedirectUrl
|
||||
: this.deviceId
|
||||
? `/pages/index/index?device_id=${this.deviceId}`
|
||||
: '/pages/index/index';
|
||||
uni.redirectTo({ url });
|
||||
},
|
||||
|
||||
showAgreement(type) {
|
||||
const title = type === 'user' ? '用户协议' : '隐私政策';
|
||||
uni.showToast({ title: title + '开发中', icon: 'none' });
|
||||
@@ -349,10 +368,8 @@ page {
|
||||
|
||||
/* 底部协议 */
|
||||
.agreement {
|
||||
position: absolute;
|
||||
bottom: 80rpx;
|
||||
left: 0;
|
||||
right: 0;
|
||||
margin-top: 80rpx;
|
||||
padding-bottom: env(safe-area-inset-bottom);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
|
||||
@@ -72,7 +72,9 @@ export function logout() {
|
||||
*/
|
||||
export function getCachedUserInfo() {
|
||||
try {
|
||||
return uni.getStorageSync('user_info') || null;
|
||||
const userInfo = uni.getStorageSync('user_info');
|
||||
console.log('[auth-guard] getCachedUserInfo raw:', JSON.stringify(userInfo, null, 2));
|
||||
return userInfo || null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
|
||||
+25
-1
@@ -1,8 +1,31 @@
|
||||
import { defineConfig } from 'vite'
|
||||
import uni from '@dcloudio/vite-plugin-uni'
|
||||
import { existsSync, readFileSync } from 'fs'
|
||||
import { resolve } from 'path'
|
||||
|
||||
// 开发服务器:显式处理 public 目录下的静态文件,避免被 SPA history 路由拦截
|
||||
function servePublicFiles() {
|
||||
return {
|
||||
name: 'serve-public-files',
|
||||
configureServer(server) {
|
||||
server.middlewares.use((req, res, next) => {
|
||||
if (req.url) {
|
||||
const filePath = resolve(__dirname, 'public', req.url.slice(1))
|
||||
if (existsSync(filePath) && filePath.endsWith('.txt')) {
|
||||
const content = readFileSync(filePath, 'utf-8')
|
||||
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
|
||||
res.end(content)
|
||||
return
|
||||
}
|
||||
}
|
||||
next()
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [uni()],
|
||||
plugins: [servePublicFiles(), uni()],
|
||||
css: {
|
||||
preprocessorOptions: {
|
||||
scss: {
|
||||
@@ -10,4 +33,5 @@ export default defineConfig({
|
||||
},
|
||||
},
|
||||
},
|
||||
publicDir: 'public',
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user