index.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <template>
  2. <view class="wrapper" :style="colorStyle">
  3. <scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" @scroll="scroll" @scrolltolower="scrollLower">
  4. <view class='searchGood'>
  5. <view class='search acea-row row-between-wrapper'>
  6. <view class='input acea-row row-between-wrapper'>
  7. <text class='iconfont icon-sousuo'></text>
  8. <input type='text' v-model='searchValue' @confirm="inputConfirm" focus
  9. :placeholder='$t(`搜索商品名称`)' placeholder-class='placeholder' @input="setValue"></input>
  10. </view>
  11. <view class='bnt' @tap='searchBut'>{{$t(`搜索`)}}</view>
  12. </view>
  13. <template v-if="history.length">
  14. <view class='title acea-row row-between-wrapper'>
  15. <view>{{$t(`搜索历史`)}}</view>
  16. <view class="iconfont icon-shanchu" @click="clear"></view>
  17. </view>
  18. <view class='list acea-row'>
  19. <block v-for="(item,index) in history" :key="index">
  20. <view class='item history-item line1' @tap='setHotSearchValue(item.keyword)'
  21. v-if="item.keyword">{{item.keyword}}</view>
  22. </block>
  23. </view>
  24. </template>
  25. <!-- <view class='title'>{{$t(`热门搜索`)}}</view>
  26. <view class='list acea-row'>
  27. <block v-for="(item,index) in hotSearchList" :key="index">
  28. <view class='item line1' @tap='setHotSearchValue(item.val)' v-if="item.val">{{item.val}}</view>
  29. </block>
  30. </view> -->
  31. <view class='line' v-if='bastList.length'></view>
  32. <goodList :bastList="bastList" v-if="bastList.length > 0"></goodList>
  33. <view class='loadingicon acea-row row-center-wrapper' v-if="bastList.length > 0">
  34. <text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
  35. </view>
  36. </view>
  37. <view class='noCommodity'>
  38. <view class='pictrue' v-if="bastList.length == 0">
  39. <image :src="imgHost + '/statics/images/noSearch.png'"></image>
  40. </view>
  41. <recommend :hostProduct='hostProduct' v-if="bastList.length == 0 && page > 1"></recommend>
  42. </view>
  43. </scroll-view>
  44. <home></home>
  45. <view v-if="scrollTopShow" class="back-top" @click="goTop">
  46. <text class="iconfont icon-xiangshang"></text>
  47. </view>
  48. </view>
  49. </template>
  50. <script>
  51. import {
  52. getSearchKeyword,
  53. getProductslist,
  54. getProductHot
  55. } from '@/api/store.js';
  56. import {
  57. searchList,
  58. clearSearch
  59. } from '@/api/api.js';
  60. import goodList from '@/components/goodList';
  61. import recommend from '@/components/recommend';
  62. import home from '@/components/home';
  63. import colors from "@/mixins/color";
  64. import {
  65. HTTP_REQUEST_URL
  66. } from '@/config/app';
  67. export default {
  68. components: {
  69. goodList,
  70. recommend,
  71. home
  72. },
  73. mixins: [colors],
  74. data() {
  75. return {
  76. imgHost: HTTP_REQUEST_URL,
  77. hostProduct: [],
  78. searchValue: '',
  79. focus: true,
  80. bastList: [],
  81. hotSearchList: [],
  82. first: 0,
  83. limit: 8,
  84. page: 1,
  85. loading: false,
  86. loadend: false,
  87. loadTitle: this.$t(`加载更多`),
  88. hotPage: 1,
  89. isScroll: true,
  90. history: [],
  91. scrollTop: 0,
  92. old: {
  93. scrollTop: 0
  94. },
  95. scrollTopShow: false
  96. };
  97. },
  98. onShow: function() {
  99. // this.getRoutineHotSearch();
  100. this.getHostProduct();
  101. this.searchList();
  102. try {
  103. this.hotSearchList = uni.getStorageSync('hotList');
  104. } catch (err) {}
  105. },
  106. // 滚动监听
  107. onPageScroll(e) {
  108. // 传入scrollTop值并触发所有easy-loadimage组件下的滚动监听事件
  109. uni.$emit('scroll');
  110. },
  111. methods: {
  112. scrollLower(){
  113. if (this.bastList.length > 0) {
  114. this.getProductList();
  115. } else {
  116. this.getHostProduct();
  117. }
  118. },
  119. scroll(e) {
  120. this.scrollTopShow = e.detail.scrollTop > 150
  121. this.old.scrollTop = e.detail.scrollTop
  122. },
  123. goTop(e) {
  124. // 解决view层不同步的问题
  125. this.scrollTop = this.old.scrollTop
  126. this.$nextTick(() => {
  127. this.scrollTop = 0
  128. });
  129. },
  130. searchList() {
  131. searchList({
  132. page: 1,
  133. limit: 10
  134. }).then(res => {
  135. this.history = res.data;
  136. });
  137. },
  138. clear() {
  139. let that = this;
  140. clearSearch().then(res => {
  141. uni.showToast({
  142. title: res.msg,
  143. success() {
  144. that.history = [];
  145. }
  146. });
  147. });
  148. },
  149. inputConfirm: function(event) {
  150. if (event.detail.value) {
  151. uni.hideKeyboard();
  152. this.setHotSearchValue(event.detail.value);
  153. }
  154. },
  155. getRoutineHotSearch: function() {
  156. let that = this;
  157. getSearchKeyword().then(res => {
  158. that.$set(that, 'hotSearchList', res.data);
  159. });
  160. },
  161. getProductList: function() {
  162. let that = this;
  163. if (that.loadend) return;
  164. if (that.loading) return;
  165. that.loading = true;
  166. that.loadTitle = '';
  167. getProductslist({
  168. keyword: that.searchValue.trim(),
  169. page: that.page,
  170. limit: that.limit
  171. }).then(res => {
  172. let list = res.data,
  173. loadend = list.length < that.limit;
  174. that.bastList = that.$util.SplitArray(list, that.bastList);
  175. that.$set(that, 'bastList', that.bastList);
  176. that.loading = false;
  177. that.loadend = loadend;
  178. that.loadTitle = loadend ? that.$t(`没有更多内容啦~`) : that.$t(`加载更多`);
  179. that.page = that.page + 1;
  180. }).catch(err => {
  181. that.loading = false,
  182. that.loadTitle = that.$t(`加载更多`)
  183. });
  184. },
  185. getHostProduct: function() {
  186. let that = this;
  187. if (!this.isScroll) return
  188. getProductHot(that.hotPage, that.limit).then(res => {
  189. that.isScroll = res.data.length >= that.limit
  190. that.hostProduct = that.hostProduct.concat(res.data)
  191. that.hotPage += 1;
  192. });
  193. },
  194. setHotSearchValue: function(event) {
  195. this.$set(this, 'searchValue', event);
  196. this.page = 1;
  197. this.loadend = false;
  198. this.$set(this, 'bastList', []);
  199. this.getProductList();
  200. },
  201. setValue: function(event) {
  202. this.$set(this, 'searchValue', event.detail.value);
  203. },
  204. searchBut: function() {
  205. let that = this;
  206. if (!that.searchValue.trim()) return this.$util.Tips({
  207. title: that.$t(`请输入要搜索的商品`)
  208. });
  209. that.focus = false;
  210. // if (that.searchValue.length > 0) {
  211. that.page = 1;
  212. that.loadend = false;
  213. that.$set(that, 'bastList', []);
  214. uni.showLoading({
  215. title: that.$t(`正在搜索中`)
  216. });
  217. that.getProductList();
  218. uni.hideLoading();
  219. // } else {
  220. // return this.$util.Tips({
  221. // title: '请输入要搜索的商品',
  222. // icon: 'none',
  223. // duration: 1000,
  224. // mask: true,
  225. // });
  226. // }
  227. }
  228. }
  229. }
  230. </script>
  231. <style lang="scss">
  232. page {
  233. background-color: #fff !important;
  234. }
  235. .scroll-Y {
  236. height: 100vh;
  237. }
  238. .wrapper {
  239. position: relative;
  240. max-height: 100vh;
  241. overflow: hidden;
  242. .back-top {
  243. position: absolute;
  244. right: 40rpx;
  245. bottom: 60rpx;
  246. width: 60rpx;
  247. height: 60rpx;
  248. border-radius: 50%;
  249. display: flex;
  250. justify-content: center;
  251. align-items: center;
  252. border: 1rpx solid #ccc;
  253. background-color: #fff;
  254. .icon-xiangshang {
  255. color: #ccc;
  256. font-weight: bold;
  257. }
  258. }
  259. }
  260. .noCommodity {
  261. border-top-width: 0;
  262. }
  263. .searchGood .search {
  264. padding-left: 30rpx;
  265. }
  266. .searchGood .search {
  267. padding-top: 20rpx;
  268. }
  269. .searchGood .search .input {
  270. width: 598rpx;
  271. background-color: #f7f7f7;
  272. border-radius: 33rpx;
  273. padding: 0 35rpx;
  274. box-sizing: border-box;
  275. height: 66rpx;
  276. }
  277. .searchGood .search .input input {
  278. width: 472rpx;
  279. font-size: 28rpx;
  280. }
  281. .searchGood .search .input .placeholder {
  282. color: #999;
  283. }
  284. .searchGood .search .input .iconfont {
  285. color: #555;
  286. font-size: 35rpx;
  287. }
  288. .searchGood .search .bnt {
  289. width: 120rpx;
  290. text-align: center;
  291. height: 66rpx;
  292. line-height: 66rpx;
  293. font-size: 30rpx;
  294. color: #282828;
  295. }
  296. .searchGood .title {
  297. font-size: 28rpx;
  298. color: #999;
  299. margin: 50rpx 30rpx 25rpx 30rpx;
  300. }
  301. .searchGood .title .iconfont {
  302. font-size: 28rpx;
  303. }
  304. .searchGood .list {
  305. padding-left: 10rpx;
  306. }
  307. .searchGood .list .item {
  308. font-size: 26rpx;
  309. color: #454545;
  310. padding: 0 21rpx;
  311. height: 60rpx;
  312. border-radius: 3rpx;
  313. line-height: 60rpx;
  314. border: 1rpx solid #aaa;
  315. margin: 0 0 20rpx 20rpx;
  316. }
  317. .searchGood .list .item.history-item {
  318. height: 50rpx;
  319. border: none;
  320. border-radius: 25rpx;
  321. background-color: #F7F7F7;
  322. line-height: 50rpx;
  323. }
  324. .searchGood .line {
  325. border-bottom: 1rpx solid #eee;
  326. margin: 20rpx 30rpx 0 30rpx;
  327. }
  328. </style>