116 lines
2.7 KiB
TypeScript
116 lines
2.7 KiB
TypeScript
import request from '@/utils/request'
|
|
import store from '@/stores'
|
|
|
|
/** 通用响应结构 */
|
|
export interface ApiResult<T = any> {
|
|
code: string
|
|
msg: string
|
|
data?: T
|
|
timestamp: string
|
|
uuid: string
|
|
}
|
|
|
|
/** 登录成功返回的用户信息 */
|
|
export interface LoginData {
|
|
userId: string
|
|
nick: string
|
|
}
|
|
|
|
/**
|
|
* 发送短信验证码
|
|
* POST /public/sms/sendCode?mobileNumber=xxx
|
|
* 返回 data: true 表示发送成功,data: false 表示发送失败
|
|
*/
|
|
export function sendSmsCode(mobileNumber: string) {
|
|
return request.post<any, ApiResult<boolean>>('/public/sms/sendCode', null, {
|
|
params: { mobileNumber },
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 短信验证码登录
|
|
* POST /public/login/smsLogin
|
|
* Body: { mobileNumber, code, inviteCode? }
|
|
* 登录成功后后端会 Set-Cookie: Token=xxx
|
|
*
|
|
* inviteCode 优先使用传入参数,其次从全局 store 中读取(URL 邀请码)
|
|
* 登录成功后自动清空全局邀请码,避免重复发送
|
|
*/
|
|
export async function smsLogin(mobileNumber: string, code: string, inviteCode?: string) {
|
|
// 优先使用显式传入的邀请码,否则从全局 store 取
|
|
const finalInviteCode = inviteCode || store.state.inviteCode || ''
|
|
|
|
const res = await request.post<any, ApiResult<LoginData>>('/public/login/smsLogin', {
|
|
mobileNumber,
|
|
code,
|
|
...(finalInviteCode ? { inviteCode: finalInviteCode } : {}),
|
|
})
|
|
|
|
// 登录成功后清空全局邀请码
|
|
if (res.code === '0' && store.state.inviteCode) {
|
|
store.commit('SET_INVITE_CODE', '')
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
/**
|
|
* 检查登录状态
|
|
* GET /public/checkLogin
|
|
* 有 Cookie 且认证通过返回 data: true,否则返回 data: false
|
|
*/
|
|
export function checkLogin() {
|
|
return request.get<any, ApiResult<boolean>>('/public/checkLogin')
|
|
}
|
|
|
|
/**
|
|
* 退出登录
|
|
* POST /public/logout
|
|
* 不需要参数,Cookie 会自动携带
|
|
*/
|
|
export function logout() {
|
|
return request.post<any, ApiResult>('/public/logout')
|
|
}
|
|
|
|
/**
|
|
* 注销账号
|
|
* POST /user/manage/cancel
|
|
* Cookie 自动携带 Token
|
|
*/
|
|
export function cancelAccount() {
|
|
return request.post<any, ApiResult>('/user/manage/cancel')
|
|
}
|
|
|
|
/** 用户个人信息 DTO */
|
|
export interface UserInfo {
|
|
/** 用户ID */
|
|
id?: number
|
|
/** 手机号 */
|
|
mobileNumber?: string
|
|
/** 邮箱 */
|
|
email?: string
|
|
/** 昵称 */
|
|
nick?: string
|
|
/** 真实姓名 */
|
|
realName?: string
|
|
/** 头像 */
|
|
picture?: string
|
|
/** 生日 */
|
|
birthday?: { seconds?: number; nanos?: number }
|
|
/** 性别 1男 2女 */
|
|
sex?: number
|
|
/** 邀请码 */
|
|
inviteCode?: string
|
|
/** 注册时间 */
|
|
createTime?: { seconds?: number; nanos?: number }
|
|
}
|
|
|
|
/**
|
|
* 查询个人信息
|
|
* GET /user/manage/info
|
|
* Cookie 自动携带 Token
|
|
*/
|
|
export function fetchUserInfo() {
|
|
return request.get<any, ApiResult<UserInfo>>('/user/manage/info')
|
|
}
|