shareWx.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // 分享模块
  2. var shareCls = {
  3. getWxCofing (shareBaseUrl){
  4. var url = location.href.split('#')[0];
  5. console.log(url,baseUrl + '/shareThumb.jpg')
  6. // 分享内容
  7. var shareObj = {
  8. title: "新年焕新血拼季", // 分享标题
  9. desc: '新年焕新血拼季', // 分享描述
  10. link: url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  11. imgUrl: baseUrl + '/shareThumb.jpg', // 分享图标
  12. success : function() {
  13. // 用户确认分享后执行的回调函数
  14. console.log("用户确认分享后执行的回调函数")
  15. }
  16. };
  17. //二次分享处理函数调用
  18. this.shareAgainFn(shareObj.link);
  19. $.ajax({
  20. url: "https://wx.test.chelingzhu.com/clzwx/redirect/jsapi",
  21. type:'get',
  22. dataType:'json',
  23. headers: {
  24. 'X-HEADER-APPID': '!z2Yc.aes|j/r6hk33Kyyfehctv+KGW0HnQCXBK61W5sJRNoN9JqM='
  25. },
  26. data: {
  27. url: url
  28. },
  29. success:function(res){
  30. console.log(res,'jsapi')
  31. var wxData = res
  32. wx.config({
  33. //debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  34. appId: wxData.appId, // 必填,公众号的唯一标识
  35. timestamp: wxData.timestamp, // 必填,生成签名的时间戳
  36. nonceStr: wxData.nonceStr, // 必填,生成签名的随机串
  37. signature: wxData.signature,// 必填,签名
  38. jsApiList: [
  39. 'checkJsApi',
  40. 'onMenuShareAppMessage',
  41. 'onMenuShareTimeline',
  42. 'updateAppMessageShareData',
  43. 'updateTimelineShareData',
  44. 'getLocation',
  45. 'openLocation'
  46. ] // 必填,需要使用的JS接口列表
  47. });
  48. // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
  49. wx.error(function(res){
  50. console.log(res,'wx error')
  51. });
  52. //需在用户可能点击分享按钮前就先调用
  53. wx.ready(function () {
  54. if(wx.onMenuShareAppMessage){
  55. //分享给朋友
  56. wx.onMenuShareAppMessage(shareObj);
  57. }
  58. if(wx.onMenuShareTimeline){
  59. // 分享到朋友圈
  60. wx.onMenuShareTimeline(shareObj);
  61. }
  62. if(wx.updateAppMessageShareData){
  63. // 自定义“分享给朋友”及“分享到QQ”按钮的分享内容
  64. wx.updateAppMessageShareData(shareObj)
  65. }
  66. if(wx.updateTimelineShareData){
  67. // 自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容
  68. wx.updateTimelineShareData(shareObj)
  69. }
  70. if(location.href.indexOf("store.html")>0){
  71. wx.getLocation({
  72. type: 'gcj02', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
  73. success: function (res) {
  74. console.log(res, '地理位置')
  75. vm.lng = res.longitude
  76. vm.lat = res.latitude
  77. vm.getData()
  78. },
  79. cancel: function (res) {
  80. vm.getData()
  81. },
  82. fail: function (res) {
  83. vm.getData()
  84. }
  85. });
  86. }
  87. });
  88. }
  89. })
  90. },
  91. getQueryString(name){
  92. let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  93. let r = window.location.search.substr(1).match(reg);
  94. if (r != null)
  95. return unescape(r[2]);
  96. return null;
  97. },
  98. shareAgainFn(url){
  99. let from = this.getQueryString('from');
  100. let appinstall = this.getQueryString('appinstall');
  101. let sec = this.getQueryString('sec');
  102. let timekey = this.getQueryString('timekey');
  103. if (from || appinstall || sec || timekey) {
  104. window.location.href = url;
  105. }
  106. }
  107. }