Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c84c9df5d4 | |||
| 71606ebfe5 | |||
| a7e0366fb7 | |||
| 5bb31516a0 | |||
| 2560d9d529 | |||
| 6534723050 |
@@ -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: 'test_open_id',
|
||||
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: '',
|
||||
|
||||
@@ -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 → 调用后端登录
|
||||
@@ -56,9 +66,9 @@ 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
|
||||
);
|
||||
|
||||
const res = await gatewayGet('/api/v1/user/auth/url', {
|
||||
@@ -103,8 +113,11 @@ export default {
|
||||
});
|
||||
|
||||
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);
|
||||
@@ -116,11 +129,13 @@ export default {
|
||||
// 已注册 → 缓存 JWT → 跳转首页
|
||||
uni.setStorageSync('token', result.token);
|
||||
uni.setStorageSync('user_info', result.user_info);
|
||||
console.log('[loading] stored user_info:', JSON.stringify(result.user_info, null, 2));
|
||||
this.goToHome();
|
||||
} else {
|
||||
// 未注册 → 缓存临时数据 → 跳转注册页
|
||||
uni.setStorageSync('temp_token', result.temp_token);
|
||||
uni.setStorageSync('auth_info', result.auth_info);
|
||||
console.log('[loading] stored auth_info:', JSON.stringify(result.auth_info, null, 2));
|
||||
this.goToRegister();
|
||||
}
|
||||
} else {
|
||||
@@ -142,6 +157,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 +174,64 @@ export default {
|
||||
goToScan() {
|
||||
uni.redirectTo({ url: '/pages/index/scan' });
|
||||
},
|
||||
|
||||
/**
|
||||
* Mock 登录(开发环境模拟不同用户状态)
|
||||
*/
|
||||
async mockLogin() {
|
||||
const mode = config.mockWechatLogin;
|
||||
const mock = config.mockUser;
|
||||
|
||||
if (mode === 'register' || mode === 'login') {
|
||||
// 调用真实登录接口,用测试 code
|
||||
this.statusText = '模拟:获取授权信息...';
|
||||
const appNo = uni.getStorageSync('app_no') || getAppNo(detectPlatform()) || config.mockAppNo;
|
||||
|
||||
try {
|
||||
const res = await gatewayPost('/api/v1/user/login', {
|
||||
app_no: appNo,
|
||||
code: 'test_code',
|
||||
});
|
||||
|
||||
const data = res.data || {};
|
||||
if (data.code === 0 && data.data) {
|
||||
const result = data.data;
|
||||
|
||||
if (result.register) {
|
||||
// 已注册
|
||||
uni.setStorageSync('token', result.token);
|
||||
uni.setStorageSync('user_info', result.user_info);
|
||||
console.log('[Auth] Mock: existing user → home');
|
||||
this.goToHome();
|
||||
} else {
|
||||
// 未注册 → 存真实的 temp_token,合并 mock 头像昵称
|
||||
const authInfo = {
|
||||
...mock.user_info,
|
||||
...result.auth_info,
|
||||
};
|
||||
uni.setStorageSync('temp_token', result.temp_token);
|
||||
uni.setStorageSync('auth_info', authInfo);
|
||||
console.log('[Auth] Mock: new user → register, temp_token:', result.temp_token);
|
||||
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' });
|
||||
}
|
||||
|
||||
} else {
|
||||
// 'skip' → 直接登录
|
||||
this.statusText = '模拟登录中...';
|
||||
uni.setStorageSync('token', mock.token);
|
||||
uni.setStorageSync('user_info', mock.user_info);
|
||||
console.log('[Auth] Mock: skip → home');
|
||||
setTimeout(() => this.goToHome(), 500);
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
||||
@@ -254,11 +254,13 @@ 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 fetchProducts() {
|
||||
this.loading = true;
|
||||
|
||||
@@ -183,9 +183,16 @@ export default {
|
||||
|
||||
const ret = res.data || {};
|
||||
if (ret.code === 0 && ret.data) {
|
||||
// 合并微信授权信息(头像、昵称)到 user_info
|
||||
const authInfo = uni.getStorageSync('auth_info') || {};
|
||||
const userInfo = {
|
||||
...authInfo,
|
||||
...ret.data.user_info,
|
||||
};
|
||||
|
||||
// 保存 token 和用户信息
|
||||
uni.setStorageSync('token', ret.data.token);
|
||||
uni.setStorageSync('user_info', ret.data.user_info);
|
||||
uni.setStorageSync('user_info', userInfo);
|
||||
|
||||
// 清除临时数据
|
||||
uni.removeStorageSync('temp_token');
|
||||
@@ -194,9 +201,14 @@ export default {
|
||||
uni.hideLoading();
|
||||
uni.showToast({ title: '注册成功', icon: 'success' });
|
||||
|
||||
// 跳转首页
|
||||
// 跳转到扫码时的页面(优先使用缓存的扫码URL)
|
||||
setTimeout(() => {
|
||||
const url = this.deviceId
|
||||
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 });
|
||||
@@ -349,10 +361,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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user