| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <template>
- <view class="sharesheet" :class="{ show: modelValue }">
- <view class="mask-class sharesheet__mask" @click="onMaskClick"></view>
- <view class="sharesheet__container">
- <!-- 分享选项列表 -->
- <view class="sharesheet__list">
- <!-- 选项按钮: 发送给朋友(仅支持小程序) -->
- <!-- #ifdef MP -->
- <button class="share-item btn-normal" open-type="share" @click="handleCancel()">
- <view class="item-image" :style="{ backgroundColor: '#44DB74' }">
- <text class="iconfont icon-weixin"></text>
- </view>
- <view class="item-name">
- <text>发送给朋友</text>
- </view>
- </button>
- <!-- #endif -->
- <view class="share-item" @click="handleCopyLink()">
- <view class="item-image" :style="{ backgroundColor: '#38beec' }">
- <text class="iconfont icon-link"></text>
- </view>
- <view class="item-name">
- <text>复制链接</text>
- </view>
- </view>
- <!-- <view class="share-item">
- <view class="item-image" :style="{ backgroundColor: '#FE8A4F' }">
- <text class="iconfont icon-weibo"></text>
- </view>
- <view class="item-name">
- <text>新浪微博</text>
- </view>
- </view> -->
- <!-- <view class="share-item">
- <view class="item-image" :style="{ backgroundColor: '#56C0F2' }">
- <text class="iconfont icon-qq"></text>
- </view>
- <view class="item-name">
- <text>QQ好友</text>
- </view>
- </view> -->
- <!-- <view class="share-item">
- <view class="item-image" :style="{ backgroundColor: '#FFBB0D' }">
- <text class="iconfont icon-qzone"></text>
- </view>
- <view class="item-name">
- <text>QQ空间</text>
- </view>
- </view> -->
- </view>
- <!-- 取消按钮 -->
- <view v-if="cancelText" class="sharesheet__footer" @click="handleCancel()">
- <view class="btn-cancel">{{ cancelText }}</view>
- </view>
- </view>
- </view>
- </template>
- <!-- 参考的uniapp文档 -->
- <!-- https://uniapp.dcloud.io/component/button?id=button -->
- <!-- https://uniapp.dcloud.io/api/plugins/share -->
- <script>
- import Config from '@/core/config'
- import { getCurrentPage, buildUrL } from '@/core/app'
- import { inArray } from '@/utils/util'
- import StoreModel from '@/common/model/Store'
- export default {
- name: 'ShareSheet',
- components: {},
- emits: ['update:modelValue'],
- props: {
- // 控制组件显示隐藏
- modelValue: {
- Type: Boolean,
- default: false
- },
- // 点击遮罩层取消
- cancelWithMask: {
- type: Boolean,
- default: true
- },
- // 分享链接的标题
- shareTitle: {
- type: String,
- default: '商品分享'
- },
- // 分享链接的封面图
- shareImageUrl: {
- type: String,
- default: ''
- },
- // 取消按钮文字
- cancelText: {
- type: String,
- default: '关闭'
- }
- },
- data() {
- return {
-
- }
- },
- // 初始化方法
- created() {
- this.initSharesheet()
- },
- methods: {
- // 初始化选择项
- initSharesheet() {},
- // 点击遮罩层(关闭菜单)
- onMaskClick() {
- if (this.cancelWithMask) {
- this.handleCancel()
- }
- },
- // 获取分享链接 (H5外链)
- getShareUrl() {
- const { path, query } = getCurrentPage()
- return new Promise((resolve, reject) => {
- // 获取h5站点地址
- StoreModel.h5Url().then(baseUrl => {
- // 生成完整的分享链接
- const shareUrl = buildUrL(baseUrl, path, query)
- resolve(shareUrl)
- })
- })
- },
- // 复制商品链接
- handleCopyLink() {
- const app = this
- app.getShareUrl().then(shareUrl => {
- // 复制到剪贴板
- uni.setClipboardData({
- data: shareUrl,
- success: () => app.$toast('链接复制成功,快去发送给朋友吧~'),
- fail: ({ errMsg }) => app.$toast('复制失败 ' + errMsg),
- complete: () => app.handleCancel()
- })
- })
- },
- // 关闭菜单
- handleCancel() {
- this.$emit('update:modelValue', false)
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .sharesheet {
- background-color: #f8f8f8;
- font-size: 28rpx;
- }
- .sharesheet__mask {
- position: fixed;
- top: 0;
- left: var(--window-left);
- right: var(--window-right);
- bottom: var(--window-bottom);
- z-index: 12;
- background: rgba(0, 0, 0, 0.7);
- display: none;
- }
- .sharesheet__container {
- position: fixed;
- left: var(--window-left);
- right: var(--window-right);
- bottom: var(--window-bottom);
- background: #ffffff;
- transform: translate3d(0, 50%, 0);
- transform-origin: center;
- transition: all 0.2s ease;
- z-index: 13;
- opacity: 0;
- visibility: hidden;
- border-top-left-radius: 26rpx;
- border-top-right-radius: 26rpx;
- padding: 50rpx 30rpx 0 30rpx;
- // 设置ios刘海屏底部横线安全区域
- padding-bottom: calc(constant(safe-area-inset-bottom) + 30rpx);
- padding-bottom: calc(env(safe-area-inset-bottom) + 30rpx);
- }
- .sharesheet__list {
- display: flex;
- flex-wrap: wrap;
- justify-content: flex-start;
- margin-bottom: -35rpx;
- .share-item {
- flex: 0 0 25%;
- margin-bottom: 40rpx;
- .item-name,
- .item-image {
- width: 140rpx;
- margin: 0 auto;
- }
- .item-image {
- display: flex;
- justify-content: center;
- align-items: center;
- width: 86rpx;
- height: 86rpx;
- border-radius: 50%;
- color: #fff;
- font-size: 38rpx;
- }
- .item-name {
- margin-top: 12rpx;
- text-align: center;
- font-size: 26rpx;
- }
- }
- }
- .sharesheet__footer {
- background: #fff;
- margin-top: 40rpx;
- .btn-cancel {
- font-size: 28rpx;
- text-align: center;
- }
- }
- // 显示状态
- .show {
- .sharesheet__mask {
- display: block;
- }
- .sharesheet__container {
- opacity: 1;
- -webkit-transform: translate3d(0, 0, 0);
- transform: translate3d(0, 0, 0);
- visibility: visible;
- }
- }
- </style>
|