From 006a976a7607e19826d7d617c5ff1c807f25b48d Mon Sep 17 00:00:00 2001 From: zm2000 <18386612143@163.com> Date: Tue, 24 Mar 2026 15:54:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84=E6=89=AB=E7=A0=81?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E9=89=B4=E6=9D=83=E4=B8=8E=E8=B7=B3=E8=BD=AC?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .hbuilderx/launch.json | 13 ++ App.vue | 39 ++++-- README.md | 91 ++++++++++++- core/request/apis.js | 6 + core/request/index.js | 42 ++++-- core/store/modules/user.js | 81 +++++++++-- core/utils/auth-guard.js | 111 +++++++++++++++ day.log | 9 ++ pages/login/index.vue | 269 +++++++++++++++++++++++++++++++------ 9 files changed, 581 insertions(+), 80 deletions(-) create mode 100644 .hbuilderx/launch.json create mode 100644 core/utils/auth-guard.js create mode 100644 day.log diff --git a/.hbuilderx/launch.json b/.hbuilderx/launch.json new file mode 100644 index 0000000..e466870 --- /dev/null +++ b/.hbuilderx/launch.json @@ -0,0 +1,13 @@ +{ + "version" : "1.0", + "configurations" : [ + { + "playground" : "standard", + "type" : "uni-app:app-android" + }, + { + "playground" : "standard", + "type" : "uni-app:app-ios" + } + ] +} diff --git a/App.vue b/App.vue index d003721..530aef0 100644 --- a/App.vue +++ b/App.vue @@ -1,18 +1,35 @@ diff --git a/README.md b/README.md index 1d1af10..7916770 100644 --- a/README.md +++ b/README.md @@ -1 +1,90 @@ -天豹云仓的WMS_PDA项目 \ No newline at end of file +天豹云仓的WMS_PDA项目 + +## 文件说明 + +- `day.log`:日报记录文件,用于按天填写工作内容(今日完成、进行中、明日计划、问题与风险、备注)。 + + +## API接口文档 + +- `http://192.168.11.56:6060/api/docs/system/raw` + +## 接口文档与登录接口关系 + +### 1)接口文档(后端) + +- 接口文档定义了后端可调用的 method、参数和返回结构。 +- 前端并不直接写死所有 URL,而是通过本地接口映射表进行统一管理。 + +### 2)本地接口映射(前端“接口文档”) + +- 文件:`core/request/apis.js` +- 作用:把业务别名映射为请求配置(`path/url`、`method`、`auth`) + +登录相关映射: + +- `user.login` + - `path: "user.login"` + - 用于账号密码登录 +- `user.loginByUuid` + - `url: "https://ye4s.wms.gztbyc.com/index/wms/wms/loginByUuid"` + - 用于扫码/工牌 UUID 登录 +- `user.info` + - `path: "user.info"` + - 用于拿登录后的用户信息 + +### 3)请求封装与实际调用关系 + +#### 3.1 请求入口 + +- 文件:`core/request/index.js` +- 统一入口:`http('user.login', data)` / `http('user.loginByUuid', data)` + +#### 3.2 URL组装规则 + +- 文件:`core/request/request.js` +- 若配置中有 `path`:实际请求变为 `API_URL?method={path}&v=1.0×tamp=...` +- 若配置中有完整 `url`:直接请求该 URL(如 `loginByUuid`) + +#### 3.3 公共请求头 + +- 文件:`core/request/request.js` +- 默认头包含: + - `platform: pda` + - `project: warehouse` + +> 注意:若后端提示 `project 不支持`,优先检查这里的 `project` 与业务参数中的 `project` 是否一致。 + +### 4)登录调用链(账号密码) + +1. 页面发起:`pages/login/index.vue` -> `this.$store.dispatch('login', loginForm)` +2. 状态层处理:`core/store/modules/user.js` -> `actions.login` +3. 接口调用:`http('user.login', { username, password, device, project })` +4. 返回处理:从响应拿 `token`,调用 `setToken(token)` 写入本地 +5. 页面跳转:登录页切换到 `/pages/index/index` + +### 5)登录调用链(扫码/工牌) + +1. 页面监听广播:`pages/login/index.vue`(`broadcast/setCallbacks` + `onResult`) +2. 获取扫码结果:`result.data`(通常是 UUID 或包含 UUID 的编码串) +3. 点击确认登录:调用 `loginByUuid(uuid)` +4. 接口调用:`http('user.loginByUuid', { uuid })` +5. 返回处理:提取 token 后 `setToken(token)` +6. 页面跳转:切换到 `/pages/index/index` + +### 6)常见错误与关系定位 + +- 报错:`project 不支持` + - 关联:`core/request/request.js` 默认请求头 + `core/store/modules/user.js` 的 `state.app.project` +- 报错:`登录成功但未返回token` + - 关联:后端返回结构与前端 token 提取逻辑不一致 +- 报错:`用户信息加载失败` + - 关联:`user.info` 接口失败、token 无效或参数不匹配 + +### 7)建议的排查顺序 + +1. 先看 `apis.js` 映射是否正确(`path/url`) +2. 再看请求头和业务参数中的 `project/device` 是否与后端一致 +3. 再看接口返回结构是否包含 token +4. 最后看登录后是否成功调用 `user.info` + diff --git a/core/request/apis.js b/core/request/apis.js index 57c6e4c..3ef856e 100644 --- a/core/request/apis.js +++ b/core/request/apis.js @@ -61,6 +61,12 @@ export default { method: "POST", isSystemApi: true }, + loginByCode: { + path: "user.loginByCode", + auth: false, + method: "POST", + isSystemApi: true + }, changeRole: { path: "user.changeRole", auth: true, diff --git a/core/request/index.js b/core/request/index.js index 56b2b5c..4f03898 100644 --- a/core/request/index.js +++ b/core/request/index.js @@ -3,6 +3,7 @@ import apiList from './apis.js' import store from '@/core/store/index.js' import { setToken,removeToken } from '../utils/auth'; import router from '@/core/router' +import { handleTokenExpired } from '@/core/utils/auth-guard' const Request = new coreRequest(); export default function http( @@ -39,10 +40,31 @@ export default function http( const headers = res.header; console.log(res); const response = res.data; - const token = headers?.authorization || headers?.Authorization // 新的token - if (token) { - const newToken = token.substring(7) // 去掉'Bearer '前缀 - setToken(newToken) + const getHeaderValue = (headerMap, key) => { + if (!headerMap) { + return ''; + } + const targetKey = Object.keys(headerMap).find(item => String(item).toLowerCase() === String(key).toLowerCase()); + return targetKey ? headerMap[targetKey] : ''; + }; + const normalizeToken = (rawToken) => { + if (!rawToken) { + return ''; + } + const text = String(rawToken).trim(); + if (!text) { + return ''; + } + return text.startsWith('Bearer ') ? text.substring(7).trim() : text; + }; + const token = + getHeaderValue(headers, 'authorization') || + getHeaderValue(headers, 'token') || + getHeaderValue(headers, 'x-access-token') || + getHeaderValue(headers, 'access-token'); + const normalizedToken = normalizeToken(token); + if (normalizedToken) { + setToken(normalizedToken) } uni.hideLoading(); if (response?.flag === "failure") { @@ -59,14 +81,10 @@ export default function http( // token过期注销 if (res.statusCode === 401) { - removeToken(); - uni.showToast({ - title: '登录已过期,请重新登录', - icon: 'none', - duration: 1000, - mask: true - }); - uni.reLaunch('/pages/login/index') + handleTokenExpired(store) + return Promise.reject({ + msg: '登录已过期,请重新登录' + }) } return response }) diff --git a/core/store/modules/user.js b/core/store/modules/user.js index 595a2e5..f5631a0 100644 --- a/core/store/modules/user.js +++ b/core/store/modules/user.js @@ -8,6 +8,7 @@ import { removeToken } from '@/core/utils/auth' import { router } from '../../router' +import { goLogin } from '@/core/utils/auth-guard' const state = { token: uni.getStorageSync("token") || "", isLogin: uni.getStorageSync("isLogin") || false, // 是否登陆 @@ -19,7 +20,7 @@ const state = { user: {}, app: { device: "pda", - project:"warehouse" + project:"wms" }, permissions: [], menus:[], @@ -115,11 +116,11 @@ const actions = { commit, dispatch }) { - removeToken(); + removeToken() commit('SET_PERMISSIONS', []) commit('SET_USER', {}) commit('SET_ROLEID', 0) - router.replace('/pages/login/index') + return goLogin() }, checkPath({ commit, @@ -143,24 +144,80 @@ const actions = { password } = userInfo return new Promise((resolve, reject) => { + const safeUsername = String(username || '').trim(); + if (!safeUsername || !password) { + reject({ msg: '请输入账号和密码' }) + return; + } http('user.login', { - username: username.trim(), + username: safeUsername, password: password, device: state.app.device, - project:state.app.project + project: state.app.project }).then(response => { - const { - data - } = response; - console.log("登录返回数据",data); - setToken(data.token) - resolve() + if (response && response.flag === 'failure') { + reject({ msg: response.message || '账号密码登录失败' }) + return; + } + + const data = response && response.data; + console.log("登录返回数据", data); + const token = data && data.token; + if (!token) { + reject({ msg: response?.message || '登录成功但未返回token' }) + return; + } + setToken(token) + resolve() }).catch(error => { console.log("出错",error) - reject(error) + reject({ msg: error?.msg || error?.message || error?.data?.message || '登录失败' }) }) }) }, + loginByCode({ + commit, + state + }, payload) { + const code = String(payload?.code || payload?.uuid || '').trim(); + + return new Promise((resolve, reject) => { + if (!code) { + reject({ msg: '缺少工牌标识' }); + return; + } + + http('user.loginByCode', { + code, + device: state.app.device, + project: state.app.project + }).then(response => { + if (response && response.flag === 'failure') { + reject({ msg: response.message || '工牌登录失败' }); + return; + } + + const token = + response?.data?.token || + response?.token || + response?.data?.access_token || + response?.access_token || + response?.data?.accessToken || + response?.accessToken; + + if (!token) { + reject({ msg: response?.message || '登录成功但未返回token' }); + return; + } + + setToken(token); + resolve(response); + }).catch(error => { + reject({ msg: error?.msg || error?.message || error?.data?.message || '工牌登录失败' }); + }); + }) + }, + } const mutations = { diff --git a/core/utils/auth-guard.js b/core/utils/auth-guard.js new file mode 100644 index 0000000..42ac413 --- /dev/null +++ b/core/utils/auth-guard.js @@ -0,0 +1,111 @@ +import { getToken, removeToken } from '@/core/utils/auth' + +export const LOGIN_URL = '/pages/login/index' +export const HOME_URL = '/pages/index/index' + +let redirecting = false + +export function getCurrentRoute() { + const pages = getCurrentPages() + if (!pages || !pages.length) { + return '' + } + const current = pages[pages.length - 1] + return current?.route ? `/${current.route}` : '' +} + +export function isLoginRoute(route = '') { + return route === LOGIN_URL +} + +export function clearSession(store) { + removeToken() + if (!store) return + + try { + store.commit('SET_PERMISSIONS', []) + } catch (e) {} + + try { + store.commit('SET_USER', {}) + } catch (e) {} + + try { + store.commit('SET_ROLEID', 0) + } catch (e) {} +} + +function navigate(url, type) { + const currentRoute = getCurrentRoute() + if (redirecting || currentRoute === url) { + return Promise.resolve() + } + + redirecting = true + + return new Promise(resolve => { + const done = () => { + setTimeout(() => { + redirecting = false + resolve() + }, 80) + } + + if (type === 'switchTab') { + uni.switchTab({ + url, + complete: done + }) + return + } + + uni.reLaunch({ + url, + complete: done + }) + }) +} + +export function goLogin() { + return navigate(LOGIN_URL, 'reLaunch') +} + +export function goHome() { + return navigate(HOME_URL, 'switchTab') +} + +export async function ensureEntry(store, forceValidate = false) { + const token = getToken() + const currentRoute = getCurrentRoute() + + if (!token) { + if (!currentRoute || !isLoginRoute(currentRoute)) { + await goLogin() + } + return { loggedIn: false } + } + + try { + if (forceValidate && store) { + await store.dispatch('getUserInfo', token) + } + + if (!currentRoute || isLoginRoute(currentRoute)) { + await goHome() + } + + return { loggedIn: true } + } catch (error) { + clearSession(store) + await goLogin() + return { loggedIn: false, expired: true, error } + } +} + +export async function handleTokenExpired(store) { + clearSession(store) + const currentRoute = getCurrentRoute() + if (!isLoginRoute(currentRoute)) { + await goLogin() + } +} \ No newline at end of file diff --git a/day.log b/day.log new file mode 100644 index 0000000..810c510 --- /dev/null +++ b/day.log @@ -0,0 +1,9 @@ +2026-03-23 +统计口径:当前工作区不是 git 仓库,以下内容按文件最后修改时间整理。 + +一、今日完成 + +二、问题记录 +暂无 + +三、后续计划 diff --git a/pages/login/index.vue b/pages/login/index.vue index 2884579..eda3dd4 100644 --- a/pages/login/index.vue +++ b/pages/login/index.vue @@ -94,54 +94,235 @@