api.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { api } from '@/lib/api'
  2. import type {
  3. ApiResponse,
  4. UserProfile,
  5. UpdateUserRequest,
  6. UpdateUserSettingsRequest,
  7. DeleteAccountRequest,
  8. CheckinStatusResponse,
  9. CheckinResponse,
  10. } from './types'
  11. // ============================================================================
  12. // User Profile APIs
  13. // ============================================================================
  14. /**
  15. * Get current user profile
  16. */
  17. export async function getUserProfile(): Promise<ApiResponse<UserProfile>> {
  18. const res = await api.get('/api/user/self')
  19. return res.data
  20. }
  21. /**
  22. * Update user profile
  23. */
  24. export async function updateUserProfile(
  25. data: UpdateUserRequest
  26. ): Promise<ApiResponse> {
  27. const res = await api.put('/api/user/self', data)
  28. return res.data
  29. }
  30. /**
  31. * Update user settings
  32. */
  33. export async function updateUserSettings(
  34. data: UpdateUserSettingsRequest
  35. ): Promise<ApiResponse> {
  36. const res = await api.put('/api/user/setting', data)
  37. return res.data
  38. }
  39. /**
  40. * Delete user account
  41. */
  42. export async function deleteUserAccount(
  43. data?: DeleteAccountRequest
  44. ): Promise<ApiResponse> {
  45. const res = await api.delete('/api/user/self', { data })
  46. return res.data
  47. }
  48. /**
  49. * Generate/regenerate system access token
  50. */
  51. export async function generateAccessToken(): Promise<ApiResponse<string>> {
  52. const res = await api.get('/api/user/token')
  53. return res.data
  54. }
  55. // ============================================================================
  56. // Account Binding APIs
  57. // ============================================================================
  58. /**
  59. * Send email verification code
  60. */
  61. export async function sendEmailVerification(
  62. email: string,
  63. turnstileToken?: string
  64. ): Promise<ApiResponse> {
  65. const params = new URLSearchParams({ email })
  66. if (turnstileToken) {
  67. params.append('turnstile', turnstileToken)
  68. }
  69. const res = await api.get(`/api/verification?${params}`)
  70. return res.data
  71. }
  72. /**
  73. * Bind email account
  74. */
  75. export async function bindEmail(
  76. email: string,
  77. code: string
  78. ): Promise<ApiResponse> {
  79. const res = await api.post('/api/oauth/email/bind', {
  80. email,
  81. code,
  82. })
  83. return res.data
  84. }
  85. /**
  86. * Bind WeChat account
  87. */
  88. export async function bindWeChat(code: string): Promise<ApiResponse> {
  89. const res = await api.get(`/api/oauth/wechat/bind?code=${code}`)
  90. return res.data
  91. }
  92. // ============================================================================
  93. // Custom OAuth Binding APIs
  94. // ============================================================================
  95. export interface CustomOAuthBinding {
  96. provider_id: string
  97. provider_name: string
  98. external_id?: string
  99. }
  100. /**
  101. * Get current user's custom OAuth bindings
  102. */
  103. export async function getSelfOAuthBindings(): Promise<
  104. ApiResponse<CustomOAuthBinding[]>
  105. > {
  106. const res = await api.get('/api/user/oauth/bindings')
  107. return res.data
  108. }
  109. /**
  110. * Unbind a custom OAuth provider for current user
  111. */
  112. export async function unbindCustomOAuth(
  113. providerId: string
  114. ): Promise<ApiResponse> {
  115. const res = await api.delete(`/api/user/oauth/bindings/${providerId}`)
  116. return res.data
  117. }
  118. // ============================================================================
  119. // Checkin APIs
  120. // ============================================================================
  121. /**
  122. * Get checkin status for a specific month
  123. */
  124. export async function getCheckinStatus(
  125. month: string
  126. ): Promise<ApiResponse<CheckinStatusResponse>> {
  127. const res = await api.get(`/api/user/checkin?month=${month}`)
  128. return res.data
  129. }
  130. /**
  131. * Perform daily checkin
  132. */
  133. export async function performCheckin(
  134. turnstileToken?: string
  135. ): Promise<ApiResponse<CheckinResponse>> {
  136. const url = turnstileToken
  137. ? `/api/user/checkin?turnstile=${encodeURIComponent(turnstileToken)}`
  138. : '/api/user/checkin'
  139. const res = await api.post(url)
  140. return res.data
  141. }