index.vue 6.6 KB

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