login.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import type { IAuthLoginRes, ICaptcha, IDoubleTokenRes, IUpdateInfo, IUpdatePassword, IUserInfoRes } from './types/login'
  2. import { http } from '@/http/http'
  3. /**
  4. * 登录表单
  5. */
  6. export interface ILoginForm {
  7. username: string
  8. password: string
  9. }
  10. /**
  11. * 获取验证码
  12. * @returns ICaptcha 验证码
  13. */
  14. export function getCode() {
  15. return http.get<ICaptcha>('/user/getCode')
  16. }
  17. /**
  18. * 用户登录
  19. * @param loginForm 登录表单
  20. */
  21. export function login(loginForm: ILoginForm) {
  22. return http.post<IAuthLoginRes>('/auth/login', loginForm)
  23. }
  24. /**
  25. * 刷新token
  26. * @param refreshToken 刷新token
  27. */
  28. export function refreshToken(refreshToken: string) {
  29. return http.post<IDoubleTokenRes>('/auth/refreshToken', { refreshToken })
  30. }
  31. /**
  32. * 获取用户信息
  33. */
  34. export function getUserInfo() {
  35. return http.get<IUserInfoRes>('/user/info')
  36. }
  37. /**
  38. * 退出登录
  39. */
  40. export function logout() {
  41. return http.get<void>('/auth/logout')
  42. }
  43. /**
  44. * 修改用户信息
  45. */
  46. export function updateInfo(data: IUpdateInfo) {
  47. return http.post('/user/updateInfo', data)
  48. }
  49. /**
  50. * 修改用户密码
  51. */
  52. export function updateUserPassword(data: IUpdatePassword) {
  53. return http.post('/user/updatePassword', data)
  54. }
  55. /**
  56. * 获取微信登录凭证
  57. * @returns Promise 包含微信登录凭证(code)
  58. */
  59. export function getWxCode() {
  60. return new Promise<UniApp.LoginRes>((resolve, reject) => {
  61. uni.login({
  62. provider: 'weixin',
  63. success: res => resolve(res),
  64. fail: err => reject(new Error(err)),
  65. })
  66. })
  67. }
  68. export function getUserProfile() {
  69. return new Promise<UniApp.GetUserProfileRes>((resolve, reject) => {
  70. uni.getUserInfo({
  71. provider: 'weixin',
  72. desc: '用于获取登录用户信息',
  73. success: res => resolve(res),
  74. fail: err => reject(new Error(err)),
  75. })
  76. })
  77. }
  78. /**
  79. * 微信登录
  80. * @param params 微信登录参数,包含code
  81. * @returns Promise 包含登录结果
  82. */
  83. export function wxLogin(code: string) {
  84. return http.post<IAuthLoginRes>('/couponCenter/APP/wx/login', { code })
  85. }