index.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <view class="search_container">
  3. <view class="search_bar_wrap">
  4. <image @click="cancelSearch" class="back_icon" style="width: 12px; height: 20px" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/shop/image/common_left_arrow.png" alt="" srcset="" />
  5. <u-search class="search_bar" shape="square" placeholder="搜索" v-model="keyword" :show-action="false" @change="keywordChange" />
  6. </view>
  7. <search-description v-if="!keyword" />
  8. <u-list @scrolltolower="scrolltolower" lowerThreshold="100" v-if="searchResult.length > 0 && keyword" class="search_list" height="1">
  9. <u-list-item v-for="message in searchResult" :key="message.clientMsgID">
  10. <nomal-message-item :source="message" @click="jumpToMessage" />
  11. </u-list-item>
  12. </u-list>
  13. <view v-if="searchResult.length === 0 && keyword" class="search_empty">
  14. <image src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/shop/image/search_empty.png" alt="search_empty" />
  15. <view class="search_empty_text">
  16. <text>没有找到</text>
  17. <text>{{ `“${keyword}”` }}</text>
  18. <text>相关结果</text>
  19. </view>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import IMSDK from 'openim-uniapp-polyfill';
  25. import SearchDescription from './components/SearchDescription.vue';
  26. import NomalMessageItem from './components/NomalMessageItem.vue';
  27. export default {
  28. components: {
  29. SearchDescription,
  30. NomalMessageItem
  31. },
  32. data() {
  33. return {
  34. keyword: '',
  35. searchResult: [],
  36. loadState: {
  37. loading: false,
  38. hasMore: true,
  39. pageIndex: 1
  40. }
  41. };
  42. },
  43. mounted() {},
  44. methods: {
  45. cancelSearch() {
  46. uni.navigateBack();
  47. },
  48. scrolltolower() {
  49. if (this.loadState.hasMore && !this.loadState.loading) {
  50. this.loadSearchData();
  51. }
  52. },
  53. keywordChange(value) {
  54. uni.$u.debounce(() => {
  55. this.loadState.pageIndex = 1;
  56. this.searchResult = [];
  57. this.loadSearchData();
  58. });
  59. },
  60. loadSearchData() {
  61. if (!this.keyword) return;
  62. this.loadState.loading = true;
  63. const options = {
  64. conversationID: this.$store.getters.storeCurrentConversation.conversationID,
  65. keywordList: [this.keyword],
  66. keywordListMatchType: 0,
  67. senderUserIDList: [],
  68. messageTypeList: [],
  69. searchTimePosition: 0,
  70. searchTimePeriod: 0,
  71. pageIndex: this.loadState.pageIndex,
  72. count: 20
  73. };
  74. IMSDK.asyncApi(IMSDK.IMMethods.SearchLocalMessages, IMSDK.uuid(), options)
  75. .then(({ data }) => {
  76. const searchData = data.searchResultItems ? data.searchResultItems[0].messageList : [];
  77. if (this.loadState.pageIndex === 1) {
  78. this.searchResult = [...searchData];
  79. } else {
  80. this.searchResult = [...this.searchResult, ...searchData];
  81. }
  82. console.log(this.searchResult);
  83. this.loadState.pageIndex += 1;
  84. this.loadState.hasMore = searchData.length === 20;
  85. })
  86. .catch(() => uni.$u.toast('搜索失败'))
  87. .finally(() => (this.loadState.loading = false));
  88. },
  89. jumpToMessage(message) {
  90. uni.$u.route('/pages/common/previewHistoryMessage/index', {
  91. conversationData: JSON.stringify({
  92. ...this.$store.getters.storeCurrentConversation
  93. }),
  94. jumpMessage: JSON.stringify(message)
  95. });
  96. }
  97. }
  98. };
  99. </script>
  100. <style lang="scss" scoped>
  101. .search_container {
  102. @include colBox(false);
  103. height: 100vh;
  104. overflow: hidden;
  105. .search_bar_wrap {
  106. margin-top: var(--status-bar-height);
  107. display: flex;
  108. flex-direction: row;
  109. align-items: center;
  110. justify-content: center;
  111. padding: 44rpx;
  112. .search_bar {
  113. margin-left: 26rpx !important;
  114. }
  115. ::v-deep .u-search__action {
  116. color: $uni-color-primary;
  117. }
  118. }
  119. .search_empty {
  120. text-align: center;
  121. margin-top: 96rpx;
  122. image {
  123. width: 250rpx;
  124. height: 150rpx;
  125. }
  126. &_text {
  127. margin-top: 46rpx;
  128. color: $u-tips-color;
  129. }
  130. }
  131. .search_list {
  132. flex: 1;
  133. }
  134. }
  135. </style>