search.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 class="nomore" v-if="nomore">没搜到任何相关商品</view>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. data() {
  28. return {
  29. products: [],
  30. page: 1,
  31. pageLoading: false,
  32. search: "",
  33. history: [],
  34. nomore: false
  35. }
  36. },
  37. onPageScroll(e) {
  38. const query = uni.createSelectorQuery();
  39. query.select("#body").boundingClientRect(data => {
  40. if (e.scrollTop > data.height - uni.getSystemInfoSync().windowHeight * 2) {
  41. this.getProductList();
  42. }
  43. }).exec();
  44. },
  45. onLoad() {
  46. this.history = uni.getStorageSync("history") || []
  47. },
  48. onNavigationBarSearchInputChanged(e) {
  49. this.search = e.text;
  50. },
  51. onNavigationBarSearchInputConfirmed(e) {
  52. this.page = 1
  53. this.products = []
  54. this.getProductList()
  55. },
  56. onNavigationBarButtonTap(e) {
  57. if (e.index === 0) {
  58. this.page = 1
  59. this.products = []
  60. this.getProductList()
  61. }
  62. },
  63. methods: {
  64. getProductList() {
  65. if (!this.pageLoading) {
  66. console.log("加载下一页");
  67. this.pageLoading = true;
  68. this.$http.get({
  69. url: "/product/lists",
  70. data: {
  71. limit: 10,
  72. page: this.page,
  73. search: this.search
  74. },
  75. success: (res) => {
  76. this.products = [...this.products, ...res.data.data.rows]
  77. this.page++;
  78. this.pageLoading = false;
  79. if (this.products.length === 0) {
  80. this.nomore = true;
  81. } else {
  82. this.nomore = false;
  83. }
  84. }
  85. })
  86. if (!this.history.includes(this.search) && this.search !== '') {
  87. console.log(this.history)
  88. this.history.push(this.search)
  89. uni.setStorageSync("history", this.history)
  90. }
  91. }
  92. },
  93. fastSearch(vlaue) {
  94. this.search = vlaue
  95. this.page = 1
  96. this.products = []
  97. this.getProductList()
  98. // #ifdef APP-PLUS
  99. const currentWebview = this.$scope.$getAppWebview();
  100. currentWebview.setTitleNViewSearchInputText(this.search);
  101. currentWebview.setTitleNViewSearchInputFocus(true)
  102. currentWebview.setTitleNViewSearchInputFocus(false)
  103. // #endif
  104. },
  105. removeHistory() {
  106. uni.removeStorageSync("history")
  107. this.history = []
  108. }
  109. }
  110. }
  111. </script>
  112. <style lang="scss" scoped>
  113. .list {
  114. display: flex;
  115. justify-content: space-between;
  116. flex-wrap: wrap;
  117. padding: 18upx;
  118. }
  119. .list .product-item {
  120. margin-bottom: 18upx;
  121. }
  122. .history {
  123. margin: 20upx;
  124. padding: 10upx;
  125. background: white;
  126. border-radius: 10upx;
  127. .header {
  128. display: flex;
  129. font-size: 28upx;
  130. align-items: center;
  131. padding-bottom: 10upx;
  132. border-bottom: 2upx solid #EEEEEE;
  133. }
  134. .content {
  135. min-height: 160upx;
  136. display: flex;
  137. justify-content: start;
  138. flex-wrap: wrap;
  139. .item {
  140. font-size: 28upx;
  141. margin: 10upx;
  142. padding: 0upx 20upx;
  143. color: #999999;
  144. border: 2upx solid #999999;
  145. height: 40upx;
  146. border-radius: 40upx;
  147. }
  148. }
  149. }
  150. .nomore {
  151. text-align: center;
  152. font-size: 28upx;
  153. }
  154. </style>