当前位置:   article > 正文

Taro+Vue3开发微信小程序的分享好友功能_vue3 onshareappmessage

vue3 onshareappmessage

1、背景:使用taro框架和vue3框架开发小程序,实现一个把pdf文件直接分享给微信好友。

一开始看taro文档理解有偏差,导致分享出去的文件是个app的小程序连接。如下图

 实现代码如下:

  1. onShareAppMessage: function () {
  2. Taro.showShareMenu({
  3. withShareTicket: true
  4. // 用户点击右上角分享给好友,要先在分享好友这里设置menus的两个参数,才可以分享朋友圈
  5. // menus: ['shareAppMessage', 'shareTimeline']
  6. });
  7. },
  8. js:
  9. <nut-button
  10. shape="square"
  11. class="bg-primary button mb-md"
  12. style="padding: 24rpx 146rpx"
  13. open-type="share" // 增加这个属性才能转发
  14. >分享至微信聊天</nut-button
  15. >

后续仔细查看taro文档, Taro 文档,发现这种写法是个页面组件,转发出去的是个小程序,无法单独将文件转发。

2、实现文件单独转发给好友:微信小程序下载文件和转发文件给好友总结_海海呐的博客-CSDN博客_小程序分享文件到微信

代码实现:

  1. <nut-button
  2. shape="square"
  3. class="bg-primary button mb-md"
  4. style="padding: 24rpx 146rpx"
  5. @tap="shareFile"
  6. :class="{ 'button-disable': isShare }"
  7. >分享至微信聊天</nut-button
  8. >
  9. shareFile() {
  10. if (this.isShare) {
  11. return false;
  12. }
  13. this.isShare = true;
  14. let that = this;
  15. const params = {
  16. startDateId: this.userValue.project.startDateId,
  17. endDateId: this.userValue.project.endDateId,
  18. fileType: 'pdf'
  19. };
  20. Taro.downloadFile({ // 先进行下载,接口返回后再分享文件
  21. filePath: Taro.env.USER_DATA_PATH + `/${that.projectName}`,
  22. url: `${config.serverUrl[getAppEnv()]}/watson/report/generateReport/${
  23. this.userValue.project.enterpriseId
  24. }?${queryToString(params)}`,
  25. header: {
  26. 'content-type': 'application/json',
  27. cookie: Taro.getStorageSync('cookieKey')
  28. },
  29. success(res) {
  30. if (res.statusCode === 200) {
  31. that.isShare = false;
  32. Taro.shareFileMessage({
  33. filePath: res.filePath,
  34. fileName: that.projectName,
  35. fail() {
  36. showToast('分享失败');
  37. }
  38. });
  39. }
  40. },
  41. fail() {
  42. showToast('分享失败');
  43. }
  44. });
  45. },

效果如下:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/777941
推荐阅读
相关标签
  

闽ICP备14008679号