category.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. <template>
  2. <view class="container">
  3. <view class="tui-searchbox">
  4. <view class="tui-search-input" @tap="search">
  5. <icon type="search" :size="13" color="#999"></icon>
  6. <text class="tui-search-text">搜索</text>
  7. </view>
  8. </view>
  9. <scroll-view scroll-y scroll-with-animation class="tab-view" :scroll-into-view="scrollViewId"
  10. :style="{ height: height + 'px', top: top + 'px' }">
  11. <view :id="`id_${index}`" v-for="(item, index) in Category" :key="item.id" class="tab-bar-item"
  12. :class="[currentTab == index ? 'active' : '']" :data-current="index"
  13. @tap.stop="swichNav(item.id,index)">
  14. <text>{{ item.title }}</text>
  15. </view>
  16. </scroll-view>
  17. <scroll-view @scrolltolower="tolower" scroll-y class="right-box" :scroll-into-view="floorNum"
  18. :style="{ height: height + 'px', top: top + 'px' }">
  19. <view class="page-view" id="scroll-panel">
  20. <swiper v-if="banners.length>0" indicator-dots autoplay circular :interval="5000" :duration="150"
  21. class="swiper">
  22. <swiper-item v-for="(item, index) in banners" :key="index">
  23. <image :data-url="item.url" @tap="navigationTo" :src="item.pic" mode="widthFix"
  24. class="slide-image" />
  25. </swiper-item>
  26. </swiper>
  27. <view class="class-box">
  28. <block v-if="categorystyle==1">
  29. <block v-for="(goods, index) in goodsList" :key="index">
  30. <view class="goods-item" @tap="toDetailsTap" :data-id="goods.id">
  31. <image class="goods-pic" mode="widthFix" lazy-load="true" :src="goods.pic"></image>
  32. <view class="goods-info">
  33. <text>{{goods.name}}</text>
  34. <text class="price" style="color:red;"><text class="f-24">¥</text>{{goods.price}}
  35. <block v-if="goods.ptype==2">
  36. <block v-if="goods.is_times == 0">
  37. <text v-if="goods.time_amount > 0"
  38. class="f-24">/{{goods.time_amount}}{{goods.quantity_unit}}</text>
  39. <text v-if="goods.time_amount == 0"
  40. class="f-24">/{{goods.quantity_unit}}</text>
  41. </block>
  42. <text v-if="goods.is_times == 1" class="f-24">
  43. /<text v-if="goods.is_times">{{goods.timesmum}}</text>
  44. 次</text>
  45. </block>
  46. </text>
  47. </view>
  48. </view>
  49. </block>
  50. </block>
  51. <block v-if="categorystyle==2">
  52. <view class="class-item" :id="'b'+item.id" v-if="Category.length>0"
  53. v-for="(item, index) in Category" :key="index">
  54. <view class="class-name">{{ item.title }}</view>
  55. <view class="g-container">
  56. <view class="g-box" v-for="(son, i1) in item.son" :key="i1" @tap.stop="gogoodsList"
  57. :data-cid="son.id" :data-key="son.title">
  58. <image :src="son.icon" class="c-image" />
  59. <view class="g-title">{{ son.title }}</view>
  60. </view>
  61. </view>
  62. </view>
  63. </block>
  64. <block v-if="categorystyle==3">
  65. <view class="class-item main-item">
  66. <view class="g-container">
  67. <view class="g-box" v-for="(goods, i1) in goodsList" :key="i2" @tap.stop="toDetailsTap"
  68. @tap="toDetailsTap" :data-id="goods.id">
  69. <image :src="goods.image" class="g-image" mode="widthFix" />
  70. <view class="g-title">{{ goods.name }}</view>
  71. <view class="price" style="color:red;">
  72. <text class="f-24">¥</text>{{goods.price}}
  73. <block v-if="goods.ptype==2">
  74. <text v-if="goods.time_amount > 0"
  75. class="f-24">/{{goods.time_amount}}{{goods.quantity_unit}}</text>
  76. <text v-else class="f-24">/<text
  77. v-if="goods.is_times">{{goods.timesmum}}</text>次</text>
  78. </block>
  79. </view>
  80. </view>
  81. </view>
  82. </view>
  83. <view class="fill-last" :style="{ 'height':fillHeight + 'px' }"></view>
  84. </block>
  85. </view>
  86. </view>
  87. <view :style='"height:"+(height-300)+"rpx;"' v-if="number<15"></view>
  88. </scroll-view>
  89. <tui-tabbar :current="current">
  90. </tui-tabbar>
  91. </view>
  92. </template>
  93. <script>
  94. export default {
  95. data() {
  96. return {
  97. current: '',
  98. banners: [],
  99. Category: [],
  100. ptype: '',
  101. categoryId: "",
  102. categorystyle: "1",
  103. goodsList: [],
  104. page: 1,
  105. getSon: [],
  106. height: 0, //scroll-view高度
  107. top: 0,
  108. currentTab: 0, //预设当前项的值
  109. floorNum: '',
  110. number: "",
  111. scrollViewId: "id_0",
  112. scrollHeight: 400,
  113. scrollTopSize: 0,
  114. fillHeight: 0, // 填充高度,用于最后一项低于滚动区域时使用
  115. topArr: [], // 左侧列表元素
  116. };
  117. },
  118. onLoad: function(e) {
  119. let _this = this
  120. uni.setStorageSync('NewMessage', '');
  121. // #ifdef MP-WEIXIN
  122. this.current = "/" + this.__route__;
  123. // #endif
  124. //#ifdef H5
  125. this.current = this.$route.path;
  126. //#endif
  127. if (e.ptype) {
  128. _this.ptype = e.ptype;
  129. }
  130. setTimeout(() => {
  131. uni.getSystemInfo({
  132. success: res => {
  133. let header = 92;
  134. let top = 0;
  135. console.log(res.windowHeight);
  136. this.height = res.windowHeight - uni.upx2px(header) - 90;
  137. this.top = top + uni.upx2px(header);
  138. }
  139. });
  140. }, 50);
  141. this.getbanners()
  142. this.getCategoryall();
  143. if (_this.ptype) {
  144. this.current = this.current + '?ptype=' + _this.ptype;
  145. }
  146. },
  147. onShow() {
  148. let _this = this;
  149. _this.$request.post('config', {
  150. mo: 'pagestyle',
  151. }).then(res => {
  152. if (res.errno == 0) {
  153. if (res.data.categorystyle) {
  154. _this.categorystyle = res.data.categorystyle
  155. }
  156. }
  157. });
  158. },
  159. methods: {
  160. // 点击标题切换当前页时改变样式
  161. swichNav: function(Categoryid, e) {
  162. let cur = e;
  163. if (this.currentTab == cur) {
  164. return false;
  165. } else {
  166. this.currentTab = cur;
  167. this.checkCor();
  168. }
  169. if (this.categorystyle == 1 || this.categorystyle == 3) {
  170. this.categoryId = Categoryid;
  171. this.page = 1;
  172. this.getGoodsList();
  173. } else {
  174. this.floorNum = 'b' + Categoryid;
  175. }
  176. },
  177. /* 获取元素顶部信息 */
  178. getElementTop() {
  179. new Promise((resolve, reject) => {
  180. let view = uni.createSelectorQuery().selectAll('.main-item');
  181. view.boundingClientRect(data => {
  182. resolve(data);
  183. }).exec();
  184. }).then((res) => {
  185. console.log('res', res);
  186. let topArr = res.map((item) => {
  187. return item.top - this.scrollTopSize; /* 减去滚动容器距离顶部的距离 */
  188. });
  189. this.topArr = topArr;
  190. /* 获取最后一项的高度,设置填充高度。判断和填充时做了 +-20 的操作,是为了滚动时更好的定位 */
  191. let last = res[res.length - 1].height;
  192. if (last - 20 < this.scrollHeight) {
  193. this.fillHeight = this.scrollHeight - last + 20;
  194. }
  195. });
  196. },
  197. /* 主区域滚动监听 */
  198. mainScroll(e) {
  199. // 节流方法
  200. clearTimeout(this.mainThrottle);
  201. this.mainThrottle = setTimeout(() => {
  202. scrollFn();
  203. }, 10);
  204. let scrollFn = () => {
  205. let top = e.detail.scrollTop;
  206. let index = 0;
  207. /* 查找当前滚动距离 */
  208. this.topArr.forEach((item, id) => {
  209. /*
  210. console.log('------------**');
  211. console.log(top);
  212. console.log('------------');
  213. console.log(item);
  214. console.log('------------+++');
  215. console.log(id);
  216. */
  217. if ((top + 2) >= item) {
  218. index = id;
  219. this.currentTab = (index < 0 ? 0 : index);
  220. //console.log(index);
  221. }
  222. })
  223. }
  224. },
  225. //判断当前滚动超过一屏时,设置tab标题滚动条。
  226. checkCor: function() {
  227. if (this.currentTab > 6) {
  228. this.scrollViewId = `id_${this.currentTab - 2}`;
  229. } else {
  230. this.scrollViewId = `id_0`;
  231. }
  232. },
  233. gogoodsList(e) {
  234. let key = e.currentTarget.dataset.key;
  235. let cid = e.currentTarget.dataset.cid;
  236. uni.navigateTo({
  237. url: '/pages/goodsList/goodsList?cid=' + cid + '&searchKey=' + key
  238. });
  239. },
  240. toDetailsTap: function(e) {
  241. uni.navigateTo({
  242. url: "/pages/goodsDetail/goodsDetail?id=" + e.currentTarget.dataset.id
  243. });
  244. },
  245. navigationTo: function(e) {
  246. this.sam.navigateTo(e.currentTarget.dataset.url)
  247. },
  248. search: function() {
  249. uni.navigateTo({
  250. url: '/pages/common/search/search'
  251. });
  252. },
  253. getbanners: function() {
  254. let _this = this;
  255. this.$request.get('ad.indexlist').then(res => {
  256. if (res.errno == 0) {
  257. this.banners = res.data;
  258. }
  259. })
  260. },
  261. getCategoryall: function() {
  262. let _this = this;
  263. this.$request.get('Category.all', {
  264. ptype: _this.ptype,
  265. showLoading: true
  266. }).then(res => {
  267. if (res.errno == 0) {
  268. _this.Category = res.data;
  269. _this.categoryId = _this.Category[0].id;
  270. _this.getGoodsList();
  271. setTimeout(function() {
  272. _this.infoScroll();
  273. }, 500)
  274. console.info("services---request---getsonList--右侧二级三级分类成功")
  275. }
  276. })
  277. },
  278. getGoodsList: function() {
  279. let that = this;
  280. if (that.page == 1) {
  281. that.goodsList = [];
  282. }
  283. this.$request.post('goods', {
  284. categoryId: that.categoryId,
  285. page: that.page,
  286. pageSize: 1000
  287. }).then(res => {
  288. that.goodsList = that.goodsList.concat(res.data.data);
  289. that.page = that.page + 1
  290. //console.log(goodsList);
  291. uni.hideNavigationBarLoading();
  292. }).catch(e => {
  293. uni.hideNavigationBarLoading();
  294. });
  295. },
  296. infoScroll: function() {
  297. let _this = this;
  298. let len = _this.Category.length;
  299. if (_this.Category[len - 1].son) {
  300. this.number = _this.Category[len - 1].son.length;
  301. }
  302. },
  303. /* 初始化滚动区域 */
  304. initScrollView() {
  305. return new Promise((resolve, reject) => {
  306. let view = uni.createSelectorQuery().select('#scroll-panel');
  307. view.boundingClientRect(res => {
  308. this.scrollTopSize = res.top;
  309. this.scrollHeight = res.height;
  310. this.$nextTick(() => {
  311. resolve();
  312. });
  313. }).exec();
  314. });
  315. },
  316. tolower: function() {
  317. if (this.categorystyle == 1) {
  318. this.getGoodsList();
  319. }
  320. }
  321. },
  322. /**
  323. * 页面相关事件处理函数--监听用户下拉动作
  324. */
  325. onPullDownRefresh: function() {
  326. setTimeout(() => {
  327. uni.stopPullDownRefresh()
  328. }, 200);
  329. },
  330. };
  331. </script>
  332. <style>
  333. page {
  334. background: #fcfcfc;
  335. }
  336. /* 左侧导航布局 start*/
  337. /* 隐藏scroll-view滚动条*/
  338. ::-webkit-scrollbar {
  339. width: 0;
  340. height: 0;
  341. color: transparent;
  342. }
  343. .tui-searchbox {
  344. width: 100%;
  345. height: 92rpx;
  346. padding: 0 30rpx;
  347. box-sizing: border-box;
  348. background: #fff;
  349. display: flex;
  350. align-items: center;
  351. justify-content: center;
  352. position: fixed;
  353. left: 0;
  354. top: 0;
  355. z-index: 100;
  356. }
  357. .tui-searchbox::after {
  358. content: '';
  359. position: absolute;
  360. border-bottom: 1rpx solid #d2d2d2;
  361. -webkit-transform: scaleY(0.5);
  362. transform: scaleY(0.5);
  363. bottom: 0;
  364. right: 0;
  365. left: 0;
  366. }
  367. .tui-search-input {
  368. width: 100%;
  369. height: 60rpx;
  370. background: #f1f1f1;
  371. border-radius: 30rpx;
  372. font-size: 26rpx;
  373. color: #999;
  374. display: flex;
  375. align-items: center;
  376. justify-content: center;
  377. }
  378. .tui-search-text {
  379. padding-left: 16rpx;
  380. }
  381. .tab-view {
  382. /* height: 100%; */
  383. background: #f6f6f6;
  384. width: 200rpx;
  385. position: fixed;
  386. left: 0;
  387. z-index: 10;
  388. padding-bottom: 100rpx;
  389. }
  390. .tab-bar-item {
  391. width: 200rpx;
  392. height: 110rpx;
  393. background: #f6f6f6;
  394. box-sizing: border-box;
  395. display: flex;
  396. align-items: center;
  397. justify-content: center;
  398. font-size: 26rpx;
  399. color: #444;
  400. font-weight: 400;
  401. }
  402. .active {
  403. position: relative;
  404. color: #000;
  405. font-size: 30rpx;
  406. font-weight: 600;
  407. background: #fcfcfc;
  408. }
  409. .active::before {
  410. content: '';
  411. position: absolute;
  412. border-left: 8rpx solid #e41f19;
  413. height: 30rpx;
  414. left: 0;
  415. }
  416. /* 左侧导航布局 end*/
  417. .right-box {
  418. width: 100%;
  419. position: fixed;
  420. padding-left: 220rpx;
  421. box-sizing: border-box;
  422. left: 0;
  423. }
  424. .page-view {
  425. width: 100%;
  426. overflow: hidden;
  427. padding-top: 20rpx;
  428. padding-right: 20rpx;
  429. box-sizing: border-box;
  430. padding-bottom: env(safe-area-inset-bottom);
  431. }
  432. .swiper {
  433. width: 100%;
  434. height: 150rpx;
  435. border-radius: 12rpx;
  436. overflow: hidden;
  437. transform: translateZ(0);
  438. }
  439. /* #ifdef MP-WEIXIN */
  440. .swiper .wx-swiper-dot {
  441. width: 8rpx;
  442. height: 8rpx;
  443. display: inline-flex;
  444. background: none;
  445. justify-content: space-between;
  446. }
  447. .swiper .wx-swiper-dot::before {
  448. content: '';
  449. flex-grow: 1;
  450. background: rgba(255, 255, 255, 0.8);
  451. border-radius: 16rpx;
  452. overflow: hidden;
  453. }
  454. .swiper .wx-swiper-dot-active::before {
  455. background: #fff;
  456. }
  457. .swiper .wx-swiper-dot.wx-swiper-dot-active {
  458. width: 16rpx;
  459. }
  460. /* #endif */
  461. /* #ifndef MP-WEIXIN */
  462. >>>.swiper .uni-swiper-dot {
  463. width: 8rpx;
  464. height: 8rpx;
  465. display: inline-flex;
  466. background: none;
  467. justify-content: space-between;
  468. }
  469. >>>.swiper .uni-swiper-dot::before {
  470. content: '';
  471. flex-grow: 1;
  472. background: rgba(255, 255, 255, 0.8);
  473. border-radius: 16rpx;
  474. overflow: hidden;
  475. }
  476. >>>.swiper .uni-swiper-dot-active::before {
  477. background: #fff;
  478. }
  479. >>>.swiper .uni-swiper-dot.uni-swiper-dot-active {
  480. width: 16rpx;
  481. }
  482. /* #endif */
  483. .slide-image {
  484. width: 100%;
  485. height: 100%;
  486. }
  487. .class-box {
  488. padding-top: 10rpx;
  489. }
  490. .class-item {
  491. background: #fff;
  492. width: 100%;
  493. box-sizing: border-box;
  494. padding: 20rpx;
  495. margin-bottom: 20rpx;
  496. border-radius: 12rpx;
  497. }
  498. .class-name {
  499. font-size: 22rpx;
  500. }
  501. .g-container {
  502. /* padding-top: 20rpx; */
  503. display: flex;
  504. display: -webkit-flex;
  505. justify-content: flex-start;
  506. flex-direction: row;
  507. flex-wrap: wrap;
  508. }
  509. .g-box {
  510. width: 33.3333%;
  511. text-align: center;
  512. padding-top: 40rpx;
  513. }
  514. .price {
  515. font-weight: normal;
  516. font-size: 36rpx;
  517. }
  518. .c-image {
  519. width: 120rpx;
  520. height: 120rpx;
  521. border-radius: 12rpx;
  522. }
  523. .g-image {
  524. width: 100rpx;
  525. height: 100rpx;
  526. border-radius: 50rpx;
  527. }
  528. .g-title {
  529. font-size: 22rpx;
  530. padding-right: 8rpx;
  531. padding-left: 8rpx;
  532. word-break: break-all;
  533. overflow: hidden;
  534. text-overflow: ellipsis;
  535. display: -webkit-box;
  536. -webkit-box-orient: vertical;
  537. -webkit-line-clamp: 2;
  538. }
  539. .goods-item {
  540. width: 96%;
  541. height: 150rpx;
  542. display: flex;
  543. padding-left: 5px;
  544. padding-right: 5px;
  545. padding-top: 12px;
  546. padding-bottom: 12px;
  547. flex-direction: row;
  548. margin-left: 20rpx;
  549. align-items: center;
  550. justify-content: flex-start;
  551. border-bottom: 1rpx dotted #f2f2f2;
  552. }
  553. .goods-pic {
  554. width: 80px;
  555. height: 80px;
  556. flex-shrink: 0;
  557. }
  558. .goods-info {
  559. display: flex;
  560. font-weight: bold;
  561. flex-direction: column;
  562. padding: 6px 5px 0px 5px;
  563. height: 70px;
  564. font-size: 28rpx;
  565. justify-content: space-between;
  566. }
  567. .fill-last {
  568. height: 0;
  569. width: 100%;
  570. background: none;
  571. }
  572. </style>