index.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <view>
  3. <view class="search-header">
  4. <view class="search-title">最近在搜</view>
  5. <view class="search-actions">
  6. <transition>
  7. <van-icon v-show="!isDelete" name="delete-o" class="icon-delete" @click="handleCompleteStatus(true)"/>
  8. </transition>
  9. <transition>
  10. <view v-show="isDelete" class="delete-controls">
  11. <span class="clear-text" @click="handleHistoryEmpty">清空</span>
  12. <van-button type="primary" size="small" @click="handleCompleteStatus(false)">完成</van-button>
  13. </view>
  14. </transition>
  15. </view>
  16. </view>
  17. <view class="search-suggestions">
  18. <view
  19. v-for="item in searchSuggestion"
  20. :key="item.uuid"
  21. class="suggestion-item"
  22. @touchstart.passive="startLongPress"
  23. @touchend="endLongPress"
  24. >
  25. <van-icon name="clock-o"/>
  26. <span class="item-text" @click='handleSearch(item)'>{{ item.searchCriteria }}</span>
  27. <transition>
  28. <van-icon name="close" @click="toggleDelete(item.uuid)" v-show="isDelete"/>
  29. </transition>
  30. </view>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. import {deleteSearchRecord, getSearchRecordList, searchCommitment} from "@/api/commitment";
  36. export default {
  37. name: 'SearchComponent',
  38. data() {
  39. return {
  40. isDelete: false,
  41. searchSuggestion: [],
  42. longPressTimer: null,
  43. }
  44. },
  45. mounted() {
  46. this.handleSearchSuggestion()
  47. },
  48. methods: {
  49. startLongPress() {
  50. this.longPressTimer = setTimeout(() => {
  51. this.isDelete = true
  52. }, 1000)
  53. },
  54. endLongPress() {
  55. clearTimeout(this.longPressTimer)
  56. },
  57. toggleDelete(uuid) {
  58. deleteSearchRecord({uuid})
  59. this.handleSearchSuggestion()
  60. },
  61. handleHistoryEmpty() {
  62. deleteSearchRecord({
  63. createUser: '2'
  64. })
  65. this.handleSearchSuggestion()
  66. },
  67. handleCompleteStatus(flag) {
  68. this.isDelete = flag
  69. },
  70. async handleSearch(item) {
  71. this.$emit('search', item.searchCriteria)
  72. },
  73. async handleSearchSuggestion() {
  74. try {
  75. const res = await getSearchRecordList({
  76. userId: '2'
  77. })
  78. this.searchSuggestion = res.data.slice(0, 6)
  79. } catch (e) {
  80. console.log(e)
  81. }
  82. },
  83. }
  84. }
  85. </script>
  86. <style lang="scss" scoped>
  87. .search-header {
  88. display: flex;
  89. justify-content: space-between;
  90. padding: 0.625rem 0.5rem;
  91. border-bottom: 1px solid #ccc;
  92. }
  93. .search-title {
  94. display: flex;
  95. align-items: center;
  96. }
  97. .search-actions {
  98. display: flex;
  99. align-items: center;
  100. }
  101. .icon-delete {
  102. margin-right: 0.25rem;
  103. }
  104. .delete-controls {
  105. display: flex;
  106. align-items: center;
  107. justify-content: center;
  108. }
  109. .clear-text {
  110. font-size: 12px;
  111. color: #007aff;
  112. margin-right: 0.5rem;
  113. }
  114. .search-suggestions {
  115. margin: 0.375rem 0.5rem;
  116. display: flex;
  117. flex-wrap: wrap;
  118. }
  119. .suggestion-item {
  120. flex-basis: 50%;
  121. margin-bottom: 0.625rem;
  122. }
  123. .item-text {
  124. margin-left: 0.25rem;
  125. margin-right: 0.75rem;
  126. }
  127. </style>