【无标题】

一、微信小程序中的分享

1、分享图片

css 复制代码
<view class="share-btn" @click="shareToWechat('file')">分享图片</view>
javascript 复制代码
async shareToWechat(type) {
			  if (type !== 'file') return;
			  // this.rptPDF
			  if (!this.rptPDF) {
			    uni.showToast({ title: '图片不存在', icon: 'none' });
			    return;
			  }
			
			  uni.showLoading({ title: '准备分享...' });
			  // this.rptImgName
			  uni.downloadFile({
			  	url: this.rptPDF, //下载地址接口返回
			  	success: (data) => {
			  		if (data.statusCode === 200) {
			  			// 2. 弹出和长按完全一样的微信原生分享菜单
			  			 wx.showShareImageMenu({
			  			  path: data.tempFilePath, // 必须是临时/本地路径
			  			  needShowEntrance: true // 分享后带小程序入口(和长按一致)
			  			});
			  		}
			  	},
			  	fail: (err) => {
			  		console.log(err);
			  		uni.showToast({
			  			icon: 'none',
			  			mask: true,
			  			title: '失败请重新下载',
			  		});
			  	},
			  });
			},

2、分享文件、例如pdf

css 复制代码
<view class="share-btn" @click="previewPdf()">分享文件</view>
javascript 复制代码
previewPdf() {
			  if (!this.rptPDF) {
			    uni.showToast({ title: 'PDF地址异常', icon: 'none' });
			    return;
			  }
			  uni.showLoading({ title: '加载中...' });
			  uni.downloadFile({
			    url: this.rptPDF,
			    success: (res) => {
			      uni.hideLoading();
			      if (res.statusCode === 200) {
					// 直接发 PDF 文件给微信好友
					  console.log('res.tempFilePath',res.tempFilePath)
					  wx.shareFileMessage({
					    filePath: res.tempFilePath, // 必须是本地临时路径
					    fileName: '申报单.pdf',    // 好友聊天里显示的文件名
					    success() {
					      uni.showToast({ title: '分享成功', icon: 'success' });
					    },
					    fail(err) {
					      console.error('分享失败:', err);
					      uni.showToast({ title: '分享失败', icon: 'none' });
					    }
					  });
			      } else {
			        uni.showToast({ title: '下载失败', icon: 'none' });
			      }
			    },
			    fail: () => {
			      uni.hideLoading();
			      uni.showToast({ title: '网络异常', icon: 'none' });
			    }
			  });
			},
相关推荐
spmcor21 小时前
微信小程序 setStorageSync 踩坑实录:别让"顺手一存"变成"隐形炸弹"
微信小程序
用户4324281061141 天前
小程序埋点设计规范:如何设计可扩展的数据采集体系
微信小程序
玩烂小程序3 天前
微信小程序手串DIY功能开发实录:飞入动画 + 环形排布 + 拖拽换序 + 旋转查看 + 保存设计
微信小程序
何时梦醒3 天前
HTML5 Canvas 从入门到实战:手把手教你打造一款"打飞机"小游戏
微信小程序
master3363 天前
SSL 证书链问题导致微信小程序无法正常工作
网络协议·微信小程序·ssl
wuxia21184 天前
在5种环境中编写点击元素改变内容和颜色的JavaScript程序
javascript·微信小程序·vue·jquery·react
it-10244 天前
抖音快手短视频去水印微信小程序/一键去水印/小程序去水印接口代码
微信小程序·小程序·php
夏天测5 天前
微信小程序自动化漏洞挖掘流水线:从缓存提取到密钥验证全流程实战
python·网络安全·微信小程序·漏洞挖掘
it-10245 天前
微信小程序短视频去水印/抖音短视频去水印/免费去水印源码
微信小程序·小程序·视频去水印
kidding7236 天前
高效备忘清单工具类小程序
前端·计算机网络·微信小程序·小程序