search.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <template>
  2. <view>
  3. <!-- #ifdef APP-PLUS -->
  4. <view class="status_bar">
  5. <!-- 这里是状态栏,除了h5不需要,其他端都需要 -->
  6. </view>
  7. <!-- #endif -->
  8. <!-- 搜索头部分 start -->
  9. <view class='sea_input_part'>
  10. <!-- #ifndef MP-WEIXIN -->
  11. <text class="back_icon iconfont iconziyuan2" @click="navBack"></text>
  12. <!-- #endif -->
  13. <view class="search_center">
  14. <image class="search_icon" :src="imgUrl+'search.png'"></image>
  15. <input class='sea_input' type='text' :value="input_val" :placeholder="$L('请输入关键词')" @input="inputChange"
  16. @confirm='search' maxlength="50"></input>
  17. <image class='clear_content' v-show="input_val" @click="clearInputVal"
  18. :src="imgUrl+'input_clear.png'" />
  19. </view>
  20. <text class='sea_btn' @click="btnSearch()">{{$L('搜索')}}</text>
  21. </view>
  22. <!-- 搜索头部分 end -->
  23. <!-- 搜索历史 start -->
  24. <view class="search-item" v-if="history_val && history_val.length" style="padding-bottom: 0;">
  25. <view class="search-title">
  26. <text>{{$L('搜索历史')}}</text>
  27. <view class="del" @click="clearHistory">
  28. <image :src="imgUrl+'del_search.png'" />
  29. </view>
  30. </view>
  31. <view class="search-con">
  32. <view class="item" v-for="(item,index) in history_val" :key="index" @click="btnSearch(item)">{{item}}
  33. </view>
  34. </view>
  35. </view>
  36. <!-- 搜索历史 end -->
  37. <!-- 热门搜索 start -->
  38. <view class="search-item" v-if="hotSearchData && hotSearchData.length">
  39. <view class="search-title">
  40. <text>{{$L('热门搜索')}}</text>
  41. </view>
  42. <view class="search-con">
  43. <block v-for="(item,index) in hotSearchData" :key="index" >
  44. <view class="item" @click="btnSearch(item)" v-if="item">{{item}}
  45. </view>
  46. </block>
  47. </view>
  48. </view>
  49. <!-- 热门搜索 end -->
  50. </view>
  51. </template>
  52. <script>
  53. export default {
  54. data() {
  55. return {
  56. imgUrl: getApp().globalData.imgUrl,
  57. input_val: '', //搜索内容
  58. history_val: [],
  59. hotSearchData: [],
  60. }
  61. },
  62. onLoad(option) {
  63. this.getHotSearchData();
  64. this.getHistoryList();
  65. },
  66. onShow() {
  67. },
  68. methods: {
  69. //获取热门搜索词
  70. getHotSearchData() {
  71. var param = {}
  72. param.url = 'v3/system/front/setting/getSettings'
  73. param.data = {}
  74. param.data.names = 'hot_search_words'
  75. this.$request(param).then(res => {
  76. if (res.state == 200) {
  77. this.hotSearchData = res.data[0].split(',');
  78. }
  79. })
  80. },
  81. //获取历史记录
  82. getHistoryList() {
  83. let history_data = uni.getStorageSync('his_keyword');
  84. if (history_data) {
  85. let his_array = history_data.split("~");
  86. let last_arr = [];
  87. for (var i = 0; i < his_array.length; i++) {
  88. !this.$checkSpace(his_array[i]) && last_arr.push(his_array[i]);
  89. }
  90. this.history_val = last_arr;
  91. }
  92. },
  93. //清除搜索历史
  94. clearHistory() {
  95. uni.removeStorageSync('his_keyword');
  96. this.history_val = [];
  97. },
  98. inputChange(e) {
  99. this.input_val = e.detail.value
  100. },
  101. //点击弹起的键盘按钮时触发
  102. search(e) {
  103. if (this.input_val != '') {
  104. this.btnSearch();
  105. }
  106. },
  107. //搜索事件
  108. btnSearch(val = '') {
  109. let {
  110. input_val
  111. } = this;
  112. if (val) {
  113. input_val = val;
  114. this.input_val = val;
  115. }
  116. input_val = input_val.trim();
  117. if (input_val.length > 0) {
  118. this.setHistoryData();
  119. }
  120. //跳转商品列表页
  121. uni.navigateTo({
  122. url: `/pages/product/list?keyword=${encodeURIComponent(input_val)}&source=search`
  123. });
  124. },
  125. //设置缓存
  126. setHistoryData() {
  127. let {
  128. history_val,
  129. input_val
  130. } = this;
  131. let tmp_data = [...history_val];
  132. tmp_data.unshift(input_val);
  133. // 最多取15条,不重复且不为空的数据
  134. tmp_data = tmp_data.reduce((a, b) => {
  135. (a.length <= 14 && b && a.indexOf(b) == -1) ? a.push(b): null;
  136. return a;
  137. }, [])
  138. let history_val_str = tmp_data.join('~');
  139. this.history_val = tmp_data;
  140. uni.setStorageSync("his_keyword", history_val_str)
  141. },
  142. //清空输入内容
  143. clearInputVal() {
  144. this.input_val = '';
  145. },
  146. navBack() {
  147. uni.navigateBack();
  148. },
  149. }
  150. }
  151. </script>
  152. <style lang='scss'>
  153. .status_bar {
  154. height: var(--status-bar-height);
  155. width: 100%;
  156. }
  157. page {
  158. background-color: #fff;
  159. width: 750rpx;
  160. margin: 0 auto;
  161. }
  162. .sea_input_part {
  163. position: relative;
  164. display: flex;
  165. align-items: center;
  166. height: 88rpx;
  167. .back_icon {
  168. padding-left: 20rpx;
  169. }
  170. .sea_input {
  171. flex: 1;
  172. height: 65rpx;
  173. font-size: 24rpx;
  174. color: #333;
  175. }
  176. .search_center {
  177. display: flex;
  178. align-items: center;
  179. border: none;
  180. flex: 1;
  181. height: 65rpx;
  182. margin-left: 20rpx;
  183. padding-left: 20rpx;
  184. border-radius: 32.5rpx;
  185. background-color: #f5f5f5;
  186. .search_icon {
  187. width: 30rpx;
  188. height: 30rpx;
  189. margin-right: 22rpx;
  190. }
  191. }
  192. .clear_content {
  193. width: 45rpx !important;
  194. height: 45rpx !important;
  195. margin-right: 15rpx !important;
  196. }
  197. .sea_btn {
  198. font-size: 28rpx;
  199. color: #2D2D2D;
  200. padding: 10rpx 25rpx;
  201. flex-shrink: 0;
  202. }
  203. &:after {
  204. position: absolute;
  205. content: '';
  206. left: 0;
  207. bottom: 0;
  208. width: 100%;
  209. height: 1rpx;
  210. background-color: #eee;
  211. transform: scaleY(0.5);
  212. }
  213. }
  214. .search-item {
  215. padding: 30rpx 28rpx;
  216. .search-title {
  217. display: flex;
  218. align-items: center;
  219. justify-content: space-between;
  220. height: 48rpx;
  221. color: #2D2D2D;
  222. font-size: 28rpx;
  223. font-weight: bold;
  224. image {
  225. width: 48rpx;
  226. height: 48rpx;
  227. }
  228. }
  229. .search-con {
  230. display: flex;
  231. align-items: center;
  232. flex-wrap: wrap;
  233. .item {
  234. height: 50rpx;
  235. padding: 0 18rpx;
  236. color: #2D2D2D;
  237. line-height: 50rpx;
  238. font-size: 24rpx;
  239. background-color: #F5F5F5;
  240. border-radius: 25rpx;
  241. margin-right: 20rpx;
  242. margin-top: 20rpx;
  243. max-width: 274rpx;
  244. white-space: nowrap;
  245. text-overflow: ellipsis;
  246. overflow: hidden;
  247. word-break: break-all;
  248. }
  249. }
  250. }
  251. </style>