feat(fe): Phase D —— 主页装扮,「我的」和「宠友主页」共用一套渲染

## profile-head 组件
头图 / 头像 / 昵称 / 签名 / 帖子关注粉丝 / 养宠数据 / 宠物名片墙,
pages/mine 和 pages/user 用的是同一个组件、同一个接口
(GET /users/:id/profile)。

这件事必须一开始就做对:自己看和别人看要是两套渲染,会慢慢长歪,
装扮功能也就失去意义了——你调半天颜色,结果只有自己那页变了。
两边唯一的差别是底部按钮:自己看是「装扮我的主页」,别人看是「关注 TA」。

顺带把 mine 的头部从 /user/summary 切到了 /users/:id/profile。原来两边
字段对不上,装扮效果在「我的」页根本预览不到。

主题只在组件 wxss 里定义 --pf-a/--pf-b/--pf-ink 三个变量,下面所有着色
都走变量。后端存 key 不存色值,就是为了这套配色随时能在前端改。

## pages/decorate 装扮页
头图上传 / 五套主题色 / 个性签名 60 字 / 宠物墙开关,顶部实时预览。

预览用的就是 profile-head 本身,不是仿一个——仿的迟早会和真的长得不一样。
喂给它一份改过的 card 就行,组件那边不需要知道自己在被预览。

头图有个坑单独处理了:bg_file_id 只在真动过的时候才传。新上传带新 id,
点「恢复默认渐变」带空串,都没动就不传字段。否则「只改个主题色」会把
已有头图冲掉。上传时先用本地临时路径顶上,不让用户对着转圈等。

联调验证(本地 9090,真上传了一张图走完存储链路):
  上传             → file id + MinIO url
  保存装扮          → bg_url 落库,主题 violet,签名落库
  主页读回          → bio/theme/show_pets/bg_url/宠物墙/养宠数据 全对
  只改主题          → theme 变 sky,头图还在(没被冲掉)
  恢复默认渐变       → bg_url 清空,theme 和 bio 都没被动
  别人视角          → is_self=false、followed=false,装扮和宠物墙都能看到

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Blizzard
2026-07-29 18:02:07 +08:00
parent 8606c1dfc8
commit 1584aabcc5
18 changed files with 426 additions and 100 deletions
+129
View File
@@ -0,0 +1,129 @@
const store = require('../../utils/store.js');
const api = require('../../utils/api.js');
const { toastErr } = require('../../utils/ui.js');
// key 要和后端 service.UserThemes 一致,色值只在 profile-head 的 wxss 里
const THEMES = [
{ key: 'warm', label: '暖橙' },
{ key: 'mint', label: '薄荷' },
{ key: 'sky', label: '天青' },
{ key: 'violet', label: '藕紫' },
{ key: 'ink', label: '墨灰' },
];
Page({
data: {
themes: THEMES,
form: { bio: '', bg_url: '', bg_file_id: '', theme: 'warm', show_pets: true },
bioLen: 0,
preview: null,
saving: false,
},
onLoad() {
store
.ready()
.then(() => this.loadCard())
.catch((e) => toastErr(e));
},
loadCard() {
const uid = (store.getUser() || {}).id;
if (!uid) return;
api
.userCard(uid)
.then((card) => {
this.setData({
form: {
bio: card.bio || '',
bg_url: card.bg_url || '',
bg_file_id: '', // 没重新上传就不带这个字段,避免把已有头图覆盖成空
theme: card.theme || 'warm',
show_pets: card.show_pets !== false,
},
bioLen: (card.bio || '').length,
});
this._card = card;
this.syncPreview();
})
.catch((e) => toastErr(e));
},
// 预览喂给 profile-head 的就是一份改过的 card,组件那边不用知道自己在被预览
syncPreview() {
const f = this.data.form;
this.setData({
preview: Object.assign({}, this._card || {}, {
bio: f.bio,
bg_url: f.bg_url,
theme: f.theme,
show_pets: f.show_pets,
}),
});
},
pickBg() {
wx.chooseMedia({
count: 1,
mediaType: ['image'],
sizeType: ['compressed'],
success: (res) => {
const path = res.tempFiles[0].tempFilePath;
// 先用本地路径顶上,别让用户对着转圈等上传
this.setData({ 'form.bg_url': path });
this.syncPreview();
wx.showLoading({ title: '上传中…' });
api
.uploadFile(path)
.then((file) => {
wx.hideLoading();
this.setData({ 'form.bg_file_id': file.id, 'form.bg_url': file.url });
this.syncPreview();
})
.catch((e) => {
wx.hideLoading();
this.setData({ 'form.bg_url': (this._card && this._card.bg_url) || '' });
this.syncPreview();
toastErr(e, '上传失败');
});
},
});
},
clearBg() {
// 空字符串是「清掉头图」的显式信号,和「没传」不是一回事
this.setData({ 'form.bg_url': '', 'form.bg_file_id': '' });
this.syncPreview();
},
pickTheme(e) {
this.setData({ 'form.theme': e.currentTarget.dataset.key });
this.syncPreview();
},
onBio(e) {
this.setData({ 'form.bio': e.detail.value, bioLen: e.detail.value.length });
this.syncPreview();
},
onShowPets(e) {
this.setData({ 'form.show_pets': e.detail.value });
this.syncPreview();
},
save() {
if (this.data.saving) return;
const f = this.data.form;
const body = { bio: f.bio, theme: f.theme, show_pets: f.show_pets };
// 只有真的动过头图才带 bg_file_id:新上传带新 id,点了「恢复默认渐变」带空串,
// 两者都没有就不传,让后端保持原样
const had = (this._card && this._card.bg_url) || '';
if (f.bg_file_id) body.bg_file_id = f.bg_file_id;
else if (had && !f.bg_url) body.bg_file_id = '';
this.setData({ saving: true });
api
.updateDecoration(body)
.then((user) => {
this.setData({ saving: false });
store.setUser(user); // 返回的就是改完的 user,别再多请求一次
wx.showToast({ title: '已保存', icon: 'success' });
setTimeout(() => wx.navigateBack(), 600);
})
.catch((e) => {
this.setData({ saving: false });
toastErr(e, '保存失败');
});
},
});
+8
View File
@@ -0,0 +1,8 @@
{
"usingComponents": {
"nav-bar": "/components/nav-bar/nav-bar",
"pt-icon": "/components/pt-icon/index",
"profile-head": "/components/profile-head/index"
},
"navigationBarTitleText": "装扮主页"
}
+53
View File
@@ -0,0 +1,53 @@
<nav-bar title="装扮主页" show-back="{{true}}"></nav-bar>
<scroll-view class="page-scroll" scroll-y="{{true}}" enhanced="{{true}}" show-scrollbar="{{false}}">
<view class="page-body no-fab">
<!-- 预览用的就是主页那个组件本身,不是仿一个。仿的迟早会和真的长得不一样 -->
<profile-head card="{{preview}}" preview="{{true}}"></profile-head>
<view class="card">
<view class="section-head"><view class="sh-title">头图</view></view>
<view class="dc-bg" bindtap="pickBg">
<image wx:if="{{form.bg_url}}" class="dc-bg-img" src="{{form.bg_url}}" mode="aspectFill"></image>
<view wx:else class="dc-bg-empty">
<pt-icon name="photo" size="{{48}}"></pt-icon>
<text class="dc-hint">点这里换一张头图</text>
</view>
</view>
<view wx:if="{{form.bg_url}}" class="link dc-clear" bindtap="clearBg">恢复默认渐变</view>
</view>
<view class="card">
<view class="section-head"><view class="sh-title">主题色</view></view>
<view class="dc-themes">
<view wx:for="{{themes}}" wx:key="key"
class="dc-theme {{form.theme === item.key ? 'on' : ''}}"
data-key="{{item.key}}" bindtap="pickTheme">
<view class="dc-swatch sw-{{item.key}}"></view>
<text class="dc-tname">{{item.label}}</text>
</view>
</view>
</view>
<view class="card">
<view class="section-head">
<view class="sh-title">个性签名</view>
<view class="dc-count">{{bioLen}}/60</view>
</view>
<textarea class="textarea" placeholder="介绍一下你和毛孩子" placeholder-class="placeholder"
value="{{form.bio}}" maxlength="60" bindinput="onBio"></textarea>
</view>
<view class="card">
<view class="dc-row">
<view class="dc-row-main">
<view class="tt-b">展示我的毛孩子</view>
<view class="tt-p">关掉后别人访问你的主页看不到宠物名片墙</view>
</view>
<switch checked="{{form.show_pets}}" color="#F5A947" bindchange="onShowPets"></switch>
</view>
</view>
<view class="btn btn-dark btn-block" bindtap="save">{{saving ? '保存中…' : '保存'}}</view>
</view>
</scroll-view>
+30
View File
@@ -0,0 +1,30 @@
.dc-bg{
height:240rpx;border-radius:var(--r-md);overflow:hidden;
background:var(--surface-2);display:flex;align-items:center;justify-content:center;
}
.dc-bg-img{width:100%;height:100%;display:block}
.dc-bg-empty{display:flex;flex-direction:column;align-items:center;gap:var(--sp-2);color:var(--muted)}
.dc-hint{font-size:var(--fs-cap)}
.dc-clear{margin-top:var(--sp-3);text-align:center}
.dc-themes{display:flex;gap:var(--sp-3)}
.dc-theme{
flex:1;display:flex;flex-direction:column;align-items:center;gap:8rpx;
padding:var(--sp-3) 0;border-radius:var(--r-sm);border:2rpx solid transparent;
}
.dc-theme.on{border-color:var(--primary);background:var(--primary-soft)}
.dc-swatch{width:64rpx;height:64rpx;border-radius:50%;box-shadow:var(--sd-1)}
.dc-tname{font-size:var(--fs-cap);color:var(--muted)}
.dc-theme.on .dc-tname{color:var(--primary-dark);font-weight:var(--fw-b)}
/* 色值和 profile-head 的主题保持一致,改那边这里也要跟着改 */
.sw-warm{background:linear-gradient(135deg,#F5A947,#FFE6BA)}
.sw-mint{background:linear-gradient(135deg,#3FB98A,#D7F3E7)}
.sw-sky{background:linear-gradient(135deg,#4A9BE0,#D8ECFB)}
.sw-violet{background:linear-gradient(135deg,#9B7BE0,#E8DFFA)}
.sw-ink{background:linear-gradient(135deg,#5A5550,#E4DED7)}
.dc-count{font-size:var(--fs-cap);color:var(--muted)}
.dc-row{display:flex;align-items:center;gap:var(--sp-4)}
.dc-row-main{flex:1;min-width:0}