Files
danyang-match/miniapp/services/profileService.js
T
WorkBuddy 8dee67040c fix(miniapp): 幂等键改为按操作稳定;删除指向 404 的互选死代码
1) 幂等键(配合后端机制)

原写法把 Date.now() 拼进幂等键,每次调用都生成新键,即使后端实现了幂等
也永远不会命中 —— 声明端与实现端各自都坏。

api.idempotencyKey(scope, subject):键缓存在 _pendingIdempotency(TTL 10min),
同一逻辑操作在成功之前复用同一把键,收到 200+success 才释放;失败(含网络错误)
不释放,下次重试沿用同一把键。25 处调用点(8 个 service + 2 个 TS 页面)全部替换,
Date.now 幂等键写法残留经 grep 为 0。

2) 删除死代码 submitInterests

它请求 POST /activities/{id}/interests —— 后端从未实现该端点
(Phase 16 已把表达意向合并进 /activities/{id}/feedback 的
interestTargetUserIds)。全仓确认无任何页面/组件调用方。
两个文件各留注释说明删除原因与恢复条件:若产品确认需要独立的
「提交互选」端点,应先定义语义并补后端实现,再恢复前端方法。

store 头部关于 AGENTS 第 6 条(单向意向保密)的注释同步改为引用
submitFeedback 返回的 newMutuals。
2026-09-18 16:37:33 +08:00

190 lines
6.2 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.
/**
* 用户资料服务(API_INVENTORY 行 17-25, 68-70)
*
* 封装:本人资料、择偶条件、生活照、隐私设置、审核状态、关系状态切换。
* 严格区分 GET 与 PUT,避免误操作;PUT 调用走幂等键防重。
* 自我介绍/择偶条件字数限制由 stores/profileStore 校验,本层只负责传输。
*/
const api = require('../utils/api');
/**
* 获取本人资料(API_INVENTORY 行 17)
* @returns {Promise<import('../types').Profile>}
*/
function getProfile() {
return api.get('/me/profile');
}
/**
* 更新本人资料(API_INVENTORY 行 18)
* @param {Partial<import('../types').Profile>} payload
* @returns {Promise<import('../types').Profile>} 服务端返回的最新资料
*/
function updateProfile(payload) {
// PUT 默认幂等:用 userId + 字段指纹作为幂等键,避免重复提交时脏写
const idempotencyKey = api.idempotencyKey('profile-put', (payload && payload.userId) || 'me');
return api.put('/me/profile', payload, undefined, idempotencyKey);
}
/**
* 获取择偶条件(API_INVENTORY 行 19)
* @returns {Promise<import('../types').PartnerPreference>}
*/
function getPreferences() {
return api.get('/me/preferences');
}
/**
* 更新择偶条件(API_INVENTORY 行 20)
* @param {Partial<import('../types').PartnerPreference>} payload
* @returns {Promise<import('../types').PartnerPreference>}
*/
function updatePreferences(payload) {
const idempotencyKey = api.idempotencyKey('preferences-put');
return api.put('/me/preferences', payload, undefined, idempotencyKey);
}
/**
* 获取生活照列表(API_INVENTORY 行 21)
* @returns {Promise<{ photos: Array<{ photoId: string, url: string, status: string }> }>}
*/
function getPhotos() {
return api.get('/me/photos');
}
/**
* 删除生活照(API_INVENTORY 行 21b)
* @param {string} photoId
* @returns {Promise<void>}
*/
function deletePhoto(photoId) {
return api.delete(`/me/photos/${photoId}`);
}
/**
* 设置主头像(将指定照片设为主头像)
* @param {string} photoId
* @returns {Promise<{ avatarUrl: string }>}
*/
function setMainPhoto(photoId) {
const idempotencyKey = api.idempotencyKey('photo-main', photoId);
return api.put(`/me/photos/${photoId}/main`, {}, undefined, idempotencyKey);
}
/**
* 申请照片上传凭证(API_INVENTORY 行 22)
* @param {{ fileName: string, contentType?: string }} payload
* @returns {Promise<{ uploadUrl: string, photoId: string, expiresAt: number, accessUrl: string }>}
*/
function requestUploadToken(payload) {
return api.post('/me/photos/upload-token', payload, undefined, payload.fileName);
}
/**
* 获取隐私设置(API_INVENTORY 行 23)
* @returns {Promise<import('../types').PrivacySettings>}
*/
function getPrivacySettings() {
return api.get('/me/privacy-settings');
}
/**
* 更新隐私设置(API_INVENTORY 行 24)
* @param {Partial<import('../types').PrivacySettings>} payload
* @returns {Promise<import('../types').PrivacySettings>}
*/
function updatePrivacySettings(payload) {
const idempotencyKey = api.idempotencyKey('privacy-put');
return api.put('/me/privacy-settings', payload, undefined, idempotencyKey);
}
/**
* 获取审核状态(API_INVENTORY 行 25,REST §36-39)
*
* 服务端返回结构:
* {
* status: 'not_submitted' | 'pending' | 'approved' | 'need_edit' | 'rejected',
* items: [
* { itemType: 'avatar'|'self_intro'|'basic'|'lifestyle'|'intent'|'preference'|'interests',
* status: 'pending'|'approved'|'need_edit'|'rejected',
* reasonCode?: string, // 驳回时存在,如 AVATAR_NOT_CLEAR
* reasonText?: string } // 驳回时存在,用户可读文案
* ],
* submittedAt?: number,
* reviewedAt?: number
* }
*
* @returns {Promise<{ status: string, items: Array<{itemType:string,status:string,reasonCode?:string,reasonText?:string}>, submittedAt?: number, reviewedAt?: number }>}
*/
function getReviewStatus() {
return api.get('/me/profile/review-status');
}
/**
* 提交资料审核(REST §37,POST /me/profile/submit-review)
*
* 幂等:用 Idempotency-Key 防止网络重试导致重复生成审核任务。
* 服务端校验资料完整度、敏感词、实名认证前置条件后生成 ReviewTask。
*
* @returns {Promise<{ reviewStatus: 'pending', submittedAt: number }>}
*/
function submitReview() {
const idempotencyKey = api.idempotencyKey('review-submit');
return api.post('/me/profile/submit-review', {}, undefined, idempotencyKey);
}
/**
* 获取婚恋关系状态(REST §83 章 GET /me/relationship-status)
* @returns {Promise<{ status: string, recommendationEnabled: boolean, matchingWith?: { matchId: string, displayName: string } }>}
*/
function getRelationshipStatus() {
return api.get('/me/relationship-status');
}
/**
* 暂停寻找(REST §84 章 PATCH /me/relationship-status)
* 成功牵手不能通过此接口直接设置(后端返回 SUCCESS_CONFIRMATION_REQUIRED)。
* @returns {Promise<{ status: 'paused', recommendationEnabled: boolean }>}
*/
function pauseSearching() {
const idempotencyKey = api.idempotencyKey('rel-pause');
return api.patch('/me/relationship-status', { status: 'paused' }, undefined, idempotencyKey);
}
/**
* 恢复寻找(REST §84 章 PATCH /me/relationship-status)
* @returns {Promise<{ status: 'searching', recommendationEnabled: boolean }>}
*/
function resumeSearching() {
const idempotencyKey = api.idempotencyKey('rel-resume');
return api.patch('/me/relationship-status', { status: 'searching' }, undefined, idempotencyKey);
}
/**
* 设置为"正在了解"(REST §84 章 PATCH /me/relationship-status)
* @returns {Promise<{ status: 'getting_to_know', recommendationEnabled: boolean }>}
*/
function markGettingToKnow() {
const idempotencyKey = api.idempotencyKey('rel-gtk');
return api.patch('/me/relationship-status', { status: 'getting_to_know' }, undefined, idempotencyKey);
}
module.exports = {
getProfile,
updateProfile,
getPreferences,
updatePreferences,
getPhotos,
deletePhoto,
setMainPhoto,
requestUploadToken,
getPrivacySettings,
updatePrivacySettings,
getReviewStatus,
submitReview,
getRelationshipStatus,
pauseSearching,
resumeSearching,
markGettingToKnow,
};