searchhistory.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <view class="pages">
  3. <u-navbar
  4. title="产品搜索"
  5. :placeholder="true"
  6. :autoBack="true"
  7. :safeAreaInsetTop="true"
  8. >
  9. </u-navbar>
  10. <view class="search-wrap">
  11. <u-search
  12. placeholder="请输入搜索的商品"
  13. :clearabled="true"
  14. :showAction="false"
  15. height="80rpx"
  16. @search="search"
  17. @custom="search"
  18. bgColor="#fff"
  19. borderColor="#00A447"
  20. v-model="keyword">
  21. </u-search>
  22. </view>
  23. <view class="search-history page-wrap" v-if="searchHistory.length>0">
  24. <view class="search-title u-flex u-row-between">
  25. <text>最近搜索</text>
  26. <u-icon @click="clearStorage" name="trash" color="#666" size="30"></u-icon>
  27. </view>
  28. <view class="history-wrap">
  29. <view class="history" v-for="(item,index) in searchHistory" :key="index" @click="keywordClick(item)">
  30. {{item.name}}
  31. <!-- <u-icon @click="clearStorage" name="trash" color="#666" size="20"></u-icon> -->
  32. </view>
  33. </view>
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. export default {
  39. data() {
  40. return {
  41. keyword:'',
  42. searchHistory:[],
  43. }
  44. },
  45. onShow() {
  46. let that = this;
  47. uni.getStorage({
  48. key:'searchgoodshistory',
  49. success: function(storagedata) {
  50. that.searchHistory = storagedata.data;
  51. console.log('storagedata',storagedata.data);
  52. }
  53. })
  54. },
  55. onLoad() {
  56. },
  57. methods: {
  58. goSearch(keyword){
  59. uni.$u.route('/shopping/productsearch', {
  60. goodsName: keyword
  61. });
  62. },
  63. search(e){
  64. let that = this;
  65. // 存储历史数据
  66. let index = this.searchHistory.findIndex(item => item.name === this.keyword);
  67. // console.log('index',index);
  68. console.log('11',this.keyword.length<1);
  69. if (index !== -1||this.keyword.length<1) {
  70. this.goSearch(this.keyword)
  71. // 已经存在,不用重复添加了
  72. return;
  73. }
  74. this.searchHistory.push({ name: this.keyword });
  75. uni.setStorage({
  76. key: 'searchgoodshistory',
  77. data: that.searchHistory,
  78. success: function() {
  79. that.goSearch(this.keyword)
  80. // console.log('数据存储成功')
  81. },
  82. fail: function() {
  83. // console.log('数据存储失败')
  84. }
  85. });
  86. },
  87. keywordClick(item){
  88. this.goSearch(item.name)
  89. // console.log('keywordClick',item);
  90. },
  91. clearStorage(){
  92. let that = this;
  93. uni.clearStorage({
  94. key:'searchgoodshistory',
  95. success:function(){
  96. that.searchHistory = []
  97. }
  98. })
  99. }
  100. }
  101. }
  102. </script>
  103. <style lang="scss" scoped>
  104. .search-history{
  105. color: #333333;
  106. margin-bottom: 30rpx;
  107. .search-title{
  108. font-size: 32rpx;
  109. font-weight: 600;
  110. line-height: 45rpx;
  111. margin-bottom: 20rpx;
  112. }
  113. .history{
  114. display: inline-block;
  115. height: 48rpx;
  116. line-height: 48rpx;
  117. background: #F5F5F5;
  118. border-radius: 24rpx;
  119. padding: 0 24rpx;
  120. min-width: 100rpx;
  121. margin-left: 20rpx;
  122. margin-bottom: 20rpx;
  123. text-align: center;
  124. position: relative;
  125. /deep/ .u-icon{
  126. position: absolute;
  127. right: 0;
  128. top: 2px;
  129. }
  130. }
  131. }
  132. </style>