xiangyixiangqin/miniapp/api/profile.js
2026-01-02 18:00:49 +08:00

129 lines
3.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 资料接口模块
* Requirements: 4.4, 4.5
*/
import { get, post, del } from './request'
import { getToken } from '../utils/storage'
// API 基础地址
const BASE_URL = 'http://localhost:5000/api/app'
/**
* 提交/更新用户资料
* WHEN a user completes all required fields and submits,
* THE XiangYi_MiniApp SHALL call endpoint to save the profile
* Requirements: 4.5
*
* @param {Object} data - 资料数据
* @param {number} data.relationship - 关系1父亲 2母亲 3本人
* @param {string} data.surname - 姓氏
* @param {number} data.childGender - 子女性别1男 2女
* @param {number} data.birthYear - 出生年份
* @param {number} data.education - 学历
* @param {string} data.workProvince - 工作省份
* @param {string} data.workCity - 工作城市
* @param {string} data.workDistrict - 工作区县
* @param {string} data.occupation - 职业
* @param {number} data.monthlyIncome - 月收入
* @param {number} data.height - 身高
* @param {number} data.weight - 体重
* @param {number} data.houseStatus - 房产状态
* @param {number} data.carStatus - 车辆状态
* @param {number} data.marriageStatus - 婚姻状态
* @param {number} data.expectMarryTime - 期望结婚时间
* @param {string} data.introduction - 自我介绍
* @param {boolean} data.isPhotoPublic - 照片是否公开
* @param {string} data.weChatNo - 微信号
* @param {Object} data.requirement - 择偶要求
* @returns {Promise<Object>} 资料ID
*/
export async function createOrUpdate(data) {
const response = await post('/profile', data)
return response
}
/**
* 获取我的资料
*
* @returns {Promise<Object>} 用户资料
*/
export async function getMyProfile() {
const response = await get('/profile')
return response
}
/**
* 上传照片
* WHEN a user uploads photos, THE XiangYi_MiniApp SHALL limit to maximum 5 photos
* Requirements: 4.4
*
* @param {Array<string>} filePaths - 本地文件路径数组
* @returns {Promise<Object>} 上传结果
*/
export async function uploadPhotos(filePaths) {
return new Promise((resolve, reject) => {
const token = getToken()
// 使用 uni.uploadFile 上传文件
const uploadTasks = filePaths.map((filePath, index) => {
return new Promise((res, rej) => {
uni.uploadFile({
url: `${BASE_URL}/profile/photos`,
filePath: filePath,
name: 'files',
header: {
'Authorization': `Bearer ${token}`
},
success: (uploadRes) => {
if (uploadRes.statusCode === 200) {
try {
const data = JSON.parse(uploadRes.data)
res(data)
} catch (e) {
rej(new Error('解析响应失败'))
}
} else if (uploadRes.statusCode === 401) {
rej(new Error('未授权,请重新登录'))
} else {
rej(new Error('上传失败'))
}
},
fail: (err) => {
rej(new Error('网络连接失败'))
}
})
})
})
// 并行上传所有文件
Promise.all(uploadTasks)
.then(results => {
// 合并所有上传结果
const photos = results.flatMap(r => r.data?.photos || [])
resolve({ success: true, data: { photos } })
})
.catch(err => {
reject(err)
})
})
}
/**
* 删除照片
*
* @param {number} photoId - 照片ID
* @returns {Promise<Object>} 删除结果
*/
export async function deletePhoto(photoId) {
const response = await del(`/profile/photos/${photoId}`)
return response
}
export default {
createOrUpdate,
getMyProfile,
uploadPhotos,
deletePhoto
}