category.vue 14 KB

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