search.vue 13 KB

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