index.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <view class="search_container">
  3. <custom-nav-bar :route="false">
  4. <view slot="left"></view>
  5. <view class="search_bar" slot="center">
  6. <u-search actionText="取消" @change="keywordChange" @custom="cancel" @search="startSearch" shape="square" :placeholder="getPlaceholder" v-model="keyword" />
  7. </view>
  8. </custom-nav-bar>
  9. <view v-show="!empty && !searching" @click="startSearch(keyword)" class="result_row">
  10. <image class="icon" :src="getIcon" alt="" />
  11. <view class="">
  12. <text>查找:</text>
  13. <text>{{ keyword }}</text>
  14. </view>
  15. </view>
  16. <view v-show="searching && !empty" class="result_row result_row_empty">
  17. <u-loading-icon></u-loading-icon>
  18. </view>
  19. <view v-show="empty" class="result_row result_row_empty">
  20. <text>未搜索到相关结果</text>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. import IMSDK from 'openim-uniapp-polyfill';
  26. import CustomNavBar from '../../../components/CustomNavBar/index.vue';
  27. import { businessSearchUserInfo } from '../../../api/login';
  28. import { filterEmptyValue } from '../../../util/common';
  29. const searchGroup = 'https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/shop/image/contact_add_join_group_fill.png';
  30. const searchUser = 'https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/shop/image/contact_add_search_user_fill.png';
  31. export default {
  32. components: {
  33. CustomNavBar
  34. },
  35. data() {
  36. return {
  37. keyword: '',
  38. searching: false,
  39. empty: false,
  40. isSearchGroup: false
  41. };
  42. },
  43. computed: {
  44. getIcon() {
  45. return this.isSearchGroup ? searchGroup : searchUser;
  46. },
  47. getPlaceholder() {
  48. return this.isSearchGroup ? '请输入群聊ID' : '搜索ID或手机号添加好友';
  49. }
  50. },
  51. onLoad(options) {
  52. const { isSearchGroup } = options;
  53. this.isSearchGroup = JSON.parse(isSearchGroup);
  54. },
  55. methods: {
  56. cancel() {
  57. uni.navigateBack();
  58. },
  59. keywordChange() {
  60. if (this.empty) {
  61. this.empty = !this.empty;
  62. }
  63. },
  64. async startSearch(value) {
  65. if (!value) return;
  66. this.searching = true;
  67. try {
  68. if (this.isSearchGroup) {
  69. let info = this.$store.getters.storeGroupList.find((item) => item.groupID === value);
  70. if (!info) {
  71. const { data } = await IMSDK.asyncApi(IMSDK.IMMethods.GetSpecifiedGroupsInfo, IMSDK.uuid(), [value]);
  72. info = data[0];
  73. }
  74. if (info) {
  75. uni.navigateTo({
  76. url: `/pages_im/pages/common/groupCard/index?sourceInfo=${JSON.stringify(info)}`
  77. });
  78. } else {
  79. this.empty = true;
  80. }
  81. } else {
  82. let info = this.$store.getters.storeFriendList.find((item) => item.userID === value);
  83. if (!info) {
  84. const { total, users } = await businessSearchUserInfo(value);
  85. if (total > 0) {
  86. const { data } = await IMSDK.asyncApi(IMSDK.IMMethods.GetUsersInfo, IMSDK.uuid(), [users[0].userID]);
  87. const imData = data[0];
  88. info = { ...imData, ...users[0] };
  89. }
  90. }
  91. if (info) {
  92. console.log('-----qxj info:' + JSON.stringify(info));
  93. uni.navigateTo({
  94. url: `/pages_im/pages/common/userCard/index?sourceInfo=${JSON.stringify(info)}`
  95. });
  96. } else {
  97. this.empty = true;
  98. }
  99. }
  100. } catch (e) {
  101. //TODO handle the exception
  102. }
  103. this.searching = false;
  104. }
  105. }
  106. };
  107. </script>
  108. <style lang="scss" scoped>
  109. .search_container {
  110. height: 100vh;
  111. background-color: #f8f8f8;
  112. .search_bar {
  113. width: 100%;
  114. padding: 0 44rpx;
  115. }
  116. .result_row {
  117. @include vCenterBox();
  118. padding: 24rpx 44rpx;
  119. font-size: 28rpx;
  120. color: $uni-text-color;
  121. background-color: #fff;
  122. .icon {
  123. width: 20px;
  124. height: 20px;
  125. margin-right: 24rpx;
  126. }
  127. &_empty {
  128. display: flex;
  129. justify-content: center;
  130. color: #999;
  131. }
  132. }
  133. }
  134. </style>