search.vue 11 KB

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