index.vue 15.6 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 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
<!--
 * @Date: 2022-09-19 14:11:06
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2025-09-04 22:34:52
 * @FilePath: /lls_program/src/pages/ActivitiesCover/index.vue
 * @Description: 活动海报页面 - 展示活动信息并处理定位授权
-->
<template>
  <view class="activities-cover-container">
    <!-- 背景图片 -->
    <image
      :src="defaultPoster"
      class="background-image"
      mode="scaleToFill"
    />

    <!-- 分享按钮组件 -->
    <ShareButton
      :activity-data="activityData"
      @share-activity="onShareActivity"
      @share-poster="onSharePoster"
      class="share-button-wrapper"
    />

    <!-- 底部按钮区域 -->
    <view class="bottom-section">
      <!-- 未授权定位提示 -->
      <view v-if="!hasLocationAuth && !locationError" class="location-tip" @click="retryGetLocation">
        <view class="tip-content">
          <view class="tip-icon">📍</view>
          <view class="tip-text">需要获取您的位置信息来参与活动</view>
        </view>
        <view class="tip-retry">点击重新获取</view>
      </view>

      <!-- 位置获取失败提示 -->
      <view v-if="hasLocationAuth && locationError" class="location-tip location-error" @click="retryGetLocation">
        <view class="tip-content">
          <view class="tip-icon">⚠️</view>
          <view class="tip-text">可能是网络问题,获取位置信息失败</view>
        </view>
        <view class="tip-retry">点击重新获取</view>
      </view>

      <nut-button
        type="primary"
        size="large"
        class="join-button"
        color="#3B82F6"
        :loading="isJoining"
        @click="checkFamilyStatusAndJoinActivity"
      >
        {{ hasLocationAuth && !locationError ? '进入活动' : '参加活动' }}
      </nut-button>
    </view>

    <!-- 底部导航 -->
    <BottomNav />



    <!-- 海报预览弹窗 -->
    <nut-popup
      v-model:visible="show_post"
      position="center"
      class="poster-preview-popup"
    >
      <view class="wrapper">
        <view class="preview-area" @click="onClickPost">
          <image v-if="posterPath" :src="posterPath" mode="widthFix" />
        </view>
      </view>
    </nut-popup>

    <!-- 海报生成组件 -->
    <PosterBuilder
      v-if="startDraw"
      custom-style="position: fixed; left: 200%;"
      :config="base"
      @success="drawSuccess"
      @fail="drawFail"
    />

    <!-- 保存选项弹窗 -->
    <nut-action-sheet
      v-model:visible="show_save"
      :menu-items="actions_save"
      @choose="onSelectSave"
      @cancel="onCancelSave"
      cancel-txt="取消"
    />
  </view>
</template>

<script setup>
import '@tarojs/taro/html.css'
import { ref, onMounted } from "vue"
import Taro, { useLoad } from '@tarojs/taro'
import "./index.less"
import BottomNav from '../../components/BottomNav.vue'
import PosterBuilder from '../../components/PosterBuilder/index.vue'
import ShareButton from '../../components/ShareButton/index.vue'
// 接口信息
import { getMyFamiliesAPI } from '@/api/family'
import { handleSharePageAuth, addShareFlag } from '@/utils/authRedirect'

// 默认海报图
const defaultPoster = 'https://cdn.ipadbiz.cn/lls_prog/images/welcome_1.png';
/**
 * 活动海报页面组件
 * 功能:展示活动信息、处理定位授权、跳转到活动页面
 */

// 页面状态
const hasLocationAuth = ref(false) // 是否已授权定位
const locationError = ref(false) // 位置获取是否失败
const isJoining = ref(false) // 是否正在加入活动
const userLocation = ref({ lng: null, lat: null }) // 用户位置信息
const hasJoinedFamily = ref(false);

// 海报生成相关状态
const show_post = ref(false) // 显示海报预览
const show_save = ref(false) // 显示保存弹窗
const startDraw = ref(false) // 开始绘制海报
const posterPath = ref('') // 海报路径
const nickname = ref('老来赛用户') // 用户昵称
const avatar = ref('https://cdn.ipadbiz.cn/icon/tou@2x.png') // 用户头像

// 保存选项
const actions_save = ref([{
  name: '保存至相册'
}])

// 海报配置
let base = {}
let qrcode_url = 'https://cdn.ipadbiz.cn/space/068a790496c87cb8d2ed6e551401c544.png' // Mock二维码

// Mock活动数据
const activityData = ref({
  title: '南京路商圈时尚Citywalk',
  subtitle: '探索城市魅力,感受时尚脉搏',
  dateRange: '2024年1月15日 - 2024年1月31日',
  posterUrl: 'https://img.yzcdn.cn/vant/cat.jpeg', // 临时使用示例图片
  description: '漫步南京路,感受上海的繁华与历史交融。从外滩到人民广场,体验这座城市独特的魅力和时尚气息。',
  rules: [
    '年满60岁的老年人可参与活动',
    '需要在指定时间内完成所有打卡点',
    '每个打卡点需上传照片验证',
    '完成全部打卡可获得电子勋章和积分奖励'
  ],
  rewards: [
    '完成打卡获得500积分',
    '获得专属电子勋章',
    '有机会获得商户优惠券',
    '参与月度积分排行榜'
  ]
})

/**
 * 检查定位授权状态
 */
const checkLocationAuth = async () => {
  try {
    const authSetting = await Taro.getSetting()
    hasLocationAuth.value = authSetting.authSetting['scope.userLocation'] === true
    console.log('定位授权状态:', hasLocationAuth.value)
  } catch (error) {
    console.error('检查定位授权失败:', error)
    hasLocationAuth.value = false
  }
}

/**
 * 获取用户位置信息
 */
const getUserLocation = async () => {
  try {
    locationError.value = false // 重置错误状态
    const location = await Taro.getLocation({
      type: 'gcj02'
    })

    userLocation.value = {
      lng: location.longitude,
      lat: location.latitude
    }

    console.log('获取到用户位置:', userLocation.value)
    return true
  } catch (error) {
    console.error('获取位置失败:', error)

    if (error.errMsg && error.errMsg.includes('auth deny')) {
      // 用户拒绝授权,引导用户手动开启
      hasLocationAuth.value = false
      locationError.value = false
      await Taro.showModal({
        title: '需要位置权限',
        content: '参与活动需要获取您的位置信息,请在设置中开启位置权限',
        confirmText: '去设置',
        success: (res) => {
          if (res.confirm) {
            Taro.openSetting()
          }
        }
      })
    } else {
      // 网络或其他问题导致的获取失败
      locationError.value = true
      Taro.showToast({
        title: '获取位置失败',
        icon: 'none'
      })
    }

    return false
  }
}

/**
 * 检查用户是否加入家庭并处理参加活动按钮点击
 */
const checkFamilyStatusAndJoinActivity = async () => {
  // Mock data: hasJoinedFamily is set to false
  if (!hasJoinedFamily.value) {
    Taro.showModal({
      title: '提示',
      content: '没有加入家庭是无法参加活动的',
      cancelText: '关闭',
      confirmText: '前往加入',
      success: (res) => {
        if (res.confirm) {
          Taro.navigateTo({
            url: '/pages/Welcome/index',
          });
        }
      },
    });
  } else {
    handleJoinActivity();
  }
};


/**
 * 重新获取位置信息
 */
const retryGetLocation = async () => {
  try {
    const success = await getUserLocation()
    if (success) {
      hasLocationAuth.value = true
      locationError.value = false
      Taro.showToast({
        title: '位置获取成功',
        icon: 'success'
      })
    }
  } catch (error) {
    console.error('重新获取位置失败:', error)
  }
}

/**
 * 处理参加活动按钮点击
 */
const handleJoinActivity = async () => {
  isJoining.value = true

  try {
    // 如果没有定位授权,先获取授权和位置信息
    if (!hasLocationAuth.value) {
      const success = await getUserLocation()
      if (!success) {
        isJoining.value = false
        return
      }
      hasLocationAuth.value = true
    } else {
      // 已有授权,直接获取位置
      const success = await getUserLocation()
      if (!success) {
        isJoining.value = false
        return
      }
    }

    // 跳转到Activities页面,并传递位置参数
    await Taro.navigateTo({
      url: `/pages/Activities/index?current_lng=${userLocation.value.lng}&current_lat=${userLocation.value.lat}`
    })

  } catch (error) {
    console.error('参加活动失败:', error)
    Taro.showToast({
      title: '参加活动失败',
      icon: 'none'
    })
  } finally {
    isJoining.value = false
  }
}

/**
 * 处理分享活动事件
 */
const onShareActivity = () => {
  console.log('分享活动海报')
  // 分享给朋友
  Taro.showToast({
      title: '请点击右上角分享给朋友',
      icon: 'none',
      duration: 2000
  });
}

/**
 * 定义分享给朋友的内容
 * @returns {Object} 分享配置对象
 */
const onShareAppMessage = () => {
    return {
      title: '分享活动海报',
      path: addShareFlag('/pages/ActivitiesCover/index'),
      success: (res) => {
        // 分享成功
      },
      fail: (err) => {
        // 分享失败
      }
    }
}

// 导出分享方法供Taro使用
defineExpose({
    onShareAppMessage,
})

/**
 * 处理分享海报事件
 */
const onSharePoster = () => {
  console.log('分享海报')
  Taro.navigateTo({
    url: '/pages/PosterCheckin/index',
  })
}

/**
 * 点击海报预览
 */
const onClickPost = () => {
  show_save.value = true
}

/**
 * 取消保存
 */
const onCancelSave = () => {
  show_save.value = false
  show_post.value = false
}

/**
 * 选择保存方式
 */
const onSelectSave = (item) => {
  if (item.name === '保存至相册') {
    show_save.value = false
    show_post.value = false
    savePoster()
  }
}

/**
 * 开始生成海报
 */
const startGeneratePoster = async () => {
  // 配置海报参数
  base = {
    width: 1024,
    height: 1334,
    backgroundColor: '',
    debug: false,
    blocks: [
      { // 上部分canvas画布高度
        x: 40,
        y: 20,
        width: 950,
        height: 950,
        paddingLeft: 0,
        paddingRight: 0,
        borderWidth: 1,
        borderColor: '#fff',
        backgroundColor: '#fff',
        borderRadiusGroup: [16, 16, 0, 0],
      },
      { // 活动时间背景图
        x: 40,
        y: 730,
        height: 75,
        paddingLeft: 80,
        paddingRight: 0,
        borderWidth: 0,
        text: {
          x: 0,
          y: 0,
          text: activityData.value.dateRange,
          fontSize: 40,
          color: '#222',
          opacity: 1,
          baseLine: 'top',
          lineHeight: 48,
          lineNum: 2,
          textAlign: 'left',
          zIndex: 0,
        },
        backgroundColor: '#FFF9F3',
        borderRadiusGroup: [0, 25, 25, 0],
      },
      { // 活动地点背景图
        x: 40,
        y: 830,
        height: 75,
        paddingLeft: 80,
        paddingRight: 0,
        borderWidth: 0,
        text: {
          x: 0,
          y: 0,
          text: '上海市黄浦区南京东路',
          fontSize: 40,
          color: '#222',
          opacity: 1,
          baseLine: 'top',
          lineHeight: 48,
          lineNum: 2,
          textAlign: 'left',
          zIndex: 0,
        },
        backgroundColor: '#FFF9F3',
        borderRadiusGroup: [0, 25, 25, 0],
      },
      { // 下部分canvas画布高度
        x: 40,
        y: 1060,
        width: 950,
        height: 250,
        paddingLeft: 0,
        paddingRight: 0,
        borderWidth: 1,
        borderColor: '#fff',
        backgroundColor: '#fff',
        borderRadiusGroup: [0, 0, 16, 16],
      }
    ],
    texts: [
      {
        x: 80,
        y: 630,
        text: activityData.value.title,
        fontSize: 50,
        color: '#000',
        opacity: 1,
        baseLine: 'middle',
        lineHeight: 60,
        lineNum: 2,
        textAlign: 'left',
        width: 800,
        zIndex: 999,
        fontFamily: 'Monospace',
      },
      {
        x: 135,
        y: 770,
        text: activityData.value.dateRange,
        fontSize: 40,
        color: '#222',
        opacity: 1,
        baseLine: 'middle',
        lineHeight: 48,
        lineNum: 2,
        textAlign: 'left',
        zIndex: 999,
      },
      {
        x: 135,
        y: 870,
        text: '上海市黄浦区南京东路',
        fontSize: 40,
        color: '#222',
        opacity: 1,
        baseLine: 'middle',
        lineHeight: 48,
        lineNum: 2,
        textAlign: 'left',
        zIndex: 999,
      },
      {
        x: 300,
        y: 1150,
        text: nickname.value,
        fontSize: 50,
        color: '#333',
        opacity: 1,
        baseLine: 'middle',
        textAlign: 'left',
        lineHeight: 50,
        lineNum: 1,
        zIndex: 999,
      },
      {
        x: 300,
        y: 1220,
        text: '邀请你一起来活动!',
        fontSize: 42,
        color: '#8F9399',
        opacity: 1,
        baseLine: 'middle',
        textAlign: 'left',
        lineHeight: 42,
        lineNum: 1,
        zIndex: 999,
      }
    ],
    images: [
      {
        url: qrcode_url,
        width: 949,
        height: 108,
        x: 40,
        y: 960,
        zIndex: 10,
      },
      {
        url: qrcode_url,
        width: 950,
        height: 500,
        x: 40,
        y: 20,
        borderRadiusGroup: [18, 18, 0, 0],
        zIndex: 10,
      },
      {
        url: qrcode_url,
        width: 40,
        height: 40,
        x: 80,
        y: 750,
        borderRadius: 100,
        borderWidth: 0,
        zIndex: 10,
      },
      {
        url: qrcode_url,
        width: 35,
        height: 40,
        x: 80,
        y: 850,
        borderRadius: 100,
        borderWidth: 0,
        zIndex: 10,
      },
      {
        url: qrcode_url,
        width: 170,
        height: 170,
        x: 80,
        y: 1090,
        borderRadius: 100,
        borderWidth: 0,
        zIndex: 10,
      },
      {
        url: qrcode_url,
        width: 170,
        height: 170,
        x: 750,
        y: 1090,
        borderRadius: 100,
        borderWidth: 0,
        zIndex: 10,
      },
    ],
    lines: []
  }

  startDraw.value = true
  if (!posterPath.value) Taro.showLoading({ title: '生成海报中...' })
}

/**
 * 海报绘制成功回调
 */
const drawSuccess = (result) => {
  console.log('绘制成功', result)
  const { tempFilePath, errMsg } = result
  if (errMsg === 'canvasToTempFilePath:ok') {
    posterPath.value = tempFilePath
    Taro.hideLoading()
  } else {
    Taro.hideLoading()
    Taro.showToast({
      title: '生成失败,请稍后重试',
      icon: 'none',
      duration: 2500
    })
  }
}

/**
 * 海报绘制失败回调
 */
const drawFail = (result) => {
  console.log('绘制失败', result)
  Taro.hideLoading()
  Taro.showToast({
    title: '生成失败,请稍后重试',
    icon: 'none',
    duration: 2500
  })
}

/**
 * 保存海报到相册
 */
const savePoster = () => {
  Taro.saveImageToPhotosAlbum({
    filePath: posterPath.value,
    success() {
      Taro.showToast({
        title: '已保存到相册',
        icon: 'success',
        duration: 2000
      })
    },
    fail() {
      Taro.showToast({
        title: '保存失败',
        icon: 'none',
        duration: 2000
      })
    }
  })
}

/**
 * 初始化页面数据
 */
const initPageData = async () => {
  // 获取用户是否加入家庭
  const { code, data } = await getMyFamiliesAPI()
  if (code) {
    // 如果加入家庭
    if (data.length) {
      hasJoinedFamily.value = true
    }
  }
  // 检查定位授权弹窗
  checkLocationAuth()
}

// 处理页面加载时的授权检查
useLoad((options) => {
  // 处理分享页面的授权逻辑
  handleSharePageAuth(options, () => {
    initPageData();
  });
});

// 页面挂载时检查定位授权状态
onMounted(async () => {
  initPageData()
})
</script>

<script>
export default {
  name: "ActivitiesCover",
};
</script>