wxshare.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. function toConfigure() {
  2. return new Promise((resolve, reject) => {
  3. $.ajax({
  4. url: '//www.4dage.com/wechat/jssdk/share',
  5. type: "get",
  6. data: {
  7. uri: location.href.split("#")[0],
  8. name: "厦门四维时代微信公众号"
  9. },
  10. dataType: "jsonp",
  11. //jsonpCallback: "success_jsonp",
  12. success: function(data, textStatus) {
  13. wx.config({
  14. debug: false,
  15. appId: data.appId,
  16. timestamp: data.timestamp,
  17. nonceStr: data.nonceStr,
  18. signature: data.signature,
  19. jsApiList: ['checkJsApi', 'onMenuShareTimeline',
  20. 'onMenuShareAppMessage', 'onMenuShareQQ',
  21. 'onMenuShareWeibo', 'hideMenuItems',
  22. 'showMenuItems', 'hideAllNonBaseMenuItem',
  23. 'showAllNonBaseMenuItem', 'translateVoice',
  24. 'startRecord', 'stopRecord', 'onRecordEnd',
  25. 'playVoice', 'pauseVoice', 'stopVoice',
  26. 'uploadVoice', 'downloadVoice', 'chooseImage',
  27. 'previewImage', 'uploadImage', 'downloadImage',
  28. 'getNetworkType', 'openLocation', 'getLocation',
  29. 'hideOptionMenu', 'showOptionMenu', 'closeWindow',
  30. 'scanQRCode', 'chooseWXPay',
  31. 'openProductSpecificView', 'addCard', 'chooseCard',
  32. 'openCard'
  33. ]
  34. });
  35. resolve()
  36. },
  37. error: function(XMLHttpRequest, textStatus, errorThrown) {
  38. reject("jsonp.error:" + textStatus)
  39. }
  40. });
  41. })
  42. }
  43. function strToJSON(search) {
  44. let args = search.substr(1).split('&')
  45. let obj = {}
  46. args.forEach(arg => {
  47. let index = arg.indexOf('=')
  48. if (!~index) {
  49. obj[arg] = null
  50. } else {
  51. obj[arg.substr(0, index)] = arg.substr(index + 1)
  52. }
  53. })
  54. return obj
  55. }
  56. function jsonToStr(data) {
  57. let strs = []
  58. Object.keys(data).forEach(k => {
  59. if (data[k] !== null) {
  60. strs.push(`${k}=${data[k]}`)
  61. }
  62. })
  63. return '?' + strs.join('&')
  64. }
  65. function setup({ title, link, imgUrl, desc }) {
  66. let defaultFn = () => {}
  67. let defaultChar = ''
  68. let search = link.substr(link.indexOf('?'))
  69. let path = link.substr(0, link.indexOf('?'))
  70. let data = strToJSON(search)
  71. wx.ready(function() {
  72. // 微信朋友圈
  73. data.open = 'wx_friends'
  74. wx.onMenuShareTimeline({ title, link: path + jsonToStr(data), imgUrl, desc });
  75. // 微信好友
  76. data.open = 'wx_friend'
  77. wx.onMenuShareAppMessage({ title, desc, link: path + jsonToStr(data), imgUrl, type: defaultChar, dataUrl: defaultChar })
  78. // 微博
  79. data.open = 'weibo'
  80. wx.onMenuShareWeibo({ title, desc, link: path + jsonToStr(data), imgUrl, success: defaultFn, cancel: defaultFn });
  81. // 空间
  82. data.open = 'qq_zone'
  83. wx.onMenuShareQZone({ title, desc, link: path + jsonToStr(data), imgUrl, success: defaultFn, cancel: defaultFn });
  84. // QQ好友
  85. data.open = 'qq'
  86. wx.onMenuShareQQ({ title, desc, link: path + jsonToStr(data), imgUrl, success: defaultFn, cancel: defaultFn });
  87. wx.error(function(e) {})
  88. })
  89. }
  90. export default async (args) => {
  91. if (typeof wx != "undefined") { //需要引入 https://res.wx.qq.com/open/js/jweixin-1.2.0.js
  92. await toConfigure()
  93. setup(args)
  94. }
  95. }