index.vue 6.8 KB

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