index.vue 7.0 KB

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