index.vue 5.1 KB

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