search.vue 10 KB

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