金诚优选前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.2 KiB

  1. <template>
  2. <view>
  3. <lf-nav title="搜索" :showIcon="true"></lf-nav>
  4. <view class="head">
  5. <u-search placeholder="请输入商品名或商户名"
  6. :focus="is_focus"
  7. v-model="value"
  8. @search="search"
  9. @custom="customClick">
  10. </u-search>
  11. </view>
  12. <view class="content">
  13. <!-- 大家都在搜 TODO 暂时不需要做 -->
  14. <!-- <block v-if="hot_list.length">
  15. <view>
  16. <text class="title">大家都在搜</text>
  17. </view>
  18. <view class="list">
  19. <view class="item"
  20. @click="activeKeyWord(item)"
  21. v-for="(item, index) in hot_list"
  22. :key="index">{{ item }}
  23. </view>
  24. </view>
  25. </block> -->
  26. <!-- 我搜过的 -->
  27. <block v-if="history_list.length">
  28. <view class="lf-row-between">
  29. <text class="title">我搜过的</text>
  30. <text class="lf-iconfont icon-shanchu lf-color-777" @click="remove"></text>
  31. </view>
  32. <view class="list">
  33. <view class="item item-2"
  34. @click="activeKeyWord(item)"
  35. v-for="(item, index) in history_list"
  36. :key="index">{{ item }}
  37. </view>
  38. </view>
  39. </block>
  40. </view>
  41. </view>
  42. </template>
  43. <script>
  44. export default {
  45. data() {
  46. return {
  47. value: '', // 搜索的值
  48. is_focus: true, // 是否自动获得焦点
  49. hot_list: [], // 热门搜索
  50. history_list: [], // 历史搜索
  51. max_history_count: 12 // 最多存多少个历史搜索值
  52. }
  53. },
  54. onLoad(){
  55. this.getHistorySearch();
  56. this.getHotSearch();
  57. },
  58. onHide(){
  59. this.setHistorySearch();
  60. },
  61. methods: {
  62. // 获取搜索历史
  63. getHistorySearch(){
  64. let history_search = this.$cookieStorage.get('history_search') || [];
  65. this.history_list = JSON.parse(JSON.stringify(history_search));
  66. },
  67. // 设置搜索历史
  68. setHistorySearch(){
  69. if(this.history_list.length){
  70. this.$cookieStorage.set('history_search', this.history_list);
  71. }else{
  72. this.$cookieStorage.clear('history_search');
  73. }
  74. },
  75. // TODO 获取热门搜索 待对接请求接口
  76. getHotSearch(){
  77. // this.$http.get({
  78. // api: ''
  79. // }).then(res => {
  80. // console.log("hot", res);
  81. // })
  82. let list = ['秋上新','纪梵希','雅诗兰黛','中秋','苹果爱普','基督教烧烤'];
  83. this.hot_list = list;
  84. },
  85. // 激活关键词搜索
  86. activeKeyWord(value){
  87. this.value = value;
  88. this.customClick(value);
  89. },
  90. // 用户按下搜索按钮,或回车键
  91. search(value){
  92. this.customClick(value);
  93. },
  94. // 点击了搜索按钮
  95. customClick(value){
  96. if(!value || !value.trim()) return this.$msg('搜索内容不能为空');
  97. this.updateHistory(value).then(() => {
  98. this.$url('/pages/shop/searchList?value='+ value);
  99. })
  100. },
  101. // 更新搜索历史列表
  102. updateHistory(value){
  103. return new Promise((resolve, reject) => {
  104. let history_list = [...this.history_list];
  105. if(history_list.includes(value)){
  106. history_list.map((item, index) => {
  107. if(item == value){
  108. this.history_list.splice(index, 1); // 删除掉已经存在的key值
  109. }
  110. })
  111. this.history_list.unshift(value); // 向头部追加新的key值
  112. if(this.history_list.length > this.max_history_count){
  113. this.history_list.pop(); // 如果历史记录超出预设最大范围,将最尾key值移除
  114. }
  115. }else{
  116. this.history_list.unshift(value);
  117. if(this.history_list.length > this.max_history_count){
  118. this.history_list.pop();
  119. }
  120. }
  121. resolve(value); // 操作完成,回调成功
  122. })
  123. },
  124. // 清空搜索记录
  125. remove(){
  126. uni.showModal({
  127. title: '温馨提示',
  128. content: '确定清空搜索历史吗?',
  129. confirmColor: '#15716E',
  130. success: result => {
  131. if(result.confirm){
  132. this.history_list = [];
  133. }
  134. }
  135. })
  136. }
  137. }
  138. }
  139. </script>
  140. <style lang="scss" scoped>
  141. .head{
  142. padding: 30rpx 32rpx;
  143. }
  144. .content{
  145. padding: 20rpx 32rpx;
  146. .title{
  147. font-size: 28rpx;
  148. color: #999999;
  149. }
  150. .list{
  151. display: flex;
  152. flex-wrap: wrap;
  153. padding: 20rpx 0 40rpx;
  154. .item{
  155. padding: 4rpx 20rpx;
  156. background-color: #15716E;
  157. color: #FFFFFF;
  158. border-radius: 34rpx;
  159. font-size: 28rpx;
  160. margin-right: 20rpx;
  161. margin-bottom: 20rpx;
  162. }
  163. .item-2{
  164. background-color: #F4F8F8;
  165. color: #333333;
  166. }
  167. }
  168. }
  169. </style>