index.vue 5.3 KB

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