Files
pig-farm-controller-fe/src/api/user.js
2025-10-31 18:25:29 +08:00

144 lines
3.3 KiB
JavaScript

import http from '../utils/http';
/**
* @typedef {object} CreateUserRequest
* @property {string} username
* @property {string} password
*/
/**
* @typedef {object} CreateUserResponse
* @property {number} id
* @property {string} username
*/
/**
* @typedef {object} LoginRequest
* @property {string} identifier - Identifier 可以是用户名、邮箱、手机号、微信号或飞书账号
* @property {string} password
*/
/**
* @typedef {object} LoginResponse
* @property {number} id
* @property {string} username
* @property {string} token
*/
/**
* @typedef {('成功'|'失败')} AuditStatus
*/
/**
* @typedef {object} UserActionLogDTO
* @property {number} id
* @property {number} user_id
* @property {string} username
* @property {string} action_type
* @property {string} description
* @property {string} http_method
* @property {string} http_path
* @property {string} source_ip
* @property {Array<number>} target_resource
* @property {AuditStatus} status
* @property {string} result_details
* @property {string} time
*/
/**
* @typedef {object} PaginationDTO
* @property {number} page
* @property {number} page_size
* @property {number} total
*/
/**
* @typedef {object} ListUserActionLogResponse
* @property {Array<UserActionLogDTO>} list
* @property {PaginationDTO} pagination
*/
/**
* @typedef {object} UserHistoryParams
* @property {string} [action_type]
* @property {string} [end_time]
* @property {string} [order_by]
* @property {number} [page]
* @property {number} [page_size]
* @property {string} [start_time]
* @property {string} [status]
* @property {number} [user_id]
* @property {string} [username]
*/
/**
* @typedef {('邮件'|'企业微信'|'飞书'|'日志')} NotifierType
*/
/**
* @typedef {object} SendTestNotificationRequest
* @property {NotifierType} type - Type 指定要测试的通知渠道
*/
/**
* @typedef {object} Response
* @property {number} code - 业务状态码
* @property {object} [data] - 业务数据
* @property {string} [message] - 提示信息
*/
/**
* 创建一个新用户
* @param {CreateUserRequest} userData - 用户信息
* @returns {Promise<CreateUserResponse>}
*/
const createUser = (userData) => {
return http.post('/api/v1/users', userData);
};
/**
* 用户登录
* @param {LoginRequest} credentials - 登录凭证
* @returns {Promise<LoginResponse>}
*/
const login = (credentials) => {
return http.post('/api/v1/users/login', credentials);
};
/**
* 获取用户操作日志列表
* @param {UserHistoryParams} params - 查询参数
* @returns {Promise<ListUserActionLogResponse>}
*/
const getUserActionLogs = (params) => {
const newParams = {
action_type: params.action_type,
end_time: params.end_time,
order_by: params.order_by,
page: params.page,
page_size: params.page_size,
start_time: params.start_time,
status: params.status,
user_id: params.user_id,
username: params.username,
};
return http.get('/api/v1/monitor/user-action-logs', { params: newParams });
};
/**
* 发送测试通知
* @param {number} id - 用户ID
* @param {SendTestNotificationRequest} data - 请求体
* @returns {Promise<Response>}
*/
const sendTestNotification = (id, data) => {
return http.post(`/api/v1/users/${id}/notifications/test`, data);
};
export const UserApi = {
createUser,
login,
getUserActionLogs,
sendTestNotification,
};