search.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <template>
  2. <view class="bg-white" style="min-height: 100vh;">
  3. <view class="cu-bar bg-white ">
  4. <view class="search-form round">
  5. <text class="cuIcon-search" />
  6. <input tyle="font-size: 34rpx;" v-model="searchKey" :adjust-position="false" type="text"
  7. placeholder="请输入搜索内容" confirm-type="search" />
  8. <text class="cuIcon-roundclose" style="font-size: 34rpx;color: #6c6c6c;" v-if="searchClose"
  9. @click="searchKey= ''" />
  10. </view>
  11. <view class="action" @click="bindsearch">
  12. <view class="cu-btn round sm" style="background-color: #f0b363;padding: 28rpx 36rpx;">
  13. 搜索
  14. </view>
  15. </view>
  16. </view>
  17. <view v-if="searchKey.length<=0||datalist==null">
  18. <view class="search">
  19. <view class="top">
  20. <text class="left">历史搜索</text>
  21. <view class="center" v-if="!$isEmpty(oldKeywordList)">
  22. <text @click="delOldKeywordList" class="cuIcon-delete text-gray"
  23. style="font-size: 34rpx;"></text>
  24. </view>
  25. </view>
  26. <view class="list">
  27. <view class="cu-tag "
  28. style="background-color: #f3f3f4;color: #6c6c6c;padding: 16rpx 26rpx;border-radius: 4rpx;margin-bottom: 16rpx;"
  29. v-for="(item,index) in oldKeywordList" :key="index">
  30. {{item}}
  31. </view>
  32. </view>
  33. </view>
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. export default {
  39. data() {
  40. return {
  41. searchKey: '',
  42. oldKeywordList: [],
  43. datalist: null
  44. }
  45. },
  46. onLoad() {
  47. this.loadOldKeyword();
  48. },
  49. computed: {
  50. searchClose: {
  51. get() {
  52. if (this.$isEmpty(this.searchKey)) {
  53. return false
  54. } else {
  55. return true
  56. }
  57. }
  58. }
  59. },
  60. methods: {
  61. delOldKeywordList() {
  62. this.oldKeywordList = [];
  63. uni.removeStorage({
  64. key: 'OldKeys'
  65. });
  66. },
  67. bindsearch() {
  68. if (this.$isEmpty(this.searchKey)) {
  69. return
  70. }
  71. //搜索todo
  72. this.saveKeyword()
  73. },
  74. //加载历史搜索,自动读取本地Storage
  75. loadOldKeyword() {
  76. uni.getStorage({
  77. key: 'OldKeys',
  78. success: res => {
  79. var OldKeys = JSON.parse(res.data);
  80. this.oldKeywordList = OldKeys;
  81. }
  82. });
  83. },
  84. //保存关键字到历史记录
  85. saveKeyword() {
  86. let keyword = this.searchKey
  87. uni.getStorage({
  88. key: 'OldKeys',
  89. success: res => {
  90. //获取历史搜索词
  91. var OldKeys = JSON.parse(res.data);
  92. var findIndex = OldKeys.indexOf(keyword);
  93. if (findIndex == -1) {
  94. //如果历史搜索词里没有该输入的搜索词,就添加进数组头
  95. OldKeys.unshift(keyword);
  96. } else {
  97. //如果有,就删除该搜索词并插进数组头
  98. OldKeys.splice(findIndex, 1);
  99. OldKeys.unshift(keyword);
  100. }
  101. //最多10个纪录,如果超过十条记录,就弹出最后一个元素
  102. OldKeys.length > 10 && OldKeys.pop();
  103. uni.setStorage({
  104. key: 'OldKeys',
  105. data: JSON.stringify(OldKeys)
  106. });
  107. this.oldKeywordList = OldKeys; //更新历史搜索
  108. },
  109. fail: e => {
  110. var OldKeys = [keyword];
  111. uni.setStorage({
  112. key: 'OldKeys',
  113. data: JSON.stringify(OldKeys)
  114. });
  115. this.oldKeywordList = OldKeys; //更新历史搜索
  116. }
  117. });
  118. }
  119. }
  120. }
  121. </script>
  122. <style lang="scss" scoped>
  123. page {
  124. background-color: #FFFFFF;
  125. }
  126. .search {
  127. padding: 20rpx;
  128. margin: 0rpx 20rpx;
  129. .top {
  130. display: flex;
  131. justify-content: space-between;
  132. .left {
  133. font-size: 28rpx;
  134. font-weight: 800;
  135. }
  136. }
  137. .list {
  138. padding-top: 30rpx;
  139. display: flex;
  140. flex-wrap: wrap;
  141. justify-content: flex-start;
  142. }
  143. }
  144. </style>