文件
xionggeshop/xgsc.gynhc.com_wx/components/shopro-share/shopro-share.vue
2026-09-14 17:24:53 +08:00

706 行
17 KiB
Vue

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<template>
<view class="content">
<!-- 海报弹窗:u-popup 替代 cu-modal,修复真机仅显示遮罩、弹层不出现的问题 -->
<u-popup
v-model="showPoster"
mode="center"
:mask-close-able="true"
border-radius="20"
:z-index="10090"
@close="onClosePoster"
>
<view class="poster-dialog">
<view class="poster-img-box">
<image class="poster-img" :src="posterImage" mode="widthFix"></image>
</view>
<view class="poster-btn-box u-m-t-20 u-flex u-row-between u-col-center" v-show="posterImage">
<button class="cancel-btn u-reset-button" @tap="showPoster = false">取消</button>
<button class="save-btn u-reset-button"
@tap="saveImage">{{ ['wxOfficialAccount', 'H5'].includes(platform) ? '长按图片保存' : '保存图片' }}</button>
</view>
</view>
</u-popup>
<!-- 分享面板 -->
<u-popup
v-model="showShare"
mode="bottom"
border-radius="20"
:mask-close-able="true"
:z-index="10085"
>
<view class="share-box safe-area-inset-bottom">
<view class="share-list-box u-flex">
<!-- #ifdef MP-WEIXIN -->
<button class="share-item share-btn u-flex-col u-col-center" open-type="share">
<image class="share-img" :src="$IMG_URL + '/imgs/share/share_wx.png'" mode=""></image>
<text class="share-title">微信好友</text>
</button>
<!-- #endif -->
<!-- #ifndef MP-WEIXIN -->
<view v-if="platform !== 'H5'" class="share-item u-flex-col u-col-center" @tap="shareFriend">
<image class="share-img" :src="$IMG_URL + '/imgs/share/share_wx.png'" mode=""></image>
<text class="share-title">微信好友</text>
</view>
<!-- #endif -->
<view class="share-item u-flex-col u-col-center" @tap="onPoster">
<image class="share-img" :src="$IMG_URL + '/imgs/share/share_poster.png'" mode=""></image>
<text class="share-title">生成海报</text>
</view>
</view>
<view class="share-foot u-flex u-row-center u-col-center" @tap="showShare = false">取消</view>
</view>
</u-popup>
<!-- 分享指引 -->
<u-popup
v-model="showShareGuide"
mode="bottom"
border-radius="20"
:mask-close-able="true"
:z-index="10085"
>
<view class="guide-wrap safe-area-inset-bottom u-flex u-col-top u-row-center">
<image class="guide-img" :src="$IMG_URL + '/imgs/share/share_guide.png'" mode=""></image>
</view>
</u-popup>
<!-- 各海报模块 -->
<shopro-canvas v-if="showPoster" ref="shoproCanvas" :canvasParams="canvasParams"
@success="onSuccess"></shopro-canvas>
</view>
</template>
<script>
/**
* 分享弹窗
* @property {Boolean} value = showModal - v-model控制显隐
* @property {String} posterType - 海报类别
* @property {Object} posterInfo - 海报数据
*/
import {
mapMutations,
mapActions,
mapState,
mapGetters
} from 'vuex';
import Auth from '@/shopro/permission/index.js';
import share from '@/shopro/share';
export default {
name: 'shoproShare',
components: {},
data() {
return {
showShareGuide: false, //H5的指引。
showPoster: false, //海报弹窗
useQrcodeFallback: false, //小程序码接口失败时降级为普通二维码
platform: this.$platform.get(),
posterImage: '',
canvasParams: {}
};
},
props: {
posterType: {
type: String,
default: ''
},
posterInfo: {
type: Object,
default: () => {}
},
value: {}
},
computed: {
...mapGetters(['initShare', 'initShop', 'userInfo', 'isLogin', 'shareInfo']),
showShare: {
get() {
return this.value;
},
set(val) {
if (!this.showPoster) {
// val ? uni.hideTabBar() : uni.showTabBar();
}
this.$emit('input', val);
}
}
},
created() {
uni.$on('ON_WX_SHARE', () => {
this.showShare = false;
});
},
methods: {
// 海报用头像:canvas 不支持 data:image/svg 占位图,需使用 https 网络图
getPosterAvatarUrl() {
const avatar = (this.userInfo && this.userInfo.avatar) || '';
if (avatar && /^https?:\/\//i.test(avatar)) {
return avatar;
}
return this.getWxCodeCenterLogoUrl();
},
// 小程序码中心品牌 Logo
getWxCodeCenterLogoUrl() {
return this.$IMG_URL + '/uploads/demo/xiongge/xiongge-brand-emblem.png';
},
// 小程序码 scene(微信限制32字符,只传 spm 值)
getWxacodeScene() {
let scene = (this.shareInfo && this.shareInfo.query) || '';
if (scene.indexOf('spm=') === 0) {
scene = scene.substring(4);
}
return scene;
},
getWxacodeUrl() {
const scene = encodeURIComponent(this.getWxacodeScene());
const path = encodeURIComponent('pages/index/index');
return `${this.$API_URL}wechat/wxacode?scene=${scene}&path=${path}`;
},
getWxCodeLogoDrawItem({ dy, dx, size = 180, isBgCenter = false }) {
const logoSize = Math.round(size * 0.28);
const item = {
name: 'wxCodeLogo',
type: 'image',
url: this.getWxCodeCenterLogoUrl(),
alpha: 1,
dy: dy + (size - logoSize) / 2,
dWidth: logoSize,
dHeight: logoSize,
roundRectSet: { r: 6 }
};
if (isBgCenter) {
item.isBgCenter = true;
} else if (dx !== undefined) {
item.dx = dx + (size - logoSize) / 2;
}
return item;
},
getWxCodeDrawItems({ dy, dx, size = 180, isBgCenter = false }) {
if (this.useQrcodeFallback) {
const qrcodeItem = {
name: '普通二维码',
type: 'qrcode',
text: this.shareInfo.copyLink || `${this.initShop?.domain || ''}?${this.shareInfo.query || ''}`,
size,
dy,
image: this.getWxCodeCenterLogoUrl(),
imageSize: Math.round(size * 0.28)
};
if (isBgCenter) {
qrcodeItem.isBgCenter = true;
} else if (dx !== undefined) {
qrcodeItem.dx = dx;
}
return [qrcodeItem];
}
const wxCodeItem = {
name: 'wxCode',
type: 'image',
url: this.getWxacodeUrl(),
alpha: 1,
dy,
dWidth: size,
dHeight: size
};
if (isBgCenter) {
wxCodeItem.isBgCenter = true;
} else if (dx !== undefined) {
wxCodeItem.dx = dx;
}
return [wxCodeItem, this.getWxCodeLogoDrawItem({ dy, dx, size, isBgCenter })];
},
checkWxacodeAvailable() {
return new Promise(resolve => {
uni.request({
url: this.getWxacodeUrl(),
method: 'GET',
responseType: 'arraybuffer',
success: res => {
const contentType = res.header['Content-Type'] || res.header['content-type'] || '';
const isImage = contentType.includes('image') || (res.data && res.data.byteLength > 200);
resolve(res.statusCode === 200 && isImage);
},
fail: () => resolve(false)
});
});
},
// 关闭弹窗
onClosePoster() {
this.showPoster = false;
},
// 绘制成功
onSuccess(e) {
this.posterImage = e;
},
// 开始绘制
async onPoster() {
this.posterImage = '';
if (!this.$store.getters.isLogin) {
this.$store.dispatch('showAuthModal', 'accountLogin');
this.showShare = false;
return;
}
try {
share.setShareInfo();
} catch (e) {
this.$u.toast('请先在商城后台配置分享信息');
return;
}
if (!this.initShare || !this.initShare.user_poster_bg) {
this.$u.toast('请先在后台配置用户分享海报背景');
return;
}
this.useQrcodeFallback = false;
// #ifdef MP-WEIXIN
const wxacodeOk = await this.checkWxacodeAvailable();
if (!wxacodeOk) {
const confirmed = await new Promise(resolve => {
uni.showModal({
title: '小程序码获取失败',
content: '后台微信小程序 AppSecret 配置错误。请到后台【Shopro → 平台配置 → 微信小程序】更新 AppID 和 AppSecret 后重试。是否先用普通二维码生成海报?',
confirmText: '继续生成',
cancelText: '取消',
success: res => resolve(!!res.confirm)
});
});
if (!confirmed) {
return;
}
this.useQrcodeFallback = true;
}
// #endif
this.canvasParams = this.getPosterFormatter();
this.showPoster = true;
this.showShare = false;
},
// 分享好友
shareFriend() {
let that = this;
// #ifdef APP-PLUS
uni.share({
provider: 'weixin',
scene: 'WXSceneSession',
type: 0,
href: that.shareInfo.path,
title: that.shareInfo.title,
summary: that.shareInfo.title,
image: that.shareInfo.image,
success: res => {
console.log('success:' + JSON.stringify(res));
this.showShare = false;
},
fail: err => {
console.log('fail:' + JSON.stringify(err));
}
});
// #endif
// #ifdef H5
this.showShare = false;
this.showShareGuide = true;
// #endif
},
// 保存图片
async saveImage() {
let that = this;
if (['wxOfficialAccount', 'H5'].includes(this.platform)) {
this.$u.toast('长按图片保存');
return false;
}
let authState = await new Auth('writePhotosAlbum').check();
if (authState) {
uni.saveImageToPhotosAlbum({
filePath: that.posterImage,
success: res => {
this.$u.toast('保存成功');
this.showPoster = false;
},
fail: err => {
// console.log(`图片保存失败:`, err);
// this.$u.toast('保存失败');
}
});
}
},
// 复制链接
copySharePath() {
let that = this;
uni.setClipboardData({
data: that.shareInfo.copyLink,
success: data => {
that.$u.toast('已复制到剪切板');
that.showShare = false;
}
});
},
// 获取海报格式,规则说明在@/shopro/poster/tools.js中的initDrawArray
getPosterFormatter() {
const that = this;
let data = {};
switch (this.posterType) {
case 'user':
data = {
backgroundImage: that.initShare.user_poster_bg,
drawArray: [{
name: '用户昵称',
type: 'text',
text: that.userInfo.nickname,
isBgCenter: true,
size: 28,
dy: 250,
color: '#333',
textAlign: 'middle',
textBaseLine: 'middle'
},
{
name: 'avatar',
type: 'image',
url: that.getPosterAvatarUrl(),
alpha: 1,
isBgCenter: true,
dy: 95,
dWidth: 120,
dHeight: 120,
circleSet: {}
},
// #ifdef MP-WEIXIN
...that.getWxCodeDrawItems({ dy: 560, size: 180, isBgCenter: true }),
// #endif
// #ifndef MP-WEIXIN
{
name: '普通二维码',
type: 'qrcode',
text: that.shareInfo.path,
size: 180,
dy: 560,
isBgCenter: true,
image: that.getWxCodeCenterLogoUrl(),
imageSize: 50
}
// #endif
]
};
break;
case 'goods':
data = {
backgroundImage: that.initShare.goods_poster_bg,
drawArray: [{
name: 'avatar',
type: 'image',
url: that.getPosterAvatarUrl(),
alpha: 1,
dy: 40,
dx: 38,
dWidth: 80,
dHeight: 80,
circleSet: {}
},
{
type: 'text',
text: that.userInfo.nickname,
size: 28,
color: '#333',
alpha: 1,
textAlign: 'middle',
textBaseline: 'bottom',
dx: 140,
dy: 40
},
{
type: 'text',
text: '有一个好物给你,请查收!',
size: 26,
color: '#333',
alpha: 1,
textAlign: 'middle',
textBaseline: 'middle',
dx: 140,
dy: 80
},
{
name: 'goodsImage',
type: 'image',
url: that.posterInfo.image,
alpha: 1,
isBgCenter: true,
dy: 140,
dWidth: 620,
dHeight: 620,
roundRectSet: {
r: 20
}
},
{
type: 'text', //标题
text: that.posterInfo.title,
size: 28,
color: '#333',
alpha: 1,
textAlign: 'left',
textBaseline: 'middle',
fontWeight: 'bold',
lineFeed: {
maxWidth: 620,
lineHeight: 40,
lineNum: 2
},
dx: 36,
dy: 780
},
{
type: 'text',
text: `¥${that.posterInfo.price}`,
size: 38,
color: '#E1212B',
alpha: 1,
textAlign: 'left',
textBaseline: 'middle',
fontWeight: 'bold',
fontFamily: 'OPPOSANS',
dx: 30,
dy: 860
},
{
type: 'text',
text: `¥${that.posterInfo.original_price}`,
size: 28,
color: '#999999',
alpha: 1,
textAlign: 'left',
textBaseline: 'top',
lineThrough: {
style: '#999999'
},
dx: 400,
dy: 860
},
// #ifdef MP-WEIXIN
...that.getWxCodeDrawItems({ dx: 522, dy: 911, size: 110 }),
// #endif
// #ifndef MP-WEIXIN
{
type: 'qrcode',
text: that.shareInfo.copyLink,
size: 110,
dx: 530,
dy: 930,
image: that.getWxCodeCenterLogoUrl(),
imageSize: 30
}
// #endif
]
};
break;
case 'groupon':
data = {
backgroundImage: that.initShare.groupon_poster_bg,
drawArray: [{
name: 'avatar',
type: 'image',
url: that.getPosterAvatarUrl(),
alpha: 1,
dy: 40,
dx: 38,
dWidth: 80,
dHeight: 80,
circleSet: {}
},
{
type: 'text',
text: that.userInfo.nickname,
size: 28,
color: '#333',
alpha: 1,
textAlign: 'middle',
textBaseline: 'bottom',
dx: 140,
dy: 40
},
{
type: 'text',
text: '发现一个好物,快来和我一起拼吧!',
size: 26,
color: '#333',
alpha: 1,
textAlign: 'middle',
textBaseline: 'middle',
dx: 140,
dy: 80
},
{
name: 'goodsImage',
type: 'image',
url: that.posterInfo.goods.image,
alpha: 1,
isBgCenter: true,
dy: 140,
dWidth: 620,
dHeight: 620,
roundRectSet: {
r: 20
}
},
{
type: 'text', //标题
text: that.posterInfo.goods.title,
size: 28,
color: '#333',
alpha: 1,
textAlign: 'left',
textBaseline: 'middle',
fontWeight: 'bold',
lineFeed: {
maxWidth: 620,
lineHeight: 40,
lineNum: 2
},
dx: 36,
dy: 780
},
{
type: 'text',
text: `拼团价:¥${that.posterInfo.goods.groupon_price}`,
size: 32,
color: '#E1212B',
alpha: 1,
textAlign: 'left',
textBaseline: 'middle',
fontWeight: 'bold',
fontFamily: 'OPPOSANS',
dx: 30,
dy: 860
},
{
type: 'text',
text: `${that.posterInfo.num}人团`,
size: 24,
color: '#fff',
alpha: 1,
textAlign: 'left',
textBaseline: 'top',
dx: 565,
dy: 863
},
// #ifdef MP-WEIXIN
...that.getWxCodeDrawItems({ dx: 530, dy: 930, size: 110 }),
// #endif
// #ifndef MP-WEIXIN
{
type: 'qrcode',
text: that.shareInfo.path,
size: 110,
dx: 530,
dy: 930,
image: that.getWxCodeCenterLogoUrl(),
imageSize: 30
}
// #endif
]
};
break;
default:
console.log('%cerr:没有此类型海报数据', 'color:green;background:yellow');
break;
}
return data;
}
}
};
</script>
<style lang="scss">
// 指引
.guide-wrap {
height: 100%;
.guide-img {
width: 580rpx;
height: 430rpx;
}
}
.poster-dialog {
width: 640rpx;
padding: 20rpx;
box-sizing: border-box;
}
// 分享海报
.poster-btn-box {
.cancel-btn {
width: 300rpx;
height: 70rpx;
line-height: 70rpx;
background: #ffffff;
border-radius: 35rpx;
font-size: 28rpx;
font-weight: 500;
color: #999999;
}
.save-btn {
width: 300rpx;
height: 70rpx;
line-height: 70rpx;
background: $brand-gold-gradient;
border-radius: 35rpx;
font-size: 28rpx;
font-weight: 500;
color: #ffffff;
}
}
.poster-img-box {
.poster-img {
width: 660rpx;
min-height: 800rpx;
border-radius: 20rpx;
}
}
// 分享tool
.share-box {
background: #fff;
width: 750rpx;
border-radius: 30rpx 30rpx 0 0;
padding-top: 30rpx;
position: relative;
.share-foot {
font-size: 24rpx;
color: #bfbfbf;
height: 80rpx;
border-top: 1rpx solid #eee;
}
.share-list-box {
.share-btn {
background: none;
border: none;
line-height: 1;
padding: 0;
&::after {
border: none;
}
}
.share-item {
flex: 1;
padding-bottom: 20rpx;
.share-img {
width: 70rpx;
height: 70rpx;
background: rgba(246, 246, 254, 1);
border-radius: 50%;
margin-bottom: 20rpx;
}
.share-title {
font-size: 24rpx;
color: #666;
}
}
}
}
</style>