list.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. <template>
  2. <view class="content">
  3. <view class="navbar" :style="{ position: headerPosition, top: headerTop }">
  4. <!-- 兼容小程序搜索 -->
  5. <!-- #ifdef MP -->
  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. <view class="flex navbar-box">
  15. <view class="nav-item" :class="{ current: filterIndex === 0 }" @click="tabClick(0)">综合排序</view>
  16. <view class="nav-item" :class="{ current: filterIndex === 1 }" @click="tabClick(1)">
  17. <text>销量优先</text>
  18. <view class="p-box">
  19. <text :class="{ active: numberOrder === 1 && filterIndex === 1 }" class="iconfont iconfold"></text>
  20. <text :class="{ active: numberOrder === 2 && filterIndex === 1 }" class="iconfont iconfold xia"></text>
  21. </view>
  22. </view>
  23. <view class="nav-item" :class="{ current: filterIndex === 2 }" @click="tabClick(2)">
  24. <text>价格</text>
  25. <view class="p-box">
  26. <text :class="{ active: priceOrder === 1 && filterIndex === 2 }" class="iconfont iconfold"></text>
  27. <text :class="{ active: priceOrder === 2 && filterIndex === 2 }" class="iconfont iconfold xia"></text>
  28. </view>
  29. </view>
  30. <text class="cate-item iconfont iconapps" @click="toggleCateMask('show')"></text>
  31. </view>
  32. </view>
  33. <view class="goods-list">
  34. <view v-for="(item, index) in goodsList" :key="index" class="goods-item" @click="navToDetailPage(item)">
  35. <view class="image-wrapper"><image :src="item.image" mode="aspectFill"></image></view>
  36. <text class="title">{{ item.store_name }}</text>
  37. <view class="price-box">
  38. <text class="price">{{ item.price }}</text>
  39. <text>已售 {{ item.sales+(item.ficti||0) }}</text>
  40. </view>
  41. </view>
  42. </view>
  43. <uni-load-more :status="loadingType"></uni-load-more>
  44. <view class="cate-mask" :class="cateMaskState === 0 ? 'none' : cateMaskState === 1 ? 'show' : ''" @click="toggleCateMask">
  45. <view class="cate-content" @click.stop.prevent="stopPrevent" @touchmove.stop.prevent="stopPrevent">
  46. <scroll-view scroll-y class="cate-list">
  47. <view><view class="cate-item b-b" @click="changeCate(tItem)">全部</view></view>
  48. <view v-for="item in cateList" :key="item.id">
  49. <view class="cate-item b-b two">{{ item.cate_name }}</view>
  50. <view v-for="tItem in item.children" :key="tItem.id" class="cate-item b-b" :class="{ active: tItem.id == cateId }" @click="changeCate(tItem)">
  51. {{ tItem.cate_name }}
  52. </view>
  53. </view>
  54. </scroll-view>
  55. </view>
  56. </view>
  57. </view>
  58. </template>
  59. <script>
  60. import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
  61. import { getProducts,productDetail } from '@/api/product.js';
  62. import { getList } from '@/api/category.js';
  63. import { mapState } from 'vuex';
  64. export default {
  65. computed: {
  66. ...mapState(['shopId'])
  67. },
  68. components: {
  69. uniLoadMore
  70. },
  71. data() {
  72. return {
  73. cateMaskState: 0, //分类面板展开状态
  74. headerPosition: 'fixed',
  75. headerTop: '0px',
  76. loadingType: 'more', //加载更多状态
  77. filterIndex: 0, //查询类型
  78. numberOrder: 0, //1 销量从低到高 2销量从高到低
  79. limit: 5, //每次加载数据条数
  80. page: 1, //当前页数
  81. cateId: 0, //已选三级分类id
  82. priceOrder: 0, //1 价格从低到高 2价格从高到低
  83. cateList: [], //分类列表
  84. goodsList: [], //商品列表
  85. keyword: '' ,//关键字
  86. specList:'',
  87. specSelected:'',
  88. productValue:[]
  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(options) {
  109. // #ifdef H5
  110. //this.headerTop = document.getElementsByTagName('uni-page-head')[0].offsetHeight + 'px';
  111. // #endif
  112. this.cateId = options.tid || '';
  113. this.loadCateList();
  114. this.loadData();
  115. },
  116. onPageScroll(e) {
  117. //兼容iOS端下拉时顶部漂移
  118. if (e.scrollTop >= 0) {
  119. this.headerPosition = 'fixed';
  120. } else {
  121. this.headerPosition = 'absolute';
  122. }
  123. },
  124. //下拉刷新
  125. onPullDownRefresh() {
  126. this.loadData('refresh');
  127. },
  128. //加载更多
  129. onReachBottom() {
  130. this.loadData();
  131. },
  132. methods: {
  133. // 点击触发搜索事件
  134. navTo() {
  135. this.tabCurrentIndex = 1;
  136. this.infoData();
  137. },
  138. // 查询切换后初始化
  139. infoData() {
  140. // 初始化页数
  141. this.page = 1;
  142. // 初始化数组
  143. uni.pageScrollTo({
  144. duration: 300,
  145. scrollTop: 0
  146. });
  147. // 加载数据
  148. this.loadData('refresh', 1);
  149. uni.showLoading({
  150. title: '正在加载'
  151. });
  152. },
  153. //加载分类
  154. async loadCateList(fid, sid) {
  155. let obj = this;
  156. getList({}).then(function(e) {
  157. obj.cateList = e.data;
  158. });
  159. },
  160. //加载商品 ,带下拉刷新和上滑加载
  161. async loadData(type = 'add', loading) {
  162. let obj = this;
  163. let data = {
  164. page: obj.page,
  165. limit: obj.limit,
  166. sid: obj.cateId, //分类id
  167. mer_id: obj.shopId || '',
  168. keyword: obj.keyword || '',
  169. isPink: 1
  170. };
  171. //没有更多直接返回
  172. if (type === 'add') {
  173. if (obj.loadingType === 'nomore') {
  174. return;
  175. }
  176. obj.loadingType = 'loading';
  177. } else {
  178. obj.loadingType = 'more';
  179. }
  180. if (this.filterIndex == 1) {
  181. data.salesOrder = obj.numberOrder == 1 ? 'asc' : 'desc';
  182. }
  183. if (this.filterIndex == 2) {
  184. data.priceOrder = obj.priceOrder == 1 ? 'asc' : 'desc';
  185. }
  186. getProducts(data).then(function(e) {
  187. if (type === 'refresh') {
  188. console.log('jinru');
  189. // 清空数组
  190. obj.goodsList = [];
  191. }
  192. let goodsList = obj.goodsList.concat(e.data);
  193. let a = e.data.length;
  194. for(let i = 0 ;i<a;i++){
  195. let goodsid;
  196. goodsid = e.data[i].id;
  197. productDetail({}, goodsid)
  198. .then(({data}) => {
  199. obj.specList = data.productAttr;//保存产品属性
  200. obj.specSelected = []; //初始化默认选择对象
  201. obj.productValue = data.productValue;
  202. for (let i = 0; i < obj.specList.length; i++) {
  203. // 设置默认数据
  204. let attrValue = obj.specList[i].attr_value[0];
  205. attrValue.check = true;
  206. obj.specSelected.push(attrValue.attr);
  207. }
  208. let str = obj.specSelected.join(',');
  209. let actionPrice = obj.productValue[str].price;
  210. if(obj.page == 1)
  211. {
  212. goodsList[i].price = actionPrice;
  213. }else{
  214. let a = (obj.page - 1) * 5 + i;
  215. goodsList[a].price = actionPrice;
  216. }
  217. });
  218. }
  219. setTimeout(function(){
  220. obj.goodsList = goodsList;
  221. //判断是否还有下一页,有是more 没有是nomore
  222. if (obj.limit == e.data.length) {
  223. obj.page++;
  224. obj.loadingType = 'more';
  225. } else {
  226. obj.loadingType = 'nomore';
  227. }
  228. if (type === 'refresh') {
  229. if (loading == 1) {
  230. uni.hideLoading();
  231. } else {
  232. uni.stopPullDownRefresh();
  233. }
  234. }
  235. }, 1000);
  236. });
  237. },
  238. //筛选点击
  239. tabClick(index) {
  240. // 防止重复点击综合排序
  241. if (this.filterIndex === 0 && this.filterIndex === index) {
  242. return;
  243. }
  244. this.filterIndex = index;
  245. // 判断是否为销量优先
  246. if (index === 1) {
  247. this.numberOrder = this.numberOrder === 1 ? 2 : 1;
  248. }
  249. // 判断是否为价格优先
  250. if (index === 2) {
  251. this.priceOrder = this.priceOrder === 1 ? 2 : 1;
  252. }
  253. // 初始化页数
  254. this.page = 1;
  255. // 初始化数组
  256. uni.pageScrollTo({
  257. duration: 300,
  258. scrollTop: 0
  259. });
  260. this.loadData('refresh', 1);
  261. uni.showLoading({
  262. title: '正在加载'
  263. });
  264. },
  265. //显示分类面板
  266. toggleCateMask(type) {
  267. let timer = type === 'show' ? 10 : 300;
  268. let state = type === 'show' ? 1 : 0;
  269. this.cateMaskState = 2;
  270. setTimeout(() => {
  271. this.cateMaskState = state;
  272. }, timer);
  273. },
  274. //分类点击
  275. changeCate(item) {
  276. if (item) {
  277. this.cateId = item.id;
  278. } else {
  279. this.cateId = '';
  280. }
  281. this.toggleCateMask();
  282. this.infoData();
  283. },
  284. //详情
  285. // navToDetailPage(item) {
  286. // let id = item.id;
  287. // uni.navigateTo({
  288. // url: `/pages/product/product?type=2&id=${id}`
  289. // });
  290. // },
  291. //商品详情
  292. navToDetailPage(item) {
  293. let id = item.id;
  294. uni.navigateTo({
  295. url: '/pages/product/product?id=' + id
  296. });
  297. },
  298. stopPrevent() {}
  299. }
  300. };
  301. </script>
  302. <style lang="scss">
  303. page,
  304. .content {
  305. background: $page-color-base;
  306. }
  307. .content {
  308. padding-top: 40px;
  309. /* #ifdef MP */
  310. padding-top: 84px;
  311. /* #endif */
  312. }
  313. /* #ifdef MP || APP-PLUS*/
  314. .input-box {
  315. padding: 25rpx;
  316. /* #ifdef APP-PLUS */
  317. margin-top: var(--status-bar-height);
  318. /* #endif */
  319. background-color: #ffffff;
  320. height: 44px;
  321. .iconsearch {
  322. font-size: 50rpx;
  323. }
  324. .input-content {
  325. border-radius: 99rpx;
  326. flex-grow: 1;
  327. padding: 5rpx 30rpx;
  328. background-color: rgba(231, 231, 231, 0.7);
  329. .input {
  330. flex-grow: 1;
  331. input {
  332. font-size: $font-base;
  333. }
  334. }
  335. }
  336. .input-button {
  337. padding-left: 20rpx;
  338. font-size: $font-base;
  339. height: 100%;
  340. }
  341. }
  342. /* #endif */
  343. .navbar {
  344. position: fixed;
  345. left: 0;
  346. top: var(--window-top);
  347. width: 100%;
  348. //height: 40px;
  349. background: #fff;
  350. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.06);
  351. z-index: 10;
  352. .navbar-box {
  353. height: 40px;
  354. .nav-item {
  355. flex: 1;
  356. display: flex;
  357. justify-content: center;
  358. align-items: center;
  359. height: 100%;
  360. font-size: 30rpx;
  361. color: $font-color-dark;
  362. position: relative;
  363. &.current {
  364. color: $base-color;
  365. // &:after {
  366. // content: '';
  367. // position: absolute;
  368. // left: 50%;
  369. // bottom: 0;
  370. // transform: translateX(-50%);
  371. // width: 120rpx;
  372. // height: 0;
  373. // border-bottom: 4rpx solid $base-color;
  374. // }
  375. }
  376. }
  377. .p-box {
  378. display: flex;
  379. flex-direction: column;
  380. .iconfont {
  381. display: flex;
  382. align-items: center;
  383. justify-content: center;
  384. width: 30rpx;
  385. height: 14rpx;
  386. line-height: 1;
  387. margin-left: 4rpx;
  388. font-size: 26rpx;
  389. color: #888;
  390. &.active {
  391. color: $base-color;
  392. }
  393. }
  394. .xia {
  395. transform: scaleY(-1);
  396. }
  397. }
  398. .cate-item {
  399. display: flex;
  400. justify-content: center;
  401. align-items: center;
  402. height: 100%;
  403. width: 80rpx;
  404. position: relative;
  405. font-size: 44rpx;
  406. &:after {
  407. content: '';
  408. position: absolute;
  409. left: 0;
  410. top: 50%;
  411. transform: translateY(-50%);
  412. border-left: 1px solid #ddd;
  413. width: 0;
  414. height: 36rpx;
  415. }
  416. }
  417. }
  418. }
  419. /* 分类 */
  420. .cate-mask {
  421. position: fixed;
  422. left: 0;
  423. top: var(--window-top);
  424. bottom: 0;
  425. width: 100%;
  426. background: rgba(0, 0, 0, 0);
  427. z-index: 95;
  428. transition: 0.3s;
  429. .cate-content {
  430. width: 630rpx;
  431. height: 100%;
  432. background: #fff;
  433. float: right;
  434. transform: translateX(100%);
  435. transition: 0.3s;
  436. }
  437. &.none {
  438. display: none;
  439. }
  440. &.show {
  441. background: rgba(0, 0, 0, 0.4);
  442. .cate-content {
  443. transform: translateX(0);
  444. }
  445. }
  446. }
  447. .cate-list {
  448. display: flex;
  449. flex-direction: column;
  450. height: 100%;
  451. .cate-item {
  452. display: flex;
  453. align-items: center;
  454. height: 90rpx;
  455. padding-left: 30rpx;
  456. font-size: 28rpx;
  457. color: #555;
  458. position: relative;
  459. }
  460. .two {
  461. height: 64rpx;
  462. color: #303133;
  463. font-size: 30rpx;
  464. background: #f8f8f8;
  465. }
  466. .active {
  467. color: $base-color;
  468. }
  469. }
  470. /* 商品列表 */
  471. .goods-list {
  472. display: flex;
  473. flex-wrap: wrap;
  474. padding: 0 30rpx;
  475. // background: #fff;
  476. // padding-top: 30rpx;
  477. .goods-item {
  478. display: flex;
  479. flex-direction: column;
  480. width: 48%;
  481. padding-bottom: 30rpx;
  482. border-radius: 10rpx;
  483. background: #FFFFFF;
  484. margin-top: 20rpx;
  485. &:nth-child(2n + 1) {
  486. margin-right: 4%;
  487. }
  488. }
  489. .image-wrapper {
  490. width: 100%;
  491. height: 330rpx;
  492. border-radius: 10rpx 10rpx 0 0;
  493. overflow: hidden;
  494. image {
  495. width: 100%;
  496. height: 100%;
  497. opacity: 1;
  498. }
  499. }
  500. .title {
  501. font-size: $font-base;
  502. color: $font-color-dark;
  503. line-height: 1.5;
  504. // height: 85rpx;
  505. padding-top: 20rpx;
  506. padding: 20rpx;
  507. overflow: hidden;
  508. text-overflow: ellipsis;
  509. white-space: nowrap;
  510. }
  511. .price-box {
  512. display: flex;
  513. align-items: center;
  514. justify-content: space-between;
  515. padding: 0 20rpx;
  516. font-size: 24rpx;
  517. color: $font-color-light;
  518. }
  519. .price {
  520. font-size: $font-lg;
  521. color: $uni-color-primary;
  522. line-height: 1;
  523. &:before {
  524. content: '¥';
  525. font-size: 26rpx;
  526. }
  527. }
  528. }
  529. </style>