index.vue 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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-sousuo2'></text>
  7. <input type='text' v-model='searchValue' @confirm="inputConfirm" confirm-type="search" focus placeholder='点击搜索商品'
  8. placeholder-class='placeholder' @input="setValue"></input>
  9. </view>
  10. <view class='bnt' @tap='searchBut'>搜索</view>
  11. </view>
  12. <template v-if="history.length">
  13. <view class='title acea-row row-between-wrapper'>
  14. <view>搜索历史</view>
  15. <view class="iconfont icon-shanchu" @click="clear"></view>
  16. </view>
  17. <view class='list acea-row'>
  18. <block v-for="(item,index) in history" :key="index">
  19. <view class='item history-item' @tap='setHotSearchValue(item.keyword)' v-if="item.keyword">{{item.keyword}}</view>
  20. </block>
  21. </view>
  22. </template>
  23. <view class='title'>热门搜索</view>
  24. <view class='list acea-row'>
  25. <block v-for="(item,index) in hotSearchList" :key="index">
  26. <view class='item' @tap='setHotSearchValue(item.val)' v-if="item.val">{{item.val}}</view>
  27. </block>
  28. </view>
  29. <view class='line'></view>
  30. <goodList :bastList="bastList" v-if="bastList.length > 0"></goodList>
  31. <view class='loadingicon acea-row row-center-wrapper' v-if="bastList.length > 0">
  32. <text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
  33. </view>
  34. </view>
  35. <view class='noCommodity'>
  36. <view class='pictrue' v-if="bastList.length == 0">
  37. <image src='../../static/images/noSearch.png'></image>
  38. </view>
  39. <recommend :hostProduct='hostProduct' v-if="bastList.length == 0 && page > 1"></recommend>
  40. </view>
  41. </view>
  42. </template>
  43. <script>
  44. import {
  45. getSearchKeyword,
  46. getProductslist,
  47. getProductHot
  48. } from '@/api/store.js';
  49. import {
  50. searchList,
  51. clearSearch
  52. } from '@/api/api.js';
  53. import goodList from '@/components/goodList';
  54. import recommend from '@/components/recommend';
  55. export default {
  56. components: {
  57. goodList,
  58. recommend
  59. },
  60. data() {
  61. return {
  62. hostProduct: [],
  63. searchValue: '',
  64. focus: true,
  65. bastList: [],
  66. hotSearchList: [],
  67. first: 0,
  68. limit: 8,
  69. page: 1,
  70. loading: false,
  71. loadend: false,
  72. loadTitle: '加载更多',
  73. hotPage: 1,
  74. isScroll: true,
  75. history: []
  76. };
  77. },
  78. onShow: function() {
  79. // this.getRoutineHotSearch();
  80. this.getHostProduct();
  81. this.searchList();
  82. try {
  83. this.hotSearchList = uni.getStorageSync('hotList');
  84. } catch (err) {}
  85. },
  86. onReachBottom: function() {
  87. if (this.bastList.length > 0) {
  88. this.getProductList();
  89. } else {
  90. this.getHostProduct();
  91. }
  92. },
  93. methods: {
  94. searchList() {
  95. searchList({
  96. page: 1,
  97. limit: 10
  98. }).then(res => {
  99. this.history = res.data;
  100. });
  101. },
  102. clear() {
  103. let that = this;
  104. clearSearch().then(res => {
  105. uni.showToast({
  106. title: res.msg,
  107. success() {
  108. that.history = [];
  109. }
  110. });
  111. });
  112. },
  113. inputConfirm: function(event) {
  114. if (event.detail.value) {
  115. uni.hideKeyboard();
  116. this.setHotSearchValue(event.detail.value);
  117. }
  118. },
  119. getRoutineHotSearch: function() {
  120. let that = this;
  121. getSearchKeyword().then(res => {
  122. that.$set(that, 'hotSearchList', res.data);
  123. });
  124. },
  125. getProductList: function() {
  126. let that = this;
  127. if (that.loadend) return;
  128. if (that.loading) return;
  129. that.loading = true;
  130. that.loadTitle = '';
  131. getProductslist({
  132. keyword: that.searchValue,
  133. page: that.page,
  134. limit: that.limit
  135. }).then(res => {
  136. let list = res.data,
  137. loadend = list.length < that.limit;
  138. that.bastList = that.$util.SplitArray(list, that.bastList);
  139. that.$set(that, 'bastList', that.bastList);
  140. that.loading = false;
  141. that.loadend = loadend;
  142. that.loadTitle = loadend ? "😕人家是有底线的~~" : "加载更多";
  143. that.page = that.page + 1;
  144. }).catch(err => {
  145. that.loading = false,
  146. that.loadTitle = '加载更多'
  147. });
  148. },
  149. getHostProduct: function() {
  150. let that = this;
  151. if (!this.isScroll) return
  152. getProductHot(that.hotPage, that.limit).then(res => {
  153. that.isScroll = res.data.length >= that.limit
  154. that.hostProduct = that.hostProduct.concat(res.data)
  155. that.hotPage += 1;
  156. });
  157. },
  158. setHotSearchValue: function(event) {
  159. this.$set(this, 'searchValue', event);
  160. this.page = 1;
  161. this.loadend = false;
  162. this.$set(this, 'bastList', []);
  163. this.getProductList();
  164. },
  165. setValue: function(event) {
  166. this.$set(this, 'searchValue', event.detail.value);
  167. },
  168. searchBut: function() {
  169. let that = this;
  170. that.focus = false;
  171. if (that.searchValue.length > 0) {
  172. that.page = 1;
  173. that.loadend = false;
  174. that.$set(that, 'bastList', []);
  175. uni.showLoading({
  176. title: '正在搜索中'
  177. });
  178. that.getProductList();
  179. uni.hideLoading();
  180. } else {
  181. return this.$util.Tips({
  182. title: '请输入要搜索的商品',
  183. icon: 'none',
  184. duration: 1000,
  185. mask: true,
  186. });
  187. }
  188. }
  189. }
  190. }
  191. </script>
  192. <style lang="scss">
  193. page {
  194. background-color: #fff !important;
  195. }
  196. .searchGood .search {
  197. padding-left: 30rpx;
  198. }
  199. .searchGood .search {
  200. margin-top: 20rpx;
  201. }
  202. .searchGood .search .input {
  203. width: 598rpx;
  204. background-color: #f7f7f7;
  205. border-radius: 33rpx;
  206. padding: 0 35rpx;
  207. box-sizing: border-box;
  208. height: 66rpx;
  209. }
  210. .searchGood .search .input input {
  211. width: 472rpx;
  212. font-size: 28rpx;
  213. }
  214. .searchGood .search .input .placeholder {
  215. color: #bbb;
  216. }
  217. .searchGood .search .input .iconfont {
  218. color: #000;
  219. font-size: 35rpx;
  220. }
  221. .searchGood .search .bnt {
  222. width: 120rpx;
  223. text-align: center;
  224. height: 66rpx;
  225. line-height: 66rpx;
  226. font-size: 30rpx;
  227. color: #282828;
  228. }
  229. .searchGood .title {
  230. font-size: 28rpx;
  231. color: #999;
  232. margin: 50rpx 30rpx 25rpx 30rpx;
  233. }
  234. .searchGood .title .iconfont {
  235. font-size: 28rpx;
  236. }
  237. .searchGood .list {
  238. padding-left: 10rpx;
  239. }
  240. .searchGood .list .item {
  241. font-size: 26rpx;
  242. color: #454545;
  243. padding: 0 21rpx;
  244. height: 60rpx;
  245. border-radius: 3rpx;
  246. line-height: 60rpx;
  247. border: 1rpx solid #aaa;
  248. margin: 0 0 20rpx 20rpx;
  249. }
  250. .searchGood .list .item.history-item {
  251. height: 50rpx;
  252. border: none;
  253. border-radius: 25rpx;
  254. background-color: #F7F7F7;
  255. line-height: 50rpx;
  256. }
  257. .searchGood .line {
  258. border-bottom: 1rpx solid #eee;
  259. margin: 20rpx 30rpx 0 30rpx;
  260. }
  261. </style>