Initial commit
This commit is contained in:
87
miniprogram/pages/friends/friends.ts
Normal file
87
miniprogram/pages/friends/friends.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import { UserProfile } from '../../types/index'
|
||||
import { api } from '../../utils/api'
|
||||
import { getAvatarGradient } from '../../utils/format'
|
||||
|
||||
const app = getApp<{ userInfo: any }>()
|
||||
|
||||
Page({
|
||||
data: {
|
||||
statusBarHeight: 0,
|
||||
tabBarHeight: 56,
|
||||
activeTab: 'following' as 'following' | 'followers',
|
||||
list: [] as any[],
|
||||
followingCount: 0,
|
||||
followersCount: 0,
|
||||
loading: false,
|
||||
refreshing: false,
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
const info = wx.getSystemInfoSync()
|
||||
this.setData({ statusBarHeight: info.statusBarHeight })
|
||||
this.loadList()
|
||||
},
|
||||
|
||||
onShow() {
|
||||
const tabBar = this.getTabBar() as any
|
||||
tabBar?.setSelected?.(2)
|
||||
},
|
||||
|
||||
async loadList() {
|
||||
this.setData({ loading: true })
|
||||
try {
|
||||
const list = await api.getFollowList(this.data.activeTab)
|
||||
const enriched = list.map(u => ({
|
||||
...u,
|
||||
gradient: getAvatarGradient(u._id),
|
||||
isOnline: u.isOnline,
|
||||
}))
|
||||
this.setData({
|
||||
list: enriched,
|
||||
[this.data.activeTab === 'following' ? 'followingCount' : 'followersCount']: list.length,
|
||||
})
|
||||
} catch {
|
||||
wx.showToast({ title: '加载失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ loading: false, refreshing: false })
|
||||
}
|
||||
},
|
||||
|
||||
switchTab(e: WechatMiniprogram.CustomEvent) {
|
||||
const tab = e.currentTarget.dataset.tab as 'following' | 'followers'
|
||||
if (tab === this.data.activeTab) return
|
||||
this.setData({ activeTab: tab, list: [] })
|
||||
this.loadList()
|
||||
},
|
||||
|
||||
async onFollowTap(e: WechatMiniprogram.CustomEvent) {
|
||||
const uid = e.currentTarget.dataset.uid as string
|
||||
try {
|
||||
await api.followUser(uid)
|
||||
if (this.data.activeTab === 'following') {
|
||||
const list = this.data.list.filter((u: any) => u._id !== uid)
|
||||
this.setData({ list, followingCount: list.length })
|
||||
}
|
||||
} catch {
|
||||
wx.showToast({ title: '操作失败', icon: 'none' })
|
||||
}
|
||||
},
|
||||
|
||||
onItemTap(e: WechatMiniprogram.CustomEvent) {
|
||||
const uid = e.currentTarget.dataset.uid
|
||||
wx.navigateTo({ url: `/pages/pet-detail/pet-detail?userId=${uid}` })
|
||||
},
|
||||
|
||||
onFindFriends() {
|
||||
wx.switchTab({ url: '/pages/nearby/nearby' })
|
||||
},
|
||||
|
||||
onGoNearby() {
|
||||
wx.switchTab({ url: '/pages/nearby/nearby' })
|
||||
},
|
||||
|
||||
async onRefresh() {
|
||||
this.setData({ refreshing: true })
|
||||
await this.loadList()
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user