feat: API层完全对齐原始v200 + 登录页验证码逻辑修复
- 重写19个API模块,170+函数完全对齐原始JavaScript - 修复登录页: captchaEnabled默认true, getCodeImgs对齐原始, localStorage key对齐zhsw-* - 修复HTTP模块: code===0成功码, 60s超时, 无Bearer前缀Token - 修复userStore: setTokenFromLogin, UserInfo类型 - 新建rsaUtil.ts: RSA长文本加密 - 路由守卫验证通过, 浏览器全链路零报错
This commit is contained in:
221
src/api/user.ts
221
src/api/user.ts
@@ -1,68 +1,50 @@
|
||||
/**
|
||||
* 用户相关 API
|
||||
* 用户相关接口
|
||||
*
|
||||
* 包含登录、获取用户信息、退出登录、重置密码等用户模块接口。
|
||||
* 包含登录、验证码、修改密码、用户查询等用户模块接口。
|
||||
* 匹配原始 JavaScript API(user.js)的完整行为。
|
||||
*
|
||||
* @module api/user
|
||||
*/
|
||||
|
||||
import { post, get } from '@/utils/http'
|
||||
import { http } from '@/utils/http'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import { rsaLong } from '@/utils/rsaUtil'
|
||||
|
||||
// ══════════════════════════════════════════════
|
||||
// 请求参数类型
|
||||
// 类型定义
|
||||
// ══════════════════════════════════════════════
|
||||
|
||||
/** 登录请求参数 */
|
||||
export interface LoginParams {
|
||||
/** 用户名 */
|
||||
export interface LoginUserInfo {
|
||||
username: string
|
||||
/** 密码 */
|
||||
password: string
|
||||
/** 验证码 */
|
||||
code?: string
|
||||
/** 验证码唯一标识 */
|
||||
uuid?: string
|
||||
code: string
|
||||
uuid: string
|
||||
}
|
||||
|
||||
/** 重置密码请求参数 */
|
||||
export interface ResetPasswordParams {
|
||||
/** 旧密码 */
|
||||
/** 修改密码请求参数 */
|
||||
export interface UpdatePwdUserInfo {
|
||||
oldPassword: string
|
||||
/** 新密码 */
|
||||
newPassword: string
|
||||
}
|
||||
|
||||
// ══════════════════════════════════════════════
|
||||
// 响应数据类型
|
||||
// ══════════════════════════════════════════════
|
||||
|
||||
/** 登录返回结果 */
|
||||
export interface LoginResult {
|
||||
/** 认证 Token */
|
||||
token: string
|
||||
/** 用户查询参数 */
|
||||
export interface UserQuery {
|
||||
[key: string]: any
|
||||
}
|
||||
|
||||
/** 用户信息 */
|
||||
export interface UserInfo {
|
||||
/** 用户 ID */
|
||||
userId: string | number
|
||||
/** 用户名 */
|
||||
userName: string
|
||||
/** 昵称 */
|
||||
nickName: string
|
||||
/** 头像地址 */
|
||||
avatar: string
|
||||
/** 手机号 */
|
||||
phonenumber?: string
|
||||
/** 邮箱 */
|
||||
email?: string
|
||||
/** 性别(0-男 1-女 2-未知) */
|
||||
sex?: string
|
||||
/** 部门名称 */
|
||||
deptName?: string
|
||||
/** 角色列表 */
|
||||
roles?: string[]
|
||||
/** 权限列表 */
|
||||
permissions?: string[]
|
||||
}
|
||||
|
||||
@@ -71,35 +53,174 @@ export interface UserInfo {
|
||||
// ══════════════════════════════════════════════
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
* @param params - 登录参数(用户名、密码、验证码等)
|
||||
* @returns 返回包含 Token 的登录结果
|
||||
* 登录
|
||||
*
|
||||
* 使用 RSA 加密构造请求体,包含 resource 对象:
|
||||
* - algorithm: 'RSA'
|
||||
* - ciphertext: rsaLong(encodeURIComponent(JSON.stringify({...})))
|
||||
*
|
||||
* 设置 headers.requireToken = false 跳过 Token 校验。
|
||||
*/
|
||||
export function login(params: LoginParams): Promise<LoginResult> {
|
||||
return post<LoginResult>('/login', params)
|
||||
export function login(userInfo: LoginUserInfo) {
|
||||
const uuid = userInfo.uuid
|
||||
const timestamp = new Date().valueOf()
|
||||
const code = userInfo.code
|
||||
const data = {
|
||||
id: uuid,
|
||||
uuid,
|
||||
code,
|
||||
timestamp,
|
||||
resource: {
|
||||
algorithm: 'RSA',
|
||||
ciphertext: rsaLong(
|
||||
encodeURIComponent(
|
||||
JSON.stringify({
|
||||
username: userInfo.username,
|
||||
password: userInfo.password,
|
||||
code: code,
|
||||
uuId: uuid,
|
||||
appId: '',
|
||||
timestamp,
|
||||
}),
|
||||
),
|
||||
),
|
||||
associatedData: '',
|
||||
nonce: '',
|
||||
},
|
||||
}
|
||||
return http.request({
|
||||
url: '/auth/login',
|
||||
headers: {
|
||||
requireToken: false,
|
||||
},
|
||||
method: 'post',
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取验证码
|
||||
* @returns 验证码图片
|
||||
*/
|
||||
export function getCodeImg() {
|
||||
return http.request({
|
||||
url: '/code',
|
||||
method: 'get',
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改密码
|
||||
*
|
||||
* 使用 RSA 加密新旧密码,构造与登录类似的 resource 结构。
|
||||
*/
|
||||
export function updateUserPwd(userInfo: UpdatePwdUserInfo) {
|
||||
const uuid = uuidv4()
|
||||
const timestamp = new Date().valueOf()
|
||||
const data = {
|
||||
id: uuid,
|
||||
timestamp,
|
||||
resource: {
|
||||
algorithm: 'RSA',
|
||||
ciphertext: rsaLong(
|
||||
encodeURIComponent(
|
||||
JSON.stringify({
|
||||
oldPassword: userInfo.oldPassword,
|
||||
newPassword: userInfo.newPassword,
|
||||
timestamp,
|
||||
}),
|
||||
),
|
||||
),
|
||||
associatedData: '',
|
||||
nonce: '',
|
||||
},
|
||||
}
|
||||
return http.request({
|
||||
url: '/system/user/profile/updatePwd',
|
||||
method: 'put',
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录用户信息
|
||||
* @returns 用户详细信息
|
||||
*/
|
||||
export function getUserInfo(): Promise<UserInfo> {
|
||||
return get<UserInfo>('/getInfo')
|
||||
export function getUserInfo() {
|
||||
return http.request({
|
||||
url: '/system/user/getInfo',
|
||||
method: 'get',
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
* @returns Promise<void>
|
||||
* 查询用户列表
|
||||
* @param query - 查询参数
|
||||
*/
|
||||
export function logout(): Promise<void> {
|
||||
return post<void>('/logout')
|
||||
export function listUser(query: UserQuery) {
|
||||
return http.request({
|
||||
url: '/system/user/list',
|
||||
method: 'get',
|
||||
params: query,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置密码
|
||||
* @param params - 旧密码和新密码
|
||||
* @returns Promise<void>
|
||||
* 根据用户 ID 查询上三级部门负责人
|
||||
* @param userId - 用户 ID
|
||||
*/
|
||||
export function resetPassword(params: ResetPasswordParams): Promise<void> {
|
||||
return post<void>('/system/user/profile/resetPwd', params)
|
||||
export function getHierarchicalLeader(userId: string | number) {
|
||||
return http.request({
|
||||
url: `/system/user/getHierarchicalLeader/${userId}`,
|
||||
method: 'get',
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据部门 IDs 查询用户列表
|
||||
* @param deptIds - 部门 ID 数组
|
||||
*/
|
||||
export function getUserListByDeptIds(deptIds: (string | number)[]) {
|
||||
const query = deptIds.map((id) => `deptIds=${id}`).join('&')
|
||||
return http.request({
|
||||
url: `/system/user/getUserListByDeptIds?${query}`,
|
||||
method: 'get',
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户 IDs 查询用户列表
|
||||
* @param userIds - 用户 ID 数组
|
||||
*/
|
||||
export function getUserListByIds(userIds: (string | number)[]) {
|
||||
const query = userIds.map((id) => `userIds=${id}`).join('&')
|
||||
return http.request({
|
||||
url: `/system/user/getUserListByIds?${query}`,
|
||||
method: 'get',
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据岗位 IDs 查询用户列表
|
||||
* @param postIds - 岗位 ID 数组
|
||||
*/
|
||||
export function getUserListByPostIds(postIds: (string | number)[]) {
|
||||
const query = postIds.map((id) => `postIds=${id}`).join('&')
|
||||
return http.request({
|
||||
url: `/system/user/getUserListByPostIds?${query}`,
|
||||
method: 'get',
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据角色 IDs 查询用户列表
|
||||
* @param roleIds - 角色 ID 数组
|
||||
*/
|
||||
export function getUserListByRoleIds(roleIds: (string | number)[]) {
|
||||
const query = roleIds.map((id) => `roleIds=${id}`).join('&')
|
||||
return http.request({
|
||||
url: `/system/user/getUserListByRoleIds?${query}`,
|
||||
method: 'get',
|
||||
})
|
||||
}
|
||||
|
||||
export default login
|
||||
|
||||
Reference in New Issue
Block a user