Files
sundynix-pets/pets-fe/pages/profile/profile.js
T
Blizzard 25f655a6fc feat: 补齐关注的入口——用户主页 + 关注/粉丝列表
关注之前只做了一半:能从帖子上点「+关注」,也有「关注」信息流,但
关注完就石沉大海——没有用户主页、看不到自己关注了谁、也不知道谁关注
了自己。功能是通的,路是断的。

后端新增:
- GET /api/users/:id/profile  公开资料 + 帖子/关注/粉丝三个计数 + 我是否已关注
- GET /api/users/:id/posts    TA 发布的帖子
- GET /api/users/:id/relations?kind=following|followers
- /api/user/summary 补 following / followers,否则「我的」页那两行永远是 0

关系列表里「我是否关注了这批人」用一次 IN 查询查完,不是每行一次。

小程序端新增两个页面:
- pages/user/user      用户主页:头像/昵称/三项计数/关注按钮 + TA 的帖子
- pages/relations      关注列表与粉丝列表(同一页,kind 区分)

入口:
- 社区点头像或昵称 → TA 的主页
- 主页点「关注/粉丝」数字 → 对应列表
- 「我的」页新增「我的关注」「我的粉丝」两行

关注按钮先本地翻转再发请求,失败回滚——等接口返回按钮才变会显得很迟钝。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-29 17:13:04 +08:00

83 lines
2.6 KiB
JavaScript

const store = require('../../utils/store.js');
const api = require('../../utils/api.js');
const upload = require('../../utils/upload.js');
function completion(pet) {
if (!pet) return 0;
const fields = ['name', 'emoji', 'type', 'gender', 'birthday', 'weight', 'stage', 'age', 'color', 'breed'];
const filled = fields.filter((f) => pet[f]).length;
return Math.round((filled / fields.length) * 100);
}
Page({
data: {
pet: {},
user: {},
summary: { pets: 0, records: 0, reminders: 0, pro_status: 'none' },
profilePct: 0,
sheetShow: false,
sheetType: '',
},
onLoad() {
this._unsub = store.subscribe((pet) => this.setData({ pet, profilePct: completion(pet) }));
},
onUnload() {
if (this._unsub) this._unsub();
},
onShow() {
store
.ready()
.then(() => {
const pet = store.getPet();
this.setData({ pet, user: store.getUser() || {}, profilePct: completion(pet) });
this.loadSummary();
})
.catch((e) => wx.showToast({ title: e.message || '加载失败', icon: 'none' }));
},
loadSummary() {
api.userSummary().then((summary) => this.setData({ summary })).catch(() => {});
},
openSheet(e) {
this.setData({ sheetType: e.currentTarget.dataset.type, sheetShow: true });
},
// 自己的关注/粉丝。之前只有帖子上的「+关注」按钮,关注完没有任何地方能看
goRelations(e) {
const uid = (this.data.user && this.data.user.id) || '';
if (!uid) return wx.showToast({ title: '登录信息还没就绪', icon: 'none' });
wx.navigateTo({ url: `/pages/relations/relations?id=${uid}&kind=${e.currentTarget.dataset.kind}` });
},
goRecord() {
wx.switchTab({ url: '/pages/record/record' });
},
onPickAvatar() {
upload
.chooseAndUploadImage()
.then((f) => api.updateProfile({ avatar_file_id: f.id }))
.then((user) => {
this.setData({ user });
store.setUser(user);
wx.showToast({ title: '头像已更新', icon: 'success' });
})
.catch((e) => {
if (e && e.canceled) return;
wx.showToast({ title: (e && e.message) || '上传失败', icon: 'none' });
});
},
onAddPet() {
this.setData({ sheetType: 'addPet', sheetShow: true });
},
onEditPet() {
this.setData({ sheetType: 'editPet', sheetShow: true });
},
closeSheet() {
this.setData({ sheetShow: false });
this.loadSummary();
},
onFab() {
wx.navigateTo({ url: '/pages/ai/ai' });
},
onShareAppMessage() {
return { title: '肉垫计划 · 新手养宠助手', path: '/pages/home/home' };
},
});