【无标题】

一、微信小程序中的分享

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' });
			    }
			  });
			},
相关推荐
Brave & Real1 天前
小程序 const 在js中以及与同类的var和let之间的差异
javascript·微信小程序·小程序
silvia_Anne2 天前
微信小程序商品列表
微信小程序·小程序
ze^02 天前
Day05 APP应用&微信小程序&原生态开发&H5+Vue技术&封装打包&反编译抓包点
vue.js·微信小程序·小程序
用户8574824354802 天前
useList 通用列表管理hook
vue.js·微信小程序
陪小甜甜赏月3 天前
微信小程序分享onShareAppMessage
前端·微信小程序·小程序
ZC跨境爬虫3 天前
模块化烹饪小程序开发日记 Day7:(菜谱详情接口开发与JSON数据读取全流程)
前端·javascript·css·ui·微信小程序·json
AI砖家3 天前
微信小程序包体积优化与分包实战:从2M困境到优雅突破
微信小程序·小程序·notepad++·分包·小程序体积压缩
只要微微辣4 天前
Uniapp 微信小程序 Canvas画框标注:拖拽缩放全攻略
前端·微信小程序·uni-app·canvas·canva可画
爱学习的程序媛4 天前
微信小程序3D开发框架技术对比:XR-Frame与threejs-miniprogram
3d·微信小程序·小程序·前端框架