friend-add.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <view class="page friend-add">
  3. <view class="search-bar">
  4. <uni-search-bar v-model="searchText" :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 != $store.state.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. searchText: "",
  31. users: []
  32. }
  33. },
  34. methods: {
  35. onCancel() {
  36. uni.navigateBack();
  37. },
  38. onSearch() {
  39. this.$http({
  40. url: "/user/findByName?name=" + this.searchText,
  41. method: "GET"
  42. }).then((data) => {
  43. this.users = data;
  44. })
  45. },
  46. onAddFriend(user) {
  47. this.$http({
  48. url: "/friend/add?friendId=" + user.id,
  49. method: "POST"
  50. }).then((data) => {
  51. let friend = {
  52. id: user.id,
  53. nickName: user.nickName,
  54. headImage: user.headImage,
  55. online: user.online
  56. }
  57. this.$store.commit("addFriend", friend);
  58. uni.showToast({
  59. title: "添加成功,对方已成为您的好友",
  60. icon: "none"
  61. })
  62. })
  63. },
  64. onShowUserInfo(user) {
  65. uni.navigateTo({
  66. url: "/pages/common/user-info?id=" + user.id
  67. })
  68. },
  69. isFriend(userId) {
  70. let friends = this.$store.state.friendStore.friends;
  71. let friend = friends.find((f) => f.id == userId);
  72. return friend != undefined;
  73. }
  74. }
  75. }
  76. </script>
  77. <style scoped lang="scss">
  78. .friend-add {
  79. position: relative;
  80. border: #dddddd solid 1px;
  81. display: flex;
  82. flex-direction: column;
  83. .search-bar {
  84. background: white;
  85. }
  86. .user-items{
  87. position: relative;
  88. flex: 1;
  89. overflow: hidden;
  90. .user-item {
  91. height: 120rpx;
  92. display: flex;
  93. margin-bottom: 1rpx;
  94. position: relative;
  95. padding: 0 30rpx ;
  96. align-items: center;
  97. background-color: white;
  98. white-space: nowrap;
  99. .user-name {
  100. flex:1;
  101. padding-left: 20rpx;
  102. font-size: 30rpx;
  103. font-weight: 600;
  104. line-height: 60rpx;
  105. white-space: nowrap;
  106. overflow: hidden;
  107. }
  108. }
  109. .scroll-bar {
  110. height: 100%;
  111. }
  112. }
  113. }
  114. </style>