search.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. });
  222. },
  223. navToDetailPage(item) {
  224. //测试数据没有写id,用title代替
  225. let id = item.id;
  226. uni.navigateTo({
  227. url: '/pages/product/product?id=' + id
  228. });
  229. }
  230. }
  231. };
  232. </script>
  233. <style lang="scss">
  234. page,
  235. .content {
  236. height: 100%;
  237. background-color: $page-color-base;
  238. }
  239. /* #ifdef MP || APP-PLUS */
  240. .vheight{
  241. height: var(--status-bar-height);
  242. background-color: #FFFFFF;
  243. }
  244. .input-box {
  245. padding: 25rpx;
  246. background-color: #ffffff;
  247. height: 44px;
  248. .iconsearch {
  249. font-size: 50rpx;
  250. }
  251. .input-content {
  252. border-radius: 99rpx;
  253. flex-grow: 1;
  254. padding: 10rpx 30rpx;
  255. background-color: rgba(231, 231, 231, 0.7);
  256. .input {
  257. flex-grow: 1;
  258. input {
  259. font-size: $font-lg;
  260. }
  261. }
  262. }
  263. .input-button {
  264. padding-left: 20rpx;
  265. font-size: $font-lg;
  266. height: 100%;
  267. }
  268. }
  269. /* #endif */
  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>