js代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | var app = {}; app.appId= "" ; app.jsApiTicket= "" ; app.sign = "" ; app.nonceStr= "" ; app.timeStamp= "" ; app.func={}; var logoUrl = "http://ysj.weilaila.com:8080/demo/img/baby_logo.jpg" ; //生成随机字符串 app.func.createRandomStr= function (num){ var s = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM" ; var r = []; var x = s.split( "" ); for ( var i = 0; i<num; i++){ var random = parseInt(Math.random()*62); r.push(x[random]); } return r.join( "" ); }; app.func.getUrl = function (){ return location.href.split( "#" )[0]; } //得到js api 接口凭证 app.func.getConfigInfo= function (){ $.ajax({ type: "POST" , url: "user/getjsapisign.jhtml" , data:{ url:app.func.getUrl(), nonceStr:app.func.createRandomStr() }, async: false , success: function (data){ if (data.status== "ok" ){ app.jsApiTicket = data.jsApiTicket; app.appId=data.appId; app.sign=data.sign; app.nonceStr = data.nonceStr; app.timeStamp = data.timeStamp; app.func.wxconfig(); } else { window.location.reload(); } }, error: function (){ alert( "网络出错,请检查你的网络" ); window.location.reload(); } }); }; /*//生成时间戳 app.func.createTimeStamp=function(){ return Date.parse(new Date())/1000; };*/ /*//生成签名 app.func.createSign=function(obj){ var str=""; var param = ['jsapi_ticket','noncestr','timestamp','url']; param.sort(); for(var i = 0; i<param.length;i++){ str+=param[i]+"="+obj[param[i]]; if(i!=param.length-1){ str+="&" } } console.log(str); return CryptoJS.SHA1(str).toString(); }*/ app.func.wxconfig = function (){ wx.config({ debug: false , appId:app.appId, timestamp:app.timeStamp, nonceStr:app.nonceStr, signature:app.sign, jsApiList:[ 'onMenuShareTimeline' , 'onMenuShareAppMessage' , 'hideOptionMenu' , 'showOptionMenu' , 'hideMenuItems' , 'showMenuItems' , 'closeWindow' ] }); wx.ready( function (){ wx.checkJsApi({ jsApiList:[ 'onMenuShareTimeline' , 'onMenuShareAppMessage' ], success: function (res){ if (!res.checkResult.onMenuShareAppMessage||!res.checkResult.onMenuShareTimeline){ alert( '你的微信客户端不支持JSSDK,请升级你的客户端' ); } } }); //*********这里的这些值,可以写死。****************** wx.onMenuShareTimeline({ title: '长得好看的人都来参加花式裹孩纸大赛了,赢取IPHONE6S,不服来战!' , //分享标题 link: 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxec524716a0216797&redirect_uri=http%3a%2f%2fysj.weilaila.com%3a8080%2fdemo%2findex.jhtml&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect' ,//分享链接 imgUrl:logoUrl, // type: 'link' , //type是music或者video,link success: function (){ //以下是给用户加积分的函数请求 addNum(); } }); wx.onMenuShareAppMessage({ title: '长得好看的人都来参加花式裹孩纸大赛了,赢取IPHONE6S,不服来战!' , desc: '据说只要眼疾手快,你就可以赢得大奖,走向人生巅峰' , //分享描述 link: 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxec524716a0216797&redirect_uri=http%3a%2f%2fysj.weilaila.com%3a8080%2fdemo%2findex.jhtml&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect' , imgUrl:logoUrl, success: function (){ addNum(); } }); function addNum(num){ var addscroe = 5; $.ajax({ type: "POST" , data:{ score:addscroe, type:1 }, url: "user/addscore.jhtml" , async: true , success: function (data){ alert( "分享成功!" ); if (data.error) window.location.href = "index/rank.jhtml?score=0" ; else window.location.href = "index/rank.jhtml?score=" +addscroe; }, error: function (){ alert( '抱歉,网络出错,请重新分享' ); } }); } }); } app.func.getConfigInfo(); |
在需要调用JS接口的页面引入如下JS文件,(支持https):http://res.wx.qq.com/open/js/jweixin-1.0.0.js 请注意,如果你的页面启用了https,务必引入 https://res.wx.qq.com/open/js/jweixin-1.0.0.js ,否则将无法在iOS9.0以上系统中成功使用JSSDK 如需使用摇一摇周边功能,请引入 jweixin-1.1.0.js 备注:支持使用 AMD/CMD 标准模块加载方法加载
微信在连接上自动添加参数,ios和Android不一样,导致验证的连接加密结果不同,(android只有一个参数,ios有两个参数)
如果你的连接上有其他的参数,需要对照参数名进行过滤
1 2 | if (location.search) location.href=location.origin+location.pathname; |