我的业务流程是:查询订单详情,将查询出的订单以excel的形式分享到微信,其中excel是后端接口生成好的。开始由于我一直执着于只看uniapp的api,发现它的方法不支持分享文件,只能支持分享图片、小程序等,所以就有了曲线救国的第二个方法。第二个方法需要先预览,再通过预览中右上角的菜单转发分享操作,能实现初衷目标;用户体验感不是很好。后面去看了微信小程序开发文档,发现了转发 api ,这样一写,代码和操作都很简洁。代码如下:
<template>
<view class="wx_img">
<button class="button_2" @click="goShare">
<uni-icons type="weixin" color="#FFFFFF" size="25"></uni-icons>
<text class="button_text_20">分享订单</text>
</button>
</view>
</template>
第一种方法:
<script>
export default {
data() {
return {
excelUrl:'', // excel地址
};
},
mounted() {
},
onShow() {
},
onShareAppMessage(res) {
return {
title: '微信小程序测试分享',
}
},
methods: {
// 分享-预览
goShare() {
uni.downloadFile({
url: this.excelUrl,
success: (res) => {
uni.shareFileMessage({
filePath: res.tempFilePath,
})
}
})
})
},
}
}
</script>
第二种方法
<script>
export default {
data() {
return {
excelUrl:'', // excel地址
};
},
mounted() {
},
onShow() {
},
onShareAppMessage(res) { // 这个是uniapp的方法,但是无法分享文件,具体使用方法参考官方文档
return {
title: '微信小程序测试分享'
}
},
methods: {
// 分享-预览
goShare() {
const downloadTask = uni.downloadFile({
url: this.excelUrl,
success: function(res) {
var filePath = res.tempFilePath;
uni.openDocument({
filePath: filePath,
showMenu: true, // 这个要设置,显示右上角的菜单
success: function(res) {
console.log('打开文档成功');
}
});
}
});
downloadTask.onProgressUpdate((res) => {
console.log('下载进度' + res.progress);
console.log('已经下载的数据长度' + res.totalBytesWritten);
console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
});
})
},
}
}
</script>
总结一下:开发微信小程序,一定一定要uniapp官方文档和微信小程序开发文档都看。
文章讲述了在uniapp框架下开发微信小程序时,如何实现从后端接口获取Excel文件并分享。作者首先尝试了uniapp的分享API,但发现不支持分享文件,于是采取了预览然后通过预览界面的菜单进行转发的曲线救国方式。后来,作者发现微信小程序开发文档中的onShareAppMessage方法,通过结合uni.downloadFile和uni.openDocument,实现了更简洁的文件分享。作者强调在开发过程中应同时参考uniapp和微信小程序的官方文档。
1万+

被折叠的 条评论
为什么被折叠?



