search.vue 10 KB

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