follow.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <view class="page">
  3. <PageHeader title="" style="border: 1px solid #F2F6F2;">
  4. <template slot="center">
  5. <view class="tabs">
  6. <view class="tab" :class="{ active: activeTab === 'following' }" @click="switchTab('following')">关注
  7. </view>
  8. <view class="tab" :class="{ active: activeTab === 'followers' }" @click="switchTab('followers')">粉丝
  9. </view>
  10. </view>
  11. </template>
  12. </PageHeader>
  13. <view style="height: 90rpx;"></view>
  14. <view class="follow-list">
  15. <view class="follow-item" v-for="(item, index) in currentList" :key="index">
  16. <CircleAvatar class="avator" v-if="item.avator" :src="item.avator"></CircleAvatar>
  17. <view class="info">
  18. <view class="top-box">
  19. <view class="name">{{ item.nickname }}</view>
  20. <image src="../../static/icon/wd_icon_nan.png" mode="widthFix" v-if="item.sex_id == 1" />
  21. <image src="../../static/icon/wd_icon_nv.png" mode="widthFix" v-else-if="item.sex_id == 2" />
  22. <view class="level">Lv{{ item.level }}</view>
  23. </view>
  24. <view class="desc">{{ item.description }}</view>
  25. </view>
  26. <view :class="item.is_attention ? 'unfollow-btn' : 'unfollow-btn active'" @click="toggleFollow(item)">
  27. <image src="../../static/me/wd_icon_guanzhu.png"></image>
  28. {{ item.is_attention ? '取消关注' : '关注' }}
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. import PageHeader from '@/components/PageHeader/PageHeader.vue';
  36. import CircleAvatar from "@/components/CircleAvatar/CircleAvatar.vue";
  37. export default {
  38. components: {
  39. PageHeader,
  40. CircleAvatar
  41. },
  42. data() {
  43. return {
  44. activeTab: 'following', // 当前激活的标签
  45. followList: [],
  46. fansList: [],
  47. isLoadingFollow: false, // 是否正在加载关注列表
  48. hasMoreFollow: true, // 是否有更多关注数据可加载
  49. followOffset: 0,
  50. }
  51. },
  52. computed: {
  53. // 根据当前标签返回对应的列表数据
  54. currentList() {
  55. return this.activeTab === 'following' ? this.followList : this.fansList;
  56. }
  57. },
  58. onLoad() {
  59. this.switchTab('following');
  60. },
  61. methods: {
  62. // 切换标签
  63. switchTab(tab) {
  64. if (tab) {
  65. this.activeTab = tab;
  66. }
  67. this.isLoadingFollow = false // 是否正在加载关注列表
  68. this.hasMoreFollow = true // 是否有更多关注数据可加载
  69. this.followOffset = 0
  70. // 加载对应的数据
  71. this.loadFollowList();
  72. },
  73. toggleFollow(item) {
  74. // 关注的逻辑
  75. uni.request({
  76. url: this.$apiHost + '/Member/attention',
  77. data: {
  78. uuid: getApp().globalData.uuid,
  79. id: item.user_id,
  80. },
  81. header: {
  82. "content-type": "application/json",
  83. 'sign': getApp().globalData.headerSign
  84. },
  85. success: (res) => {
  86. console.log("关注结果:", res.data);
  87. uni.showToast({
  88. title: res.data.str,
  89. icon: 'none'
  90. });
  91. if (res.data.success === "yes") {
  92. item.is_attention = !item.is_attention;
  93. }
  94. },
  95. fail: (e) => {
  96. console.log("关注失败:", e);
  97. uni.showToast({
  98. title: '网络请求失败',
  99. icon: 'none'
  100. });
  101. }
  102. });
  103. // 这里可以添加调用后端API的逻辑
  104. },
  105. loadFollowList() {
  106. if (this.isLoadingFollow) return;
  107. this.isLoadingFollow = true;
  108. // 关系类型: following(我关注的用户) 或 followers(关注我的用户)
  109. uni.request({
  110. url: this.$apiHost + "/User/getAttentions",
  111. data: {
  112. uuid: getApp().globalData.uuid,
  113. skey: getApp().globalData.skey,
  114. offset: this.followOffset,
  115. type: this.activeTab,
  116. },
  117. header: {
  118. "content-type": "application/json",
  119. sign: getApp().globalData.headerSign,
  120. },
  121. success: (res) => {
  122. console.log("用户列表数据:", res.data);
  123. // 确保在任何情况下都完成加载
  124. if (
  125. res.data.success == "yes" &&
  126. res.data.list &&
  127. res.data.list.length > 0
  128. ) {
  129. // 只有当列表有数据时才追加
  130. if (this.activeTab == 'following') {
  131. this.followList = [...this.followList, ...res.data.list];
  132. }
  133. if (this.activeTab == 'followers') {
  134. this.fansList = [...this.fansList, ...res.data.list];
  135. }
  136. this.followOffset += res.data.list.length;
  137. if (res.data.list.length < 20) {
  138. this.hasMoreFollow = false;
  139. }
  140. } else {
  141. // 如果列表为空,确保标记没有更多数据
  142. this.hasMoreFollow = false;
  143. }
  144. // 无论是否有数据,都需要通知z-paging组件完成刷新
  145. },
  146. complete: () => {
  147. this.isLoadingFollow = false;
  148. },
  149. fail: (e) => {
  150. this.isLoadingFollow = false;
  151. },
  152. });
  153. },
  154. }
  155. }
  156. </script>
  157. <style scoped lang="scss">
  158. @import 'follow.scss';
  159. </style>