wxApi.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // #ifdef H5
  2. import wx from 'weixin-js-sdk'
  3. const wxApi = {
  4. /*
  5. * [isweixin 判断是否微信浏览器]
  6. */
  7. isweixin() {
  8. const ua = window.navigator.userAgent.toLowerCase()
  9. if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  10. return true
  11. } else {
  12. return false
  13. }
  14. },
  15. /*
  16. * getConfigRes: 获取config微信配置
  17. */
  18. getConfigRes(title, imgUrl, desc) {
  19. // TODO:
  20. let that = this;
  21. let url = window.location.href.split('#')[0] + '?#' + window.location.href.split('#')[1];
  22. // jssdkConfig(window.location.href.split('#')[0]).then(res => {
  23. // if (res) {
  24. // let wechatConfig = res.config;
  25. // that.wxRegister(wechatConfig, title, imgUrl, desc);
  26. // }
  27. // })
  28. },
  29. /*
  30. * [wxRegister 微信Api初始化]
  31. * @param {Function} callback [ready回调函数]
  32. */
  33. wxRegister(config, title, imgUrl, desc, url, callback) {
  34. wx.config({
  35. debug: false, // 开启调试模式
  36. appId: config.app_id, // 必填,公众号的唯一标识
  37. timestamp: config.timestamp, // 必填,生成签名的时间戳
  38. nonceStr: config.nonceStr, // 必填,生成签名的随机串
  39. signature: config.signature, // 必填,签名,见附录1
  40. jsApiList: [
  41. 'onMenuShareTimeline', // 获取“分享到朋友圈”按钮点击状态及自定义分享内容接口
  42. 'onMenuShareAppMessage', // 获取“分享给朋友”按钮点击状态及自定义分享内容接口
  43. 'onMenuShareQQ', // 获取“分享到QQ”按钮点击状态及自定义分享内容接口
  44. 'onMenuShareWeibo' // 获取“分享到微博”按钮点击状态及自定义分享内容接口
  45. ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  46. })
  47. wx.ready(function () {
  48. imgUrl = imgUrl ? imgUrl : '';
  49. let link = url
  50. desc = desc ? desc : '暂无描述';
  51. wx.onMenuShareAppMessage({
  52. title: title, // 分享标题
  53. imgUrl: imgUrl, // 分享图标
  54. desc: desc, // 分享描述
  55. link: link,
  56. success: function(e) {
  57. },
  58. cancel: function(e) {
  59. },
  60. error: function (e) {
  61. }
  62. })
  63. wx.onMenuShareTimeline({
  64. title: title, // 分享标题
  65. link: link, // 分享链接
  66. imgUrl: imgUrl, // 分享图标
  67. success(e) {
  68. // 用户成功分享后执行的回调函数
  69. },
  70. cancel(e) {
  71. // 用户取消分享后执行的回调函数
  72. },
  73. error(e) {
  74. }
  75. })
  76. wx.onMenuShareQQ({
  77. title: title, // 分享标题
  78. link: link, // 分享链接
  79. imgUrl: imgUrl, // 分享图标
  80. desc: desc,
  81. success() {
  82. // 用户成功分享后执行的回调函数
  83. },
  84. cancel() {
  85. // 用户取消分享后执行的回调函数
  86. }
  87. })
  88. wx.onMenuShareWeibo({
  89. title: title, // 分享标题
  90. link: link, // 分享链接
  91. imgUrl: imgUrl, // 分享图标
  92. success() {
  93. // 用户成功分享后执行的回调函数
  94. },
  95. cancel() {
  96. // 用户取消分享后执行的回调函数
  97. }
  98. })
  99. });
  100. wx.error(res => {
  101. // alert('验证失败的信息:' + res.errMsg);
  102. console.log(res, 'error');
  103. });
  104. },
  105. redirectUrl(url) {
  106. return location.origin + location.pathname + encodeURIComponent(url);
  107. }
  108. }
  109. export default wxApi
  110. // #endif
  111. // #ifndef H5
  112. export default {}
  113. // #endif