1584aabcc5
## 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>
130 lines
3.9 KiB
JavaScript
130 lines
3.9 KiB
JavaScript
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, '保存失败');
|
|
});
|
|
},
|
|
});
|