引言:
工作中開發微信網站,簡稱微網站。由於微網站的分享內容是系統自動選取的目前網址,客戶需要改變分享的內容,也就是點擊螢幕右上角的分享按鈕,選擇傳送給朋友和傳送到朋友圈,其中的內容和圖片需要自訂。於是查找文檔微信JS-SDK說明文檔一文和網站眾多高手的經驗,大體知道了調用的步驟,但是具體如何調用才能成功卻是不了解的。經過一番試驗,終於成功地呼叫發送朋友和發送到朋友圈兩個接口,此處記錄調用的具體過程。
步驟一:引用js檔。
在需要呼叫JS介面的頁面引入如下JS文件,(支援https):http://res.wx.qq.com/open/js/jweixin-1.0.0.js
步驟二:透過config介面注入權限驗證設定
wx.config({ debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId: '', // 必填,公众号的唯一标识 timestamp: , // 必填,生成签名的时间戳 nonceStr: '', // 必填,生成签名的随机串 signature: '',// 必填,签名,见附录1 jsApiList: [] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 });
網上眾多網友也是這樣寫的,但是具體如果配卻談之甚少,接下來介紹本文是如何調用的。
debug和appId,都不用說,很簡單。
timespan產生簽章的時間戳記:
/// 631fb227578dfffda61e1fa4d04b7d25 /// 生成时间戳 /// 从 1970 年 1 月 1 日 00:00:00 至今的秒数,即当前的时间,且最终需要转换为字符串形式 /// 039f3e95db2a684c7b74365531eb6044 /// 2363942ed0d6cd3e85bae1dffa568116f7735d9f6a7af371769ab5c16d23b2f3 public string getTimestamp() { TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalSeconds).ToString(); }
nonceStr。
View Code/// 631fb227578dfffda61e1fa4d04b7d25 /// 生成随机字符串 /// 039f3e95db2a684c7b74365531eb6044 /// 2363942ed0d6cd3e85bae1dffa568116f7735d9f6a7af371769ab5c16d23b2f3 public string getNoncestr() { Random random = new Random(); return MD5Util.GetMD5(random.Next(1000).ToString(), "GBK"); }
singature簽章的產生比較麻煩。 首先產生取得access_token(
有效期7200秒,開發者必須在自己的服務全域快取access_token)
/// 631fb227578dfffda61e1fa4d04b7d25 /// MD5Util 的摘要说明。 /// 039f3e95db2a684c7b74365531eb6044 public class MD5Util { public MD5Util() { // // TODO: 在此处添加构造函数逻辑 // } /** 获取大写的MD5签名结果 */ public static string GetMD5(string encypStr, string charset) { string retStr; MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider(); //创建md5对象 byte[] inputBye; byte[] outputBye; //使用GB2312编码方式把字符串转化为字节数组. try { inputBye = Encoding.GetEncoding(charset).GetBytes(encypStr); } catch (Exception ex) { inputBye = Encoding.GetEncoding("GB2312").GetBytes(encypStr); } outputBye = m5.ComputeHash(inputBye); retStr = System.BitConverter.ToString(outputBye); retStr = retStr.Replace("-", "").ToUpper(); return retStr; } }
/// <summary>
/// MD5Util 的摘要说明。
/// </summary>
public class MD5Util
{
public MD5Util()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/** 获取大写的MD5签名结果 */
public static string GetMD5(string encypStr, string charset)
{
string retStr;
MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider();
//创建md5对象
byte[] inputBye;
byte[] outputBye;
//使用GB2312编码方式把字符串转化为字节数组.
try
{
inputBye = Encoding.GetEncoding(charset).GetBytes(encypStr);
}
catch (Exception ex)
{
inputBye = Encoding.GetEncoding("GB2312").GetBytes(encypStr);
}
outputBye = m5.ComputeHash(inputBye);
retStr = System.BitConverter.ToString(outputBye);
retStr = retStr.Replace("-", "").ToUpper();
return retStr;
}
}
public string Getaccesstoken() { string appid = System.Configuration.ConfigurationManager.AppSettings["WXZjAppID"].ToString(); string secret = System.Configuration.ConfigurationManager.AppSettings["WXZjAppSecret"].ToString(); string urljson = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret; string strjson = ""; UTF8Encoding encoding = new UTF8Encoding(); HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(urljson); myRequest.Method = "GET"; myRequest.ContentType = "application/x-www-form-urlencoded"; HttpWebResponse response; Stream responseStream; StreamReader reader; string srcString; response = myRequest.GetResponse() as HttpWebResponse; responseStream = response.GetResponseStream(); reader = new System.IO.StreamReader(responseStream, Encoding.UTF8); srcString = reader.ReadToEnd(); reader.Close(); if (srcString.Contains("access_token")) { //CommonJsonModel model = new CommonJsonModel(srcString); HP.CPS.BLL.WeiXin.CommonJsonModel model = new BLL.WeiXin.CommonJsonModel(srcString); strjson = model.GetValue("access_token"); Session["access_tokenzj"] = strjson; } return strjson; }
public class CommonJsonModelAnalyzer { protected string _GetKey(string rawjson) { if (string.IsNullOrEmpty(rawjson)) return rawjson; rawjson = rawjson.Trim(); string[] jsons = rawjson.Split(new char[] { ':' }); if (jsons.Length a1553b849066844e5dd92b24025343bd 0) builder.Remove(builder.Length - 1, 1); string value = builder.ToString(); if (value.StartsWith("\"")) value = value.Substring(1); if (value.EndsWith("\"")) value = value.Substring(0, value.Length - 1); return value; } protected List98c455a79ddfebb79781bff588e7b37e _GetCollection(string rawjson) { //[{},{}] List98c455a79ddfebb79781bff588e7b37e list = new List98c455a79ddfebb79781bff588e7b37e(); if (string.IsNullOrEmpty(rawjson)) return list; rawjson = rawjson.Trim(); StringBuilder builder = new StringBuilder(); int nestlevel = -1; int mnestlevel = -1; for (int i = 0; i 26a485ceddac2a4986e7745b4f11808b 0) list.Add(builder.ToString()); return list; } } public class CommonJsonModel : CommonJsonModelAnalyzer { private string rawjson; private bool isValue = false; private bool isModel = false; private bool isCollection = false; public CommonJsonModel(string rawjson) { this.rawjson = rawjson; if (string.IsNullOrEmpty(rawjson)) throw new Exception("missing rawjson"); rawjson = rawjson.Trim(); if (rawjson.StartsWith("{")) { isModel = true; } else if (rawjson.StartsWith("[")) { isCollection = true; } else { isValue = true; } } public string Rawjson { get { return rawjson; } } public bool IsValue() { return isValue; } public bool IsValue(string key) { if (!isModel) return false; if (string.IsNullOrEmpty(key)) return false; foreach (string subjson in base._GetCollection(this.rawjson)) { CommonJsonModel model = new CommonJsonModel(subjson); if (!model.IsValue()) continue; if (model.Key == key) { CommonJsonModel submodel = new CommonJsonModel(model.Value); return submodel.IsValue(); } } return false; } public bool IsModel() { return isModel; } public bool IsModel(string key) { if (!isModel) return false; if (string.IsNullOrEmpty(key)) return false; foreach (string subjson in base._GetCollection(this.rawjson)) { CommonJsonModel model = new CommonJsonModel(subjson); if (!model.IsValue()) continue; if (model.Key == key) { CommonJsonModel submodel = new CommonJsonModel(model.Value); return submodel.IsModel(); } } return false; } public bool IsCollection() { return isCollection; } public bool IsCollection(string key) { if (!isModel) return false; if (string.IsNullOrEmpty(key)) return false; foreach (string subjson in base._GetCollection(this.rawjson)) { CommonJsonModel model = new CommonJsonModel(subjson); if (!model.IsValue()) continue; if (model.Key == key) { CommonJsonModel submodel = new CommonJsonModel(model.Value); return submodel.IsCollection(); } } return false; } /// 631fb227578dfffda61e1fa4d04b7d25 /// 当模型是对象,返回拥有的key /// 039f3e95db2a684c7b74365531eb6044 /// 2363942ed0d6cd3e85bae1dffa568116f7735d9f6a7af371769ab5c16d23b2f3 public List98c455a79ddfebb79781bff588e7b37e GetKeys() { if (!isModel) return null; List98c455a79ddfebb79781bff588e7b37e list = new List98c455a79ddfebb79781bff588e7b37e(); foreach (string subjson in base._GetCollection(this.rawjson)) { string key = new CommonJsonModel(subjson).Key; if (!string.IsNullOrEmpty(key)) list.Add(key); } return list; } /// 631fb227578dfffda61e1fa4d04b7d25 /// 当模型是对象,key对应是值,则返回key对应的值 /// 039f3e95db2a684c7b74365531eb6044 /// 4b9508bee1b4e18a78c442a485ff2a938bb7487ae6a16a43571bc14c7fcf93c2 /// 2363942ed0d6cd3e85bae1dffa568116f7735d9f6a7af371769ab5c16d23b2f3 public string GetValue(string key) { if (!isModel) return null; if (string.IsNullOrEmpty(key)) return null; foreach (string subjson in base._GetCollection(this.rawjson)) { CommonJsonModel model = new CommonJsonModel(subjson); if (!model.IsValue()) continue; if (model.Key == key) return model.Value; } return null; } /// 631fb227578dfffda61e1fa4d04b7d25 /// 模型是对象,key对应是对象,返回key对应的对象 /// 039f3e95db2a684c7b74365531eb6044 /// 4b9508bee1b4e18a78c442a485ff2a938bb7487ae6a16a43571bc14c7fcf93c2 /// 2363942ed0d6cd3e85bae1dffa568116f7735d9f6a7af371769ab5c16d23b2f3 public CommonJsonModel GetModel(string key) { if (!isModel) return null; if (string.IsNullOrEmpty(key)) return null; foreach (string subjson in base._GetCollection(this.rawjson)) { CommonJsonModel model = new CommonJsonModel(subjson); if (!model.IsValue()) continue; if (model.Key == key) { CommonJsonModel submodel = new CommonJsonModel(model.Value); if (!submodel.IsModel()) return null; else return submodel; } } return null; } /// 631fb227578dfffda61e1fa4d04b7d25 /// 模型是对象,key对应是集合,返回集合 /// 039f3e95db2a684c7b74365531eb6044 /// 4b9508bee1b4e18a78c442a485ff2a938bb7487ae6a16a43571bc14c7fcf93c2 /// 2363942ed0d6cd3e85bae1dffa568116f7735d9f6a7af371769ab5c16d23b2f3 public CommonJsonModel GetCollection(string key) { if (!isModel) return null; if (string.IsNullOrEmpty(key)) return null; foreach (string subjson in base._GetCollection(this.rawjson)) { CommonJsonModel model = new CommonJsonModel(subjson); if (!model.IsValue()) continue; if (model.Key == key) { CommonJsonModel submodel = new CommonJsonModel(model.Value); if (!submodel.IsCollection()) return null; else return submodel; } } return null; } /// 631fb227578dfffda61e1fa4d04b7d25 /// 模型是集合,返回自身 /// 039f3e95db2a684c7b74365531eb6044 /// 2363942ed0d6cd3e85bae1dffa568116f7735d9f6a7af371769ab5c16d23b2f3 public Listd4f06880a8bb972f429fc35193731b86 GetCollection() { Listd4f06880a8bb972f429fc35193731b86 list = new Listd4f06880a8bb972f429fc35193731b86(); if (IsValue()) return list; foreach (string subjson in base._GetCollection(rawjson)) { list.Add(new CommonJsonModel(subjson)); } return list; } /// 631fb227578dfffda61e1fa4d04b7d25 /// 当模型是值对象,返回key /// 039f3e95db2a684c7b74365531eb6044 private string Key { get { if (IsValue()) return base._GetKey(rawjson); return null; } } /// 631fb227578dfffda61e1fa4d04b7d25 /// 当模型是值对象,返回value /// 039f3e95db2a684c7b74365531eb6044 private string Value { get { if (!IsValue()) return null; return base._GetValue(rawjson); } } }
public class CommonJsonModelAnalyzer { protected string _GetKey(string rawjson) { if (string.IsNullOrEmpty(rawjson)) return rawjson; rawjson = rawjson.Trim(); string[] jsons = rawjson.Split(new char[] { ':' }); if (jsons.Length < 2) return rawjson; return jsons[0].Replace("\"", "").Trim(); } protected string _GetValue(string rawjson) { if (string.IsNullOrEmpty(rawjson)) return rawjson; rawjson = rawjson.Trim(); string[] jsons = rawjson.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); if (jsons.Length < 2) return rawjson; StringBuilder builder = new StringBuilder(); for (int i = 1; i < jsons.Length; i++) { builder.Append(jsons[i]); builder.Append(":"); } if (builder.Length > 0) builder.Remove(builder.Length - 1, 1); string value = builder.ToString(); if (value.StartsWith("\"")) value = value.Substring(1); if (value.EndsWith("\"")) value = value.Substring(0, value.Length - 1); return value; } protected List<string> _GetCollection(string rawjson) { //[{},{}] List<string> list = new List<string>(); if (string.IsNullOrEmpty(rawjson)) return list; rawjson = rawjson.Trim(); StringBuilder builder = new StringBuilder(); int nestlevel = -1; int mnestlevel = -1; for (int i = 0; i < rawjson.Length; i++) { if (i == 0) continue; else if (i == rawjson.Length - 1) continue; char jsonchar = rawjson[i]; if (jsonchar == '{') { nestlevel++; } if (jsonchar == '}') { nestlevel--; } if (jsonchar == '[') { mnestlevel++; } if (jsonchar == ']') { mnestlevel--; } if (jsonchar == ',' && nestlevel == -1 && mnestlevel == -1) { list.Add(builder.ToString()); builder = new StringBuilder(); } else { builder.Append(jsonchar); } } if (builder.Length > 0) list.Add(builder.ToString()); return list; } } public class CommonJsonModel : CommonJsonModelAnalyzer { private string rawjson; private bool isValue = false; private bool isModel = false; private bool isCollection = false; public CommonJsonModel(string rawjson) { this.rawjson = rawjson; if (string.IsNullOrEmpty(rawjson)) throw new Exception("missing rawjson"); rawjson = rawjson.Trim(); if (rawjson.StartsWith("{")) { isModel = true; } else if (rawjson.StartsWith("[")) { isCollection = true; } else { isValue = true; } } public string Rawjson { get { return rawjson; } } public bool IsValue() { return isValue; } public bool IsValue(string key) { if (!isModel) return false; if (string.IsNullOrEmpty(key)) return false; foreach (string subjson in base._GetCollection(this.rawjson)) { CommonJsonModel model = new CommonJsonModel(subjson); if (!model.IsValue()) continue; if (model.Key == key) { CommonJsonModel submodel = new CommonJsonModel(model.Value); return submodel.IsValue(); } } return false; } public bool IsModel() { return isModel; } public bool IsModel(string key) { if (!isModel) return false; if (string.IsNullOrEmpty(key)) return false; foreach (string subjson in base._GetCollection(this.rawjson)) { CommonJsonModel model = new CommonJsonModel(subjson); if (!model.IsValue()) continue; if (model.Key == key) { CommonJsonModel submodel = new CommonJsonModel(model.Value); return submodel.IsModel(); } } return false; } public bool IsCollection() { return isCollection; } public bool IsCollection(string key) { if (!isModel) return false; if (string.IsNullOrEmpty(key)) return false; foreach (string subjson in base._GetCollection(this.rawjson)) { CommonJsonModel model = new CommonJsonModel(subjson); if (!model.IsValue()) continue; if (model.Key == key) { CommonJsonModel submodel = new CommonJsonModel(model.Value); return submodel.IsCollection(); } } return false; } /// <summary> /// 当模型是对象,返回拥有的key /// </summary> /// <returns></returns> public List<string> GetKeys() { if (!isModel) return null; List<string> list = new List<string>(); foreach (string subjson in base._GetCollection(this.rawjson)) { string key = new CommonJsonModel(subjson).Key; if (!string.IsNullOrEmpty(key)) list.Add(key); } return list; } /// <summary> /// 当模型是对象,key对应是值,则返回key对应的值 /// </summary> /// <param name="key"></param> /// <returns></returns> public string GetValue(string key) { if (!isModel) return null; if (string.IsNullOrEmpty(key)) return null; foreach (string subjson in base._GetCollection(this.rawjson)) { CommonJsonModel model = new CommonJsonModel(subjson); if (!model.IsValue()) continue; if (model.Key == key) return model.Value; } return null; } /// <summary> /// 模型是对象,key对应是对象,返回key对应的对象 /// </summary> /// <param name="key"></param> /// <returns></returns> public CommonJsonModel GetModel(string key) { if (!isModel) return null; if (string.IsNullOrEmpty(key)) return null; foreach (string subjson in base._GetCollection(this.rawjson)) { CommonJsonModel model = new CommonJsonModel(subjson); if (!model.IsValue()) continue; if (model.Key == key) { CommonJsonModel submodel = new CommonJsonModel(model.Value); if (!submodel.IsModel()) return null; else return submodel; } } return null; } /// <summary> /// 模型是对象,key对应是集合,返回集合 /// </summary> /// <param name="key"></param> /// <returns></returns> public CommonJsonModel GetCollection(string key) { if (!isModel) return null; if (string.IsNullOrEmpty(key)) return null; foreach (string subjson in base._GetCollection(this.rawjson)) { CommonJsonModel model = new CommonJsonModel(subjson); if (!model.IsValue()) continue; if (model.Key == key) { CommonJsonModel submodel = new CommonJsonModel(model.Value); if (!submodel.IsCollection()) return null; else return submodel; } } return null; } /// <summary> /// 模型是集合,返回自身 /// </summary> /// <returns></returns> public List<CommonJsonModel> GetCollection() { List<CommonJsonModel> list = new List<CommonJsonModel>(); if (IsValue()) return list; foreach (string subjson in base._GetCollection(rawjson)) { list.Add(new CommonJsonModel(subjson)); } return list; } /// <summary> /// 当模型是值对象,返回key /// </summary> private string Key { get { if (IsValue()) return base._GetKey(rawjson); return null; } } /// <summary> /// 当模型是值对象,返回value /// </summary> private string Value { get { if (!IsValue()) return null; return base._GetValue(rawjson); } } }
View Code
public string Getjsapi_ticket() { string accesstoken = (string)Session["access_tokenzj"]; string urljson = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + accesstoken + "&type=jsapi"; string strjson = ""; UTF8Encoding encoding = new UTF8Encoding(); HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(urljson); myRequest.Method = "GET"; myRequest.ContentType = "application/x-www-form-urlencoded"; HttpWebResponse response = myRequest.GetResponse() as HttpWebResponse; Stream responseStream = response.GetResponseStream(); StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8); string srcString = reader.ReadToEnd(); reader.Close(); if (srcString.Contains("ticket")) { HP.CPS.BLL.WeiXin.CommonJsonModel model = new BLL.WeiXin.CommonJsonModel(srcString); strjson = model.GetValue("ticket"); Session["ticketzj"] = strjson; } return strjson; }
用第一步拿到的access_token 採用http GET方式請求取得jsapi_ticket(ick
public string Getsignature(string nonceStr, string timespanstr) { if (Session["access_tokenzj"] == null) { Getaccesstoken(); } if (Session["ticketzj"] == null) { Getjsapi_ticket(); } string url = Request.Url.ToString(); string str = "jsapi_ticket=" + (string)Session["ticketzj"] + "&noncestr=" + nonceStr + "×tamp=" + timespanstr + "&url=" + url;// +"&wxref=mp.weixin.qq.com"; string singature = SHA1Util.getSha1(str); string ss = singature; return ss; }
最後產生signature:
class SHA1Util { public static String getSha1(String str) { //建立SHA1对象 SHA1 sha = new SHA1CryptoServiceProvider(); //将mystr转换成byte[] ASCIIEncoding enc = new ASCIIEncoding(); byte[] dataToHash = enc.GetBytes(str); //Hash运算 byte[] dataHashed = sha.ComputeHash(dataToHash); //将运算结果转换成string string hash = BitConverter.ToString(dataHashed).Replace("-", ""); return hash; } }
class SHA1Util { public static String getSha1(String str) { //建立SHA1对象 SHA1 sha = new SHA1CryptoServiceProvider(); //将mystr转换成byte[] ASCIIEncoding enc = new ASCIIEncoding(); byte[] dataToHash = enc.GetBytes(str); //Hash运算 byte[] dataHashed = sha.ComputeHash(dataToHash); //将运算结果转换成string string hash = BitConverter.ToString(dataHashed).Replace("-", ""); return hash; } }
8019067d09615e43c7904885b5246f0a wx.config({ debug: false, appId: '8b714b3c2c6d4a77d9fafb62fc5c6527', timestamp: d338d8cc12e391b6077114439755f24c, nonceStr: 'b0a30cd4be74010f57ebf0728129f0b5', signature: '5f680811c4a8b50943bd0e6592e3a9c3', jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] }); 2cacc6d41bbb37262a98f745aa00fbf0
wx.ready(function(){ // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。});
步驟三:呼叫介面
在進行完第二步的呼叫後,步驟三就顯得非常輕巧了。
<script type="text/javascript" > wx.ready(function () { wx.onMenuShareAppMessage({ title: '<%=shareTitle %>', desc: '<%=shareContent %>', link: '<%=currentUrl %>', imgUrl: '<%=shareImageUrl %>' }); wx.onMenuShareTimeline({ title: '<%=shareContent %>', link: '<%=currentUrl %>', imgUrl: '<%=shareImageUrl %>' }); }) </script>本文的呼叫實例是:
ea1aab715d1b735c8b112b6fff119ca8 wx.ready(function () { wx.onMenuShareAppMessage({ title: '80e0a7013b745db2823b130323b41331', desc: '152f3453142071ccbd67e476c57fed3e', link: '5d9d8eaa1edde12e5f5cd9082c708a3e', imgUrl: '490b6150690e3af5006b483eb68141cc' }); wx.onMenuShareTimeline({ title: '152f3453142071ccbd67e476c57fed3e', link: '5d9d8eaa1edde12e5f5cd9082c708a3e', imgUrl: '490b6150690e3af5006b483eb68141cc' }); }) 2cacc6d41bbb37262a98f745aa00fbf0rrreee本文基本上總結到這裡。
易出現的問題:
1、檢查後台是否設定:右上角公眾號名稱--功能設定--JS介面安全網域
2、檢查程式碼裡的appid和公眾號後台的id是否一致
3.圖片的呼叫位址是絕對路徑(相對路徑好像不行)。
更多微信開發筆記-呼叫自訂分享介面相關文章請關注PHP中文網!