category.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 slist" :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 { getList } from '@/api/category.js';
  21. import { mapState, mapMutations } from 'vuex';
  22. export default {
  23. data() {
  24. return {
  25. sizeCalcState: false,
  26. tabScrollTop: 0,
  27. currentId: 1,
  28. flist: [],
  29. slist: []
  30. // tlist: []
  31. };
  32. },
  33. computed: {
  34. ...mapState(['loginInterceptor', 'storeInfo', 'canChange', 'baseURL']),
  35. ...mapState('user', ['hasLogin', 'userInfo'])
  36. },
  37. onShow() {
  38. console.log(this.storeInfo, '123456');
  39. },
  40. onLoad() {
  41. this.loadData();
  42. },
  43. // #ifndef MP
  44. // 监听导航栏输入框点击事件
  45. onNavigationBarSearchInputClicked(e) {
  46. uni.navigateTo({
  47. url: '/pages/product/search'
  48. });
  49. },
  50. // #endif
  51. methods: {
  52. // 载入数据
  53. async loadData() {
  54. let obj = this;
  55. getList({})
  56. .then(({ data }) => {
  57. console.log(data, 'data++++++++++');
  58. this.flist = data.map(function(s) {
  59. return s;
  60. });
  61. console.log(this.flist, '1111111');
  62. data.forEach(function(e) {
  63. obj.slist.push(
  64. ...e.children.map(function(s) {
  65. return s;
  66. })
  67. );
  68. });
  69. console.log(obj.slist, '22222222');
  70. // data.forEach(function(e) {
  71. // e.children.forEach(function(es) {
  72. // obj.tlist.push(...es.children.map(function (s) {
  73. // return s
  74. // }))
  75. // });
  76. // });
  77. })
  78. .catch(err => {
  79. console.log(err);
  80. });
  81. },
  82. //一级分类点击
  83. tabtap(item) {
  84. // 判断有没有初始化页面高度对象数据
  85. if (!this.sizeCalcState) {
  86. this.calcSize();
  87. }
  88. // 获取当前点击的id
  89. this.currentId = item.id;
  90. let index = this.slist.findIndex(sitem => sitem.pid === item.id);
  91. this.tabScrollTop = this.slist[index].top;
  92. },
  93. //右侧栏滚动
  94. asideScroll(e) {
  95. // 判断有没有初始化页面高度对象数据
  96. if (!this.sizeCalcState) {
  97. this.calcSize();
  98. }
  99. let scrollTop = e.detail.scrollTop;
  100. let box = 0; //列表包裹框高度初始化
  101. let bottom = 10; //距离页面底部多少像素左侧列表切换到最后一个一级分类
  102. // 查询当前页面对象
  103. let view = uni.createSelectorQuery().select('.content');
  104. view.fields(
  105. {
  106. id: true,
  107. dataset: true,
  108. rect: true,
  109. size: true,
  110. scrollOffset: true
  111. },
  112. function(e) {
  113. // 保存包裹框高度
  114. box = e.height;
  115. }
  116. ).exec();
  117. // 获取所有距离顶部大于滚轮距离页面高度的所有分类
  118. let tabs = this.slist.filter(item => item.top <= scrollTop).reverse();
  119. if (tabs.length > 0) {
  120. // 判断是否已经到达滚轮底部
  121. if (box + scrollTop + bottom >= e.detail.scrollHeight) {
  122. this.currentId = this.slist[this.slist.length - 1].pid;
  123. } else {
  124. this.currentId = tabs[0].pid;
  125. }
  126. }
  127. },
  128. //计算右侧栏每个tab的高度等信息
  129. calcSize() {
  130. let h = 0;
  131. this.slist.forEach(item => {
  132. let view = uni.createSelectorQuery().select('#main-' + item.id);
  133. view.fields(
  134. {
  135. size: true
  136. },
  137. data => {
  138. item.top = h;
  139. h += data.height;
  140. item.bottom = h;
  141. }
  142. ).exec();
  143. });
  144. this.sizeCalcState = true;
  145. },
  146. navToList(sid, tid) {
  147. // 点击导航跳转到详细页面
  148. uni.navigateTo({
  149. url: `/pages/product/listSearch?fid=${this.currentId}&sid=${sid}&tid=${tid}`
  150. });
  151. }
  152. }
  153. };
  154. </script>
  155. <style lang="scss">
  156. page,
  157. .content {
  158. height: 100%;
  159. background-color: #f8f8f8;
  160. }
  161. .content {
  162. display: flex;
  163. }
  164. .left-aside {
  165. flex-shrink: 0;
  166. width: 200rpx;
  167. height: 100%;
  168. background-color: #fff;
  169. }
  170. .f-item {
  171. display: flex;
  172. align-items: center;
  173. justify-content: center;
  174. width: 100%;
  175. height: 100rpx;
  176. font-size: 28rpx;
  177. color: $font-color-base;
  178. position: relative;
  179. &.active {
  180. color: $base-color;
  181. background: #f8f8f8;
  182. &:before {
  183. content: '';
  184. position: absolute;
  185. left: 0;
  186. top: 50%;
  187. transform: translateY(-50%);
  188. height: 36rpx;
  189. width: 8rpx;
  190. background-color: $base-color;
  191. border-radius: 0 4px 4px 0;
  192. opacity: 0.8;
  193. }
  194. }
  195. }
  196. .right-aside {
  197. flex: 1;
  198. overflow: hidden;
  199. padding-left: 20rpx;
  200. padding-right: 20rpx;
  201. }
  202. .s-item {
  203. display: flex;
  204. align-items: center;
  205. height: 70rpx;
  206. padding-top: 8rpx;
  207. font-size: 28rpx;
  208. color: $font-color-dark;
  209. }
  210. .t-list {
  211. display: flex;
  212. flex-wrap: wrap;
  213. border-radius: 15rpx;
  214. width: 100%;
  215. background: #fff;
  216. padding-top: 12rpx;
  217. &:after {
  218. content: '';
  219. flex: 99;
  220. height: 0;
  221. }
  222. }
  223. .t-item {
  224. flex-shrink: 0;
  225. display: flex;
  226. justify-content: center;
  227. align-items: center;
  228. flex-direction: column;
  229. width: 171rpx;
  230. font-size: 26rpx;
  231. color: #666;
  232. padding-bottom: 20rpx;
  233. image {
  234. width: 140rpx;
  235. height: 140rpx;
  236. }
  237. }
  238. </style>