Files
sundynix-pets/pets-fe/pages/onboarding/onboarding.js
T
Blizzard 396f531023 feat(fe): 肉垫 FAB + 冷启动闪屏;顶部筛选改纯文字对齐
- FAB 重设计成肉垫造型(呼应「肉垫计划」),修 fab.js 漏声明
  icon 属性导致的兜底文档图标;社区发帖 FAB 保持图标
- onboarding 加冷启动闪屏,登录+拉宠物在闪屏期完成再进首页,
  不再闪一下建档向导;欢迎屏 logo 换肉垫、去掉过时 AI 文案
- 顶部宠物筛选去掉头像改纯文字(带图/emoji 时高度对不齐),
  固定高度 + vertical-align:middle 彻底对齐,添加用「+」文字

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-30 17:57:06 +08:00

180 lines
6.2 KiB
JavaScript

const store = require('../../utils/store.js');
const api = require('../../utils/api.js');
const GENDERS = ['男孩', '女孩', '不确定'];
// 与后端 service.AgeStages 一致:「刚到家」不是年龄阶段,它由到家日期单独决定
const STAGE_OPTIONS = [
{ key: '幼年期', desc: '疫苗针次、驱虫、体重增长、社会化训练' },
{ key: '成年期', desc: '体重控制、口腔、日常健康管理' },
{ key: '老年期', desc: '慢病监测、半年体检、用药与护理' },
];
function today() {
const d = new Date();
const p = (n) => (n < 10 ? '0' + n : '' + n);
return d.getFullYear() + '-' + p(d.getMonth() + 1) + '-' + p(d.getDate());
}
function monthsSince(dateStr) {
const b = new Date(dateStr);
const now = new Date();
let m = (now.getFullYear() - b.getFullYear()) * 12 + (now.getMonth() - b.getMonth());
if (now.getDate() < b.getDate()) m--;
return m < 0 ? 0 : m;
}
// 与后端 StageFromBirthday 同一套规则:12 月龄成年、7 岁进入老年
function stageFromBirthday(birthday) {
if (!birthday) return '';
const m = monthsSince(birthday);
if (m < 12) return '幼年期';
if (m >= 84) return '老年期';
return '成年期';
}
function ageFromBirthday(bdayStr) {
if (!bdayStr) return '';
const b = new Date(bdayStr);
const now = new Date();
let months = (now.getFullYear() - b.getFullYear()) * 12 + (now.getMonth() - b.getMonth());
if (months < 0) months = 0;
return months < 24 ? months + '个月' : Math.floor(months / 12) + '岁';
}
Page({
data: {
booting: true, // 冷启动先显示闪屏,静默登录 + 拉宠物期间不闪建档向导
step: 0,
petType: '猫猫',
petEmoji: '🐱',
petName: '',
birthday: '',
genderIdx: 0,
weight: '',
arrivedAt: '',
today: '',
stage: '',
stageAuto: '',
stageOptions: STAGE_OPTIONS,
goals: [
{ label: '不知道每天该做什么', on: true },
{ label: '疫苗驱虫怕忘记', on: true },
{ label: '想记录体重和健康', on: false },
{ label: '担心生病不会判断', on: false },
{ label: '想控制养宠开销', on: false },
{ label: '想生成成长报告', on: false },
],
submitting: false,
},
onLoad() {
this.setData({ today: today(), arrivedAt: today() });
// 闪屏期间就把登录 + 宠物拉完,保证进首页时数据已经到位。
// 有宠物直接进首页;没有才落到建档向导。登录失败也别卡在闪屏,
// 放用户进向导(提交时会再 store.ready 重试)
store
.ready()
.then(() => {
if (store.getPets().length > 0) {
wx.switchTab({ url: '/pages/home/home' });
} else {
this.setData({ booting: false });
}
})
.catch(() => this.setData({ booting: false }));
},
nextStep() {
this.setData({ step: Math.min(this.data.step + 1, 5) });
},
choosePet(e) {
const { type, emoji } = e.currentTarget.dataset;
this.setData({ petType: type, petEmoji: emoji });
},
onName(e) { this.setData({ petName: e.detail.value }); },
onWeight(e) { this.setData({ weight: e.detail.value }); },
onBirthday(e) {
const birthday = e.detail.value;
const auto = stageFromBirthday(birthday);
const patch = { birthday, stageAuto: auto };
// 用户手动选过就尊重他的选择,否则跟着生日走
if (!this._stageTouched) patch.stage = auto;
this.setData(patch);
},
onArrived(e) { this.setData({ arrivedAt: e.detail.value }); },
selectGender(e) { this.setData({ genderIdx: Number(e.currentTarget.dataset.index) }); },
// 先随便看看:不建数据,直接进首页浏览(无宠物时首页显示空态)
skipBrowse() { wx.switchTab({ url: '/pages/home/home' }); },
savePetNext() {
if (!(this.data.petName || '').trim()) {
wx.showToast({ title: '给它起个名字吧', icon: 'none' });
return;
}
const w = parseFloat(this.data.weight);
// 体重会进趋势图和用药剂量参考,乱填不如不填
if (this.data.weight && !(w > 0 && w < 120)) {
wx.showToast({ title: '体重看起来不太对', icon: 'none' });
return;
}
if (!this.data.birthday) {
wx.showToast({ title: '生日决定疫苗和驱虫节奏,估个大概也行', icon: 'none' });
return;
}
this.nextStep();
},
chooseStage(e) {
const stage = e.currentTarget.dataset.stage;
const auto = this.data.stageAuto;
// 改成和生日矛盾的阶段是允许的(领养估的生日可能不准,大型犬发育也慢),
// 但要先说清楚代价:阶段决定疫苗针次怎么排,选错会漏针
if (auto && stage !== auto) {
wx.showModal({
title: '和生日对不上',
content: `按生日算它应该在「${auto}」。改成「${stage}」的话,疫苗针次和驱虫周期会按${stage}安排。确定要改吗?`,
confirmText: '确定改',
success: (res) => {
if (res.confirm) {
this._stageTouched = true;
this.setData({ stage });
}
},
});
return;
}
this._stageTouched = true;
this.setData({ stage });
},
toggleGoal(e) {
const i = e.currentTarget.dataset.index;
this.setData({ [`goals[${i}].on`]: !this.data.goals[i].on });
},
async submit() {
if (this.data.submitting) return;
this.setData({ submitting: true });
wx.showLoading({ title: '生成计划中...' });
const body = {
name: (this.data.petName || '').trim(),
emoji: this.data.petEmoji,
type: this.data.petType,
gender: GENDERS[this.data.genderIdx],
birthday: this.data.birthday,
arrived_at: this.data.arrivedAt,
weight: this.data.weight,
stage: this.data.stage || this.data.stageAuto,
age: ageFromBirthday(this.data.birthday),
goals: this.data.goals.filter((g) => g.on).map((g) => g.label),
};
try {
await store.ready();
await api.onboarding(body);
await store.loadPets();
wx.hideLoading();
wx.switchTab({ url: '/pages/home/home' });
} catch (e) {
wx.hideLoading();
wx.showToast({ title: e.message || '创建失败', icon: 'none' });
this.setData({ submitting: false });
}
},
finishOnboard() { this.submit(); },
});