const api = require('../../utils/api.js'); const { toastErr } = require('../../utils/ui.js'); Page({ data: { uid: '', kind: 'following', title: '关注', list: [], page: 1, hasMore: false, loaded: false }, onLoad(q) { const kind = q && q.kind === 'followers' ? 'followers' : 'following'; this.setData({ uid: (q && q.id) || '', kind, title: kind === 'followers' ? '粉丝' : '关注' }); this.load(1); }, load(page) { api .relations(this.data.uid, this.data.kind, page) .then((res) => { const merged = page === 1 ? res.list || [] : this.data.list.concat(res.list || []); this.setData({ list: merged, page, loaded: true, hasMore: merged.length < (res.total || 0) }); }) .catch((e) => { this.setData({ loaded: true }); toastErr(e); }); }, loadMore() { if (this.data.hasMore) this.load(this.data.page + 1); }, goUser(e) { wx.navigateTo({ url: `/pages/user/user?id=${e.currentTarget.dataset.id}` }); }, // 同样先本地翻转再发请求,失败回滚 toggleFollow(e) { const i = e.currentTarget.dataset.index; const u = this.data.list[i]; if (!u || u.is_self) return; const next = !u.followed; this.setData({ [`list[${i}].followed`]: next }); (next ? api.followUser(u.id) : api.unfollowUser(u.id)).catch((err) => { this.setData({ [`list[${i}].followed`]: !next }); toastErr(err, '操作失败'); }); }, });