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

245 lines
8.3 KiB
JavaScript
Raw Normal View History

2026-09-09 22:05:00 +08:00
/**
* 职业与生活页 U07(Phase 04 P04-T22)
*
* 重构要点(遵循 AGENTS.md 不变量):
* - 数据:profileService.getProfile + userStore.updateProfile(第 9 条)
* - 草稿恢复:onLoad 读 ONBOARDING_DRAFT.lifestyle,保存前写回(验收条件"Draft退出后可恢复")
* - 下一步:跳 profile-photos
*/
const { userStore, appStore } = require('../../stores');
const { profileService } = require('../../services');
const { ROUTE, STORAGE_KEYS } = require('../../constants');
// 与 profile-basic 的职业选项共用同一套码(occupation_code 是码,不是展示名)
const OCCUPATION_CODE_OPTIONS = [
{ value: 'government', label: '机关事业单位' },
{ value: 'education', label: '教育' },
{ value: 'medical', label: '医疗' },
{ value: 'enterprise', label: '企业职员' },
{ value: 'technical', label: '技术人员' },
{ value: 'self_employed', label: '个体经营' },
{ value: 'freelance', label: '自由职业' },
{ value: 'service', label: '服务行业' },
{ value: 'agriculture', label: '农业' },
{ value: 'other', label: '其他' },
2026-09-09 22:05:00 +08:00
];
const WORK_REGION_OPTIONS = [
{ value: '丹阳', label: '丹阳' },
{ value: '镇江市区', label: '镇江市区' },
{ value: '镇江其他辖区', label: '镇江其他辖区' },
{ value: '南京', label: '南京' },
{ value: '不限', label: '不限' },
];
2026-09-09 22:05:00 +08:00
const SMOKING_OPTIONS = [
{ value: 'never', label: '不吸烟' },
{ value: 'sometimes', label: '偶尔' },
2026-09-09 22:05:00 +08:00
{ value: 'often', label: '经常' },
];
const DRINKING_OPTIONS = [
{ value: 'never', label: '不饮酒' },
{ value: 'sometimes', label: '偶尔' },
2026-09-09 22:05:00 +08:00
{ value: 'often', label: '经常' },
];
const LONG_TERM_OPTIONS = [
{ value: 'yes', label: '是' },
{ value: 'no', label: '否' },
];
const FUTURE_DANYANG_OPTIONS = [
{ value: 'willing', label: '愿意' },
{ value: 'uncertain', label: '不确定' },
{ value: 'unwilling', label: '不愿意' },
];
Page({
data: {
statusBarHeight: 20,
navBarHeight: 44,
loading: false,
saving: false,
occupationCode: '',
workRegionCode: '',
2026-09-09 22:05:00 +08:00
smokingStatus: '',
drinkingStatus: '',
longTermDanyang: '',
futureDanyang: '',
// 选项
occupationCodeOptions: OCCUPATION_CODE_OPTIONS,
workRegionCodeOptions: WORK_REGION_OPTIONS,
2026-09-09 22:05:00 +08:00
smokingOptions: SMOKING_OPTIONS,
drinkingOptions: DRINKING_OPTIONS,
longTermOptions: LONG_TERM_OPTIONS,
futureDanyangOptions: FUTURE_DANYANG_OPTIONS,
occupationCodeIndex: -1,
workRegionCodeIndex: -1,
2026-09-09 22:05:00 +08:00
smokingIndex: -1,
drinkingIndex: -1,
longTermIndex: -1,
futureDanyangIndex: -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 occupationCode = d.occupationCode || profile.occupationCode || '';
const workRegionCode = d.workRegionCode || profile.workRegionCode || '';
2026-09-09 22:05:00 +08:00
const smokingStatus = d.smokingStatus || profile.smokingStatus || '';
const drinkingStatus = d.drinkingStatus || profile.drinkingStatus || '';
const longTermDanyang = d.longTermDanyang || profile.longTermDanyang || '';
const futureDanyang = d.futureDanyang || profile.futureDanyang || '';
this.setData({
occupationCode,
workRegionCode,
2026-09-09 22:05:00 +08:00
smokingStatus,
drinkingStatus,
longTermDanyang,
futureDanyang,
occupationCodeIndex: OCCUPATION_CODE_OPTIONS.findIndex((o) => o.value === occupationCode),
workRegionCodeIndex: WORK_REGION_OPTIONS.findIndex((o) => o.value === workRegionCode),
2026-09-09 22:05:00 +08:00
smokingIndex: SMOKING_OPTIONS.findIndex((o) => o.value === smokingStatus),
drinkingIndex: DRINKING_OPTIONS.findIndex((o) => o.value === drinkingStatus),
longTermIndex: LONG_TERM_OPTIONS.findIndex((o) => o.value === longTermDanyang),
futureDanyangIndex: FUTURE_DANYANG_OPTIONS.findIndex((o) => o.value === futureDanyang),
});
},
// ============ 草稿 ============
_readDraft() {
try {
const draft = wx.getStorageSync(STORAGE_KEYS.ONBOARDING_DRAFT) || {};
return draft.lifestyle || {};
} catch (e) {
return {};
}
},
_applyDraft() {
const d = this._readDraft();
if (!d || Object.keys(d).length === 0) return;
this.setData({
occupationCode: d.occupationCode || '',
workRegionCode: d.workRegionCode || '',
2026-09-09 22:05:00 +08:00
smokingStatus: d.smokingStatus || '',
drinkingStatus: d.drinkingStatus || '',
longTermDanyang: d.longTermDanyang || '',
futureDanyang: d.futureDanyang || '',
occupationCodeIndex: OCCUPATION_CODE_OPTIONS.findIndex((o) => o.value === (d.occupationCode || '')),
workRegionCodeIndex: WORK_REGION_OPTIONS.findIndex((o) => o.value === (d.workRegionCode || '')),
2026-09-09 22:05:00 +08:00
smokingIndex: SMOKING_OPTIONS.findIndex((o) => o.value === (d.smokingStatus || '')),
drinkingIndex: DRINKING_OPTIONS.findIndex((o) => o.value === (d.drinkingStatus || '')),
longTermIndex: LONG_TERM_OPTIONS.findIndex((o) => o.value === (d.longTermDanyang || '')),
futureDanyangIndex: FUTURE_DANYANG_OPTIONS.findIndex((o) => o.value === (d.futureDanyang || '')),
});
},
_writeDraft() {
try {
const draft = wx.getStorageSync(STORAGE_KEYS.ONBOARDING_DRAFT) || {};
draft.lifestyle = {
occupationCode: this.data.occupationCode,
workRegionCode: this.data.workRegionCode,
2026-09-09 22:05:00 +08:00
smokingStatus: this.data.smokingStatus,
drinkingStatus: this.data.drinkingStatus,
longTermDanyang: this.data.longTermDanyang,
futureDanyang: this.data.futureDanyang,
};
draft.currentStep = 'lifestyle';
wx.setStorageSync(STORAGE_KEYS.ONBOARDING_DRAFT, draft);
} catch (e) { /* ignore */ }
},
// ============ 表单交互 ============
onOccupationCodeChange(e) {
2026-09-09 22:05:00 +08:00
const idx = Number(e.detail.value);
this.setData({ occupationCodeIndex: idx, occupationCode: OCCUPATION_CODE_OPTIONS[idx].value });
2026-09-09 22:05:00 +08:00
},
onWorkRegionCodeChange(e) {
2026-09-09 22:05:00 +08:00
const idx = Number(e.detail.value);
this.setData({ workRegionCodeIndex: idx, workRegionCode: WORK_REGION_OPTIONS[idx].value });
2026-09-09 22:05:00 +08:00
},
onSmokingChange(e) {
const idx = Number(e.detail.value);
this.setData({ smokingIndex: idx, smokingStatus: SMOKING_OPTIONS[idx].value });
},
onDrinkingChange(e) {
const idx = Number(e.detail.value);
this.setData({ drinkingIndex: idx, drinkingStatus: DRINKING_OPTIONS[idx].value });
},
onLongTermChange(e) {
const idx = Number(e.detail.value);
this.setData({ longTermIndex: idx, longTermDanyang: LONG_TERM_OPTIONS[idx].value });
},
onFutureDanyangChange(e) {
const idx = Number(e.detail.value);
this.setData({ futureDanyangIndex: idx, futureDanyang: FUTURE_DANYANG_OPTIONS[idx].value });
},
// ============ 导航 ============
onBack() {
this._writeDraft();
wx.navigateBack();
},
async onNext() {
if (this.data.saving) return;
const { occupationCode, workRegionCode, smokingStatus, drinkingStatus, longTermDanyang, futureDanyang } = this.data;
if (!occupationCode) return appStore.toast('请选择职业类别');
if (!workRegionCode) return appStore.toast('请选择工作区域');
2026-09-09 22:05:00 +08:00
this.setData({ saving: true });
this._writeDraft();
try {
// 规范化字段名:此前用 occupationCategory / workRegion,而服务端读的是
// occupationCode / workRegionCode —— 对不上就会被静默丢弃(UAT 走查发现)
2026-09-09 22:05:00 +08:00
await userStore.updateProfile({
occupationCode,
workRegionCode,
2026-09-09 22:05:00 +08:00
smokingStatus,
drinkingStatus,
longTermDanyang,
futureDanyang,
});
this.setData({ saving: false });
wx.navigateTo({ url: ROUTE.ONBOARDING.PROFILE_PHOTOS });
} catch (err) {
this.setData({ saving: false });
appStore.toastError(err, '保存失败');
}
},
});