search.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <template>
  2. <view>
  3. <!-- 头部搜索框 -->
  4. <view class="fixed cu-bar search bg-blue" :style="style">
  5. <view class="action" @click="back()">
  6. <image style="width: 32rpx;height: 32rpx;" src="/static/user/bask.png"></image>
  7. </view>
  8. <view class="search-form round">
  9. <image src="/static/index/search-gray.png" class="login-img margin-left margin-right"></image>
  10. <input :focus="focusValue" type="text" placeholder="请输入关键字搜索" v-model="searchValue" confirm-type="search"></input>
  11. <image src="/static/index/delete-gray.png" @click="searchValue = ''" class="login-img margin-right"></image>
  12. </view>
  13. <view class="action" @click="search()">
  14. <text class="text-white">搜索</text>
  15. </view>
  16. </view>
  17. <!-- 搜索历史 -->
  18. <view >
  19. <view :style="paddingTop"></view>
  20. <view class="cu-bar bg-white">
  21. <view class="action text-black">
  22. <text>历史搜索</text>
  23. </view>
  24. <view class="action" @click="crealData()">
  25. <image src="/static/index/delete.png" class="del-img"></image>
  26. </view>
  27. </view>
  28. </view>
  29. <view class="cu-list grid bg-black col-4 no-border">
  30. <view class="cu-item" v-for="(item,index) in searchKey" v-if="index<10" :key="index">
  31. <button class="padding-xs cu-btn bg-gray-secondary round margin-right" @click="sel(item)">
  32. <text class="text-cut text-black">{{item}}</text>
  33. </button>
  34. </view>
  35. </view>
  36. <!--提示窗口 -->
  37. <view class="cu-modal bottom-modal" :class="modalName?'show':''" >
  38. <view class="cu-dialog" style="background-color: white;border-radius: 10px 10px 0 0;padding-bottom: 20rpx;">
  39. <view class="cu-bar bg-white">
  40. <view class="action text-gray"></view>
  41. <view class="action text-black text-bold" style="color: #101010;">清空历史记录</view>
  42. <view class="action text-gray" @tap="modalName=false">取消</view>
  43. </view>
  44. <view class="">
  45. <radio-group class="block" @change="clearKey">
  46. <view class="cu-list menu text-left" v-for="(item,index) in radioList" :key="index">
  47. <view class="cu-item" >
  48. <label class="flex justify-between align-center flex-sub">
  49. <view class="flex-sub" style="color: #101010;">{{item.name}}</view>
  50. <radio class="round" :class="radio==item.vlaue ?'checked':''" :checked="radio==item.vlaue ? true:false"
  51. :value="item.value"></radio>
  52. </label>
  53. </view>
  54. </view>
  55. </radio-group>
  56. </view>
  57. </view>
  58. </view>
  59. </view>
  60. </template>
  61. <script>
  62. export default {
  63. computed: {
  64. style() {
  65. var StatusBar = this.StatusBar;
  66. var CustomBar = this.CustomBar;
  67. var bgImage = this.bgImage;
  68. var style = `height:${CustomBar}px;padding-top:${StatusBar}px;`;
  69. if (this.bgImage) {
  70. style = `${style}background-image:url(${bgImage});`;
  71. }
  72. return style
  73. }
  74. },
  75. data() {
  76. return {
  77. searchValue: "",
  78. searchKey: "",
  79. modalName: false,
  80. pageType: 0,
  81. radioList:[{name:"确定",value:"1"}],
  82. radio: 0,
  83. paddingTop:"height:"+(this.StatusBar+50)+"px",
  84. focusValue:true
  85. }
  86. },
  87. onLoad() {
  88. if (uni.getStorageSync("pageType")) { //区分搜索页面跳转
  89. this.pageType = uni.getStorageSync("pageType");
  90. }
  91. },
  92. onShow() {
  93. this.focusValue=true;
  94. this.searchKey = uni.getStorageSync("searchHistory");
  95. if(this.searchKey){
  96. this.searchKey = this.searchKey.reverse();
  97. }
  98. uni.$on("clear",res=>{
  99. this.searchValue=""
  100. })
  101. },
  102. onHide() {
  103. this.focusValue=false;
  104. },
  105. methods: {
  106. clearKey(e) {
  107. this.radio = e.detail.value;
  108. if(this.radio==1){
  109. this.searchKey = [];
  110. this.modalName = false;
  111. uni.removeStorageSync('searchHistory');
  112. this.$prompt.none("已清空");
  113. }
  114. },
  115. crealData(){
  116. this.searchKey = [];
  117. uni.removeStorageSync('searchHistory');
  118. this.$prompt.none("已清空");
  119. },
  120. sel(item) {
  121. this.searchValue=item;
  122. this.search()
  123. },
  124. search() {
  125. var that =this;
  126. if (!that.searchValue) {
  127. that.$prompt.none("请输入搜索内容");
  128. return
  129. }
  130. /* 记录搜索的记录 */
  131. let history = uni.getStorageSync("searchHistory") == "" ? [] : uni.getStorageSync("searchHistory");
  132. if(history.length==0){
  133. history.push(that.searchValue);
  134. }else{
  135. history.push(that.searchValue);
  136. history =that.unique(history);//历史记录去重复
  137. }
  138. uni.setStorageSync("searchHistory", history);
  139. if (that.pageType == 4) { //4为智能查询跳转过来
  140. uni.navigateTo({
  141. url: "/pages/smartSearchList/smartSearchList?searchValue=" + that.searchValue
  142. })
  143. } else {
  144. uni.navigateTo({
  145. url: "/pages/searchResults/searchResults?searchValue=" + that.searchValue
  146. })
  147. }
  148. },
  149. unique(arr){
  150.   let hash=[];
  151.   for (let i = 0; i < arr.length; i++) {
  152.     if(hash.indexOf(arr[i]) === -1){
  153.       hash.push(arr[i]);
  154.     }
  155.   }
  156. return hash;
  157. },
  158. back() {
  159. uni.navigateBack({
  160. delta: 1
  161. })
  162. }
  163. },
  164. }
  165. </script>
  166. <style>
  167. page {
  168. background: #FFF;
  169. }
  170. .del-img {
  171. width: 50rpx;
  172. height: 50rpx;
  173. }
  174. .cu-list.grid>.cu-item uni-text {
  175. display: block;
  176. margin-top: 4px;
  177. color: black;
  178. font-size: 11px;
  179. line-height: 17px;
  180. }
  181. .action .text-white{
  182. font-size: 34rpx;
  183. }
  184. .cu-modal.show {
  185. overflow-y: auto;
  186. pointer-events: auto;
  187. }
  188. </style>