index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <template>
  2. <view class="container">
  3. <view class="search-wrapper">
  4. <view class="search-input">
  5. <view class="search-input-wrapper">
  6. <view class="left">
  7. <text class="search-icon iconfont icon-sousuo"></text>
  8. </view>
  9. <view class="right">
  10. <input v-model="searchValue" class="input" focus="true" placeholder="请输入搜索关键字" type="text"></input>
  11. </view>
  12. </view>
  13. </view>
  14. <view class="search-button">
  15. <button class="button" @click="onSearch" type="warn"> 搜索 </button>
  16. </view>
  17. </view>
  18. <view class="history" v-if="historySearch.length">
  19. <view class="his-head">
  20. <text class="title">最近搜索</text>
  21. <text class="icon iconfont icon-lajixiang col-7" @click="clearSearch"></text>
  22. </view>
  23. <view class="his-list">
  24. <view class="his-item" v-for="(val, index) in historySearch" :key="index">
  25. <view class="history-button" @click="handleQuick(val)">{{ val }}</view>
  26. </view>
  27. </view>
  28. </view>
  29. <!-- </view> -->
  30. </view>
  31. </template>
  32. <script>
  33. const HISTORY_SEARCH = 'historySearch'
  34. export default {
  35. data() {
  36. return {
  37. historySearch: [],
  38. searchValue: ''
  39. }
  40. },
  41. /**
  42. * 生命周期函数--监听页面加载
  43. */
  44. onLoad(options) {
  45. // 获取历史搜索
  46. this.historySearch = this.getHistorySearch()
  47. },
  48. methods: {
  49. /**
  50. * 获取历史搜索
  51. */
  52. getHistorySearch() {
  53. return uni.getStorageSync(HISTORY_SEARCH) || []
  54. },
  55. /**
  56. * 搜索提交
  57. */
  58. onSearch() {
  59. const { searchValue } = this
  60. if (searchValue) {
  61. // 记录历史搜索
  62. this.setHistory(searchValue)
  63. // 跳转到商品列表页
  64. this.$navTo('pages/goods/list', { search: searchValue })
  65. }
  66. },
  67. /**
  68. * 记录历史搜索
  69. */
  70. setHistory(searchValue) {
  71. const data = this.getHistorySearch()
  72. const index = data.indexOf(searchValue)
  73. index > -1 && data.splice(index, 1)
  74. data.unshift(searchValue)
  75. this.historySearch = data
  76. this.onUpdateStorage()
  77. },
  78. /**
  79. * 清空最近搜索记录
  80. */
  81. clearSearch() {
  82. this.historySearch = []
  83. this.onUpdateStorage()
  84. },
  85. /**
  86. * 更新历史搜索缓存
  87. * @param {Object} data
  88. */
  89. onUpdateStorage(data) {
  90. uni.setStorageSync(HISTORY_SEARCH, this.historySearch)
  91. },
  92. /**
  93. * 跳转到最近搜索
  94. */
  95. handleQuick(search) {
  96. this.$navTo('pages/goods/list', { search })
  97. }
  98. }
  99. }
  100. </script>
  101. <style lang="scss" scoped>
  102. .container {
  103. padding: 20rpx;
  104. min-height: 100vh;
  105. background: #f7f7f7;
  106. }
  107. .search-wrapper {
  108. display: flex;
  109. height: 78rpx;
  110. }
  111. // 搜索输入框
  112. .search-input {
  113. width: 80%;
  114. background: #fff;
  115. border-radius: 50rpx 0 0 50rpx;
  116. box-sizing: border-box;
  117. overflow: hidden;
  118. border: solid 1px #cccccc;
  119. .search-input-wrapper {
  120. display: flex;
  121. .left {
  122. display: flex;
  123. width: 60rpx;
  124. justify-content: center;
  125. align-items: center;
  126. .search-icon {
  127. display: block;
  128. color: #b4b4b4;
  129. font-size: 30rpx;
  130. font-weight: bold;
  131. }
  132. }
  133. .right {
  134. flex: 1;
  135. input {
  136. font-size: 28rpx;
  137. height: 78rpx;
  138. line-height: 78rpx;
  139. .input-placeholder {
  140. color: #aba9a9;
  141. }
  142. }
  143. }
  144. }
  145. }
  146. // 搜索按钮
  147. .search-button {
  148. width: 20%;
  149. box-sizing: border-box;
  150. .button {
  151. line-height: 78rpx;
  152. height: 78rpx;
  153. font-size: 28rpx;
  154. border-radius: 0 20px 20px 0;
  155. background: $fuint-theme;
  156. }
  157. }
  158. // 最近搜索
  159. .history {
  160. .his-head {
  161. font-size: 28rpx;
  162. padding: 50rpx 0 0 0;
  163. color: #777;
  164. .icon {
  165. float: right;
  166. }
  167. }
  168. .his-list {
  169. padding: 10px 0;
  170. overflow: hidden;
  171. .his-item {
  172. width: 33.3%;
  173. float: left;
  174. padding: 10rpx;
  175. box-sizing: border-box;
  176. .history-button {
  177. text-align: center;
  178. padding: 14rpx;
  179. line-height: 30rpx;
  180. border-radius: 100rpx;
  181. background: #fff;
  182. font-size: 26rpx;
  183. border: 1px solid #efefef;
  184. overflow: hidden;
  185. white-space: nowrap;
  186. text-overflow: ellipsis;
  187. }
  188. }
  189. }
  190. }
  191. </style>