feat: 重构评估流程为内存状态管理并接入真实提交与报告接口
本次重构将评估草稿改为内存状态管理,移除本地存储草稿,新增评估流程内存状态模块;接入真实的账户资料获取、头像上传接口,新增账户缓存清理逻辑;重构评估提交逻辑,接入真实提交接口并返回评估ID;新增分析页和报告页的真实状态查询、报告获取功能;更新README文档,记录开发进度和接口状态。
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { getUserStorage, setUserStorage } from '@/services/storage.uts'
|
||||
import { API_BASE_URL, getSession, requestJson } from '@/services/api.uts'
|
||||
import { activeAssessmentType, loadAssessmentDraft, draftText } from '@/services/assessment.uts'
|
||||
import { getSession, requestJson, uploadJson } from '@/services/api.uts'
|
||||
import { draftText } from '@/services/assessment.uts'
|
||||
|
||||
export type AccountProfile = { nickname: string, avatar: string, phone: string }
|
||||
export const DEFAULT_AVATAR = '/packages/account/static/default-avatar.png'
|
||||
@@ -27,9 +27,6 @@ export function getAccountProfile(): AccountProfile {
|
||||
const saved = getUserStorage(profileKey(phone))
|
||||
if (saved != null && typeof saved == 'object') {
|
||||
nickname = draftText(saved as UTSJSONObject, 'nickname'); avatar = draftText(saved as UTSJSONObject, 'avatar')
|
||||
} else {
|
||||
const draft = loadAssessmentDraft(activeAssessmentType())
|
||||
if (draft != null && draftText(draft, 'phone') == phone && draftText(draft, 'name').length > 0) nickname = draftText(draft, 'name')
|
||||
}
|
||||
}
|
||||
return { phone, nickname: nickname.length > 0 ? nickname : '融资用户', avatar }
|
||||
@@ -40,13 +37,32 @@ async function accountRequest(path: string, method: RequestMethod, payload: UTSJ
|
||||
requireAccount()
|
||||
return await requestJson(path, method, payload)
|
||||
}
|
||||
export async function saveAccountProfile(nickname: string, avatar: string): Promise<void> {
|
||||
function cacheProfile(result: UTSJSONObject, phone: string): AccountProfile {
|
||||
const profile: AccountProfile = { phone, nickname: draftText(result, 'nickname'), avatar: draftText(result, 'avatarUrl') }
|
||||
setUserStorage(profileKey(phone), { nickname: profile.nickname, avatar: profile.avatar })
|
||||
return profile
|
||||
}
|
||||
export async function fetchAccountProfile(): Promise<AccountProfile> {
|
||||
const session = requireAccount()
|
||||
const result = await accountRequest('/account/profile', 'GET', {})
|
||||
if (!sameAccount(session)) throw new Error('登录状态已变化,请重新进入页面')
|
||||
return cacheProfile(result, draftText(session, 'phone'))
|
||||
}
|
||||
export async function saveAccountProfile(nickname: string, avatarId: string): Promise<void> {
|
||||
const session = requireAccount(); const phone = draftText(session, 'phone')
|
||||
const name = nickname.trim()
|
||||
if (name.length < 2 || name.length > 20) throw new Error('昵称请输入 2–20 个字符')
|
||||
if (API_BASE_URL.length > 0) await accountRequest('/account/profile', 'PUT', { nickname: name, avatar })
|
||||
const payload: UTSJSONObject = { nickname: name }
|
||||
if (avatarId == 'default') payload['avatarId'] = null
|
||||
else if (avatarId.length > 0) payload['avatarId'] = avatarId
|
||||
const result = await accountRequest('/account/profile', 'PUT', payload)
|
||||
if (!sameAccount(session)) throw new Error('登录状态已变化,请重新进入页面')
|
||||
setUserStorage(profileKey(phone), { nickname: name, avatar })
|
||||
cacheProfile(result, phone)
|
||||
}
|
||||
|
||||
export async function uploadAccountAvatar(path: string): Promise<UTSJSONObject> {
|
||||
requireAccount()
|
||||
return await uploadJson('/account/avatar', path)
|
||||
}
|
||||
export async function requestSecurityCode(purpose: string, newPhone: string): Promise<void> {
|
||||
if (!['password', 'phone-current', 'phone-new'].includes(purpose)) throw new Error('验证码用途无效')
|
||||
@@ -68,27 +84,3 @@ export async function changeAccountPhone(newPhone: string, currentCode: string,
|
||||
if (!validCode(currentCode) || !validCode(newCode)) throw new Error('请输入两组 6 位数字验证码')
|
||||
await accountRequest('/account/phone', 'PUT', { newPhone, currentCode, newCode })
|
||||
}
|
||||
|
||||
// 限制头像体积,避免把临时路径或过大的图片写入账户缓存。
|
||||
export function readAvatar(path: string): Promise<string> {
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
// #ifdef WEB
|
||||
fetch(path).then((response) => response.blob()).then((blob) => {
|
||||
if (blob.size > 512000 || !['image/jpeg', 'image/png', 'image/webp'].includes(blob.type)) { reject(new Error('请选择 500KB 以内的 JPG、PNG 或 WebP 图片')); return }
|
||||
const reader = new FileReader()
|
||||
reader.onload = () => resolve(reader.result as string)
|
||||
reader.onerror = () => reject(new Error('图片读取失败,请重新选择'))
|
||||
reader.readAsDataURL(blob)
|
||||
}).catch(() => reject(new Error('图片读取失败,请重新选择')))
|
||||
// #endif
|
||||
// #ifndef WEB
|
||||
uni.getFileSystemManager().readFile({ filePath: path, encoding: 'base64', success: (result) => {
|
||||
const data = result.data as string
|
||||
if (data.length > 683000) { reject(new Error('请选择 500KB 以内的图片')); return }
|
||||
const mime = data.startsWith('/9j/') ? 'image/jpeg' : (data.startsWith('iVBOR') ? 'image/png' : (data.startsWith('UklGR') ? 'image/webp' : ''))
|
||||
if (mime.length == 0) { reject(new Error('请选择 JPG、PNG 或 WebP 图片')); return }
|
||||
resolve('data:' + mime + ';base64,' + data)
|
||||
}, fail: () => reject(new Error('图片读取失败,请重新选择')) })
|
||||
// #endif
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user