【无标题】

一、微信小程序中的分享

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' });
			    }
			  });
			},
相关推荐
小羊Yveesss2 小时前
2026年微信小程序后端怎么搭建?后台、数据、接口和运维怎么选
运维·微信小程序·小程序
小皮虾2 小时前
小程序首页性能优化实战:从 4 秒到 1.8 秒
前端·微信小程序
衍生星球2 天前
SpringBoot3 + Vue3 + 微信小程序如何学习,以及学哪些技术,组件
sql·微信小程序·uni-app·vue·springboot
新华财经频道2 天前
2026微信小程序搭建平台实测测评,优缺点解析
微信小程序·小程序
kidding7233 天前
旋转大转盘小程序
前端·css·微信小程序·小程序·前端框架
这是个栗子5 天前
uni-app 微信小程序开发:常用事件指令(@xxx)(一)
微信小程序·小程序·uni-app
博客zhu虎康7 天前
小程序:微信小程序连接打印机蓝牙打印全攻略
微信小程序·小程序·notepad++
tcdos7 天前
不止扫码 — 微信生态深度融合(登录 + 支付 + 消息)
后端·微信小程序
小徐_23338 天前
Wot UI 2.2.0 发布:Button 新增 subtle,VideoPreview 预览体验继续增强
前端·微信小程序·uni-app
蜗牛前端10 天前
codex 全流程开发上线的高颜值礼簿小程序
前端·微信小程序