Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 71606ebfe5 | |||
| a7e0366fb7 | |||
| 5bb31516a0 | |||
| 2560d9d529 | |||
| 6534723050 | |||
| f031501286 |
@@ -0,0 +1 @@
|
|||||||
|
2K94E8Rn63mn6Nys
|
||||||
+49
-18
@@ -1,47 +1,78 @@
|
|||||||
<script>
|
<script>
|
||||||
import { initEnv } from '@/utils/env.js';
|
import { initEnv } from '@/utils/env.js';
|
||||||
|
import config from '@/config/env.js';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
onLaunch: function(options) {
|
onLaunch: function(options) {
|
||||||
console.log('App Launch')
|
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
|
// 1. 环境检测 + 缓存 app_no/platform
|
||||||
initEnv();
|
initEnv();
|
||||||
|
|
||||||
// #ifdef H5
|
// #ifdef H5
|
||||||
// 2. 解析 URL 路径中的设备编号(如 /A1036)
|
// 2. 解析设备编号:优先从 query 参数获取,其次从路径解析
|
||||||
let deviceId = '';
|
let deviceId = '';
|
||||||
const path = window.location.pathname;
|
const urlParams = new URLSearchParams(window.location.search);
|
||||||
const segments = path.split('/').filter(Boolean);
|
deviceId = urlParams.get('device_id') || '';
|
||||||
if (segments.length > 0) {
|
|
||||||
const last = segments[segments.length - 1];
|
// 兜底:从路径解析(如 /pages/index/index/N1154)
|
||||||
if (/^[A-Z][A-Z0-9]+$/.test(last)) {
|
if (!deviceId) {
|
||||||
deviceId = last;
|
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
|
// 3. 检查 URL 中是否有授权回调的 code
|
||||||
const urlParams = new URLSearchParams(window.location.search);
|
|
||||||
const code = urlParams.get('code');
|
const code = urlParams.get('code');
|
||||||
|
|
||||||
if (code) {
|
if (code) {
|
||||||
// 有 code → 进入授权 Loading 页处理
|
// 有 code → 进入授权 Loading 页处理,保留 code 参数
|
||||||
const loadingUrl = deviceId
|
let loadingUrl = '/pages/auth/loading?code=' + encodeURIComponent(code);
|
||||||
? `/pages/auth/loading?device_id=${deviceId}`
|
if (deviceId) {
|
||||||
: '/pages/auth/loading';
|
loadingUrl += '&device_id=' + deviceId;
|
||||||
|
}
|
||||||
uni.redirectTo({ url: loadingUrl });
|
uni.redirectTo({ url: loadingUrl });
|
||||||
return;
|
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');
|
const token = uni.getStorageSync('token');
|
||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
// 有 JWT → 直接进入首页
|
if (isRootOrDevice) {
|
||||||
const homeUrl = deviceId
|
// 根路径/设备编号路径 → 跳转首页
|
||||||
? `/pages/index/index?device_id=${deviceId}`
|
const homeUrl = deviceId
|
||||||
: '/pages/index/scan';
|
? `/pages/index/index?device_id=${deviceId}`
|
||||||
uni.redirectTo({ url: homeUrl });
|
: '/pages/index/scan';
|
||||||
|
uni.redirectTo({ url: homeUrl });
|
||||||
|
} else {
|
||||||
|
// 其他业务页面 → 留在当前页面
|
||||||
|
console.log('[App] 已登录,保留在当前页面');
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// 无 JWT → 进入授权 Loading 页
|
// 无 JWT → 进入授权 Loading 页
|
||||||
const loadingUrl = deviceId
|
const loadingUrl = deviceId
|
||||||
|
|||||||
+22
-3
@@ -6,16 +6,35 @@ const dev = {
|
|||||||
apiBaseUrl: 'http://192.168.0.23:4000',
|
apiBaseUrl: 'http://192.168.0.23:4000',
|
||||||
|
|
||||||
// 网关地址(vms-gateway,端口 4001)— 用户授权、短信等接口走网关
|
// 网关地址(vms-gateway,端口 4001)— 用户授权、短信等接口走网关
|
||||||
apiGatewayUrl: 'https://gateway.arklinksmart.cn',
|
apiGatewayUrl: 'http://192.168.0.23:4001',
|
||||||
|
|
||||||
// 授权回调地址(支付宝/微信 OAuth redirect_uri)
|
// 授权回调地址(微信 OAuth redirect_uri,必须 HTTPS,指向前端页面)
|
||||||
authRedirectUri: 'https://gateway.arklinksmart.cn/api/v1/user/auth/callback',
|
authRedirectUri: 'https://m.arklinksmart.cn/pages/auth/loading',
|
||||||
|
|
||||||
// 开发阶段硬编码 Token(生产环境应从登录流程获取)
|
// 开发阶段硬编码 Token(生产环境应从登录流程获取)
|
||||||
bearerToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImRlZXB2ZW5kaW5nIiwic3ViIjoiMTY0MDI0MTEyNiIsImZyb20iOiJwb3MiLCJyb2xlIjoicG9zIiwibWFpbmJvYXJkX2lkIjoiVDEwMDIiLCJwb3Nfc24iOiIxNjQwMjQxMTI2IiwiaWF0IjoxNzc4NDkxOTA3LCJleHAiOjE3NzkwOTY3MDd9.DjscZtrTCr30XOngToUifxoel4q2EGES_uqGQernkvg',
|
bearerToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImRlZXB2ZW5kaW5nIiwic3ViIjoiMTY0MDI0MTEyNiIsImZyb20iOiJwb3MiLCJyb2xlIjoicG9zIiwibWFpbmJvYXJkX2lkIjoiVDEwMDIiLCJwb3Nfc24iOiIxNjQwMjQxMTI2IiwiaWF0IjoxNzc4NDkxOTA3LCJleHAiOjE3NzkwOTY3MDd9.DjscZtrTCr30XOngToUifxoel4q2EGES_uqGQernkvg',
|
||||||
|
|
||||||
// 是否开启请求日志
|
// 是否开启请求日志
|
||||||
enableRequestLog: true,
|
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;
|
export default dev;
|
||||||
|
|||||||
+2
-2
@@ -8,8 +8,8 @@ const prod = {
|
|||||||
// 网关地址(vms-gateway)— 用户授权、短信等接口走网关
|
// 网关地址(vms-gateway)— 用户授权、短信等接口走网关
|
||||||
apiGatewayUrl: 'https://gateway.arklinksmart.cn',
|
apiGatewayUrl: 'https://gateway.arklinksmart.cn',
|
||||||
|
|
||||||
// 授权回调地址(支付宝/微信 OAuth redirect_uri)
|
// 授权回调地址(微信 OAuth redirect_uri,必须 HTTPS,指向前端页面)
|
||||||
authRedirectUri: 'https://gateway.arklinksmart.cn/api/v1/user/auth/callback',
|
authRedirectUri: 'https://m.arklinksmart.cn/pages/auth/loading',
|
||||||
|
|
||||||
// 生产环境 Token 从登录流程获取,此处为空
|
// 生产环境 Token 从登录流程获取,此处为空
|
||||||
bearerToken: '',
|
bearerToken: '',
|
||||||
|
|||||||
@@ -21,9 +21,19 @@ export default {
|
|||||||
this.deviceId = options.device_id || '';
|
this.deviceId = options.device_id || '';
|
||||||
|
|
||||||
// #ifdef H5
|
// #ifdef H5
|
||||||
// 检查 URL 中是否有授权回调的 code
|
// Mock 模式:跳过微信 OAuth,直接登录
|
||||||
const urlParams = new URLSearchParams(window.location.search);
|
if (config.mockWechatLogin) {
|
||||||
const code = urlParams.get('code');
|
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) {
|
if (code) {
|
||||||
// 有 code → 调用后端登录
|
// 有 code → 调用后端登录
|
||||||
@@ -56,9 +66,9 @@ export default {
|
|||||||
try {
|
try {
|
||||||
this.statusText = '正在获取授权...';
|
this.statusText = '正在获取授权...';
|
||||||
|
|
||||||
// 构建回调地址(当前页面地址)
|
// 回调地址必须是 HTTPS,使用配置文件中的地址
|
||||||
const redirectUri = encodeURIComponent(
|
const redirectUri = config.authRedirectUri || (
|
||||||
window.location.origin + window.location.pathname
|
window.location.origin.replace(/^http:/, 'https:') + window.location.pathname
|
||||||
);
|
);
|
||||||
|
|
||||||
const res = await gatewayGet('/api/v1/user/auth/url', {
|
const res = await gatewayGet('/api/v1/user/auth/url', {
|
||||||
@@ -151,6 +161,64 @@ export default {
|
|||||||
goToScan() {
|
goToScan() {
|
||||||
uni.redirectTo({ url: '/pages/index/scan' });
|
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>
|
</script>
|
||||||
|
|||||||
@@ -183,9 +183,16 @@ export default {
|
|||||||
|
|
||||||
const ret = res.data || {};
|
const ret = res.data || {};
|
||||||
if (ret.code === 0 && ret.data) {
|
if (ret.code === 0 && ret.data) {
|
||||||
|
// 合并微信授权信息(头像、昵称)到 user_info
|
||||||
|
const authInfo = uni.getStorageSync('auth_info') || {};
|
||||||
|
const userInfo = {
|
||||||
|
...authInfo,
|
||||||
|
...ret.data.user_info,
|
||||||
|
};
|
||||||
|
|
||||||
// 保存 token 和用户信息
|
// 保存 token 和用户信息
|
||||||
uni.setStorageSync('token', ret.data.token);
|
uni.setStorageSync('token', ret.data.token);
|
||||||
uni.setStorageSync('user_info', ret.data.user_info);
|
uni.setStorageSync('user_info', userInfo);
|
||||||
|
|
||||||
// 清除临时数据
|
// 清除临时数据
|
||||||
uni.removeStorageSync('temp_token');
|
uni.removeStorageSync('temp_token');
|
||||||
@@ -349,10 +356,8 @@ page {
|
|||||||
|
|
||||||
/* 底部协议 */
|
/* 底部协议 */
|
||||||
.agreement {
|
.agreement {
|
||||||
position: absolute;
|
margin-top: 80rpx;
|
||||||
bottom: 80rpx;
|
padding-bottom: env(safe-area-inset-bottom);
|
||||||
left: 0;
|
|
||||||
right: 0;
|
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
|
|||||||
+25
-1
@@ -1,8 +1,31 @@
|
|||||||
import { defineConfig } from 'vite'
|
import { defineConfig } from 'vite'
|
||||||
import uni from '@dcloudio/vite-plugin-uni'
|
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({
|
export default defineConfig({
|
||||||
plugins: [uni()],
|
plugins: [servePublicFiles(), uni()],
|
||||||
css: {
|
css: {
|
||||||
preprocessorOptions: {
|
preprocessorOptions: {
|
||||||
scss: {
|
scss: {
|
||||||
@@ -10,4 +33,5 @@ export default defineConfig({
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
publicDir: 'public',
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user