xunji-rili-tianjia-moban.vue 14 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
<template>
  <view class="template-select-page">
    <view class="page-header" hover-class="none">
      <!-- 动态导航栏:高度与胶囊对齐 -->
      <view class="nav-bar" :style="{
        paddingTop: menuButtonInfo.top + 'px',
        height: menuButtonInfo.height + 'px'
      }">
        <view class="nav-left" @click="goBack">
          <uni-icons class="back-icon" type="left" size="28"></uni-icons>
        </view>
        <view class="nav-title">选择模板</view>
        <view class="nav-right"></view>
      </view>

      <!-- 标签页 -->
      <view class="tab-bar">
        <view class="tab-item" :class="{ active: activeTab === 'official' }" @click="switchTab('official')">
          官方
        </view>
        <view class="tab-item" :class="{ active: activeTab === 'mine' }" @click="switchTab('mine')">
          我的
        </view>
        <!-- 部位 -->
        <view class="filter-wrapper">
          <view class="tab-item" :class="{ active: activePartId !== '0' }" @click="togglePartDropdown">
            {{ activePartId === '0' ? '部位' : activePart }}
            <uni-icons :type="showPartDropdown ? 'up' : 'down'" size="18" color="#333"></uni-icons>
          </view>

          <view class="dropdown-menu" v-if="showPartDropdown">
            <view class="dropdown-item" :class="{ selected: activePartId === item.id }" v-for="item in partList"
              :key="item.id" @click="selectPart(item)">
              {{ item.title }}
            </view>
          </view>
        </view>

        <!-- 场景 -->
        <view class="filter-wrapper">
          <view class="tab-item" :class="{ active: activeSceneId !== '0' }" @click="toggleSceneDropdown">
            {{ activeSceneId === '0' ? '场景' : activeScene }}
            <uni-icons :type="showSceneDropdown ? 'up' : 'down'" size="18" color="#333"></uni-icons>
          </view>

          <view class="dropdown-menu" v-if="showSceneDropdown">
            <view class="dropdown-item" :class="{ selected: activeSceneId === item.id }" v-for="item in sceneList"
              :key="item.id" @click="selectScene(item)">
              {{ item.title }}
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 官方模板大类列表 -->
    <scroll-view scroll-y="true" v-if="activeTab === 'official' && !isFiltering" class="template-list">
      <view v-for="(item, index) in templateList" :key="index" class="template-item" @click="goToTemplate(item)">
        <image class="template-img" :src="item.urlCover" mode="aspectFill"></image>
        <view class="template-content">
          <text class="template-title">{{ item.name }}</text>
          <text class="template-count">{{ item.templatesCount }}个模板</text>
          <text class="template-desc">{{ item.description }}</text>
        </view>
      </view>
    </scroll-view>


    <view class="v-else">

      <!-- 文件夹 -->


      <!-- 个人模板 -->
      <scroll-view scroll-y="true" class="template-list personal-template-list">
        <view class="grid-container">
          <view class="plan-card" v-for="(item, index) in templateList" :key="index" @click="goToPersonalDetail(item)">
            <image :src="item.urlCover || lostImage" mode="aspectFill" class="card-cover">
            </image>
            <view class="card-info">
              <text class="card-title">{{ item.name }}</text>
              <view class="card-stats">
                <view class="stat-item">
                  <text class="stat-number">{{ item.exerciseCount }}</text>
                  <text class="stat-label">动作</text>
                </view>
                <view class="stat-item">
                  <text class="stat-number">{{ item.totalSets }}</text>
                  <text class="stat-label">组</text>
                </view>
                <view class="stat-item">
                  <text class="stat-number">{{ item.totalWeight }}</text>
                  <text class="stat-label">kg</text>
                </view>
              </view>
              <text class="tags-text" :maxLines="1">
                {{ item.primaryMuscleNames?.join(' ') || '' }}
              </text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>




  </view>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import TemplatesApi from '@/sheep/api/Template/Templates';

// 获取模板列表
const templateList = ref([]);
const activeTab = ref('official'); // official=官方,mine=我的
const part = ref(false); // 部位是否选中
const scene = ref(false); // 场景是否选中
const isFiltering = ref(false);

const activePartId = ref('0');
const activePart = ref('不限');
const activeSceneId = ref('0');
const activeScene = ref('不限');

const showPartDropdown = ref(false);
const showSceneDropdown = ref(false);
// 部位(从接口拿)
const partList = ref([]);
const rawPartData = ref([]);

const sceneList = ref([
  { id: '0', title: '不限' },
  { id: '1', title: '健身房' },
  { id: '2', title: '仅哑铃' },
  { id: '3', title: '仅哑铃+杠铃' },
]);

// 获取部位分类(接口,和首页完全一样)
const getPartCategories = async () => {
  try {
    const res = await TemplatesApi.getPartAllCategories();
    if (res.code === 0) {
      rawPartData.value = res.data;
      partList.value = [
        { title: '不限', id: '0' },
        ...res.data.map(item => ({
          title: item.name,
          id: String(item.id)
        }))
      ];
    }
  } catch (error) {
    console.log('部位获取失败', error);
  }
};


const switchTab = async (tab) => {
  activeTab.value = tab;
  if (tab === 'official') {
    TemplatesList();
  } else {
    doFilter();
  }
};

// 弹出/关闭部位下拉
const togglePartDropdown = () => {
  showPartDropdown.value = !showPartDropdown.value;
  showSceneDropdown.value = false;
};

// 弹出/关闭场景下拉
const toggleSceneDropdown = () => {
  showSceneDropdown.value = !showSceneDropdown.value;
  showPartDropdown.value = false;
};

// 选择部位
const selectPart = (item) => {
  activePart.value = item.title;
  activePartId.value = item.id;
  showPartDropdown.value = false;
  doFilter(); // 筛选
};

// 选择场景
const selectScene = (item) => {
  activeScene.value = item.title;
  activeSceneId.value = item.id;
  showSceneDropdown.value = false;
  doFilter(); // 筛选
};

const doFilter = async () => {
  const muscleId = activePartId.value;
  const sceneId = activeSceneId.value;

  // 判断是否在筛选
  if (muscleId === '0' && sceneId === '0') {
    isFiltering.value = false;
    // 官方回到模板大类
    if (activeTab.value === 'official') {
      TemplatesList();
    } else {
      getMyTemplates();
    }
    return;
  }

  isFiltering.value = true;
  try {
    let res;
    if (activeTab.value === 'official') {
      // 官方筛选,返回模板列表
      res = await TemplatesApi.getOfficeTemplates(muscleId, sceneId);
    } else {
      // 个人筛选
      res = await TemplatesApi.queryCustTemplate(0, muscleId, sceneId);
    }
    templateList.value = res.data || [];
  } catch (err) {
    console.log('筛选失败', err);
  }
};

const TemplatesList = async () => {
  try {
    const response = await TemplatesApi.getTemplates();
    templateList.value = response.data;
    console.log('官方打印模板大类:', templateList.value);
  } catch (error) {
    console.log('模板大类获取失败', error);
  }
};

// 获取我的模板(复用同一个 templateList)
const getMyTemplates = async () => {
  // try {
  //   // 调用你自己的“我的模板”接口
  //   const res = await TemplatesApi.individualTemplates();
  //   console.log('打印个人模板res=', res);
  //   templateList.value = res.data;
  // } catch (err) {
  //   console.log('我的模板加载失败', err);
  // }
  // 获取我的模板(现在用模拟数据,不影响你原来代码)
  templateList.value = [
    {
      id: 1001,
      name: "胸部基础训练",
      urlCover: "https://picsum.photos/400/300?1",
      exerciseCount: 6,
      totalSets: 18,
      totalWeight: 50,
      primaryMuscleNames: ["胸肌", "三头肌"]
    },
    {
      id: 1002,
      name: "腿部力量训练",
      urlCover: "https://picsum.photos/400/300?2",
      exerciseCount: 5,
      totalSets: 15,
      totalWeight: 120,
      primaryMuscleNames: ["股四头", "臀部"]
    },
    {
      id: 1003,
      name: "背部塑形计划",
      urlCover: "https://picsum.photos/400/300?3",
      exerciseCount: 7,
      totalSets: 21,
      totalWeight: 70,
      primaryMuscleNames: ["背阔肌"]
    }
  ];
};

// 返回上一页
const goBack = () => {
  uni.navigateBack();
};

const lostImage = "https://fitness-hcxtec-bucket.oss-cn-shenzhen.aliyuncs.com/20260316/order-empty_1773628059920.png";
// 个人模板点击跳转(直接进详情)
const goToPersonalDetail = (item) => {
  uni.navigateTo({
    url: `/pages4/pages/xunji/xunji-moban-xiangqing?id=${item.id}`
  });
};

// 跳转到模板详情页
const goToTemplate = (item) => {
  uni.navigateTo({
    url: `/pages4/pages/xunji/xunji-moban?id=${item.id}`
  });
};

// 获取胶囊按钮信息,用于动态适配导航栏高度
const menuButtonInfo = ref({
  top: 44,    // 默认值,避免获取失败时样式异常
  height: 32
});

onMounted(() => {
  TemplatesList();
  getPartCategories();

  // 获取小程序胶囊位置信息,实现导航栏与胶囊完美对齐
  // #ifdef MP-WEIXIN
  try {
    const rect = uni.getMenuButtonBoundingClientRect();
    if (rect) {
      menuButtonInfo.value = {
        top: rect.top,
        height: rect.height
      };
    }
  } catch (e) {
    console.log('获取胶囊信息失败,使用默认值', e);
  }
  // #endif

  // #ifndef MP-WEIXIN
  // 非微信小程序环境,使用系统状态栏高度 + 标准导航栏高度
  try {
    const systemInfo = uni.getSystemInfoSync();
    const statusBarHeight = systemInfo.statusBarHeight || 20;
    menuButtonInfo.value = {
      top: statusBarHeight,
      height: 44  // 标准导航栏高度
    };
  } catch (e) {
    console.log('获取系统信息失败', e);
  }
  // #endif
});
</script>

<style scoped lang="scss">
.template-select-page {
  display: flex;
  flex-direction: column;
  background-color: #f6f6f6;
  height: 100vh;
  overflow: hidden;
}

.page-header {
  width: 100%;
  flex-shrink: 0;
  background-color: #fff;
}

/* 动态导航栏:高度与胶囊完全对齐 */
.nav-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #fff;
  /* 左右留出安全间距,避免内容贴边 */
  padding-left: 16rpx;
  padding-right: 16rpx;
  /* 高度和顶部内边距由动态 style 控制 */
  box-sizing: content-box;
}

/* 左侧返回按钮区域 - 固定宽度确保居中对齐 */
.nav-left {
  width: 60rpx;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  flex-shrink: 0;
}

.back-icon {
  display: block;
}

/* 标题区域 - 自适应居中 */
.nav-title {
  flex: 1;
  text-align: center;
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
  /* 继承行高,保证垂直居中 */
  line-height: 1;
}

/* 右侧占位区域 - 宽度与左侧相同,保证标题绝对居中 */
.nav-right {
  width: 60rpx;
  flex-shrink: 0;
}

/* 标签页 */
.tab-bar {
  display: flex;
  padding: 20rpx 30rpx;
  background-color: #fff;
  border-bottom: 1rpx solid #f0f0f0;
}

.tab-item {
  padding: 12rpx 24rpx;
  border: 1rpx solid #ddd;
  border-radius: 40rpx;
  margin-right: 20rpx;
  font-size: 28rpx;
  color: #333;
  line-height: 1;
  background: #fff;
  display: flex;
  align-items: center;
  gap: 6rpx;
  transition: all 0.2s;

  &.active {
    border-color: #000;
    color: #000;
    font-weight: 500;
  }
}

/* 模板列表 */
.template-list {
  flex: 1;
  padding: 20rpx 30rpx;
  background-color: #f6f6f6;
  overflow-y: auto;
}

.template-item {
  display: flex;
  margin-bottom: 30rpx;
  background-color: #fff;
  border-radius: 16rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
  transition: transform 0.2s, box-shadow 0.2s;
}

.template-item:active {
  transform: scale(0.98);
  box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.1);
}

.template-img {
  width: 160rpx;
  height: 160rpx;
  border-radius: 16rpx;
}

.template-content {
  flex: 1;
  padding: 20rpx;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.template-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 10rpx;
}

.template-count {
  font-size: 24rpx;
  color: #999;
  margin-bottom: 10rpx;
}

.template-desc {
  font-size: 26rpx;
  color: #666;
  line-height: 1.5;
}

/* 正确的下拉菜单 */
.filter-wrapper {
  position: relative;
  z-index: 9999;
}

.dropdown-menu {
  position: absolute;
  top: calc(100% + 8rpx);
  left: 0;
  background: #fff;
  border-radius: 16rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
  padding: 20rpx;
  min-width: 200rpx;
  max-height: 400rpx;
  overflow-y: auto;
}

.dropdown-item {
  padding: 16rpx 24rpx;
  font-size: 28rpx;
  color: #333;
  border-radius: 8rpx;
}

.dropdown-item.selected {
  background: #ffd100;
  color: #000;
  font-weight: bold;
}

/* 个人模板 - 两列网格 */
.personal-template-list {
  background: #f5f5f5;
  padding: 20rpx 0;
}

.grid-container {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20rpx;
  padding: 0 20rpx;
}

.plan-card {
  border-radius: 16rpx;
  overflow: hidden;
  position: relative;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
}

.card-cover {
  width: 100%;
  height: 300rpx;
}

.card-info {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 20rpx;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.8), transparent);
}

.card-title {
  color: #fff;
  font-size: 32rpx;
  font-weight: bold;
  margin-bottom: 10rpx;
}

.card-stats {
  display: flex;
  flex-direction: column;
  gap: 4rpx;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 6rpx;
}

.stat-number {
  color: #fff;
  font-size: 26rpx;
  font-weight: bold;
}

.stat-label {
  color: rgba(255, 255, 255, 0.8);
  font-size: 22rpx;
}

.tags-text {
  color: rgba(255, 255, 255, 0.7);
  font-size: 22rpx;
  margin-top: 6rpx;
}
</style>