friend-add.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template>
  2. <view class="page friend-add">
  3. <view class="search-bar">
  4. <uni-search-bar v-model="searchText" radius="100" :focus="true" @confirm="onSearch()" @cancel="onCancel()"
  5. placeholder="用户名/昵称"></uni-search-bar>
  6. </view>
  7. <view class="user-items">
  8. <scroll-view class="scroll-bar" scroll-with-animation="true" scroll-y="true">
  9. <view v-for="(user) in users" :key="user.id" v-show="user.id != userStore.userInfo.id">
  10. <view class="user-item">
  11. <head-image :id="user.id" :name="user.nickName"
  12. :online="user.online" :url="user.headImage"
  13. :size="100"></head-image>
  14. <view class="user-name">{{ user.nickName}}</view>
  15. <view class="user-btns">
  16. <button type="primary" v-show="!isFriend(user.id)" size="mini"
  17. @click.stop="onAddFriend(user)">加为好友</button>
  18. <button type="default" v-show="isFriend(user.id)" size="mini" disabled>已添加</button>
  19. </view>
  20. </view>
  21. </view>
  22. </scroll-view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. data() {
  29. return {
  30. friendStore: this.useFriendStore(),
  31. userStore: this.useUserStore(),
  32. searchText: "",
  33. users: []
  34. }
  35. },
  36. methods: {
  37. onCancel() {
  38. uni.navigateBack();
  39. },
  40. onSearch() {
  41. this.$http({
  42. url: "/user/findByName?name=" + this.searchText,
  43. method: "GET"
  44. }).then((data) => {
  45. this.users = data;
  46. })
  47. },
  48. onAddFriend(user) {
  49. this.$http({
  50. url: "/friend/add?friendId=" + user.id,
  51. method: "POST"
  52. }).then((data) => {
  53. let friend = {
  54. id: user.id,
  55. nickName: user.nickName,
  56. headImage: user.headImage,
  57. online: user.online
  58. }
  59. this.friendStore.addFriend(friend);
  60. uni.showToast({
  61. title: "添加成功,对方已成为您的好友",
  62. icon: "none"
  63. })
  64. })
  65. },
  66. onShowUserInfo(user) {
  67. uni.navigateTo({
  68. url: "/pages/common/user-info?id=" + user.id
  69. })
  70. },
  71. isFriend(userId) {
  72. let friends = this.friendStore.friends;
  73. let friend = friends.find((f) => f.id == userId);
  74. return !!friend;
  75. }
  76. }
  77. }
  78. </script>
  79. <style scoped lang="scss">
  80. .friend-add {
  81. position: relative;
  82. border: #dddddd solid 1px;
  83. display: flex;
  84. flex-direction: column;
  85. .search-bar {
  86. background: white;
  87. }
  88. .user-items{
  89. position: relative;
  90. flex: 1;
  91. overflow: hidden;
  92. .user-item {
  93. height: 120rpx;
  94. display: flex;
  95. margin-bottom: 1rpx;
  96. position: relative;
  97. padding: 0 30rpx ;
  98. align-items: center;
  99. background-color: white;
  100. white-space: nowrap;
  101. .user-name {
  102. flex:1;
  103. padding-left: 20rpx;
  104. font-size: 30rpx;
  105. font-weight: 600;
  106. line-height: 60rpx;
  107. white-space: nowrap;
  108. overflow: hidden;
  109. }
  110. }
  111. .scroll-bar {
  112. height: 100%;
  113. }
  114. }
  115. }
  116. </style>