| | |
| | | import request from '@/utils/http' |
| | | |
| | | function buildLoginPayload({ username, password }) { |
| | | return { username, password } |
| | | } |
| | | |
| | | function normalizeLoginParams(params) { |
| | | return { |
| | | username: params?.username || params?.userName || '', |
| | | password: params?.password |
| | | } |
| | | } |
| | | |
| | | function normalizeLoginResponse(payload) { |
| | | const data = payload?.data || payload || {} |
| | | return { |
| | | accessToken: data.accessToken || '', |
| | | refreshToken: data.refreshToken || '', |
| | | user: data.user || {} |
| | | } |
| | | } |
| | | |
| | | function normalizeUserInfo(data) { |
| | | const normalizedRoles = normalizeRoleCodes(data?.roles) |
| | | const normalizedButtons = normalizeButtonMarks(data) |
| | | return { |
| | | ...data, |
| | | roles: normalizedRoles, |
| | | buttons: normalizedButtons |
| | | } |
| | | } |
| | | |
| | | function normalizeRoleCodes(roles) { |
| | | if (!Array.isArray(roles)) { |
| | | return [] |
| | | } |
| | | |
| | | return Array.from( |
| | | new Set( |
| | | roles |
| | | .map((item) => { |
| | | if (typeof item === 'string') { |
| | | return item.trim() |
| | | } |
| | | if (item && typeof item === 'object') { |
| | | return item.code || item.name || '' |
| | | } |
| | | return '' |
| | | }) |
| | | .filter(Boolean) |
| | | ) |
| | | ) |
| | | } |
| | | |
| | | function normalizeButtonMarks(data) { |
| | | const directButtons = Array.isArray(data?.buttons) ? data.buttons : [] |
| | | const authorityButtons = Array.isArray(data?.authorities) |
| | | ? data.authorities.map((item) => item?.authority || item?.authMark || '') |
| | | : [] |
| | | |
| | | return Array.from( |
| | | new Set( |
| | | [...directButtons, ...authorityButtons] |
| | | .map((item) => (typeof item === 'string' ? item.trim() : '')) |
| | | .filter(Boolean) |
| | | ) |
| | | ) |
| | | } |
| | | |
| | | function fetchLogin(params) { |
| | | return request.post({ |
| | | url: '/api/auth/login', |
| | | params |
| | | // showSuccessMessage: true // 显示成功消息 |
| | | // showErrorMessage: false // 不显示错误消息 |
| | | }) |
| | | return request |
| | | .post({ |
| | | url: '/login', |
| | | params: buildLoginPayload(normalizeLoginParams(params)) |
| | | // showSuccessMessage: true // 显示成功消息 |
| | | // showErrorMessage: false // 不显示错误消息 |
| | | }) |
| | | .then((response) => { |
| | | const normalized = normalizeLoginResponse(response) |
| | | return { |
| | | token: normalized.accessToken, |
| | | accessToken: normalized.accessToken, |
| | | refreshToken: normalized.refreshToken, |
| | | user: normalized.user |
| | | } |
| | | }) |
| | | } |
| | | function fetchGetUserInfo() { |
| | | return request |
| | | .get({ |
| | | url: '/auth/user' |
| | | // 自定义请求头 |
| | | // headers: { |
| | | // 'X-Custom-Header': 'your-custom-value' |
| | | // } |
| | | }) |
| | | .then((response) => normalizeUserInfo(response)) |
| | | } |
| | | function fetchGetMenuList() { |
| | | return request.get({ |
| | | url: '/api/user/info' |
| | | // 自定义请求头 |
| | | // headers: { |
| | | // 'X-Custom-Header': 'your-custom-value' |
| | | // } |
| | | url: '/auth/menu' |
| | | }) |
| | | } |
| | | export { fetchGetUserInfo, fetchLogin } |
| | | export { |
| | | buildLoginPayload, |
| | | fetchGetMenuList, |
| | | fetchGetUserInfo, |
| | | fetchLogin, |
| | | normalizeLoginResponse, |
| | | normalizeUserInfo |
| | | } |