| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import { ApiError, getApiUrl, requestApi, type ApiResponse } from './api'
- import type { UserProfile } from './auth'
- import { getToken } from '@/utils/auth'
- export interface UpdateProfilePayload {
- nickName: string
- phonenumber: string
- email: string
- sex: string
- }
- export interface AvatarResponse extends ApiResponse {
- imgUrl?: string
- }
- export function updateUserProfile(payload: UpdateProfilePayload) {
- return requestApi<ApiResponse>('/system/user/profile', {
- method: 'PUT',
- data: payload,
- })
- }
- export function updatePassword(oldPassword: string, newPassword: string) {
- return requestApi<ApiResponse>('/system/user/profile/updatePwd', {
- method: 'PUT',
- data: { oldPassword, newPassword },
- })
- }
- export function uploadAvatar(filePath: string): Promise<AvatarResponse> {
- return new Promise((resolve, reject) => {
- uni.uploadFile({
- url: getApiUrl('/system/user/profile/avatar'),
- filePath,
- name: 'avatarfile',
- header: { Authorization: `Bearer ${getToken()}` },
- timeout: 20000,
- success(response) {
- if (response.statusCode < 200 || response.statusCode >= 300) {
- reject(new ApiError(`系统接口 ${response.statusCode} 异常`, response.statusCode))
- return
- }
- try {
- const result = JSON.parse(response.data) as AvatarResponse
- const code = result.code ?? 200
- if (code === 200) resolve(result)
- else reject(new ApiError(result.msg || result.message || '头像上传失败', code))
- } catch {
- reject(new ApiError('头像上传响应格式异常'))
- }
- },
- fail(error) {
- const message = error.errMsg?.includes('timeout') ? '头像上传超时' : '头像上传失败'
- reject(new ApiError(message))
- },
- })
- })
- }
- export function resolveAvatarUrl(avatar?: UserProfile['avatar']) {
- if (!avatar) return ''
- if (/^(https?:|data:|blob:)/.test(avatar)) return avatar
- return getApiUrl(avatar)
- }
|