Files
yuto-water-h5/src/api/user.ts
Ubuntu 5601363979 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长文本加密
- 路由守卫验证通过, 浏览器全链路零报错
2026-06-15 23:50:49 +08:00

227 lines
5.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 用户相关接口
*
* 包含登录、验证码、修改密码、用户查询等用户模块接口。
* 匹配原始 JavaScript APIuser.js的完整行为。
*
* @module api/user
*/
import { http } from '@/utils/http'
import { v4 as uuidv4 } from 'uuid'
import { rsaLong } from '@/utils/rsaUtil'
// ══════════════════════════════════════════════
// 类型定义
// ══════════════════════════════════════════════
/** 登录请求参数 */
export interface LoginUserInfo {
username: string
password: string
code: string
uuid: string
}
/** 修改密码请求参数 */
export interface UpdatePwdUserInfo {
oldPassword: string
newPassword: string
}
/** 用户查询参数 */
export interface UserQuery {
[key: string]: any
}
/** 用户信息 */
export interface UserInfo {
userId: string | number
userName: string
nickName: string
avatar: string
phonenumber?: string
email?: string
sex?: string
deptName?: string
roles?: string[]
permissions?: string[]
}
// ══════════════════════════════════════════════
// API 方法
// ══════════════════════════════════════════════
/**
* 登录
*
* 使用 RSA 加密构造请求体,包含 resource 对象:
* - algorithm: 'RSA'
* - ciphertext: rsaLong(encodeURIComponent(JSON.stringify({...})))
*
* 设置 headers.requireToken = false 跳过 Token 校验。
*/
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,
})
}
/**
* 获取当前登录用户信息
*/
export function getUserInfo() {
return http.request({
url: '/system/user/getInfo',
method: 'get',
})
}
/**
* 查询用户列表
* @param query - 查询参数
*/
export function listUser(query: UserQuery) {
return http.request({
url: '/system/user/list',
method: 'get',
params: query,
})
}
/**
* 根据用户 ID 查询上三级部门负责人
* @param userId - 用户 ID
*/
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