search.vue 10 KB

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