searchProduct.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <template>
  2. <view v-if="isShow" @touchmove.stop.prevent="stopTouchMove">
  3. <view class="search-wrap">
  4. <!-- #ifdef H5-->
  5. <view class="state_top"></view>
  6. <!-- #endif -->
  7. <view class="head_top" :style="'height:'+topBarTop()+'px;'"></view>
  8. <!-- 搜索框 -->
  9. <view class="index-search-box d-b-c" id="searchBox"
  10. :style="topBarHeight() == 0 ? '': 'height:'+topBarHeight()+'px;padding-right: 200rpx'">
  11. <view class="reg180" :style="topBarHeight() == 0 ? '': 'height:'+topBarHeight()+'px;'">
  12. <text @click="closeSearch" class="icon iconfont icon-jiantou"></text>
  13. </view>
  14. <view class="index-search t-c flex-1" :style="topBarHeight() == 0 ? '': 'height:'+topBarHeight()+'px;'">
  15. <text class="icon iconfont icon-sousuo"></text>
  16. <input type="text" v-model="form.keyWord" class="flex-1 ml10 f30 gray3" value=""
  17. placeholder-class="f24 gray6" placeholder="输入你要的商品" confirm-type="search" @confirm="search()" />
  18. <!-- <text @click="closeSearch">取消</text> -->
  19. </view>
  20. </view>
  21. <view class="p-0-20 bg-white">
  22. <view class="group-hd">
  23. <view class="left"><text class="min-name">最近搜索</text></view>
  24. <view class="right">
  25. <text class="icon iconfont icon-lajitong" @click='clearStorage'></text>
  26. </view>
  27. </view>
  28. <view class="before-search">
  29. <text class="item" v-for="(item,index) in arr" :key="index"
  30. @click="search(arr[index])">{{arr[index]}}</text>
  31. </view>
  32. </view>
  33. </view>
  34. </view>
  35. </template>
  36. <script>
  37. export default {
  38. data() {
  39. return {
  40. form: {
  41. },
  42. arr: [],
  43. }
  44. },
  45. mounted() {
  46. /*获取缓存数据*/
  47. this.getData();
  48. },
  49. props: ['isShow'],
  50. methods: {
  51. /*获取缓存里的搜索历史*/
  52. getData() {
  53. let self = this;
  54. /*获取搜索记录*/
  55. uni.getStorage({
  56. key: 'search_list',
  57. success: function(res) {
  58. if (res != null && res.data != null) {
  59. self.arr = res.data;
  60. }
  61. }
  62. });
  63. },
  64. /* 禁止手动滑动 */
  65. stopTouchMove() {
  66. return true
  67. },
  68. /*搜索*/
  69. search(str) {
  70. let self = this;
  71. let search = null;
  72. if (str != null) {
  73. search = str;
  74. } else {
  75. search = this.form.keyWord;
  76. let arrs = this.arr;
  77. if (typeof search != "undefined" && search != null && search != '') {
  78. arrs.push(search);
  79. } else {
  80. uni.showToast({
  81. title: '请输入搜索的关键字',
  82. icon: 'none',
  83. duration: 2000
  84. });
  85. return false;
  86. }
  87. /*设置搜索记录*/
  88. uni.setStorage({
  89. key: 'search_list',
  90. data: arrs,
  91. success: function() {
  92. console.log('success');
  93. }
  94. });
  95. }
  96. let category_id = 0;
  97. let sortType = 'all';
  98. self.gotoPage('/pages/product/list/list?search=' + search + '&category_id=' + category_id + '&sortType=' +
  99. sortType);
  100. },
  101. /*清楚缓存*/
  102. clearStorage() {
  103. let self = this;
  104. uni.removeStorage({
  105. key: 'search_list',
  106. success: function(res) {
  107. self.arr = [];
  108. }
  109. });
  110. },
  111. closeSearch(){
  112. this.$emit('close')
  113. }
  114. }
  115. }
  116. </script>
  117. <style>
  118. .search-wrap {
  119. position: fixed;
  120. z-index: 999;
  121. top: 0;
  122. left: 0;
  123. width: 100%;
  124. height: 100%;
  125. background-color: #f5f5f5;
  126. }
  127. .search-wrap .index-search-box .search-box {
  128. padding: 0 20rpx;
  129. height: 64rpx;
  130. line-height: 64rpx;
  131. background: #F5F5F5;
  132. border-radius: 50rpx;
  133. overflow: hidden;
  134. font-size: 28rpx;
  135. color: #999;
  136. box-sizing: border-box;
  137. }
  138. .search-wrap .index-search-box button {
  139. height: 78rpx;
  140. line-height: 78rpx;
  141. border: solid 1rpx #CCCCCC;
  142. color: #333333;
  143. background: #FFFFFF;
  144. }
  145. .search-wrap .index-search-box{
  146. padding: 0 30rpx;
  147. padding-bottom: 30rpx;
  148. background-color: #F5F5F5;
  149. }
  150. .before-search {
  151. display: flex;
  152. justify-content: flex-start;
  153. align-items: center;
  154. flex-flow: wrap;
  155. }
  156. .before-search .item {
  157. padding: 16rpx;
  158. margin-right: 16rpx;
  159. margin-bottom: 16rpx;
  160. border-radius: 8rpx;
  161. color: #686868;
  162. background: #f0f2f5;
  163. font-size: 24rpx;
  164. }
  165. .reg180 {
  166. padding-left: 20rpx;
  167. text-align: center;
  168. transform: rotateY(180deg);
  169. display: flex;
  170. justify-content: flex-end;
  171. align-items: center;
  172. }
  173. .reg180 .icon-jiantou {
  174. color: #333333;
  175. display: block;
  176. width: 44rpx;
  177. height: 44rpx;
  178. line-height: 44rpx;
  179. font-size: 36rpx;
  180. }
  181. </style>