shareWx.js 3.8 KB

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