cate.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <template>
  2. <view class="u-wrap">
  3. <u-navbar :border-bottom="false" :is-back="false">
  4. <view style="display: flex;justify-content: center;align-items: center;">
  5. <view class="u-p-l-20" @click="notice">
  6. <u-icon color="#666" name="chat" size="50"></u-icon>
  7. </view>
  8. <!-- #ifndef MP-->
  9. <u-search v-model="keyword" :clearabled="true" :disabled="true" :show-action="false"
  10. class="u-p-l-20" input-align="left"
  11. placeholder="输入搜索关键字" style="width: 660rpx" @click="search"></u-search>
  12. <!-- #endif -->
  13. <!-- #ifndef H5 -->
  14. <u-search v-model="keyword" :clearabled="true" :disabled="true" :show-action="false"
  15. class="u-p-l-20" input-align="left"
  16. placeholder="输入搜索关键字" style="width: 420rpx" @click="search"></u-search>
  17. <!-- #endif -->
  18. </view>
  19. </u-navbar>
  20. <view class="u-menu-wrap">
  21. <scroll-view :scroll-top="scrollTop" class="u-tab-view menu-scroll-view" scroll-with-animation scroll-y>
  22. <view v-for="(item,index) in tabbar" :key="index" :class="[current==index ? 'u-tab-item-active' : '']"
  23. :data-current="index"
  24. class="u-tab-item" @tap.stop="swichMenu(index)">
  25. <text class="u-line-1">{{ item.classifyName }}</text>
  26. </view>
  27. </scroll-view>
  28. <block v-for="(item,index) in tabbar" :key="index">
  29. <scroll-view v-if="current==index" class="right-box" scroll-y>
  30. <view class="page-view">
  31. <view class="class-item">
  32. <view class="item-title">
  33. <text>{{ item.classifyName }}</text>
  34. </view>
  35. <view class="item-container">
  36. <view v-for="(item1, index1) in item.children" :key="index1" class="thumb-box" @click="detail(item1)">
  37. <image :src="item1.classifyUrl" class="item-menu-image" mode=""></image>
  38. <view class="item-menu-name margin-top-sm">{{ item1.classifyName }}</view>
  39. </view>
  40. </view>
  41. </view>
  42. </view>
  43. </scroll-view>
  44. </block>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. import config from "@/common/config.js" // 全局配置文件
  50. export default {
  51. data() {
  52. return {
  53. keyword: '',
  54. tabbar: [],
  55. scrollTop: 0, //tab标题的滚动条位置
  56. current: 0, // 预设当前项的值
  57. menuHeight: 0, // 左边菜单的高度
  58. menuItemHeight: 0, // 左边菜单item的高度
  59. }
  60. },
  61. onLoad() {
  62. this.findClassifyList()
  63. },
  64. methods: {
  65. findClassifyList() {
  66. let url = "/api/mallApi/findClassifyList";
  67. this.$u.get(url, {
  68. orderByColumn: 'create_time',
  69. isAsc: 'desc'
  70. }).then(result => {
  71. // 分类数据
  72. const classifyData = result.rows;
  73. for (let i = 0; i < classifyData.length; i++) {
  74. // 先转成字符串再转成对象,避免数组对象引用导致数据混乱
  75. let item = classifyData[i];
  76. item.classifyName = item.classifyName;
  77. item.classifyUrl = config.staticUrl + item.classifyUrl;
  78. for (let j = 0; j < item.children.length; j++) {
  79. // 先转成字符串再转成对象,避免数组对象引用导致数据混乱
  80. let item2 = item.children[j];
  81. item2.classifyName = item2.classifyName;
  82. item2.classifyUrl = config.staticUrl + item2.classifyUrl;
  83. }
  84. }
  85. this.tabbar = classifyData
  86. });
  87. },
  88. search() {
  89. this.$u.route({
  90. url: 'pages/search/search',
  91. })
  92. },
  93. notice() {
  94. this.$u.route({
  95. url: 'pages/notice/notice'
  96. })
  97. },
  98. detail(item) {
  99. // this.$u.route({
  100. // url: 'pages/detail/detail',
  101. // params: {
  102. // icon:item.icon,
  103. // name: item.name
  104. // }
  105. // })
  106. this.$u.route({
  107. url: 'pages/search/searchList',
  108. })
  109. },
  110. // 点击左边的栏目切换
  111. async swichMenu(index) {
  112. if (index == this.current) return;
  113. this.current = index;
  114. // 如果为0,意味着尚未初始化
  115. if (this.menuHeight == 0 || this.menuItemHeight == 0) {
  116. await this.getElRect('menu-scroll-view', 'menuHeight');
  117. await this.getElRect('u-tab-item', 'menuItemHeight');
  118. }
  119. // 将菜单菜单活动item垂直居中
  120. this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
  121. },
  122. // 获取一个目标元素的高度
  123. getElRect(elClass, dataVal) {
  124. new Promise((resolve, reject) => {
  125. const query = uni.createSelectorQuery().in(this);
  126. query.select('.' + elClass).fields({size: true}, res => {
  127. // 如果节点尚未生成,res值为null,循环调用执行
  128. if (!res) {
  129. setTimeout(() => {
  130. this.getElRect(elClass);
  131. }, 10);
  132. return;
  133. }
  134. this[dataVal] = res.height;
  135. }).exec();
  136. })
  137. }
  138. }
  139. }
  140. </script>
  141. <style>
  142. page {
  143. background-color: #ffffff;
  144. }
  145. </style>
  146. <style lang="scss" scoped>
  147. .u-wrap {
  148. height: calc(100vh);
  149. /* #ifdef H5 */
  150. height: calc(100vh - var(--window-top));
  151. /* #endif */
  152. display: flex;
  153. flex-direction: column;
  154. }
  155. .u-search-box {
  156. margin-top: 50 rpx;
  157. /* #ifdef H5 */
  158. margin-top: 0 rpx;
  159. /* #endif */
  160. padding: 18 rpx 30 rpx;
  161. }
  162. .u-menu-wrap {
  163. flex: 1;
  164. display: flex;
  165. overflow: hidden;
  166. }
  167. .u-tab-view {
  168. width: 200 rpx;
  169. height: 100%;
  170. }
  171. .u-tab-item {
  172. height: 110 rpx;
  173. background: #f6f6f6;
  174. box-sizing: border-box;
  175. display: flex;
  176. align-items: center;
  177. justify-content: center;
  178. font-size: 26 rpx;
  179. color: #444;
  180. font-weight: 400;
  181. line-height: 1;
  182. }
  183. .u-tab-item-active {
  184. position: relative;
  185. color: $base-color;
  186. font-size: 30 rpx;
  187. font-weight: 500;
  188. background: #fff;
  189. }
  190. .u-tab-item-active::before {
  191. content: "";
  192. position: absolute;
  193. border-left: 4px solid $base-color;
  194. height: 32 rpx;
  195. left: 0;
  196. top: 39 rpx;
  197. }
  198. .u-tab-view {
  199. height: 100%;
  200. }
  201. .right-box {
  202. background-color: rgb(250, 250, 250);
  203. }
  204. .page-view {
  205. padding: 16 rpx;
  206. }
  207. .class-item {
  208. margin-bottom: 30 rpx;
  209. background-color: #fff;
  210. padding: 16 rpx;
  211. border-radius: 8 rpx;
  212. }
  213. .item-title {
  214. font-size: 30 rpx;
  215. color: $u-main-color;
  216. font-weight: bold;
  217. margin: 10 rpx 0;
  218. }
  219. .item-menu-name {
  220. font-weight: normal;
  221. font-size: 24 rpx;
  222. color: $u-main-color;
  223. }
  224. .item-container {
  225. display: flex;
  226. flex-wrap: wrap;
  227. }
  228. .thumb-box {
  229. width: 33.333333%;
  230. display: flex;
  231. align-items: center;
  232. justify-content: center;
  233. flex-direction: column;
  234. margin-top: 20 rpx;
  235. margin-bottom: 10 rpx;
  236. }
  237. .item-menu-image {
  238. width: 120 rpx;
  239. height: 120 rpx;
  240. border-radius: 100 rpx;
  241. }
  242. </style>