user-search.vue 2.7 KB

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