login.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import type {
  2. AuthPermissionInfo,
  3. IAuthLoginRes,
  4. ICaptcha,
  5. IDoubleTokenRes,
  6. IUpdateInfo,
  7. IUpdatePassword,
  8. IUserInfoRes,
  9. } from './types/login'
  10. import { http } from '@/http/http'
  11. /**
  12. * 登录表单
  13. */
  14. export interface ILoginForm {
  15. type: 'username' | 'register' | 'sms'
  16. username?: string
  17. password?: string
  18. nickname?: string
  19. captchaVerification?: string
  20. mobile?: string
  21. code?: string
  22. }
  23. /** 账号密码登录 Request VO */
  24. export interface AuthLoginReqVO {
  25. password?: string
  26. username?: string
  27. captchaVerification?: string
  28. // 绑定社交登录时,需要传递如下参数
  29. socialType?: number
  30. socialCode?: string
  31. socialState?: string
  32. }
  33. /** 注册 Request VO */
  34. export interface AuthRegisterReqVO {
  35. username: string
  36. password: string
  37. captchaVerification: string
  38. }
  39. /** 短信登录 Request VO */
  40. export interface AuthSmsLoginReqVO {
  41. mobile: string
  42. code: string
  43. }
  44. /** 发送短信验证码 Request VO */
  45. export interface AuthSmsSendReqVO {
  46. mobile: string
  47. scene: number
  48. }
  49. /** 租户信息 */
  50. export interface TenantVO {
  51. id: number
  52. name: string
  53. }
  54. /** 重置密码 Request VO */
  55. export interface AuthResetPasswordReqVO {
  56. mobile: string
  57. code: string
  58. password: string
  59. }
  60. /**
  61. * 获取验证码
  62. * @returns ICaptcha 验证码
  63. */
  64. export function getCode(data: any) {
  65. return http.postOriginal<ICaptcha>('/system/captcha/get', data)
  66. }
  67. /**
  68. * 校验验证码
  69. * @param data 验证码校验参数
  70. * @returns Promise 包含校验结果
  71. */
  72. export function checkCaptcha(data: any) {
  73. return http.postOriginal('/system/captcha/check', data)
  74. }
  75. /** 使用账号密码登录 */
  76. export function login(data: AuthLoginReqVO) {
  77. return http.post<IAuthLoginRes>('/system/auth/login', data)
  78. }
  79. /** 注册用户 */
  80. export function register(data: AuthRegisterReqVO) {
  81. return http.post<IAuthLoginRes>('/system/auth/register', data)
  82. }
  83. /** 短信登录 */
  84. export function smsLogin(data: AuthSmsLoginReqVO) {
  85. return http.post<IAuthLoginRes>('/system/auth/sms-login', data)
  86. }
  87. /** 发送短信验证码 */
  88. export function sendSmsCode(data: AuthSmsSendReqVO) {
  89. return http.post<boolean>('/system/auth/send-sms-code', data)
  90. }
  91. /** 获取租户简单列表 */
  92. export function getTenantSimpleList() {
  93. return http.get<TenantVO[]>('/system/tenant/simple-list')
  94. }
  95. /** 根据租户域名获取租户信息 */
  96. export function getTenantByWebsite(website: string) {
  97. return http.get<TenantVO>(`/system/tenant/get-by-website?website=${website}`)
  98. }
  99. /** 通过短信重置密码 */
  100. export function smsResetPassword(data: AuthResetPasswordReqVO) {
  101. return http.post<boolean>('/system/auth/reset-password', data)
  102. }
  103. /**
  104. * 刷新token
  105. * @param refreshToken 刷新token
  106. */
  107. export function refreshToken(refreshToken: string) {
  108. return http.post<IDoubleTokenRes>(`/system/auth/refresh-token?refreshToken=${refreshToken}`)
  109. }
  110. /**
  111. * 获取用户信息
  112. */
  113. export function getUserInfo() {
  114. return http.get<IUserInfoRes>('/user/info')
  115. }
  116. /**
  117. * 获取权限信息
  118. */
  119. export function getAuthPermissionInfo() {
  120. return http.get<AuthPermissionInfo>('/system/auth/get-permission-info')
  121. }
  122. /** 退出登录 */
  123. export function logout() {
  124. return http.post<void>('/system/auth/logout')
  125. }
  126. /**
  127. * 修改用户信息
  128. */
  129. export function updateInfo(data: IUpdateInfo) {
  130. return http.post('/user/updateInfo', data)
  131. }
  132. /**
  133. * 修改用户密码
  134. */
  135. export function updateUserPassword(data: IUpdatePassword) {
  136. return http.post('/user/updatePassword', data)
  137. }
  138. /**
  139. * 获取微信登录凭证
  140. * @returns Promise 包含微信登录凭证(code)
  141. */
  142. export function getWxCode() {
  143. return new Promise<UniApp.LoginRes>((resolve, reject) => {
  144. uni.login({
  145. provider: 'weixin',
  146. success: res => resolve(res),
  147. fail: err => reject(new Error(err)),
  148. })
  149. })
  150. }
  151. /**
  152. * 微信登录
  153. * @param params 微信登录参数,包含code
  154. * @returns Promise 包含登录结果
  155. */
  156. export function wxLogin(data: { code: string }) {
  157. return http.post<IAuthLoginRes>('/auth/wxLogin', data)
  158. }