search.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <view id="body">
  3. <view class="history" v-if="products.length===0&&search===''">
  4. <view class="header">
  5. <image src="../../static/images/clock.png" mode="scaleToFill"
  6. style="width: 30upx;height: 30upx;margin-left: 10upx;"></image>
  7. <text style="margin-left: 10upx;">历史搜索</text>
  8. <uni-icons type="trash" style="margin-left: auto;margin-right: 10upx;" @tap="removeHistory"></uni-icons>
  9. </view>
  10. <view class="content">
  11. <view class="item" v-for="vlaue in history" @tap="fastSearch(vlaue)">
  12. {{vlaue}}
  13. </view>
  14. </view>
  15. </view>
  16. <view class="list">
  17. <template v-for="product in products">
  18. <product-item type="list" :id="product.id" :image="product.images|imagesFilter" :title="product.name"
  19. :org-price="product.org_price" :price="product.price"></product-item>
  20. </template>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. export default {
  26. data() {
  27. return {
  28. products: [],
  29. page: 1,
  30. pageLoading: false,
  31. search: "",
  32. history: []
  33. }
  34. },
  35. onPageScroll(e) {
  36. const query = uni.createSelectorQuery();
  37. query.select("#body").boundingClientRect(data => {
  38. if (e.scrollTop > data.height - uni.getSystemInfoSync().windowHeight * 2) {
  39. this.getProductList();
  40. }
  41. }).exec();
  42. },
  43. onLoad() {
  44. this.history = uni.getStorageSync("history") || []
  45. },
  46. onNavigationBarSearchInputChanged(e) {
  47. this.search = e.text;
  48. },
  49. onNavigationBarSearchInputConfirmed(e) {
  50. this.page = 1
  51. this.products = []
  52. this.getProductList()
  53. },
  54. onNavigationBarButtonTap(e) {
  55. if (e.index === 0) {
  56. this.page = 1
  57. this.products = []
  58. this.getProductList()
  59. }
  60. },
  61. methods: {
  62. getProductList() {
  63. if (!this.pageLoading) {
  64. console.log("加载下一页");
  65. this.pageLoading = true;
  66. this.$http.get({
  67. url: "/product/lists",
  68. data: {
  69. limit: 10,
  70. page: this.page,
  71. search: this.search
  72. },
  73. success: (res) => {
  74. this.products = [...this.products, ...res.data.data.rows]
  75. this.page++;
  76. this.pageLoading = false;
  77. }
  78. })
  79. if (!this.history.includes(this.search) && this.search !== '') {
  80. console.log(this.history)
  81. this.history.push(this.search)
  82. uni.setStorageSync("history", this.history)
  83. }
  84. }
  85. },
  86. fastSearch(vlaue) {
  87. this.search = vlaue
  88. this.page = 1
  89. this.products = []
  90. this.getProductList()
  91. // #ifdef APP-PLUS
  92. const currentWebview = this.$scope.$getAppWebview();
  93. currentWebview.setTitleNViewSearchInputText(this.search);
  94. // #endif
  95. },
  96. removeHistory() {
  97. uni.removeStorageSync("history")
  98. this.history = []
  99. }
  100. }
  101. }
  102. </script>
  103. <style lang="scss" scoped>
  104. .list {
  105. display: flex;
  106. justify-content: space-between;
  107. flex-wrap: wrap;
  108. padding: 18upx;
  109. }
  110. .list .product-item {
  111. margin-bottom: 18upx;
  112. }
  113. .history {
  114. margin: 20upx;
  115. padding: 10upx;
  116. background: white;
  117. border-radius: 10upx;
  118. .header {
  119. display: flex;
  120. font-size: 28upx;
  121. align-items: center;
  122. padding-bottom: 10upx;
  123. border-bottom: 2upx solid #EEEEEE;
  124. }
  125. .content {
  126. min-height: 160upx;
  127. display: flex;
  128. justify-content: start;
  129. flex-wrap: wrap;
  130. .item {
  131. font-size: 28upx;
  132. margin: 10upx;
  133. padding: 0upx 20upx;
  134. color: #999999;
  135. border: 2upx solid #999999;
  136. height: 40upx;
  137. border-radius: 40upx;
  138. }
  139. }
  140. }
  141. </style>