【uniapp小程序-分享】

//分享到聊天
		onShareAppMessage() {
			let shareMessage = {
				title: this.liveInfo.wx_title,
				path: '/subPages/livePages/liveCourse/live_course_info?courseid=' +
					this.courseid,
				imageUrl: this.liveInfo.wx_thumb
			};
		
			let shearMsg = uni.getStorageSync('shearImg' + this.courseid);
			if (shearMsg && shearMsg.id == this.courseid) {
				return {
					title: shareMessage.title,
					path: shareMessage.path,
					imageUrl: `${this.liveInfo.wx_thumb}?imageView2/1/w/640/h/360`|| shearMsg.imgPath
				};
			} else {
				uni.showLoading({
					title: '分享图片生成中...',
					icon: 'none'
				});
		
				return new Promise((resolve, reject) => {
					this.shareCode()
						.then(res => {
							uni.hideLoading();
							let img = res 
							uni.hideLoading();
							resolve({
								title: shareMessage.title,
								path: shareMessage.path,
								imageUrl: img
							});
							// 将分享图片加入缓存
							let msg = {
								imgPath: img,
								id: this.courseid
							};
							uni.setStorageSync('shearImg' + this.courseid, msg);
						})
						.catch(err => {
							uni.hideLoading();
							resolve({
								title: shareMessage.title,
								path: shareMessage.path,
								imageUrl: imgPath
							});
						});
				});
			}
		},
		// 分享到朋友圈
		onShareTimeline() {
			return {
				title: this.liveInfo.name,
				path: '/subPages/livePages/liveCourse/live_course_info?courseid=' +
					this.courseid,
				imageUrl: this.liveInfo.wx_thumb
			};
		},

methods:{
	// 分享卡片
			shareCode() {
				let _this = this;
				return new Promise(async (resolve, reject) => {
					const ctx = uni.createCanvasContext('shareCanvas', this);
					let bg = this.liveInfo.wx_thumb
					let bt = '背景图';
					ctx.draw();
					ctx.save();
					await uni
						.getImageInfo({
							src: bg
						})
						.then(res => {
							if (res[1].errMsg == 'getImageInfo:ok') {
								ctx.save();
								ctx.drawImage(res[1].path, 0, 0, 195, 160);
								ctx.restore();
							} else {
								uni.$u.toast('海报生成失败');
							}
						});
			
					ctx.restore();
					// 生成图片
					ctx.draw(true, ret => {
						setTimeout(() => {
							uni.canvasToTempFilePath(
								{
									canvasId: 'shareCanvas',
									success: res => {
										resolve(res.tempFilePath);
									},
									fail: err => {
										uni.$u.toast('分享图片生成失败');
									}
								},
								_this
							);
						}, 10);
					});
				});
			},
			textPrewrap(ctx, content, drawX, drawY, lineHeight, lineMaxWidth, lineNum) {
				var drawTxt = ''; // 当前绘制的内容
				var drawLine = 1; // 第几行开始绘制
				var drawIndex = 0; // 当前绘制内容的索引
				// 判断内容是否可以一行绘制完毕
				if (ctx.measureText(content).width <= lineMaxWidth) {
					ctx.fillText(content.substring(drawIndex, i), drawX, drawY);
				} else {
					for (var i = 0; i < content.length; i++) {
						drawTxt += content[i];
						if (ctx.measureText(drawTxt).width >= lineMaxWidth) {
							if (drawLine >= lineNum) {
								ctx.fillText(content.substring(drawIndex, i) + '..', drawX, drawY);
								break;
							} else {
								ctx.fillText(content.substring(drawIndex, i + 1), drawX, drawY);
								drawIndex = i + 1;
								drawLine += 1;
								drawY += lineHeight;
								drawTxt = '';
							}
						} else {
							// 内容绘制完毕,但是剩下的内容宽度不到lineMaxWidth
							if (i === content.length - 1) {
								ctx.fillText(content.substring(drawIndex), drawX, drawY);
							}
						}
					}
				}
			},
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值