share.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * Created by huosdk on 2018/10/9.
  3. */
  4. $(function () {
  5. // var reg = new RegExp('wx_app_id=([^&]*)[&|$]', 'g')
  6. // var exp = reg.exec(window.location.search)
  7. var wxAppId = $('#wxAppId').val()
  8. var shareTitle = $('#shareTitle').val()
  9. var shareDesc = $('#shareDesc').val()
  10. var shareImage = $('#shareImage').val()
  11. if (wxAppId) {
  12. $.ajax({
  13. url: 'https://miniapi.beibaoyx.com/share/index',
  14. data: {
  15. format: 'json',
  16. wx_app_id: wxAppId,
  17. share_url: location.href.split('#')[0]
  18. },
  19. dataType: 'jsonp',
  20. success: function (res) {
  21. if (res.code === 200) {
  22. wx.config({
  23. // debug: true,
  24. appId: res.data.wx_app_id,
  25. timestamp: res.data.timestamp,
  26. nonceStr: res.data.noncestr,
  27. signature: res.data.signature,
  28. // jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData', 'onMenuShareAppMessage']
  29. jsApiList: ['onMenuShareAppMessage', 'onMenuShareTimeline', 'onMenuShareQQ', 'onMenuShareQZone']
  30. })
  31. wx.ready(function () {
  32. wx.onMenuShareAppMessage({
  33. title: shareTitle, // 分享标题
  34. desc: shareDesc, // 分享描述
  35. link: window.location.href, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  36. imgUrl: shareImage, // 分享图标
  37. success: function () {
  38. // window.alert(1111)
  39. // 用户点击了分享后执行的回调函数
  40. }
  41. })
  42. wx.onMenuShareTimeline({
  43. title : shareTitle, // 分享标题
  44. link : window.location.href, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  45. imgUrl : shareImage, // 分享图标
  46. success: function () {
  47. // 用户点击了分享后执行的回调函数
  48. }
  49. })
  50. wx.onMenuShareQQ({
  51. title: shareTitle, // 分享标题
  52. desc: shareDesc, // 分享描述
  53. link: window.location.href, // 分享链接
  54. imgUrl: shareImage, // 分享图标
  55. success: function () {
  56. // 用户确认分享后执行的回调函数
  57. },
  58. cancel: function () {
  59. // 用户取消分享后执行的回调函数
  60. }
  61. })
  62. wx.onMenuShareQZone({
  63. title: shareTitle, // 分享标题
  64. desc: shareDesc, // 分享描述
  65. link: window.location.href, // 分享链接
  66. imgUrl: shareImage, // 分享图标
  67. success: function () {
  68. // 用户确认分享后执行的回调函数
  69. },
  70. cancel: function () {
  71. // 用户取消分享后执行的回调函数
  72. }
  73. })
  74. // // “分享给朋友”及“分享到QQ”
  75. // wx.updateAppMessageShareData({
  76. // title: 'test', // 分享标题
  77. // desc: 'testtest', // 分享描述
  78. // link: window.location.href, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  79. // imgUrl: 'https://ministatic.beibaoyx.com/upload/admin/20180929/63080d45597d524911a1ad484fc7ae39.png' // 分享图标
  80. // }, function(res) {
  81. // window.alert(JSON.stringify(res))
  82. // //这里是回调函数
  83. // })
  84. //
  85. // // “分享到朋友圈”及“分享到QQ空间”
  86. // wx.updateTimelineShareData({
  87. // title: shareTitle, // 分享标题
  88. // link: window.location.href, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  89. // imgUrl: shareImage // 分享图标
  90. // }, function(res) {
  91. // window.alert(JSON.stringify(res))
  92. // //这里是回调函数
  93. // })
  94. })
  95. }
  96. }
  97. })
  98. }
  99. })