Files
danyang-match/miniapp/pages/profile-intent/profile-intent.js
T

188 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.
/**
* 婚恋态度页 U10(Phase 04 P04-T25)
*
* 重构要点(遵循 AGENTS.md 不变量):
* - 数据:profileService.getProfile + userStore.updateProfile(第 9 条)
* - V1 不做泛交友选项(功能清单 §4.5:默认"寻找以婚姻为目标的稳定关系")
* - 草稿恢复:onLoad 读 ONBOARDING_DRAFT.intent,保存前写回
* - 下一步:跳 profile-intro
*/
const { userStore, appStore } = require('../../stores');
const { profileService } = require('../../services');
const { ROUTE, STORAGE_KEYS } = require('../../constants');
const MARRIAGE_PLAN_OPTIONS = [
{ value: 'half_year', label: '半年内' },
{ value: 'one_year', label: '1年内' },
{ value: 'one_to_two_years', label: '1~2年' },
{ value: 'two_years_plus', label: '2年以上' },
{ value: 'let_it_be', label: '顺其自然' },
];
const ACTIVITY_WILLINGNESS_OPTIONS = [
{ value: 'willing', label: '愿意' },
{ value: 'not_now', label: '暂不考虑' },
];
const MATCHMAKER_WILLINGNESS_OPTIONS = [
{ value: 'accept', label: '接受' },
{ value: 'not_now', label: '暂不接受' },
];
const DEFAULT_GOAL = '寻找以婚姻为目标的稳定关系';
Page({
data: {
statusBarHeight: 20,
navBarHeight: 44,
loading: false,
saving: false,
relationshipGoal: DEFAULT_GOAL,
marriagePlan: '',
activityWillingness: '',
matchmakerWillingness: '',
// 选项
marriagePlanOptions: MARRIAGE_PLAN_OPTIONS,
activityWillingnessOptions: ACTIVITY_WILLINGNESS_OPTIONS,
matchmakerWillingnessOptions: MATCHMAKER_WILLINGNESS_OPTIONS,
marriagePlanIndex: -1,
activityWillingnessIndex: -1,
matchmakerWillingnessIndex: -1,
},
onLoad() {
this._initSystemInfo();
this._applyDraft();
this.loadProfile();
},
_initSystemInfo() {
try {
const sysInfo = wx.getSystemInfoSync();
this.setData({
statusBarHeight: sysInfo.statusBarHeight || 20,
navBarHeight: 44,
});
} catch (e) { /* 使用默认值 */ }
},
async loadProfile() {
this.setData({ loading: true });
try {
const profile = await profileService.getProfile();
this._mergeProfile(profile);
} catch (err) { /* 首次填写 */ } finally {
this.setData({ loading: false });
}
},
_mergeProfile(profile) {
if (!profile) return;
const d = this._readDraft();
const relationshipGoal = d.relationshipGoal || profile.relationshipGoal || DEFAULT_GOAL;
const marriagePlan = d.marriagePlan || profile.marriagePlan || '';
const activityWillingness = d.activityWillingness || profile.activityWillingness || '';
const matchmakerWillingness = d.matchmakerWillingness || profile.matchmakerWillingness || '';
this.setData({
relationshipGoal,
marriagePlan,
activityWillingness,
matchmakerWillingness,
marriagePlanIndex: MARRIAGE_PLAN_OPTIONS.findIndex((o) => o.value === marriagePlan),
activityWillingnessIndex: ACTIVITY_WILLINGNESS_OPTIONS.findIndex((o) => o.value === activityWillingness),
matchmakerWillingnessIndex: MATCHMAKER_WILLINGNESS_OPTIONS.findIndex((o) => o.value === matchmakerWillingness),
});
},
// ============ 草稿 ============
_readDraft() {
try {
const draft = wx.getStorageSync(STORAGE_KEYS.ONBOARDING_DRAFT) || {};
return draft.intent || {};
} catch (e) {
return {};
}
},
_applyDraft() {
const d = this._readDraft();
if (!d || Object.keys(d).length === 0) return;
const marriagePlan = d.marriagePlan || '';
const activityWillingness = d.activityWillingness || '';
const matchmakerWillingness = d.matchmakerWillingness || '';
this.setData({
relationshipGoal: d.relationshipGoal || DEFAULT_GOAL,
marriagePlan,
activityWillingness,
matchmakerWillingness,
marriagePlanIndex: MARRIAGE_PLAN_OPTIONS.findIndex((o) => o.value === marriagePlan),
activityWillingnessIndex: ACTIVITY_WILLINGNESS_OPTIONS.findIndex((o) => o.value === activityWillingness),
matchmakerWillingnessIndex: MATCHMAKER_WILLINGNESS_OPTIONS.findIndex((o) => o.value === matchmakerWillingness),
});
},
_writeDraft() {
try {
const draft = wx.getStorageSync(STORAGE_KEYS.ONBOARDING_DRAFT) || {};
draft.intent = {
relationshipGoal: this.data.relationshipGoal,
marriagePlan: this.data.marriagePlan,
activityWillingness: this.data.activityWillingness,
matchmakerWillingness: this.data.matchmakerWillingness,
};
draft.currentStep = 'intent';
wx.setStorageSync(STORAGE_KEYS.ONBOARDING_DRAFT, draft);
} catch (e) { /* ignore */ }
},
// ============ 表单交互 ============
onGoalInput(e) {
this.setData({ relationshipGoal: e.detail.value });
},
onMarriagePlanChange(e) {
const idx = Number(e.detail.value);
this.setData({ marriagePlanIndex: idx, marriagePlan: MARRIAGE_PLAN_OPTIONS[idx].value });
},
onActivityWillingnessChange(e) {
const idx = Number(e.detail.value);
this.setData({ activityWillingnessIndex: idx, activityWillingness: ACTIVITY_WILLINGNESS_OPTIONS[idx].value });
},
onMatchmakerWillingnessChange(e) {
const idx = Number(e.detail.value);
this.setData({ matchmakerWillingnessIndex: idx, matchmakerWillingness: MATCHMAKER_WILLINGNESS_OPTIONS[idx].value });
},
// ============ 导航 ============
onBack() {
this._writeDraft();
wx.navigateBack();
},
async onNext() {
if (this.data.saving) return;
const { relationshipGoal, marriagePlan, activityWillingness, matchmakerWillingness } = this.data;
if (!marriagePlan) return appStore.toast('请选择希望多久进入婚姻');
if (!activityWillingness) return appStore.toast('请选择是否参加公益活动');
if (!matchmakerWillingness) return appStore.toast('请选择是否接受红娘推荐');
this.setData({ saving: true });
this._writeDraft();
try {
await userStore.updateProfile({
relationshipGoal,
marriagePlan,
activityWillingness,
matchmakerWillingness,
});
this.setData({ saving: false });
wx.navigateTo({ url: ROUTE.ONBOARDING.PROFILE_INTRO });
} catch (err) {
this.setData({ saving: false });
appStore.toastError(err, '保存失败');
}
},
});