uniapp后端合成分享海报

在这里插入图片描述

选择海报组件 share.vue

<template>
	<view class=''>
		<u-popup class="popup flex flex-column" height="790" v-model="isshow" mode="bottom" @close="close">
			<!-- 分享按钮 -->
			<view class="bg-white  fixed-bottom">
				<view class="center bb" style="height: 88rpx;">选择海报</view>
				<swiper class="swiper  u-p-t-20 u-p-l-50 u-p-r-50" :indicator-dots="false" :display-multiple-items="2">
					<!-- :style="{backgroundImage:'url('+item.img+')'}" -->
					<swiper-item class="item text-center relative" v-for="(item,i) in list" :key="i" @click="getPoster(i)">
						<image :src="item.img" mode=""></image>
						<image class="absolute radio" v-if="i==current" src="../static/share-active.png" mode=""></image>
						<image class="absolute radio" v-else src="../static/share-no-active.png" mode=""></image>
					</swiper-item>
				</swiper>
				<view class="flex1 u-p-30 bt u-m-t-20">
					<view class="u-flex-1 text-center" @click="toWx(1)">
						<image class="icon-img" src="../static/vx-icon.png" mode=""></image>
						<view class="gray-1 u-font-24">微信好友</view>
					</view>
					<view class="u-flex-1 text-center" @click="toWx(2)">
						<image class="icon-img" src="../static/moments-icon.png" mode=""></image>
						<view class="gray-1 u-font-24">微信朋友圈</view>
					</view>
					<view class="u-flex-1 text-center" @click="toSave()">
						<image class="icon-img" src="../static/share-save.png" mode=""></image>
						<view class="gray-1 u-font-24">保存图片</view>
					</view>
					<view class="u-flex-1 text-center" @click="toCopy()">
						<image class="icon-img" src="../static/link-icon.png" mode=""></image>
						<view class="gray-1 u-font-24">复制链接</view>
					</view>
				</view>
				<view class="center bt blue" style="height: 88rpx;" @click="close">取消</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		props: {
			isshow: {
				type: Boolean,
				value: false
			},
			code: {
				type: String,
				value: ''
			}
		},
		data() {
			return {
				current: 0,
				list: [{
						img: '../../static/share-1-zh.png'
					},
					{
						img: '../../static/share-2-zh.png'
					},
					{
						img: '../../static/share-3-zh.png'
					}
				],
				img: '', //合成的海报
			}
		},
		mounted() {

		},
		onUnload() {
			uni.hideLoading()
		},
		onShow() {
			if(this.$i18n.locale == "zh"){
				this.list[0].img = '../../static/share-1-zh.png'
				this.list[1].img = '../../static/share-2-zh.png'
				this.list[2].img = '../../static/share-3-zh.png'
			}else if(this.$i18n.locale == "hk"){
				this.list[0].img = '../../static/share-1-hk.png'
				this.list[1].img = '../../static/share-2-hk.png'
				this.list[2].img = '../../static/share-3-hk.png'
			}else{
				this.list[0].img = '../../static/share-1-en.png'
				this.list[1].img = '../../static/share-2-en.png'
				this.list[2].img = '../../static/share-3-en.png'
			}
		},
		methods: {
			getPoster(i) {
				this.current = i
			},
			close() {
				this.$emit('close')
			},
			toWx(type) {
				uni.showLoading({
					title: this.$t('share.toast3')
				})
				this.$http('/api/share_url', {
					type: this.current + 1
				}).then(data => {
					console.log(data)
					this.img = data
					uni.share({
						provider: "weixin",
						scene: type == 1 ? "WXSceneSession" : "WXSenceTimeline",
						type: 2,
						imageUrl: this.img,
						success: function(res) {
							uni.hideLoading()
							console.log("success:" + JSON.stringify(res));
						},
						fail: function(err) {
							uni.hideLoading()
							console.log("fail111:" + JSON.stringify(err));
						}
					});
				})
			},
			toSave() { //type: this.current + 1
				let _this = this
				this.$http('/api/share_url').then(data => {
					this.img = data
					uni.downloadFile({
						url: _this.img, //仅为示例,并非真实的资源
						success: (res) => {
							if (res.statusCode === 200) {
								_this.save(res.tempFilePath)
							}
						}
					});
				})
			},
			save(file) {
				uni.saveImageToPhotosAlbum({
					filePath: file,
					success: () => {
						uni.showToast({
							title: this.$t('share.toast1')
						})
					},
					fail: () => {
						uni.showToast({
							title: this.$t('share.toast2')
						})
					},
				});
			},
			toCopy() {
				uni.setClipboardData({
					data: "http://teex.xjdm88.com/webTeex/?invite=" + this.code,
					success: function() {
						uni.getClipboardData({
							success: function(res) {

							}
						});
					}
				});
			}
		},
	}
</script>

<style scoped lang='scss'>
	.popup {
		background-color: rgba(0, 0, 0, .1);
	}

	/* /deep/ .u-drawer-bottom {
		background-color: transparent !important;
	} */


	uni-swiper {
		height: 400rpx;

		image {
			height: 100%;
			width: 250rpx;
			border-radius: 10rpx;
		}
	}

	.item {
		background-repeat: no-repeat;
		background-position: center;
		background-size: contain;
		position: relative;

		.radio {
			width: 40rpx;
			height: 40rpx;
			bottom: 10rpx;
			right: 50rpx;
		}
	}

	.code-con {
		position: absolute;
		left: 50%;
		top: 64%;
		transform: translate(-50%, -50%);
		width: 400rpx;
		height: 380rpx;
		padding: 20rpx;
		border-radius: 10rpx;
		margin: 500rpx auto 0;
		text-align: center;
		padding: 50rpx;
	}

	.icon-img {
		width: 70rpx;
		height: 70rpx;
	}
</style>

页面中应用

// 全局注册
import share from "./components/share.vue";
Vue.component('share', share)
<!-- 分享 -->
 <share :isshow="showShare" :code="userInfo.invite_code" @close="closeShare"></share>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小曲曲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值