index.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <template>
  2. <view>
  3. <view class='searchGood'>
  4. <view class='search acea-row row-between-wrapper'>
  5. <view class='input acea-row row-between-wrapper'>
  6. <text class='iconfont icon-sousuo'></text>
  7. <input type='text' confirm-type="search" :value='searchValue' :focus="focus" placeholder='点击搜索商品' placeholder-class='placeholder'
  8. @input="setValue" @confirm="searchBut" />
  9. </view>
  10. <view class='bnt' @tap='searchBut'>搜索</view>
  11. </view>
  12. <view class='title'>热门搜索</view>
  13. <view class='list acea-row'>
  14. <block v-for="(item, index) in hotSearchList" :key="index">
  15. <view class='item' @tap='setHotSearchValue(item.title)'>{{ item.title }}</view>
  16. </block>
  17. </view>
  18. <view class='line'></view>
  19. <goodList :bastList="bastList" v-if="bastList.length > 0"></goodList>
  20. <view class='loadingicon acea-row row-center-wrapper' v-if="bastList.length > 0">
  21. <text class='loading iconfont icon-jiazai' :hidden='loading == false'></text>{{ loadTitle }}
  22. </view>
  23. </view>
  24. <view class='noCommodity'>
  25. <view class='pictrue' v-if="bastList.length == 0 && isbastList">
  26. <image src='../../static/images/noSearch.png'></image>
  27. </view>
  28. <recommend :hostProduct='hostProduct' v-if="bastList.length == 0"></recommend>
  29. </view>
  30. </view>
  31. </template>
  32. <script setup>
  33. import { ref } from 'vue'
  34. import { onShow, onReachBottom } from '@dcloudio/uni-app'
  35. import { getSearchKeyword, getProductslist, getProductHot } from '@/api/store.js'
  36. import goodList from '@/components/goodList'
  37. import recommend from '@/components/recommend'
  38. // 响应式数据
  39. const hostProduct = ref([])
  40. const searchValue = ref('')
  41. const focus = ref(true)
  42. const bastList = ref([])
  43. const hotSearchList = ref([])
  44. const limit = ref(8)
  45. const page = ref(1)
  46. const loading = ref(false)
  47. const loadend = ref(false)
  48. const loadTitle = ref('加载更多')
  49. const hotPage = ref(1)
  50. const isScroll = ref(true)
  51. const isbastList = ref(false)
  52. // 获取热搜
  53. function getRoutineHotSearch() {
  54. getSearchKeyword().then(res => {
  55. hotSearchList.value = res.data
  56. })
  57. }
  58. // 获取商品列表
  59. function getProductList() {
  60. if (loadend.value || loading.value) return
  61. loading.value = true
  62. loadTitle.value = ''
  63. getProductslist({
  64. keyword: searchValue.value,
  65. page: page.value,
  66. limit: limit.value
  67. }).then(res => {
  68. const list = res.data.list
  69. const isLoadend = list.length < limit.value
  70. // 合并数组
  71. bastList.value = (bastList.value || []).concat(list)
  72. loading.value = false
  73. loadend.value = isLoadend
  74. loadTitle.value = isLoadend ? "😕人家是有底线的~~" : "加载更多"
  75. page.value += 1
  76. isbastList.value = true
  77. }).catch(() => {
  78. loading.value = false
  79. loadTitle.value = '加载更多'
  80. })
  81. }
  82. // 获取热门商品
  83. function getHostProduct() {
  84. if (!isScroll.value) return
  85. getProductHot(hotPage.value, limit.value).then(res => {
  86. isScroll.value = res.data.list.length >= limit.value
  87. hostProduct.value = hostProduct.value.concat(res.data.list)
  88. hotPage.value += 1
  89. })
  90. }
  91. // 设置热搜值
  92. function setHotSearchValue(val) {
  93. searchValue.value = val
  94. page.value = 1
  95. loadend.value = false
  96. bastList.value = []
  97. getProductList()
  98. }
  99. // 输入框赋值
  100. function setValue(event) {
  101. searchValue.value = event.detail.value
  102. }
  103. // 搜索按钮
  104. function searchBut() {
  105. focus.value = false
  106. if (searchValue.value.length > 0) {
  107. page.value = 1
  108. loadend.value = false
  109. bastList.value = []
  110. uni.showLoading({ title: '正在搜索中' })
  111. getProductList()
  112. uni.hideLoading()
  113. } else {
  114. // 这里假设 $util.Tips 已全局挂载
  115. uni.$u.toast('请输入要搜索的商品')
  116. }
  117. }
  118. // 生命周期
  119. onShow(() => {
  120. getRoutineHotSearch()
  121. getHostProduct()
  122. })
  123. onReachBottom(() => {
  124. if (bastList.value.length > 0) {
  125. getProductList()
  126. } else {
  127. getHostProduct()
  128. }
  129. })
  130. </script>
  131. <style lang="scss">
  132. page {
  133. margin-top: var(--status-bar-height);
  134. background-color: #fff !important;
  135. }
  136. .searchGood .search {
  137. padding-left: 30rpx;
  138. background-color: #fff !important;
  139. }
  140. .searchGood .search {
  141. padding-top: 20rpx;
  142. }
  143. .searchGood .search .input {
  144. width: 598rpx;
  145. background-color: #f7f7f7;
  146. border-radius: 33rpx;
  147. padding: 0 35rpx;
  148. box-sizing: border-box;
  149. height: 66rpx;
  150. }
  151. .searchGood .search .input input {
  152. width: 472rpx;
  153. font-size: 26rpx;
  154. }
  155. .searchGood .search .input .placeholder {
  156. color: #bbb;
  157. }
  158. .searchGood .search .input .iconfont {
  159. color: #000;
  160. font-size: 35rpx;
  161. }
  162. .searchGood .search .bnt {
  163. width: 120rpx;
  164. text-align: center;
  165. height: 66rpx;
  166. line-height: 66rpx;
  167. font-size: 30rpx;
  168. color: #282828;
  169. }
  170. .searchGood .title {
  171. font-size: 28rpx;
  172. color: #999;
  173. margin: 50rpx 30rpx 25rpx 30rpx;
  174. }
  175. .searchGood .list {
  176. padding-left: 10rpx;
  177. }
  178. .searchGood .list .item {
  179. font-size: 26rpx;
  180. color: #454545;
  181. padding: 0 21rpx;
  182. height: 60rpx;
  183. border-radius: 30rpx;
  184. line-height: 60rpx;
  185. border: 1rpx solid #aaa;
  186. margin: 0 0 20rpx 20rpx;
  187. }
  188. .searchGood .line {
  189. border-bottom: 1rpx solid #eee;
  190. margin: 20rpx 30rpx 0 30rpx;
  191. }
  192. </style>