【无标题】

一、微信小程序中的分享

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' });
			    }
			  });
			},
相关推荐
MrFlySand_飞沙7 小时前
uniapp开发微信小程序实现接入企业微信客服
微信小程序·小程序·uni-app·企业微信
码艺-Alimjan7 小时前
JWT 的本质、误区与正确落地:一套不依赖任何框架的架构方法论-微信小程序代码案例-Api 安全性拉满
微信小程序·架构·notepad++
禁止摆烂_才浅7 小时前
微信小程序高频面试题
前端·面试·微信小程序
show4338 小时前
2026微信小程序批量处理视频文件架构方案:免费批量实测
微信小程序·小程序·架构
wordbaby1 天前
从双端内卷到一库多端:基于 pnpm + Taro 4 + RNOH 鸿蒙的 Monorepo 跨端架构实战
前端·微信小程序·app
码云之上3 天前
小程序 iOS 多输入框焦点乱序脱坑记
ios·微信小程序·taro
Greg_Zhong4 天前
微信小程序 + 腾讯云人体分析:从 0 到 1 实现 AI 抠图打卡合照(细节待更新~)
人工智能·微信小程序·腾讯云·ai抠图-ai打卡拍照
java1234_小锋7 天前
【免费】基于Python的微信小程序网约车(打车,在线叫车,移动出行)系统(FastAPI+Vue3) 锋哥原创出品,必属精品
微信小程序·小程序·fastapi·网约车系统·在线叫车系统
拖孩8 天前
用 AI 重解千年观音灵签,做了一个微信小程序,每天摇一摇,命运给你回应
前端·后端·微信小程序
java1234_小锋8 天前
【免费】微信小程序网约车(打车,在线叫车,移动出行)系统(SpringBoot4+Vue3) 锋哥原创出品,必属精品
微信小程序·网约车·在线叫车·移动出行