From 4ba720b4c74d68c977bb224add9e180c1dcf9c41 Mon Sep 17 00:00:00 2001 From: zm2000 <18386612143@163.com> Date: Sat, 4 Apr 2026 09:43:30 +0800 Subject: [PATCH] feat: improve auth guard and manual scan flow --- .hbuilderx/launch.json | 2 + androidPrivacy.json | 3 + core/request/index.js | 14 +- core/router/index.js | 37 +++-- core/store/modules/user.js | 7 +- core/utils/auth-guard.js | 52 ++++++- manifest.json | 3 + pages/login/index.vue | 140 +++++++++++++++--- pages/settings/broadcastSettings.vue | 42 ++++++ pages/settings/index.vue | 16 +- pages/user/index.vue | 6 +- .../receiving/components/detail-container.vue | 96 +++++++++++- .../receiving/components/info-container.vue | 26 +++- pages/work/storage/receiving/receiving.vue | 97 +++++++++++- 14 files changed, 483 insertions(+), 58 deletions(-) create mode 100644 androidPrivacy.json diff --git a/.hbuilderx/launch.json b/.hbuilderx/launch.json index e466870..ef0f1fd 100644 --- a/.hbuilderx/launch.json +++ b/.hbuilderx/launch.json @@ -2,6 +2,8 @@ "version" : "1.0", "configurations" : [ { + "customPlaygroundType" : "local", + "packageName" : "com.gztbyc.pda_wms", "playground" : "standard", "type" : "uni-app:app-android" }, diff --git a/androidPrivacy.json b/androidPrivacy.json new file mode 100644 index 0000000..a78485c --- /dev/null +++ b/androidPrivacy.json @@ -0,0 +1,3 @@ +{ + "prompt" : "none" +} diff --git a/core/request/index.js b/core/request/index.js index 4f03898..2e96497 100644 --- a/core/request/index.js +++ b/core/request/index.js @@ -15,14 +15,16 @@ export default function http( ) { let api = getApiPath(url); console.log(api); + const token = uni.getStorageSync('token'); + if (api.auth && !token) { + store.dispatch('logout'); + uni.hideLoading(); + return Promise.reject({ + msg: `暂未登录,已阻止此次API请求: '${api.url}'` + }); + } /* 请求之前拦截器 */ Request.interceptor.request((config, cancel) => { - let token = uni.getStorageSync('token'); - if (api.auth && !token) { - store.dispatch('logout'); - uni.hideLoading() - throw (`暂未登录,已阻止此次API请求: '${api.url}'`); - } token && Request.setConfig(config => { config.header.authorization = `Bearer ${token}` }) diff --git a/core/router/index.js b/core/router/index.js index f55d918..418a4f1 100644 --- a/core/router/index.js +++ b/core/router/index.js @@ -72,18 +72,27 @@ router.beforeEach(async (to, from, next) => { const hasGetUserInfo = store.getters.user.employees_name; console.log("有没有用户信息",hasGetUserInfo); if (hasGetUserInfo) { - const isVisible =await store.dispatch('floatWindowManager/is_visible'); - if(to.meta && to.meta.auth){ - console.log(to.path); - const hasPermiss =await store.dispatch('checkPath',to.path); - if(!hasPermiss){ - next({path:"/pages/403"}) - return; + try { + store.dispatch('floatWindowManager/is_visible').catch(error => { + console.log('浮窗显示检查失败:', error); + }); + if(to.meta && to.meta.auth){ + console.log(to.path); + const hasPermiss =await store.dispatch('checkPath',to.path); + if(!hasPermiss){ + next({path:"/pages/403"}) + return; + } } - } - if(to.path==from.path){ - next(false); - }else{ + if(to.path==from.path){ + console.log('路由重复,取消跳转:', to.path); + next(false); + }else{ + console.log('路由放行:', from.path, '->', to.path); + next(); + } + } catch (error) { + console.log('已登录分支路由守卫异常:', error); next(); } @@ -92,10 +101,14 @@ router.beforeEach(async (to, from, next) => { try { await store.dispatch('getUserInfo'); console.log("获取完成,去哪里",to) - await store.dispatch('floatWindowManager/is_visible'); + store.dispatch('floatWindowManager/is_visible').catch(error => { + console.log('浮窗显示检查失败:', error); + }); if (to.path === '/pages/index/index') { + console.log('路由放行:', from.path, '->', to.path); next(); } else { + console.log('路由重定向:', from.path, '->', to.fullPath); next({ path: to.fullPath, NAVTYPE: 'SwitchTab' diff --git a/core/store/modules/user.js b/core/store/modules/user.js index 64bce8b..a12eed8 100644 --- a/core/store/modules/user.js +++ b/core/store/modules/user.js @@ -8,7 +8,7 @@ import { removeToken } from '@/core/utils/auth' import { router } from '../../router' -import { goLogin } from '@/core/utils/auth-guard' +import { clearSession, goLogin } from '@/core/utils/auth-guard' const normalizeMenus = (user = {}) => { const sourceMenus = Array.isArray(user.menus) @@ -135,10 +135,7 @@ const actions = { commit, dispatch }) { - removeToken() - commit('SET_PERMISSIONS', []) - commit('SET_USER', {}) - commit('SET_ROLEID', 0) + clearSession(store) return goLogin() }, checkPath({ diff --git a/core/utils/auth-guard.js b/core/utils/auth-guard.js index 42ac413..aa303e7 100644 --- a/core/utils/auth-guard.js +++ b/core/utils/auth-guard.js @@ -22,6 +22,18 @@ export function clearSession(store) { removeToken() if (!store) return + try { + store.commit('token', '') + } catch (e) {} + + try { + store.commit('isLogin', false) + } catch (e) {} + + try { + store.commit('userInfo', {}) + } catch (e) {} + try { store.commit('SET_PERMISSIONS', []) } catch (e) {} @@ -33,11 +45,33 @@ export function clearSession(store) { try { store.commit('SET_ROLEID', 0) } catch (e) {} + + try { + store.commit('USER_DATA', {}) + } catch (e) {} + + try { + store.commit('AGENT_INFO', {}) + } catch (e) {} + + try { + store.commit('AUTH_TYPE', '') + } catch (e) {} + + try { + store.commit('deviceInfo/CLEAR_DEVICE_INFO') + } catch (e) {} } function navigate(url, type) { const currentRoute = getCurrentRoute() if (redirecting || currentRoute === url) { + console.log('[auth-guard] 跳转已忽略:', { + url, + type, + redirecting, + currentRoute + }) return Promise.resolve() } @@ -52,17 +86,31 @@ function navigate(url, type) { } if (type === 'switchTab') { + console.log('[auth-guard] 开始跳转:', { url, type }) uni.switchTab({ url, + success: () => { + console.log('[auth-guard] 跳转成功:', { url, type }) + }, + fail: error => { + console.log('[auth-guard] 跳转失败:', { url, type, error }) + }, complete: done - }) + }, true) return } + console.log('[auth-guard] 开始跳转:', { url, type }) uni.reLaunch({ url, + success: () => { + console.log('[auth-guard] 跳转成功:', { url, type }) + }, + fail: error => { + console.log('[auth-guard] 跳转失败:', { url, type, error }) + }, complete: done - }) + }, true) }) } diff --git a/manifest.json b/manifest.json index 98fad78..0138bfd 100644 --- a/manifest.json +++ b/manifest.json @@ -94,6 +94,9 @@ "spotlight@3x" : "unpackage/res/icons/120x120.png" } } + }, + "splashscreen" : { + "useOriginalMsgbox" : false } } }, diff --git a/pages/login/index.vue b/pages/login/index.vue index 934d1c9..39cda00 100644 --- a/pages/login/index.vue +++ b/pages/login/index.vue @@ -45,11 +45,19 @@ - + - PDA扫描工牌登录 + @@ -532,31 +540,55 @@ export default { parseScanCodeStrict(scanText) { const raw = String(scanText || '').trim(); if (!raw) { - throw new Error('扫描内容为空'); + return { + ok: false, + error: '扫描内容为空', + uuid: '' + }; } if (!this.isBase64Text(raw)) { - throw new Error('扫描内容不是Base64'); + return { + ok: false, + error: '扫描内容不是Base64', + uuid: '' + }; } const decoded = this.decodeBase64Text(raw); if (!decoded) { - throw new Error('Base64反序列化失败'); + return { + ok: false, + error: 'Base64反序列化失败', + uuid: '' + }; } let obj; try { obj = JSON.parse(decoded); } catch (e) { - throw new Error('反序列化后不是JSON字符串'); + return { + ok: false, + error: '反序列化后不是JSON字符串', + uuid: '' + }; } const uuid = String(obj?.uuid || '').trim(); if (!uuid) { - throw new Error('JSON中缺少uuid键'); + return { + ok: false, + error: 'JSON中缺少uuid键', + uuid: '' + }; } - return uuid; + return { + ok: true, + error: '', + uuid + }; }, parseScanUuid(scanText) { if (!scanText) { @@ -583,6 +615,67 @@ export default { return text; }, + normalizeUniScanResult(scanResult) { + if (!scanResult) { + return ''; + } + + if (typeof scanResult === 'string') { + return scanResult.trim(); + } + + if (typeof scanResult.result === 'string') { + return scanResult.result.trim(); + } + + if (Array.isArray(scanResult.result)) { + const firstResult = scanResult.result.find(item => item); + if (typeof firstResult === 'string') { + return firstResult.trim(); + } + if (firstResult && typeof firstResult.data === 'string') { + return firstResult.data.trim(); + } + } + + if (typeof scanResult.data === 'string') { + return scanResult.data.trim(); + } + + return ''; + }, + handleManualScan() { + uni.scanCode({ + onlyFromCamera: false, + scanType: ['qrCode', 'barCode', 'datamatrix', 'pdf417'], + success: (res) => { + const rawCode = this.normalizeUniScanResult(res); + if (!rawCode) { + uni.showToast({ + title: '未识别到有效内容', + icon: 'none' + }); + return; + } + + this.handleBroadcastResult({ + data: rawCode, + source: 'uni.scanCode' + }); + }, + fail: (error) => { + const errMsg = String(error?.errMsg || '').toLowerCase(); + if (errMsg.includes('cancel')) { + return; + } + + uni.showToast({ + title: '扫描失败,请重试', + icon: 'none' + }); + } + }); + }, extractToken(payload) { if (!payload || typeof payload !== 'object') { return ''; @@ -658,9 +751,19 @@ export default { handleBroadcastResult(result) { if (!this.scanning || !result || !result.data) return; - try { - const rawCode = String(result.data || '').trim(); // 扫描原文 - const uuid = this.parseScanCodeStrict(rawCode); // 仅用于校验与展示 + const rawCode = String(result.data || '').trim(); // 扫描原文 + const parsed = this.parseScanCodeStrict(rawCode); // 仅用于校验与展示 + if (!parsed.ok) { + this.scanning = true; + this.deviceCode = ''; + this.scanUuid = ''; + this.currentDevice = null; + this.$store.dispatch('broadcast/resume'); + uni.showToast({ title: parsed.error || '扫码数据不合法', icon: 'none' }); + return; + } + + const uuid = parsed.uuid; this.deviceCode = rawCode; // code = 扫描字符串 this.scanUuid = uuid; @@ -674,14 +777,6 @@ export default { // 自动登录 this.confirmLogin(); - } catch (e) { - this.scanning = true; - this.deviceCode = ''; - this.scanUuid = ''; - this.currentDevice = null; - this.$store.dispatch('broadcast/resume'); - uni.showToast({ title: e?.message || '扫码数据不合法', icon: 'none' }); - } }, } } @@ -923,7 +1018,6 @@ export default { display: flex; align-items: center; justify-content: center; - pointer-events: none; background: transparent; &:active { @@ -940,6 +1034,12 @@ export default { .method-desc { margin: 0; } + + .scan-login-content { + display: flex; + align-items: center; + gap: 16rpx; + } } .device-status { diff --git a/pages/settings/broadcastSettings.vue b/pages/settings/broadcastSettings.vue index b6bc3d1..0da25f2 100644 --- a/pages/settings/broadcastSettings.vue +++ b/pages/settings/broadcastSettings.vue @@ -113,6 +113,8 @@