category.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <!-- <template>
  2. <view class="content">
  3. <scroll-view scroll-y class="left-aside">
  4. <view v-for="item in flist" :key="item.id" class="f-item b-b" :class="{ active: item.id === currentId }" @click="tabtap(item)">{{ item.cate_name }}</view>
  5. </scroll-view>
  6. <scroll-view scroll-with-animation scroll-y class="right-aside" @scroll="asideScroll" :scroll-top="tabScrollTop">
  7. <view v-for="item in flist" :key="item.id" class="s-list" :id="'main-' + item.id">
  8. <text class="s-item">{{ item.cate_name }}</text>
  9. <view class="t-list ">
  10. <view @click="navToList(item.id, titem.id)" class="t-item" v-for="titem in item.children" :key="titem.id">
  11. <image :src="titem.pic"></image>
  12. <text>{{ titem.cate_name }}</text>
  13. </view>
  14. </view>
  15. </view>
  16. </scroll-view>
  17. </view>
  18. </template> -->
  19. <!-- <script>
  20. import { getCategoryList } from '@/api/product.js';
  21. export default {
  22. data() {
  23. return {
  24. sizeCalcState: false,
  25. tabScrollTop: 0,
  26. currentId: 9,
  27. flist: [],
  28. };
  29. },
  30. onLoad() {
  31. this.loadData();
  32. },
  33. // 监听导航栏输入框点击事件
  34. onNavigationBarSearchInputClicked(e) {
  35. uni.navigateTo({
  36. url: '/pages/product/search'
  37. });
  38. },
  39. methods: {
  40. // 载入数据
  41. async loadData() {
  42. let obj = this;
  43. getCategoryList({})
  44. .then(({ data }) => {
  45. obj.flist = data.map(function(s) {
  46. return s;
  47. });
  48. })
  49. .catch(err => {
  50. console.log(err);
  51. });
  52. },
  53. //一级分类点击
  54. tabtap(item) {
  55. console.log(item);
  56. // 判断有没有初始化页面高度对象数据
  57. if (!this.sizeCalcState) {
  58. this.calcSize();
  59. }
  60. // 获取当前点击的id
  61. this.currentId = item.id;
  62. console.log(item.top);
  63. this.tabScrollTop = item.top;
  64. console.log(this.tabScrollTop);
  65. },
  66. //右侧栏滚动
  67. asideScroll(e) {
  68. // 判断有没有初始化页面高度对象数据
  69. if (!this.sizeCalcState) {
  70. this.calcSize();
  71. }
  72. let scrollTop = e.detail.scrollTop;
  73. let box = 0; //列表包裹框高度初始化
  74. let bottom = 10; //距离页面底部多少像素左侧列表切换到最后一个一级分类
  75. // 查询当前页面对象
  76. let view = uni.createSelectorQuery().select('.content');
  77. view.fields(
  78. {
  79. id: true,
  80. dataset: true,
  81. rect: true,
  82. size: true,
  83. scrollOffset: true
  84. },
  85. function(e) {
  86. // 保存包裹框高度
  87. box = e.height;
  88. }
  89. ).exec();
  90. // 获取所有距离顶部大于滚轮距离页面高度的所有分类
  91. let tabs = this.flist.filter(item =>( item.top-10) <= scrollTop).reverse();
  92. if (tabs.length > 0) {
  93. // 判断是否已经到达滚轮底部
  94. if (box + scrollTop + bottom >= e.detail.scrollHeight) {
  95. this.currentId = this.flist[this.flist.length - 1].id;
  96. } else {
  97. this.currentId = tabs[0].id;
  98. }
  99. }
  100. },
  101. //计算右侧栏每个tab的高度等信息
  102. calcSize() {
  103. let h = 0;
  104. this.flist.forEach(item => {
  105. let view = uni.createSelectorQuery().select('#main-' + item.id);
  106. view.fields(
  107. {
  108. size: true
  109. },
  110. data => {
  111. item.top = h;
  112. h += data.height;
  113. item.bottom = h;
  114. }
  115. ).exec();
  116. });
  117. this.sizeCalcState = true;
  118. },
  119. navToList(sid, tid) {
  120. // 点击导航跳转到详细页面
  121. uni.navigateTo({
  122. url: '/pages/product/list?fid='+this.currentId+'&sid='+sid+'&tid='+tid
  123. });
  124. }
  125. }
  126. };
  127. </script>
  128. <style lang="scss">
  129. page,
  130. .content {
  131. height: 100%;
  132. background-color: #f8f8f8;
  133. }
  134. .content {
  135. display: flex;
  136. }
  137. .left-aside {
  138. flex-shrink: 0;
  139. width: 200rpx;
  140. height: 100%;
  141. background-color: #fff;
  142. }
  143. .f-item {
  144. display: flex;
  145. align-items: center;
  146. justify-content: center;
  147. width: 100%;
  148. height: 100rpx;
  149. font-size: 28rpx;
  150. color: $font-color-base;
  151. position: relative;
  152. &.active {
  153. color: $base-color;
  154. background: #f8f8f8;
  155. &:before {
  156. content: '';
  157. position: absolute;
  158. left: 0;
  159. top: 50%;
  160. transform: translateY(-50%);
  161. height: 36rpx;
  162. width: 8rpx;
  163. background-color: $base-color;
  164. border-radius: 0 4px 4px 0;
  165. opacity: 0.8;
  166. }
  167. }
  168. }
  169. .right-aside {
  170. flex: 1;
  171. overflow: hidden;
  172. padding-left: 20rpx;
  173. padding-right: 20rpx;
  174. }
  175. .s-item {
  176. display: flex;
  177. align-items: center;
  178. height: 70rpx;
  179. padding-top: 8rpx;
  180. font-size: 28rpx;
  181. color: $font-color-dark;
  182. }
  183. .t-list {
  184. display: flex;
  185. flex-wrap: wrap;
  186. border-radius: 15rpx;
  187. width: 100%;
  188. background: #fff;
  189. padding-top: 12rpx;
  190. &:after {
  191. content: '';
  192. flex: 99;
  193. height: 0;
  194. }
  195. }
  196. .t-item {
  197. flex-shrink: 0;
  198. display: flex;
  199. justify-content: center;
  200. align-items: center;
  201. flex-direction: column;
  202. width: 171rpx;
  203. font-size: 26rpx;
  204. color: #666;
  205. padding-bottom: 20rpx;
  206. image {
  207. width: 140rpx;
  208. height: 140rpx;
  209. }
  210. }
  211. </style>
  212. -->
  213. <template>
  214. <view class="content">
  215. <view class="dddd" style="height: 10rpx;"></view>
  216. <view class="flex cate-wrap" :style="{'height': height}">
  217. <scroll-view scroll-y="true" class="left-wrap" :style="{'height': height}">
  218. <view class="left-item" v-for="(leftitem,leftindex) in flist"
  219. :class="{'action-item': currentIndex == leftindex}" @click="leftClick(leftindex)">
  220. {{leftitem.cate_name}}
  221. </view>
  222. </scroll-view>
  223. <scroll-view scroll-y="true" class="right-wrap" :style="{'height': height}" @scrolltolower="getProducts()">
  224. <view class="right-item" v-for="gooditem in list"
  225. @click="navto('/pages/product/product?id=' + gooditem.id)">
  226. <image :src="gooditem.image" mode="" class="good-img"></image>
  227. <view class="good-info flex">
  228. <view class="good-name clamp2">
  229. {{gooditem.store_name}}
  230. </view>
  231. <view class="good-price">
  232. ¥{{gooditem.price}}
  233. </view>
  234. </view>
  235. </view>
  236. <uni-load-more :status="loadingType"></uni-load-more>
  237. </scroll-view>
  238. </view>
  239. </view>
  240. </template>
  241. <script>
  242. import {
  243. getCategoryList,
  244. getProducts
  245. } from '@/api/product.js';
  246. export default {
  247. data() {
  248. return {
  249. sizeCalcState: false,
  250. tabScrollTop: 0,
  251. currentId: 9,
  252. flist: [],
  253. height: '',
  254. currentIndex: 0,
  255. list: [],
  256. page: 1,
  257. limit: 20,
  258. loadingType: 'more',
  259. loaded: false,
  260. };
  261. },
  262. onLoad() {
  263. this.loadData();
  264. },
  265. // 监听导航栏输入框点击事件
  266. onNavigationBarSearchInputClicked(e) {
  267. uni.navigateTo({
  268. url: '/pages/product/search'
  269. });
  270. },
  271. onReady(res) {
  272. var _this = this;
  273. uni.getSystemInfo({
  274. success: resu => {
  275. const query = uni.createSelectorQuery();
  276. query.select('.cate-wrap').boundingClientRect();
  277. query.exec(function(res) {
  278. _this.height = resu.windowHeight - res[0].top + 'px';
  279. console.log('打印页面的剩余高度', _this.height);
  280. });
  281. },
  282. fail: res => {}
  283. });
  284. },
  285. methods: {
  286. leftClick(index) {
  287. this.currentIndex = index
  288. this.getProducts('reload')
  289. },
  290. getProducts(type) {
  291. let index = this.flist[this.currentIndex].id
  292. if (type == 'reload') {
  293. this.list = []
  294. this.page = 1
  295. this.loadingType = 'more'
  296. this.loaded = false
  297. }
  298. if (this.loadingType == 'loading' || this.loadingType == 'noMore') {
  299. return
  300. }
  301. this.loadingType = 'loading'
  302. getProducts({
  303. cid: index,
  304. page: this.page,
  305. limit: this.limit
  306. }).then(res => {
  307. this.list = this.list.concat(res.data)
  308. if (this.limit == res.data.length) {
  309. this.page++
  310. this.loadingType = 'more'
  311. } else {
  312. this.loadingType = 'noMore'
  313. }
  314. this.loaded = true
  315. })
  316. },
  317. // 载入数据
  318. async loadData() {
  319. let obj = this;
  320. getCategoryList({})
  321. .then(({
  322. data
  323. }) => {
  324. obj.flist = data.map(function(s) {
  325. return s;
  326. });
  327. obj.getProducts()
  328. })
  329. .catch(err => {
  330. console.log(err);
  331. });
  332. },
  333. //一级分类点击
  334. tabtap(item) {
  335. console.log(item);
  336. // 判断有没有初始化页面高度对象数据
  337. if (!this.sizeCalcState) {
  338. this.calcSize();
  339. }
  340. // 获取当前点击的id
  341. this.currentId = item.id;
  342. console.log(item.top);
  343. this.tabScrollTop = item.top;
  344. console.log(this.tabScrollTop);
  345. },
  346. //右侧栏滚动
  347. asideScroll(e) {
  348. // 判断有没有初始化页面高度对象数据
  349. if (!this.sizeCalcState) {
  350. this.calcSize();
  351. }
  352. let scrollTop = e.detail.scrollTop;
  353. let box = 0; //列表包裹框高度初始化
  354. let bottom = 10; //距离页面底部多少像素左侧列表切换到最后一个一级分类
  355. // 查询当前页面对象
  356. let view = uni.createSelectorQuery().select('.content');
  357. view.fields({
  358. id: true,
  359. dataset: true,
  360. rect: true,
  361. size: true,
  362. scrollOffset: true
  363. },
  364. function(e) {
  365. // 保存包裹框高度
  366. box = e.height;
  367. }
  368. ).exec();
  369. // 获取所有距离顶部大于滚轮距离页面高度的所有分类
  370. let tabs = this.flist.filter(item => (item.top - 10) <= scrollTop).reverse();
  371. if (tabs.length > 0) {
  372. // 判断是否已经到达滚轮底部
  373. if (box + scrollTop + bottom >= e.detail.scrollHeight) {
  374. this.currentId = this.flist[this.flist.length - 1].id;
  375. } else {
  376. this.currentId = tabs[0].id;
  377. }
  378. }
  379. },
  380. //计算右侧栏每个tab的高度等信息
  381. calcSize() {
  382. let h = 0;
  383. this.flist.forEach(item => {
  384. let view = uni.createSelectorQuery().select('#main-' + item.id);
  385. view.fields({
  386. size: true
  387. },
  388. data => {
  389. item.top = h;
  390. h += data.height;
  391. item.bottom = h;
  392. }
  393. ).exec();
  394. });
  395. this.sizeCalcState = true;
  396. },
  397. navToList(sid, tid) {
  398. // 点击导航跳转到详细页面
  399. uni.navigateTo({
  400. url: '/pages/product/list?fid=' + this.currentId + '&sid=' + sid + '&tid=' + tid
  401. });
  402. },
  403. navto(url) {
  404. uni.navigateTo({
  405. url
  406. })
  407. }
  408. }
  409. };
  410. </script>
  411. <style lang="scss">
  412. .cate-wrap {
  413. width: 750rpx;
  414. .left-wrap {
  415. width: 180rpx;
  416. flex-shrink: 0;
  417. background-color: #f2f2f2;
  418. .left-item {
  419. width: 180rpx;
  420. height: 100rpx;
  421. line-height: 100rpx;
  422. font-size: 28rpx;
  423. font-weight: 500;
  424. color: #666666;
  425. text-align: center;
  426. }
  427. .action-item {
  428. background-color: #fff;
  429. font-weight: bold;
  430. position: relative;
  431. &::before {
  432. content: '';
  433. position: absolute;
  434. top: 0;
  435. left: 0;
  436. width: 3rpx;
  437. height: 100rpx;
  438. background: #FF7144;
  439. }
  440. }
  441. }
  442. .right-wrap {
  443. flex-wrap: 1;
  444. background-color: #fff;
  445. .right-item {
  446. width: 520rpx;
  447. height: 240rpx;
  448. padding: 30rpx 0;
  449. margin: 0 auto;
  450. border-bottom: 1px solid #eee;
  451. display: flex;
  452. justify-content: flex-start;
  453. align-items: center;
  454. .good-img {
  455. flex-shrink: 0;
  456. width: 180rpx;
  457. height: 180rpx;
  458. border-radius: 20rpx;
  459. }
  460. .good-info {
  461. flex-direction: column;
  462. justify-content: space-between;
  463. align-items: flex-start;
  464. height: 100%;
  465. padding-left: 25rpx;
  466. .good-name {
  467. font-size: 30rpx;
  468. font-weight: bold;
  469. color: #333333;
  470. }
  471. .good-price {
  472. font-size: 34rpx;
  473. font-weight: bold;
  474. color: #FF4C4C;
  475. }
  476. }
  477. }
  478. }
  479. }
  480. </style>