wxShare.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import wx from "@/utils/weixin.js"
  2. /**
  3. * 微信js-sdk
  4. * 参考文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115
  5. */
  6. const wxShare = {
  7. /**
  8. * [wxRegister 微信Api初始化]
  9. * @param {Function} callback [ready回调函数]
  10. */
  11. wxRegister(data,option) { //data是微信配置信息,option是分享的配置内容
  12. console.log('data==', data)
  13. wx.config({
  14. debug: false, // 开启调试模式
  15. appId: data.appId, // 必填,公众号的唯一标识
  16. timestamp: data.timestamp, // 必填,生成签名的时间戳
  17. nonceStr: data.nonceStr, // 必填,生成签名的随机串
  18. signature: data.signature,// 必填,签名,见附录1
  19. jsApiList: [
  20. 'onMenuShareTimeline',
  21. 'onMenuShareAppMessage'
  22. ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  23. });
  24. wx.ready(function(){
  25. console.log('option==', option)
  26. //分享到朋友圈的
  27. wx.onMenuShareTimeline({
  28. title: option.title, // 分享标题
  29. desc: option.summary, //.desc, // 分享描述
  30. link: option.link, // 分享链接
  31. imgUrl: option.imgUrl, // 分享图标
  32. success: function () {
  33. console.log('ok');
  34. }
  35. });
  36. wx.onMenuShareAppMessage({
  37. title: option.title, // 分享标题
  38. desc: option.summary, //.desc, // 分享描述
  39. link: option.link, // 分享链接
  40. imgUrl: option.imgUrl, // 分享图标
  41. success() {
  42. // 用户成功分享后执行的回调函数
  43. // option.success()
  44. console.log('ok');
  45. },
  46. cancel() {
  47. // 用户取消分享后执行的回调函数
  48. // option.error()
  49. console.log('cancel');
  50. }
  51. });
  52. });
  53. wx.error(function(res){
  54. // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
  55. //alert('error:'+JSON.stringify(res));
  56. });
  57. }
  58. }
  59. export default wxShare