【无标题】

一、微信小程序中的分享

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' });
			    }
			  });
			},
相关推荐
AI工具人PM产品经理3 小时前
PIL 脚本批量生成小程序图标实战:零美工做出全套 tabBar
微信小程序·canvas·pil·python3.11·工具脚本
拖孩4 小时前
一个人 + AI 做的小程序,一个月赚了 36 块
前端·后端·微信小程序
DK185838322522 天前
【源码开源部署】电竞代练护航陪玩小程序:全游戏服务平台完整解决方案(附宝塔部署教程)
游戏·微信小程序·uni-app·开源·php
黑马程序员毕设2 天前
基于Java的仪器管理系统设计与实现
java·开发语言·spring boot·后端·微信小程序
bug总结2 天前
uniapp 微信小程序请求规范
微信小程序·小程序·uni-app
m0_462803883 天前
从信息管理视角看「按桌顺序分桌」:云分组优先分组的实现逻辑
微信小程序
bug总结4 天前
uniapp 微信小程序分包规范
微信小程序·小程序·uni-app
凉红茶4 天前
用Cursor开发微信小程序的第一天
微信小程序·小程序·ai编程
小马过河R4 天前
微信小程序自定义登录态维护:从入门到生产级落地
后端·微信小程序·小程序·架构·登录态
andrsted4 天前
用练题簿小程序 - 让错题不再白错
学习·微信小程序·小程序·学习方法