user.ts 10.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
import { service as request } from "@/utils/request.ts";
import type { AxiosPromise } from "axios";

// 定义接口类型
interface LoginParams {
  email: string;
  password: string;
  remember?: boolean;
}

interface PhoneLoginParams {
  phoneNumber: string; // 手机号
  smsCode: string; // 验证码
  remember?: boolean; // 记住我(可选)
}

interface PhonePasswordLoginParams {
  phoneNumber: string; // 手机号
  password: string; // 密码
  remember?: boolean; // 记住我(可选)
}

interface RegisterParams {
  email: string;
  password: string;
  realName: string;
  inviteCode?: string;
}

interface PhoneRegisterParams {
  realName: string; // 真实姓名
  phoneNumber: string; // 手机号
  password: string; // 密码
  smsCode: string; // 验证码
  inviteCode?: string; // 邀请码(可选)
}

interface ForgotPasswordParams {
  email: string;
}

interface ResetPasswordParams {
  token: string;
  newPassword: string;
}

interface ResetPasswordBySmsParams {
  phoneNumber: string; // 手机号
  smsCode: string; // 验证码
  newPassword: string; // 新密码
}

interface LoginResponse {
  token: string;
  refreshToken?: string;
  userId: string;
  email: string;
  role: string;
}

interface PhoneLoginResponse {
  token?: string; // 访问令牌
  refreshToken?: string; // 刷新令牌(仅在记住我时返回)
  userId?: number; // 用户ID
  email?: string; // 邮箱
  phoneNumber?: string; // 手机号
  wechatNickname?: string; // 微信昵称
}

interface UserProfile {
  id: number;
  email: string; // 邮件
  phoneNumber?: number; // 手机号
  wechatNickname?: string; // 微信昵称
  name: string; // 用户名
  realName?: string; // 真实姓名
  roleName?: string; // 角色名称 (后端确认将加入)
  gender?: string; // 性别
  institution?: string; // 所属医院/公司/机构
  field?: string; // 领域
  title?: string; // 职称
  department?: string; // 所属部门
  region?: string; // 所在地区
  bio?: string; // 个人简介
  personalInviteCode?: string; // 个人邀请码
  dictJson?: string; // 所在地区字典json
}

export interface RegionNode {
  id?: number;
  code?: string;
  name?: string;
  pCode?: string;
  children?: RegionNode[];
}

export interface DictItem {
  id?: number;
  code?: string;
  name?: string;
  pinyinCode?: string;
}

interface ChangePasswordParams {
  oldPassword: string; // 旧密码
  newPassword: string; // 新密码
}

interface BindPhoneParams {
  userId?: number; // 用户id(必需)
  phoneNumber: string; // 手机号(必需)
  smsCode: string; // 验证码(必需)
  modify?: boolean; // 是否换绑(可选,默认false)
}

interface UnbindPhoneParams {
  userId?: number; // 用户id(可选)
  phoneNumber: string; // 手机号(必需,正则:^1[3-9]\d{9}$)
}

interface UnbindEmailParams {
  email: string; // 邮箱(必需,>= 1 字符)
  password: string; // 密码(必需,>= 6 字符,<= 20 字符)
}

export interface ResponseMetadata {
  RequestId?: string;
  Action?: string;
  Version?: string;
  Service?: string;
  Region?: string;
  Result?: any;
}

export const apiGetDictItems = (
  code: string,
  bdCode?: string,
): AxiosPromise<DictItem[]> =>
  request.get(`/users/base-data/dicts/${code}`, {
    params: bdCode ? { bd_code: bdCode } : undefined,
  });

// 更新用户资料请求参数
interface UpdateUserProfileParams {
  name?: string; // 姓名
  realName?: string; // 真实姓名
  gender?: string; // 性别
  institution?: string; // 所属医院/公司/机构
  field?: string; // 领域
  title?: string; // 职称
  department?: string; // 所属部门
  region?: string; // 所在地区
  bio?: string; // 个人简介
  dictJson?: string; // 所在地区字典json
}

// 认证状态枚举
export type CertificationStatus =
  | "UNVERIFIED"
  | "PENDING"
  | "APPROVED"
  | "REJECTED";

export interface VerificationImageItem {
  path: string;
  url: string;
  size: number;
  lastModifiedEpochMs: number;
  contentType: string;
}

// 注册
export const apiRegister = (payload: RegisterParams): AxiosPromise =>
  request.post("/users/register", payload);

// 手机号注册
export const apiRegisterByPhone = (
  payload: PhoneRegisterParams,
): AxiosPromise<UserProfile> => request.post("/users/register/phone", payload);

// 登录
export const apiLogin = (payload: LoginParams): AxiosPromise<LoginResponse> =>
  request.post("/users/login", payload);

// 手机验证码登录
export const apiLoginByPhone = (
  payload: PhoneLoginParams,
): AxiosPromise<PhoneLoginResponse> =>
  request.post("/users/login/sms", payload);

// 手机密码登录
export const apiLoginByPhonePassword = (
  payload: PhonePasswordLoginParams,
): AxiosPromise<PhoneLoginResponse> =>
  request.post("/users/login/phone", payload);

// 验证邮箱
export const apiVerify = (token: string): AxiosPromise =>
  request.get("/users/verify", { params: { token } });

// 重新发送激活邮件
export const apiResendActivation = (email: string): AxiosPromise =>
  request.get("/users/resend-activation", { params: { email } });

// 忘记密码
export const apiForgotPassword = (
  payload: ForgotPasswordParams,
): AxiosPromise => request.post("/users/password/forgot", payload);

// 重置密码
export const apiResetPassword = (payload: ResetPasswordParams): AxiosPromise =>
  request.post("/users/password/reset", payload);

// 手机号重置密码
export const apiResetPasswordBySms = (
  payload: ResetPasswordBySmsParams,
): AxiosPromise => request.post("/users/password/reset/sms", payload);

// 修改密码
export const apiChangePassword = (
  payload: ChangePasswordParams,
): AxiosPromise => request.post("/users/password/change", payload);

// 获取用户信息
export const apiProfile = (): AxiosPromise<UserProfile> =>
  request.get("/users/profile");

// 获取用户信息 by id
export const apiGetById = (id: string): AxiosPromise =>
  request.get(`/users/${id}`);

// 获取邮箱信息 by email
export const apiGetByEmail = (email: string): AxiosPromise =>
  request.get("/users/by-email", { params: { email } });

// 刷新token
export const apiRefreshToken = (refreshToken: string): AxiosPromise =>
  request.post("/users/refresh", { refreshToken });

// 退出登录
export const apiLogout = (refreshToken: string): AxiosPromise =>
  request.post("/users/logout", { refreshToken });

// 加载地区树数据
export const apiGetRegionsTree = (): AxiosPromise<RegionNode[]> =>
  request.get("/users/base-data/regions/tree");

// 获取用户信息
export const getUserProfile = async (): Promise<UserProfile> => {
  try {
    const { data } = await apiProfile();
    return data;
  } catch (error) {
    console.error("获取用户信息失败:", error);
    throw error;
  }
};

// 获取用户头像
export const apiGetUserAvatar = (id: number): AxiosPromise<Blob> =>
  request.get(`/users/${id}/avatar`, {
    responseType: "blob",
  });

// 更新用户头像
export const apiUpdateAvatar = (file: File): AxiosPromise => {
  const formData = new FormData();
  formData.append("file", file);
  return request.post("/users/avatar", formData, {
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
};

// 获取用户认证图片
export const apiGetVerificationImage = (id: number): AxiosPromise<Blob> =>
  request.get(`/users/${id}/verification-image`, {
    responseType: "blob",
  });

// 获取用户多张认证图片列表
export const apiGetVerificationImages = (
  id: number,
): AxiosPromise<VerificationImageItem[]> =>
  request.get(`/users/${id}/verification-images`);

// 更新用户认证图片
export const apiUpdateVerificationImage = (file: File): AxiosPromise => {
  const formData = new FormData();
  formData.append("file", file);
  return request.post("/users/verification-image", formData, {
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
};

// 上传多张用户认证图片
export const apiUpdateVerificationImages = (
  files: File[],
): AxiosPromise<VerificationImageItem[]> => {
  const formData = new FormData();
  files.forEach((file) => {
    formData.append("files", file);
  });
  return request.post("/users/verification-images", formData, {
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
};

// 删除单张用户认证图片
export const apiDeleteVerificationImage = (
  filename: string,
): AxiosPromise =>
  request.delete(`/users/verification-images/${encodeURIComponent(filename)}`);

// 提交认证当前用户
export const apiSubmitCertification = (): AxiosPromise =>
  request.get("/users/submit-certification");

// 获取当前用户认证状态
export const apiGetCertificationStatus = (): AxiosPromise<{
  status?: CertificationStatus;
  notes?: string;
  isSales?: boolean;
  salesPosition?: string;
  salesTitle?: string;
}> => request.get("/users/certification-status", { _silent: true } as any);

// 更新用户信息
export const apiUpdateProfile = (
  payload: UpdateUserProfileParams,
): AxiosPromise<UserProfile> => request.put("/users/profile", payload);

// 绑定手机号
export const apiBindPhone = (
  payload: BindPhoneParams,
): AxiosPromise<UserProfile> => request.post("/users/bind-phone", payload);

// 解绑手机号
export const apiUnbindPhone = (
  payload: UnbindPhoneParams,
): AxiosPromise<UserProfile> => request.post("/users/unbind-phone", payload);

// 解绑邮箱
export const apiUnbindEmail = (
  payload: UnbindEmailParams,
): AxiosPromise<UserProfile> =>
  request.post("/users/bind-email/unbind", payload);

// 发送注册验证码
export const apiSendRegisterSms = (
  phoneNumber: string,
): AxiosPromise<ResponseMetadata> =>
  request.post("/sms/send/register", null, {
    params: { phoneNumber },
  });

// 发送登录验证码
export const apiSendLoginSms = (
  phoneNumber: string,
): AxiosPromise<ResponseMetadata> =>
  request.post("/sms/send/login", null, {
    params: { phoneNumber },
  });

// 发送忘记密码验证码
export const apiSendForgotPasswordSms = (
  phoneNumber: string,
): AxiosPromise<ResponseMetadata> =>
  request.post("/sms/send/forgot", null, {
    params: { phoneNumber },
  });

// 发送绑定验证码
export const apiSendBindSms = (
  phoneNumber: string,
): AxiosPromise<ResponseMetadata> =>
  request.post("/sms/send/bind", null, {
    params: { phoneNumber },
  });

// 发送绑定邮箱确认邮件
export const apiSendBindEmail = (
  email: string,
): AxiosPromise<ResponseMetadata> =>
  request.post("/users/bind-email/send", null, {
    params: { email },
  });

// Mock数据
export const getMockUserProfile = (): UserProfile => ({
  id: 0,
  email: "",
  name: "",
  realName: undefined,
  phoneNumber: undefined,
  wechatNickname: undefined,
  gender: undefined,
  institution: undefined,
  field: undefined,
  title: undefined,
  department: undefined,
  region: undefined,
  bio: undefined,
  personalInviteCode: undefined,
});