search.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <template>
  2. <view class="content">
  3. <view class="vheight"></view>
  4. <view class="input-box flex">
  5. <view class=" input-content flex">
  6. <view class="iconfont iconsearch"></view>
  7. <view class="input"><input type="text" v-model="keyword" placeholder="请输入搜索内容" /></view>
  8. </view>
  9. <view class="input-button flex" @click="navTo"><text>搜索</text></view>
  10. </view>
  11. <swiper :current="tabCurrentIndex" class="swiper-box " duration="300">
  12. <swiper-item class="search-hot">
  13. <view class="title"><text>热门搜索</text></view>
  14. <view class="hot-list">
  15. <view @click="clickHotText(ls)" class="list-item" :key="ind" v-for="(ls, ind) in list">
  16. <text>{{ ls }}</text>
  17. </view>
  18. </view>
  19. </swiper-item>
  20. <swiper-item class="search-hot position-relative">
  21. <view class="navbar">
  22. <view class="nav-item" @click="defaultSearch()">默认</view>
  23. <view class="nav-item" :class="{ current: searchType === 1 }" @click="sortTab(1)">
  24. <text>销量优先</text>
  25. <view class="p-box">
  26. <text :class="{ active: searchType === 1 && numberOrder === 1 }" class="iconfont iconfold"></text>
  27. <text :class="{ active: searchType === 1 && numberOrder === 2 }" class="iconfont iconfold xia"></text>
  28. </view>
  29. </view>
  30. <view class="nav-item" :class="{ current: searchType === 2 }" @click="sortTab(2)">
  31. <text>价格</text>
  32. <view class="p-box">
  33. <text :class="{ active: searchType === 2 && priceOrder === 1 }" class="iconfont iconfold"></text>
  34. <text :class="{ active: searchType === 2 && priceOrder === 2 }" class="iconfont iconfold xia"></text>
  35. </view>
  36. </view>
  37. <view class="nav-item" :class="{ current: newOrder == 1 }" @click="newGoodsTab()">新品</view>
  38. </view>
  39. <scroll-view scroll-y class="cate-list" @scrolltolower='getProducts'>
  40. <view class="guess-section">
  41. <view v-for="(item, index) in goodsList" :key="index" class="guess-item" @click="navToDetailPage(item)">
  42. <view class="image-wrapper"><image :src="item.image" mode="aspectFill"></image></view>
  43. <text class="title clamp margin-c-20">{{ item.store_name }}</text>
  44. <view class="cmy-hr"></view>
  45. <view class="price margin-c-20 flex">
  46. <view>
  47. <text class="font-size-sm ">¥</text>
  48. {{ item.price }}
  49. </view>
  50. <view class="font-size-sm">
  51. <text class="font-color-gray">{{ item.sales }}人购买</text>
  52. </view>
  53. </view>
  54. </view>
  55. </view>
  56. <uni-load-more :status="loadingType"></uni-load-more>
  57. </scroll-view>
  58. </swiper-item>
  59. </swiper>
  60. </view>
  61. </template>
  62. <script>
  63. import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
  64. import { searchKeyword, getProducts } from '@/api/product.js';
  65. export default {
  66. components: {
  67. uniLoadMore
  68. },
  69. data() {
  70. return {
  71. arrlist: [], //热门关键词
  72. keyword: '', //关键字
  73. list: [], //搜索内容
  74. tabCurrentIndex: 0, //切换
  75. goodsList: [],
  76. limit: 6, //每次加载数据条数
  77. page: 1, //当前页数
  78. loadingType: 'more', //加载更多状态
  79. numberOrder: 1, //1 销量从低到高 2销量从高到低
  80. priceOrder: 1, //1 价格从低到高 2价格从高到低
  81. newOrder: 0, //0 不是新品 1是新品
  82. searchType: 0 //0为默认查询 1为销量 2 为价格
  83. };
  84. },
  85. // #ifndef MP
  86. //点击导航栏 buttons 时触发
  87. onNavigationBarButtonTap(e) {
  88. const index = e.index;
  89. if (index === 0) {
  90. this.navTo();
  91. }
  92. },
  93. // 点击键盘搜索事件
  94. onNavigationBarSearchInputConfirmed(e) {
  95. this.navTo();
  96. },
  97. // 搜索栏内容变化事件
  98. onNavigationBarSearchInputChanged(e) {
  99. this.keyword = e.text;
  100. },
  101. // #endif
  102. onLoad() {
  103. this.navTo();
  104. this.loadData();
  105. },
  106. //下拉刷新
  107. onPullDownRefresh() {
  108. this.page = 1
  109. this.getProducts('refresh');
  110. },
  111. methods: {
  112. // 加载商品
  113. async getProducts(type, loading) {
  114. let obj = this;
  115. // 判断是否为加载数据
  116. if (type !== 'refresh') {
  117. //没有更多数据直接跳出方法
  118. if (obj.loadingType === 'nomore') {
  119. return;
  120. } else {
  121. // 设置当前为数据载入中
  122. obj.loadingType = 'loading';
  123. }
  124. } else {
  125. //当重新加载数据时更新状态为可继续添加数据
  126. obj.loadingType = 'more';
  127. }
  128. let data = {
  129. page: obj.page,
  130. limit: obj.limit,
  131. news: obj.newOrder,
  132. keyword: this.keyword
  133. };
  134. // 判断是否为销售数量排序
  135. if (this.searchType === 1) {
  136. data.salesOrder = obj.numberOrder === 1 ? 'asc' : 'desc';
  137. }
  138. // 判断是否为金额排序
  139. if (this.searchType === 2) {
  140. data.priceOrder = obj.priceOrder === 1 ? 'asc' : 'desc';
  141. }
  142. getProducts(data).then(e => {
  143. if (type === 'refresh') {
  144. obj.goodsList = [];
  145. }
  146. obj.goodsList = obj.goodsList.concat(e.data);
  147. //判断是否还有下一页,有是more 没有是nomore
  148. if (obj.limit == e.data.length) {
  149. obj.page++;
  150. obj.loadingType = 'more';
  151. } else {
  152. obj.loadingType = 'nomore';
  153. }
  154. // 判断是否为刷新数据
  155. if (type === 'refresh') {
  156. // 判断是否为点击搜索按钮跳转加载
  157. if (loading == 1) {
  158. uni.hideLoading();
  159. } else {
  160. uni.stopPullDownRefresh();
  161. }
  162. }
  163. });
  164. },
  165. // 点击关键词触发事件
  166. clickHotText(e) {
  167. this.keyword = e;
  168. this.navTo();
  169. },
  170. // 加载搜索关键字
  171. async loadData() {
  172. searchKeyword({})
  173. .then(e => {
  174. this.list = e.data;
  175. })
  176. .catch(e => {
  177. console.log(e);
  178. });
  179. },
  180. // 点击触发搜索事件
  181. navTo() {
  182. this.tabCurrentIndex = 1;
  183. this.infoData()
  184. },
  185. // 默认搜索
  186. defaultSearch() {
  187. // 初始化查询
  188. this.numberOrder = '';
  189. this.priceOrder = '';
  190. this.newOrder = 0;
  191. this.searchType = 0;
  192. this.infoData();
  193. },
  194. // 是否为新品
  195. newGoodsTab() {
  196. this.newOrder = this.newOrder === 1 ? 0 : 1;
  197. this.infoData();
  198. },
  199. // 排序
  200. sortTab(nub) {
  201. this.searchType = nub;
  202. if (this.searchType === 1) {
  203. this.numberOrder = this.numberOrder === 1 ? 2 : 1;
  204. }
  205. if (this.searchType === 2) {
  206. this.priceOrder = this.priceOrder === 1 ? 2 : 1;
  207. }
  208. this.infoData();
  209. },
  210. // 查询切换后初始化
  211. infoData() {
  212. // 初始化页数
  213. this.page = 1;
  214. // 初始化数组
  215. uni.pageScrollTo({
  216. duration: 300,
  217. scrollTop: 0
  218. });
  219. // 加载数据
  220. this.getProducts('refresh', 1);
  221. uni.showLoading({
  222. title: '正在加载'
  223. });
  224. },
  225. navToDetailPage(item) {
  226. //测试数据没有写id,用title代替
  227. let id = item.id;
  228. uni.navigateTo({
  229. url: '/pages/product/product?id=' + id
  230. });
  231. }
  232. }
  233. };
  234. </script>
  235. <style lang="scss">
  236. page,
  237. .content {
  238. height: 100%;
  239. background-color: $page-color-base;
  240. }
  241. .vheight{
  242. height: var(--status-bar-height);
  243. background-color: #FFFFFF;
  244. }
  245. .input-box {
  246. padding: 25rpx;
  247. background-color: #ffffff;
  248. height: 44px;
  249. .iconsearch {
  250. font-size: 50rpx;
  251. }
  252. .input-content {
  253. border-radius: 99rpx;
  254. flex-grow: 1;
  255. padding: 10rpx 30rpx;
  256. background-color: rgba(231, 231, 231, 0.7);
  257. .input {
  258. flex-grow: 1;
  259. input {
  260. font-size: $font-lg;
  261. }
  262. }
  263. }
  264. .input-button {
  265. padding-left: 20rpx;
  266. font-size: $font-lg;
  267. height: 100%;
  268. }
  269. }
  270. .swiper-box {
  271. /* #ifndef MP */
  272. height: 100%;
  273. /* #endif */
  274. /* #ifdef MP */
  275. height: calc(100% - 44px);
  276. /* #endif */
  277. .search-hot {
  278. padding: 25rpx;
  279. .title {
  280. font-size: $font-lg;
  281. color: $font-color-light;
  282. }
  283. .hot-list {
  284. display: flex;
  285. flex-wrap: wrap;
  286. margin-top: 30rpx;
  287. .list-item {
  288. padding: 10rpx 20rpx;
  289. border: 1px solid $border-color-dark;
  290. color: $font-color-dark;
  291. font-size: $font-base;
  292. margin-right: 20rpx;
  293. margin-bottom: 20rpx;
  294. }
  295. }
  296. }
  297. }
  298. // 订单
  299. %icon {
  300. margin-right: 10rpx;
  301. display: inline-block;
  302. padding: 2rpx 10rpx;
  303. border: 1rpx solid $color-yellow;
  304. color: $color-yellow;
  305. line-height: 1;
  306. font-size: $font-base;
  307. border-radius: 10rpx;
  308. }
  309. .guess-section {
  310. display: flex;
  311. flex-wrap: wrap;
  312. .guess-item {
  313. overflow: hidden;
  314. display: flex;
  315. flex-direction: column;
  316. width: 48%;
  317. margin-bottom: 4%;
  318. border-radius: $border-radius-sm;
  319. background-color: white;
  320. box-shadow: $box-shadow;
  321. &:nth-child(2n + 1) {
  322. margin-right: 4%;
  323. }
  324. }
  325. .image-wrapper {
  326. width: 100%;
  327. height: 330rpx;
  328. border-radius: 3px;
  329. overflow: hidden;
  330. image {
  331. width: 100%;
  332. height: 100%;
  333. opacity: 1;
  334. }
  335. }
  336. .title {
  337. font-size: $font-base;
  338. color: $font-color-dark;
  339. font-weight: bold;
  340. line-height: 80rpx;
  341. }
  342. .price {
  343. font-size: $font-lg;
  344. color: $font-color-base;
  345. font-weight: bold;
  346. line-height: 1;
  347. line-height: 80rpx;
  348. }
  349. .icon {
  350. @extend %icon;
  351. }
  352. .detail {
  353. line-height: 1;
  354. }
  355. .tip {
  356. color: white;
  357. background-color: $color-yellow;
  358. line-height: 1.5;
  359. font-size: $font-sm;
  360. padding-left: 20rpx;
  361. }
  362. }
  363. .navbar {
  364. position: absolute;
  365. top: 0;
  366. left: 0;
  367. display: flex;
  368. width: 100%;
  369. height: 40px;
  370. background: #fff;
  371. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.06);
  372. z-index: 10;
  373. .nav-item {
  374. flex: 1;
  375. display: flex;
  376. justify-content: center;
  377. align-items: center;
  378. height: 100%;
  379. font-size: 30rpx;
  380. color: $font-color-dark;
  381. position: relative;
  382. &.current {
  383. color: $base-color;
  384. &:after {
  385. content: '';
  386. position: absolute;
  387. left: 50%;
  388. bottom: 0;
  389. transform: translateX(-50%);
  390. width: 120rpx;
  391. height: 0;
  392. border-bottom: 4rpx solid $base-color;
  393. }
  394. }
  395. }
  396. .p-box {
  397. display: flex;
  398. flex-direction: column;
  399. .iconfont {
  400. display: flex;
  401. align-items: center;
  402. justify-content: center;
  403. width: 30rpx;
  404. height: 14rpx;
  405. line-height: 1;
  406. margin-left: 4rpx;
  407. font-size: 26rpx;
  408. color: #888;
  409. &.active {
  410. color: $base-color;
  411. }
  412. }
  413. .xia {
  414. transform: scaleY(-1);
  415. }
  416. }
  417. .cate-item {
  418. display: flex;
  419. justify-content: center;
  420. align-items: center;
  421. height: 100%;
  422. width: 80rpx;
  423. position: relative;
  424. font-size: 44rpx;
  425. &:after {
  426. content: '';
  427. position: absolute;
  428. left: 0;
  429. top: 50%;
  430. transform: translateY(-50%);
  431. border-left: 1px solid #ddd;
  432. width: 0;
  433. height: 36rpx;
  434. }
  435. }
  436. }
  437. .cate-list {
  438. height: 100%;
  439. padding-top: 40px;
  440. }
  441. </style>