feat: refine profile and home UI with dynamic fortune rules
This commit is contained in:
1 parent
869376979c
commit
4608dbd5cf
59 files changed
+3424
-1347
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import Taro from '@tarojs/taro'
|
||||
import { frontendPreviewProfile, isFrontendPreview } from './frontendPreview'
|
||||
|
||||
const BASE_URL = 'http://localhost:8080'
|
||||
|
||||
@@ -11,6 +12,12 @@ type ApiResult<T> = {
|
||||
type RequestOptions = Omit<Taro.request.Option, 'url'>
|
||||
|
||||
export async function request<T>(url: string, options: RequestOptions = {}): Promise<T> {
|
||||
if (isFrontendPreview()) {
|
||||
if (url === '/api/user/profile' && (!options.method || options.method === 'GET')) {
|
||||
return { ...frontendPreviewProfile, elementReport: { ...frontendPreviewProfile.elementReport, scores: [] } } as T
|
||||
}
|
||||
throw new Error('纯前端预览暂不请求后端或保存修改')
|
||||
}
|
||||
const token = Taro.getStorageSync('token')
|
||||
const headers = {
|
||||
'content-type': 'application/json',
|
||||
@@ -28,6 +35,34 @@ export async function request<T>(url: string, options: RequestOptions = {}): Pro
|
||||
return res.data.data
|
||||
}
|
||||
|
||||
export function resolveAssetUrl(url?: string) {
|
||||
if (!url) return ''
|
||||
return /^https?:\/\//.test(url) || url.startsWith('wxfile://') || url.startsWith('http://tmp/')
|
||||
? url
|
||||
: `${BASE_URL}${url.startsWith('/') ? url : `/${url}`}`
|
||||
}
|
||||
|
||||
async function uploadAvatar(filePath: string): Promise<UserProfile> {
|
||||
if (isFrontendPreview()) throw new Error('纯前端预览暂不上传头像或保存修改')
|
||||
const token = Taro.getStorageSync('token')
|
||||
const response = await Taro.uploadFile({
|
||||
url: `${BASE_URL}/api/user/avatar`,
|
||||
filePath,
|
||||
name: 'file',
|
||||
header: token ? { Authorization: `Bearer ${token}` } : {}
|
||||
})
|
||||
let result: ApiResult<UserProfile>
|
||||
try {
|
||||
result = JSON.parse(response.data) as ApiResult<UserProfile>
|
||||
} catch (error) {
|
||||
throw new Error('头像上传失败')
|
||||
}
|
||||
if (result.code !== 0) {
|
||||
throw new Error(result.message || '头像上传失败')
|
||||
}
|
||||
return result.data
|
||||
}
|
||||
|
||||
export const api = {
|
||||
wxLogin: (code: string) => request<{ sessionKey: string; phoneBound: boolean }>('/api/auth/wx-login', {
|
||||
method: 'POST',
|
||||
@@ -46,6 +81,7 @@ export const api = {
|
||||
method: 'PUT',
|
||||
data
|
||||
}),
|
||||
uploadAvatar,
|
||||
exchangeStatus: () => request<{ points: number; available: boolean; message: string }>('/api/points/exchange-status'),
|
||||
agreement: (type: string) => request<{ title: string; body: string }>(`/api/content/agreement/${type}`),
|
||||
follow: () => request<Record<string, string>>('/api/content/follow'),
|
||||
@@ -66,6 +102,7 @@ export type ElementScore = {
|
||||
|
||||
export type UserProfile = {
|
||||
id: number
|
||||
avatarUrl?: string
|
||||
nickname: string
|
||||
phone?: string
|
||||
gender: string
|
||||
|
||||
Reference in new issue
Block a user