UserRelation.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <template>
  2. <div>
  3. <el-row class="movie-list">
  4. <el-col :md="24">
  5. <el-card :body-style="{ padding: '0px' }" class="card">
  6. <div slot="header" class="clearfix">
  7. <el-row>
  8. <el-col :md="1">
  9. <el-avatar>
  10. <el-image :src="user.avatarUrl"/>
  11. </el-avatar>
  12. </el-col>
  13. <el-col :md="23">
  14. <router-link target="_blank" :to="`/user/${user.userId}`">
  15. <span>{{ user.screenName }}</span>
  16. </router-link>
  17. <span v-html="'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'" />
  18. <el-button
  19. type="danger"
  20. size="mini"
  21. :icon="followButton.icon"
  22. @click="followUser(user.userId)"
  23. >
  24. <span>{{followButton.text}}</span>
  25. </el-button>
  26. <el-button
  27. type="danger"
  28. size="mini"
  29. icon="el-icon-message"
  30. @click="sendMessage(user.userId)"
  31. >
  32. <span>发消息</span>
  33. </el-button>
  34. </el-col>
  35. </el-row>
  36. </div>
  37. </el-card>
  38. </el-col>
  39. </el-row>
  40. <el-row>
  41. <el-col :md="24" class="movie-list">
  42. <el-tabs v-model="activeName" @tab-click='tabClick'>
  43. <el-tab-pane name="following">
  44. <span slot="label">
  45. Ta 的关注<el-badge :value="this.user.following" :max="9999" class="item" type="warning"/>
  46. </span>
  47. <div v-if="activeName === 'following'">
  48. <el-col v-for="(user, index) in dataList" :key="index" :md="6" :sm="12" :xs="12">
  49. <user-avatar-card :userAvatar="user"></user-avatar-card>
  50. </el-col>
  51. </div>
  52. </el-tab-pane>
  53. <el-tab-pane name="follower">
  54. <span slot="label">
  55. Ta 的粉丝<el-badge :value="this.user.follower" :max="9999" class="item" type="warning"/>
  56. </span>
  57. <div v-if="activeName === 'follower'">
  58. <el-col v-for="(user, index) in dataList" :key="index" :md="6" :sm="12" :xs="12">
  59. <user-card :user="user"></user-card>
  60. </el-col>
  61. </div>
  62. </el-tab-pane>
  63. </el-tabs>
  64. </el-col>
  65. <el-col :span="24" class="pagination">
  66. <el-pagination
  67. background
  68. :small="screenWidth <= 768"
  69. hide-on-single-page
  70. layout="prev, pager, next"
  71. :page-size="pageSize"
  72. :current-page="currentPage"
  73. :total="totalSize"
  74. @current-change="handleCurrentChange"
  75. @prev-click="handleCurrentChange"
  76. @next-click="handleCurrentChange"
  77. />
  78. </el-col>
  79. </el-row>
  80. <el-row v-if="showEmpty" class="not-result">
  81. <el-col :span="12" :offset="6">
  82. <img src="@/assets/img/icon/not-collection.png">
  83. <div>该用户还没关注任何人呢</div>
  84. </el-col>
  85. </el-row>
  86. </div>
  87. </template>
  88. <script>
  89. import UserAvatarCard from '@/components/card/UserAvatarCard'
  90. import { getUserInfo, getUserFollowing, getUserFollower, checkRelation, followUser, unfollowUser } from "@/api/user";
  91. export default {
  92. name: 'Home',
  93. components: { UserAvatarCard },
  94. data() {
  95. return {
  96. // 屏幕宽度, 为了控制分页条的大小
  97. screenWidth: document.body.clientWidth,
  98. user: null,
  99. userId: null,
  100. followButton: {
  101. icon: 'el-icon-plus',
  102. text: '关注'
  103. },
  104. activeName: 'following',
  105. currentPage: 1,
  106. pageSize: 12,
  107. totalSize: 0,
  108. dataList: [],
  109. showEmpty: true,
  110. }
  111. },
  112. created() {
  113. this.userId = this.$route.params.id
  114. getUserInfo(this.userId).then(res => {
  115. if (res.code === 0) {
  116. this.user = res.data
  117. const path = this.$route.path
  118. if (path.endsWith("following")) {
  119. this.activeName = 'following'
  120. document.title = this.user.screenName + '的关注'
  121. } else if (path.endsWith("follower")) {
  122. this.activeName = 'follower'
  123. document.title = this.user.screenName + '的粉丝'
  124. } else {
  125. document.title = this.user.screenName + '的个人主页'
  126. }
  127. this.getData()
  128. }
  129. })
  130. checkRelation(this.userId).then(res => {
  131. if (res.code === 0) {
  132. if (res.data) {
  133. this.followButton.text = '已关注'
  134. this.followButton.icon = 'el-icon-check'
  135. }
  136. }
  137. })
  138. },
  139. mounted() {
  140. // 当窗口宽度改变时获取屏幕宽度
  141. window.onresize = () => {
  142. return () => {
  143. window.screenWidth = document.body.clientWidth
  144. this.screenWidth = window.screenWidth
  145. }
  146. }
  147. },
  148. watch: {
  149. $route(){
  150. this.$router.go()
  151. }
  152. },
  153. methods: {
  154. handleCurrentChange(pageNumber) {
  155. this.currentPage = pageNumber
  156. this.getData()
  157. /*if (this.activeName === 'following') {
  158. getUserFollowing(this.userId).then(res => {
  159. if (res.code === 0) {
  160. this.dataList = res.data
  161. }
  162. })
  163. } else if (this.activeName === 'follower') {
  164. getUserFollower(this.userId).then(res => {
  165. if (res.code === 0) {
  166. this.dataList = res.data
  167. }
  168. })
  169. }*/
  170. // 回到顶部
  171. scrollTo(0, 0)
  172. },
  173. tabClick(tab) {
  174. this.activeName = tab.name
  175. this.goToTab(this.activeName)
  176. },
  177. goToTab(activeName) {
  178. const path = '/user/' + this.userId + '/' + activeName
  179. if (this.$route.path === path) {
  180. this.$router.go(0)
  181. return
  182. }
  183. this.$router.push(path)
  184. },
  185. getData() {
  186. this.dataList = []
  187. if (this.activeName === 'following') {
  188. getUserFollowing(this.userId).then(res => {
  189. if (res.code === 0) {
  190. this.dataList = res.data
  191. }
  192. })
  193. } else if (this.activeName === 'follower') {
  194. getUserFollower(this.userId).then(res => {
  195. if (res.code === 0) {
  196. this.dataList = res.data
  197. }
  198. })
  199. }
  200. },
  201. followUser(userId) {
  202. if (this.followButton.text === '关注') {
  203. followUser(userId).then(res => {
  204. if (res.code === 0) {
  205. this.followButton.text = '已关注'
  206. this.followButton.icon = 'el-icon-check'
  207. }
  208. })
  209. } else {
  210. unfollowUser(userId).then(res => {
  211. if (res.code === 0) {
  212. this.followButton.text = '关注'
  213. this.followButton.icon = 'el-icon-plus'
  214. }
  215. })
  216. }
  217. },
  218. sendMessage(userId) {
  219. console.log('发送消息')
  220. }
  221. }
  222. }
  223. </script>
  224. <style scoped>
  225. .movie-list {
  226. padding-top: 15px;
  227. padding-left: 6%;
  228. padding-right: 6%;
  229. }
  230. .pagination {
  231. text-align: center;
  232. padding: 10px;
  233. }
  234. /*处于手机屏幕时*/
  235. @media screen and (max-width: 768px){
  236. .movie-list {
  237. padding-top: 8px;
  238. padding-left: 0.5%;
  239. padding-right: 0.5%;
  240. }
  241. }
  242. .not-result {
  243. padding-top: 100px;
  244. padding-bottom: 100px;
  245. text-align: center;
  246. }
  247. .item {
  248. margin-top: 10px;
  249. margin-right: 40px;
  250. }
  251. </style>