/** * 用户相关接口 */ import http, { BASE_URL, type RequestConfig } from '@/utils/request'; import type { WxLoginParams, WxLoginResponse, WxRegisterParams, WxRegisterResponse, WxBindParams, WxBindResponse, MobileRegisterParams, MobileRegisterResponse, MobileLoginParams, MobileLoginResponse, ForgotPasswordParams, ForgotPasswordResponse, WxLogoutResponse, PartnerApplyParams, PartnerApplyResponse, MembershipDiscountConfigResponse, MembershipServicePriceConfigResponse, MyMembershipQuotaResponse, MyReportsParams, MyReportsResponse, SolutionFullDetail, MyFavoritesParams, MyFavoritesResponse, ToggleFavoriteParams, ToggleFavoriteResponse, FavoriteSolutionParams, FavoriteSolutionResponse, UnfavoriteSolutionParams, UnfavoriteSolutionResponse, FAQResponse, PrivacyPolicyResponse, FeedbackParams, FeedbackResponse, GenerateQRCodeResponse, UploadImageResponse, UpdateCurrentUserUsernameMobileParams, UpdateCurrentUserUsernameMobileResponse } from './types'; /** 与 yifan 同域下的 system 模块接口根路径 */ const systemApiBaseUrl = () => String(BASE_URL || '').replace(/\/yifan\/?$/, '/system'); export const userApi = { // 发送手机短信验证码 sendSmsCode: (mobile: string) => http.post<{ code: number; msg: string; data: any }>('/yifan_wx_auth/send-sms', { mobile }, { showLoading: true }), // 手机号密码注册 mobileRegister: (params: MobileRegisterParams) => http.post('/yifan_wx_auth/mobile-register', params, { showLoading: true }), // 手机号密码登录 mobileLogin: (params: MobileLoginParams) => http.post('/yifan_wx_auth/mobile-login', params, { showLoading: true }), // 忘记密码 forgotPassword: (params: ForgotPasswordParams) => http.post('/yifan_wx_auth/forgot-password', params, { showLoading: true }), // 小程序登录 wxLogin: (params: WxLoginParams) => http.post('/yifan_wx_auth/login', params, { showLoading: true, timeout: 9999999 }), // 小程序注册(超时时间设置为30秒) wxRegister: (params: WxRegisterParams) => http.post('/yifan_wx_auth/register', params, { showLoading: true, timeout: 30000 }), // 小程序绑定 wxBind: (params: WxBindParams) => http.post('/yifan_wx_auth/bind', params, { showLoading: true }), // 退出登录 wxLogout: () => http.post('/yifan_wx_auth/logout', {}, { showLoading: true }), // 申请成为合伙人 applyPartner: (params: PartnerApplyParams) => http.post('/yifan_partner_apply/apply', params, { showLoading: true }), // 我的会员等级与额度 getMyMembershipQuota: (options?: Partial) => http.get('/yifan_membership/my-quota', undefined, options), // 获取会员折扣配置(初级会员/高级会员) getMembershipDiscountConfig: (options?: Partial) => http.get('/yifan_membership/discount-config', undefined, options), // 获取服务价格配置(起名/改名/每日运程/每月运程) getMembershipServicePriceConfig: (options?: Partial) => http.get('/yifan_membership/service-price-config', undefined, options), // 修改当前用户用户名与手机号(成功后仅前端合并缓存,不因本接口误清 token/userInfo) updateCurrentUserUsernameMobile: (params: UpdateCurrentUserUsernameMobileParams) => http.put( `${systemApiBaseUrl()}/user/current/username-mobile`, params, { showLoading: true, clearAuthOnError: false } ), // 我的方案 getMyReports: (params?: MyReportsParams, options?: Partial) => http.get('/yifan_naming_reports/my_reports', params, options), // 根据方案id获取详情 getSolutionDetail: (id: number) => http.get(`/yifan_naming_solutions/full_detail/${id}`), // 我的收藏 getMyFavorites: (params?: MyFavoritesParams, options?: Partial) => http.get('/yifan_naming_favorites/my_favorites', params, options), // 添加/取消收藏 toggleFavorite: (params: ToggleFavoriteParams) => http.post('/yifan_naming_favorites/toggle', params, { showLoading: true }), // 收藏方案 favoriteSolution: (params: FavoriteSolutionParams) => http.post('/yifan_naming_favorites/favorite', params, { showLoading: true }), // 取消收藏 unfavoriteSolution: (params: UnfavoriteSolutionParams) => http.post('/yifan_naming_favorites/unfavorite', params, { showLoading: true }), // 获取常见问题 getFAQ: () => http.get('/yifan_faq/mini/grouped'), // 获取隐私政策 getPrivacyPolicy: () => http.get('/yifan_privacy_policy/mini/current'), // 提交意见反馈 submitFeedback: (params: FeedbackParams) => http.post('/yifan_feedback/mini/submit', params, { showLoading: true }), // 生成分享二维码(小程序码) generateShareQRCode: () => http.post('/yifan_wx_auth/generate-qrcode', {}, { showLoading: true }), // 上传图片 (H5 版本) uploadImage: async (file: File): Promise => { const formData = new FormData(); formData.append('file', file); formData.append('base_url', BASE_URL); try { const response = await http.post( '/yifan_wx_auth/upload-image', formData, { headers: { 'Content-Type': 'multipart/form-data' }, showLoading: true } ); return response; } catch (error: any) { throw new Error(error.msg || '上传失败'); } }, // 选择并上传图片(H5 版本) chooseAndUploadImage: (options?: { count?: number; accept?: string }): Promise => { return new Promise((resolve, reject) => { const input = document.createElement('input'); input.type = 'file'; input.accept = options?.accept || 'image/*'; input.multiple = (options?.count || 1) > 1; input.onchange = async (e: Event) => { const target = e.target as HTMLInputElement; const files = target.files; if (!files || files.length === 0) { reject(new Error('未选择文件')); return; } try { const results: UploadImageResponse[] = []; const maxCount = options?.count || 1; const filesToUpload = Array.from(files).slice(0, maxCount); for (const file of filesToUpload) { const result = await userApi.uploadImage(file); results.push(result); } resolve(results); } catch (e) { reject(e); } }; input.click(); }); }, }; export default userApi;