代码实现

index.vue

<template>
<div id="app">
<SelfSharePage v-if="share_data.title" :shareInfo="share_data" /> <!-- 引用公用分享文件 -->
</div>
</template>
data() {
return {
share_data:{//动态获取需要描述的内容
img: "",
title:'',
intro:'',
},
};
},
SelfSharePage.vue
<template>
<div> </div>
</template> <script>
import wx from 'weixin-js-sdk'//引用微信分享插件
import {shareUrlApi} from "../../api/index"//后端获取分享条件参数 export default {
props:['shareInfo'],
data() {
return {
img:'',
title:'',
intro:'',
link:'',
};
},
methods: {
getWeiXinShare(s_img,s_title,s_intro,s_link){
this.$axios.post(shareUrlApi,{
url:window.location.href.split("#")[0]
}).then((res)=>{
if(parseInt(res.data.errCode)>=0){
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: res.data.data.appId, // 必填,公众号的唯一标识
timestamp: res.data.data.timestamp, // 必填,生成签名的时间戳
nonceStr: res.data.data.nonceStr, // 必填,生成签名的随机串
signature: res.data.data.signature, // 必填,签名,见附录1
jsApiList: [
'onMenuShareTimeline',
'onMenuShareAppMessage',
]
}); wx.ready(function () {
//分享到朋友圈
wx.onMenuShareTimeline({
title: s_title, // 分享时的标题
// link: window.location.href.split("#")[0], // 分享时的链接
link:s_link,
imgUrl: s_img, // 分享时的图标
desc: s_intro,
success: function () {
console.log("分享成功");
},
cancel: function () {
console.log("取消分享");
}
});
//分享给朋友
wx.onMenuShareAppMessage({
title:s_title,
desc: s_intro,
link: s_link,
imgUrl: s_img,
type: '',
dataUrl: '',
success: function () {
console.log("分享成功");
},
cancel: function () {
console.log("取消分享");
}
}); })
}
else{ }
}).catch((err)=>{
console.log(err)
})
}
},
mounted() {
this.img=this.shareInfo.img?this.shareInfo.img:'参数为空时默认图片'
this.title=this.shareInfo.title?this.shareInfo.title:'参数为空时默认标题'
this.intro=this.shareInfo.intro?this.shareInfo.intro:'参数为空时默认说明'
this.link=this.shareInfo.link?this.shareInfo.link:window.location.href.split("#")[0]
this.getWeiXinShare(this.img,this.title,this.intro,this.link)
}
};
</script> <style scoped> </style>

 

最新文章

  1. ubuntu14.04redis安装以及扩展
  2. 4. SVM分类器求解(2)
  3. Informatica - Powercenter 英文版资料(转载)
  4. Objective-C基础
  5. 转(linux shell)(2)
  6. 安装 Python
  7. Kali vmtools
  8. WPF中使用ValueConverter来实现“范围条件触发器”
  9. macos下使用rvm管理ruby版本和rails版本
  10. 016-类与对象-OC笔记
  11. [Swift]LeetCode703. 数据流中的第K大元素 | Kth Largest Element in a Stream
  12. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.love5/com.example.love5.Main11Activity}: java.lang.NullPointerException: Attempt to invoke virtual method &#39;void android.
  13. java格式化
  14. .net EF之CodeFirst代码先行(转)
  15. uio.c 分析【转】
  16. Java-JUC(十):线程按序交替执行
  17. Mac系统常用快捷键
  18. 使用EntityFramework6完成增删查改CRUD和事务
  19. JS(微信小程序)处理银行卡号
  20. iOS自学-监听按钮点击、提醒框

热门文章

  1. CentOS下下查看硬盘型号、大小等信息(含Raid)
  2. BT做种
  3. StarRC 转XRC flow
  4. unity简单物理系统
  5. Pycharm报错:Error running ‘‘: Cannot run program “\python.exe“ (in directory ““)系统找不到指定文件夹?已解决!
  6. docker-compose任务编排
  7. Go组件库总结之介入式链表
  8. PR如何导出mp4格式的视频
  9. 关于protobuf报错&#39;tr1/unordered_map&#39; file not found
  10. VS2019使用Qt4.8.7