Compare commits

..

5 Commits

Author SHA1 Message Date
kk 71606ebfe5 mock清除登录状态机制/mock的user_info字段更新
Uni-app H5 Deploy (Prod + Staging) / deploy (release) Successful in 48s
2026-07-10 14:02:26 +08:00
kk a7e0366fb7 支持用户授权注册登录mock 2026-07-09 18:49:56 +08:00
kk 5bb31516a0 保留code参数
Uni-app H5 Deploy (Prod + Staging) / deploy (release) Successful in 45s
2026-07-02 19:32:07 +08:00
kk 2560d9d529 上传验证文件
Uni-app H5 Deploy (Prod + Staging) / deploy (release) Successful in 42s
2026-07-02 19:20:08 +08:00
kk 6534723050 修改授权回调配置
Uni-app H5 Deploy (Prod + Staging) / deploy (release) Successful in 1m25s
2026-07-02 19:04:08 +08:00
6 changed files with 158 additions and 34 deletions
+1
View File
@@ -0,0 +1 @@
2K94E8Rn63mn6Nys
+49 -18
View File
@@ -1,47 +1,78 @@
<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 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)) {
deviceId = last;
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) {
const last = segments[segments.length - 1];
if (/^[A-Z][A-Z0-9]+$/.test(last)) {
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 → 直接进入首页
const homeUrl = deviceId
? `/pages/index/index?device_id=${deviceId}`
: '/pages/index/scan';
uni.redirectTo({ url: homeUrl });
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
View File
@@ -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
View File
@@ -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: '',
+74 -6
View File
@@ -21,9 +21,19 @@ export default {
this.deviceId = options.device_id || '';
// #ifdef H5
// 检查 URL 中是否有授权回调的 code
const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get('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);
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', {
@@ -151,6 +161,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>
+10 -5
View File
@@ -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');
@@ -349,10 +356,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;