|
|
@@ -1,6 +1,9 @@
|
|
|
import {
|
|
|
DEMO_ACCOUNT,
|
|
|
DEMO_PASSWORD,
|
|
|
+ getAuthCredentials,
|
|
|
+ logout,
|
|
|
+ saveCurrentUser,
|
|
|
saveLoginSession,
|
|
|
savePermissionData,
|
|
|
saveRememberedCredentials,
|
|
|
@@ -34,20 +37,35 @@ export function saveServerConfig(config) {
|
|
|
return normalized
|
|
|
}
|
|
|
|
|
|
+// H5 dev(pnpm dev:h5)走 Vite 代理避开浏览器 CORS;其他场景仍用配置里的真实地址
|
|
|
+const useDevProxy =
|
|
|
+ typeof window !== 'undefined' &&
|
|
|
+ typeof process !== 'undefined' &&
|
|
|
+ process.env?.NODE_ENV !== 'production'
|
|
|
+
|
|
|
export function getApiBaseUrl(config = getServerConfig()) {
|
|
|
if (config.mode !== 'server' || !config.hostname) return ''
|
|
|
+ if (useDevProxy) return '/api'
|
|
|
const port = config.port ? `:${config.port}` : ''
|
|
|
return `${config.protocol}${config.hostname}${port}/api`
|
|
|
}
|
|
|
|
|
|
function request({ url, method = 'GET', data, token, sessionId, timeout = 15000 }) {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
- const header = token
|
|
|
+ // 未显式传 token 时自动从登录态读取,登录后所有请求自动带上凭证
|
|
|
+ let effectiveToken = token
|
|
|
+ let effectiveSessionId = sessionId
|
|
|
+ if (!effectiveToken) {
|
|
|
+ const stored = getAuthCredentials()
|
|
|
+ effectiveToken = stored.token
|
|
|
+ if (!effectiveSessionId) effectiveSessionId = stored.sessionId
|
|
|
+ }
|
|
|
+ const header = effectiveToken
|
|
|
? {
|
|
|
'content-type': 'application/json',
|
|
|
- 'zoomwin-token': token,
|
|
|
- Authorization: token,
|
|
|
- 'zoomwin-sid': sessionId || '',
|
|
|
+ 'zoomwin-token': effectiveToken,
|
|
|
+ Authorization: effectiveToken,
|
|
|
+ 'zoomwin-sid': effectiveSessionId || '',
|
|
|
}
|
|
|
: { 'content-type': 'application/json', platform: 'wxapp' }
|
|
|
|
|
|
@@ -59,6 +77,10 @@ function request({ url, method = 'GET', data, token, sessionId, timeout = 15000
|
|
|
timeout,
|
|
|
success: (response) => {
|
|
|
if (response.statusCode === 401) {
|
|
|
+ // 服务端认为凭证过期:清本地会话 + 跳登录页
|
|
|
+ logout()
|
|
|
+ uni.showToast({ title: '登录已过期,请重新登录', icon: 'none', duration: 1500 })
|
|
|
+ setTimeout(() => uni.reLaunch({ url: '/pages/login/index' }), 1200)
|
|
|
reject(new Error('身份验证已过期,请重新登录'))
|
|
|
return
|
|
|
}
|
|
|
@@ -106,17 +128,24 @@ function collectAuthorities(tree) {
|
|
|
return authorities
|
|
|
}
|
|
|
|
|
|
-async function loadPermissionTree(baseUrl, userInfo) {
|
|
|
+async function loadPermissionTree(baseUrl) {
|
|
|
const response = await request({
|
|
|
url: `${baseUrl}/system/resources/getResourcesTreePDA`,
|
|
|
- token: userInfo.token,
|
|
|
- sessionId: userInfo.sessionId,
|
|
|
})
|
|
|
const tree = Array.isArray(response.data) ? response.data : []
|
|
|
savePermissionData(tree, collectAuthorities(tree))
|
|
|
return tree
|
|
|
}
|
|
|
|
|
|
+// 拉取当前登录人的扩展资料(部门、岗位、角色等),登录后调用一次写入本地
|
|
|
+export async function fetchCurrentUser(baseUrl) {
|
|
|
+ if (!baseUrl) throw new Error('缺少 baseUrl')
|
|
|
+ const response = await request({
|
|
|
+ url: `${baseUrl}/system/account/getLoginUser`,
|
|
|
+ })
|
|
|
+ return response.data || null
|
|
|
+}
|
|
|
+
|
|
|
export async function performLogin({ account, password, rememberPassword }) {
|
|
|
const config = getServerConfig()
|
|
|
const loginName = String(account || '').trim()
|
|
|
@@ -153,11 +182,17 @@ export async function performLogin({ account, password, rememberPassword }) {
|
|
|
saveLoginSession(data, loginName)
|
|
|
saveRememberedCredentials({ account: loginName, password: loginPwd, rememberPassword })
|
|
|
try {
|
|
|
- await loadPermissionTree(baseUrl, data)
|
|
|
+ await loadPermissionTree(baseUrl)
|
|
|
} catch (error) {
|
|
|
savePermissionData([], [])
|
|
|
uni.showToast({ title: error.message || '权限加载失败', icon: 'none' })
|
|
|
}
|
|
|
+ try {
|
|
|
+ const currentUser = await fetchCurrentUser(baseUrl)
|
|
|
+ if (currentUser) saveCurrentUser(currentUser)
|
|
|
+ } catch (error) {
|
|
|
+ uni.showToast({ title: error.message || '用户信息加载失败', icon: 'none' })
|
|
|
+ }
|
|
|
return data
|
|
|
}
|
|
|
|