官方文档是微信JS-SDK的使用步骤http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#JSSDK.E4.BD.BF.E7.94.A8.E6.AD.A5.E9.AA.A4
,很多人在config接口注入权限验证配置的时候出现signature(签名无效),在这把自己开发的代码分享一下
一、验证权限我们需要获取四个参数appId、timestamp、nonceStr、signature
参数获取详细见官方文档:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD.951-JS-SDK.E4.BD.BF.E7.94.A8.E6.9D.83.E9.99.90.E7.AD.BE.E5.90.8D.E7.AE.97.E6.B3.95
注意:①access_token获取方法见我之前的的文章http://www.cnblogs.com/net-xiejun/p/4422890.html
②jsapi_ticket获取到jsapi_ticket有效时间为7200s,因为每天获取的jsapi_ticket数量有限,所以也必须缓存,方法见代码
CS代码:
1 #region 验证JsApi权限配置 2 /// <summary> 3 /// 获取JsApi权限配置的数组/四个参数 4 /// </summary> 5 /// <returns></returns> 6 public string GetJsApiInfo(string Appid, string Appsecret) 7 { 8 string timestamp = CommonMethod.ConvertDateTimeInt(DateTime.Now).ToString();//生成签名的时间戳 9 string nonceStr = CommonMethod.GetRandCode(16);//生成签名的随机串 10 string url = System.Web.HttpContext.Current.Request.Url.AbsoluteUri.ToString();//当前的地址 11 string jsapi_ticket = ""; 12 //ticket 缓存7200秒 13 if (System.Web.HttpContext.Current.Session["jsapi_ticket"] == null) 14 { 15 jsapi_ticket = CommonMethod.WebRequestPostOrGet("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + BasicApi.GetTokenSession(Appid, Appsecret) + "&type=jsapi", ""); 16 System.Web.HttpContext.Current.Session["jsapi_ticket"] = jsapi_ticket; 17 System.Web.HttpContext.Current.Session.Timeout = 7200; 18 } 19 else 20 { 21 jsapi_ticket = System.Web.HttpContext.Current.Session["jsapi_ticket"].ToString(); 22 } 23 Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(jsapi_ticket); 24 jsapi_ticket = respDic["ticket"].ToString();//获取ticket 25 string[] ArrayList = { "jsapi_ticket=" + jsapi_ticket, "timestamp=" + timestamp, "noncestr=" + nonceStr, "url=" + url }; 26 Array.Sort(ArrayList); 27 string signature = string.Join("&", ArrayList); 28 signature = FormsAuthentication.HashPasswordForStoringInConfigFile(signature, "SHA1").ToLower(); 29 return "{\"appId\":\"" + Appid + "\", \"timestamp\":" + timestamp + ",\"nonceStr\":\"" + nonceStr + "\",\"signature\":\"" + signature + "\"}"; 30 } 31 #endregion
二、js文件,页面多的话建议单独见一个js文件 每次引用js文件 就可以,代码如下:
1 /*! 2 * http://xxxxx.com/ 谢俊个人博客 3 * 使用前需要配置安全域名 在公众号后台微信功能设置 4 * 引用js文件 http://res.wx.qq.com/open/js/jweixin-1.0.0.js 还有 jquery.js 5 * Copyright 2015,谢俊 6 * 时间:2015年6月9号 10:18 7 */ 8 function wxconfig(data) { 9 wx.config({ 10 debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 11 appId: data.appId, // 必填,公众号的唯一标识 12 timestamp: data.timestamp, // 必填,生成签名的时间戳 13 nonceStr: data.nonceStr, // 必填,生成签名的随机串 14 signature: data.signature,// 必填,签名,见附录1 15 jsApiList: [ 16 ‘checkJsApi‘, 17 ‘onMenuShareTimeline‘, 18 ‘onMenuShareAppMessage‘, 19 ‘onMenuShareQQ‘, 20 ‘onMenuShareWeibo‘, 21 ‘hideMenuItems‘, 22 ‘showMenuItems‘, 23 ‘hideAllNonBaseMenuItem‘, 24 ‘showAllNonBaseMenuItem‘, 25 ‘translateVoice‘, 26 ‘startRecord‘, 27 ‘stopRecord‘, 28 ‘onRecordEnd‘, 29 ‘playVoice‘, 30 ‘pauseVoice‘, 31 ‘stopVoice‘, 32 ‘uploadVoice‘, 33 ‘downloadVoice‘, 34 ‘chooseImage‘, 35 ‘previewImage‘, 36 ‘uploadImage‘, 37 ‘downloadImage‘, 38 ‘getNetworkType‘, 39 ‘openLocation‘, 40 ‘getLocation‘, 41 ‘hideOptionMenu‘, 42 ‘showOptionMenu‘, 43 ‘closeWindow‘, 44 ‘scanQRCode‘, 45 ‘chooseWXPay‘, 46 ‘openProductSpecificView‘, 47 ‘addCard‘, 48 ‘chooseCard‘, 49 ‘openCard‘ 50 ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 51 }); 52 } 53 54 wx.ready(function () { 55 //是否支持js接口 56 document.querySelector(‘#checkJsApi‘).onclick = function () { 57 wx.checkJsApi({ 58 jsApiList: [ 59 ‘onMenuShareTimeline‘, 60 ‘onMenuShareAppMessage‘, 61 ‘onMenuShareQQ‘, 62 ‘onMenuShareWeibo‘ 63 ], 64 success: function (res) { 65 alert(JSON.stringify(res)); 66 } 67 }); 68 }; 69 })
三、页面调用,引用相关js文件后代码如下
<script> var data = ‘@Html.Raw(ViewData["JsApiirray"].ToString())‘; data = eval("(" + data + ")");//实例化 wxconfig(data); </script>
注:JsApiirray为后台调用GetJsApiInfo方法获取的json格式的字符串"{\"appId\":\"" + Appid + "\", \"timestamp\":" + timestamp + ",\"nonceStr\":\"" + nonceStr + "\",\"signature\":\"" + signature + "\"}";
如有疑问加我qq咨询405877988 微信开发群