index.vue 3.69 KB
<!--
 * @Date: 2025-09-03 14:34:58
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2025-09-26 10:50:10
 * @FilePath: /lls_program/src/components/ShareButton/index.vue
 * @Description: 分享按钮组件 - 点击后弹出分享选项
-->
<template>
  <view class="share-button-container">
    <!-- 分享活动按钮 -->
    <button id="share" data-name="shareBtn" open-type="share" class="share-button share-activity-btn">
      分享朋友
    </button>

    <!-- 分享海报按钮 -->
    <view @tap="handleSharePoster" class="share-button share-poster-btn">
      分享海报
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import Taro from '@tarojs/taro'
import { addShareFlag } from '@/utils/authRedirect'

// 组件属性
const props = defineProps({
  // 活动数据
  activityData: {
    type: Object,
    default: () => ({})
  },
  // 分享配置
  shareConfig: {
    type: Object,
    default: () => ({
      title: '精彩活动等你参与',
      path: '/pages/ActivitiesCover/index',
      imageUrl: ''
    })
  }
})

// 组件事件
const emit = defineEmits(['shareActivity', 'sharePoster'])

/**
 * 处理海报分享
 */
const handleSharePoster = () => {
  emit('sharePoster', props.activityData)
}

/**
 * 定义分享给朋友的内容
 * @param {Object} res - 分享来源信息
 * @returns {Object} 分享配置对象
 */
const onShareAppMessage = (res) => {
  // 构建分享数据
  const shareData = {
    title: props.shareConfig.title || '精彩活动等你参与',
    path: addShareFlag(props.shareConfig.path || '/pages/ActivitiesCover/index'),
    imageUrl: props.shareConfig.imageUrl || ''
  }

  // 触发分享事件
  emit('shareActivity', {
    ...props.activityData,
    shareData
  })

  return shareData
}

// 使用Taro的useShareAppMessage Hook来处理分享
Taro.useShareAppMessage((res) => {
  return onShareAppMessage(res)
})

// 导出分享方法供Taro使用(保持兼容性)
defineExpose({
  onShareAppMessage,
})
</script>

<style lang="less">
.share-button-container {
  position: fixed;
  top: 40rpx;
  right: 30rpx;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 28rpx;
  align-items: center;
}

.share-button {
  // 重置所有默认样式 - 使用!important强制覆盖
  margin: 0 !important;
  padding: 0 !important;
  border: none !important;
  outline: none !important;
  background: none !important;
  font: inherit !important;
  position: relative !important;
  display: inline-block !important;

  // 应用自定义样式
  color: white !important;
  padding: 12rpx 20rpx !important;
  border-radius: 32rpx !important;
  background: rgba(0, 0, 0, 0.6) !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.2) !important;
  transition: all 0.3s ease !important;
  backdrop-filter: blur(10rpx) !important;
  font-size: 24rpx !important;
  width: 140rpx !important;
  height: 60rpx !important;
  white-space: nowrap !important;
  flex-shrink: 0 !important;
  box-sizing: border-box !important;
  line-height: normal !important;
  text-align: center !important;

  &:active {
    transform: scale(0.95) !important;
    background: rgba(0, 0, 0, 0.8) !important;
  }

  // 去除微信小程序button的默认样式
  &::after {
    border: none !important;
    content: none !important;
  }

  // 去除button默认的点击效果
  &.button-hover {
    background: rgba(0, 0, 0, 0.8) !important;
  }
}

.share-activity-btn {
  background: rgba(74, 144, 226, 0.9);

  &:active {
    background: rgba(74, 144, 226, 1);
  }
}

.share-poster-btn {
  background: rgba(52, 199, 89, 0.9);

  &:active {
    background: rgba(52, 199, 89, 1);
  }
}
</style>