search.vue 9.9 KB

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