using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO; using Newtonsoft.Json; using System.Net; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Security.Cryptography; /// <summary> /// WXJSSDK 的摘要說(shuō)明 /// </summary> public class WXJSSDK { private string appId; private string appSecret; private DataTable DT; public WXJSSDK(string appId, string appSecret) { this.appId = appId; this.appSecret = appSecret; } //得到數(shù)據(jù)包,返回使用頁(yè)面 public System.Collections.Hashtable getSignPackage() { string jsapiTicket = getJsApiTicket(); string url = HttpContext.Current.Request.Url.ToString(); string timestamp = Convert.ToString(ConvertDateTimeInt(DateTime.Now)); string nonceStr = createNonceStr(); // 這里參數(shù)的順序要按照 key 值 ASCII 碼升序排序 string rawstring = "jsapi_ticket=" + jsapiTicket + "&noncestr=" + nonceStr + "×tamp=" + timestamp + "&url=" + url + ""; string signature = SHA1_Hash(rawstring); System.Collections.Hashtable signPackage = new System.Collections.Hashtable(); signPackage.Add("appId", appId); signPackage.Add("nonceStr", nonceStr); signPackage.Add("timestamp", timestamp); signPackage.Add("url", url); signPackage.Add("signature", signature); signPackage.Add("rawString", rawstring); return signPackage; } //創(chuàng)建隨機(jī)字符串 private string createNonceStr() { int length = 16; string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; string str = ""; Random rad = new Random(); for (int i = 0; i < length; i++) { str += chars.Substring(rad.Next(0, chars.Length - 1), 1); } return str; } //得到ticket 如果文件里時(shí)間 超時(shí)則重新獲取 //注:jsapi_ticket使用規(guī)則(有過(guò)期時(shí)間)類似access_token, oauth的access_token與基礎(chǔ)access_token不同 private string getJsApiTicket() { //這里我從數(shù)據(jù)庫(kù)讀取 DT = DbSession.Default.FromSql("select jsapi_ticket,ticket_expires from table where ID=1").ToDataTable(); int expire_time = (int)DT.Rows[0]["ticket_expires"]; string ticket = DT.Rows[0]["jsapi_ticket"].ToString(); string accessToken =getAccessToken();//獲取系統(tǒng)的全局token if (expire_time < ConvertDateTimeInt(DateTime.Now)) { string url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=" + accessToken + ""; Jsapi api =JsonConvert.DeserializeObject<Jsapi>(httpGet(url)); ticket = api.ticket; if (ticket != "") { expire_time = ConvertDateTimeInt(DateTime.Now) + 7000; //存入數(shù)據(jù)庫(kù)操作 } } return ticket; } ////得到accesstoken 如果文件里時(shí)間 超時(shí)則重新獲取 //private string getAccessToken() //{ // // access_token 應(yīng)該全局存儲(chǔ)與更新,以下代碼以寫入到文件中做示例 // string access_token = ""; // string path = HttpContext.Current.Server.MapPath(@"/weixin/access_token.json"); // FileStream file = new FileStream(path, FileMode.Open); // var serializer = new DataContractJsonSerializer(typeof(AccToken)); // AccToken readJSTicket = (AccToken)serializer.ReadObject(file); // file.Close(); // if (readJSTicket.expires_in < ConvertDateTimeInt(DateTime.Now)) // { // string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret + ""; // AccToken iden = Desrialize<AccToken>(new AccToken(), httpGet(url)); // access_token = iden.access_token; // if (access_token != "") // { // iden.expires_in = ConvertDateTimeInt(DateTime.Now) + 7000; // iden.access_token = access_token; // string json = Serialize<AccToken>(iden); // StreamWriterMetod(json, path); // } // } // else // { // access_token = readJSTicket.access_token; // } // return access_token; //} //發(fā)起一個(gè)http請(qǐng)球,返回值 private string httpGet(string url) { try { WebClient MyWebClient = new WebClient(); MyWebClient.Credentials = CredentialCache.DefaultCredentials;//獲取或設(shè)置用于向Internet資源的請(qǐng)求進(jìn)行身份驗(yàn)證的網(wǎng)絡(luò)憑據(jù) Byte[] pageData = MyWebClient.DownloadData(url); //從指定網(wǎng)站下載數(shù)據(jù) string pageHtml = System.Text.Encoding.Default.GetString(pageData); //如果獲取網(wǎng)站頁(yè)面采用的是GB2312,則使用這句 return pageHtml; } catch (WebException webEx) { Console.WriteLine(webEx.Message.ToString()); return null; } } //SHA1哈希加密算法 public string SHA1_Hash(string str_sha1_in) { SHA1 sha1 = new SHA1CryptoServiceProvider(); byte[] bytes_sha1_in = System.Text.UTF8Encoding.Default.GetBytes(str_sha1_in); byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in); string str_sha1_out = BitConverter.ToString(bytes_sha1_out); str_sha1_out = str_sha1_out.Replace("-", "").ToLower(); return str_sha1_out; } /// <summary> /// StreamWriter寫入文件方法 /// </summary> private void StreamWriterMetod(string str, string patch) { try { FileStream fsFile = new FileStream(patch, FileMode.OpenOrCreate); StreamWriter swWriter = new StreamWriter(fsFile); swWriter.WriteLine(str); swWriter.Close(); } catch (Exception e) { throw e; } } /// <summary> /// 將c# DateTime時(shí)間格式轉(zhuǎn)換為Unix時(shí)間戳格式 /// </summary> /// <param name="time">時(shí)間</param> /// <returns>double</returns> public int ConvertDateTimeInt(System.DateTime time) { int intResult = 0; System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); intResult = Convert.ToInt32((time - startTime).TotalSeconds); return intResult; } } //創(chuàng)建Json序列化 及反序列化類目 #region //創(chuàng)建JSon類 保存文件 jsapi_ticket.json public class JSTicket { public string jsapi_ticket { get; set; } public double expire_time { get; set; } } //創(chuàng)建 JSon類 保存文件 access_token.json public class AccToken { public string access_token { get; set; } public double expires_in { get; set; } } //創(chuàng)建從微信返回結(jié)果的一個(gè)類 用于獲取ticket public class Jsapi { public int errcode { get; set; } public string errmsg { get; set; } public string ticket { get; set; } public string expires_in { get; set; } } #endregion
The above is the written class, and then directly call it and output it to js
WXJSSDK jssdk = new WXJSSDK(AppId,AppSecret); Hashtable hs = jssdk.getSignPackage(); string signature = hs["signature"].ToString(); string signature = hs["signature"].ToString(); string timestamp = hs["timestamp"].ToString(); string nonce = hs["nonceStr"].ToString();
Then js call:
<script type="text/javascript"> var dataForWeixin = { appId: "<%=appid%>", MsgImg: "<%=WeChatImg%>", TLImg: "<%=WeChatImg%>", url: "<%=url%>", title: "<%=Title%>", desc: "<%=desc%>", timestamp: '<%=timestamp%>', nonceStr: '<%=nonce%>', signature: '<%=signature%>', jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo'], fakeid: "", callback: function () { } }; wx.config({ debug: false, // 開(kāi)啟調(diào)試模式,調(diào)用的所有api的返回值會(huì)在客戶端alert出來(lái),若要查看傳入的參數(shù),可以在pc端打開(kāi),參數(shù)信息會(huì)通過(guò)log打出,僅在pc端時(shí)才會(huì)打印。 appId: dataForWeixin.appId, // 必填,公眾號(hào)的唯一標(biāo)識(shí) timestamp: dataForWeixin.timestamp, // 必填,生成簽名的時(shí)間戳 nonceStr: dataForWeixin.nonceStr, // 必填,生成簽名的隨機(jī)串 signature: dataForWeixin.signature,// 必填,簽名,見(jiàn)附錄1 jsApiList: dataForWeixin.jsApiList // 必填,需要使用的JS接口列表,所有JS接口列表見(jiàn)附錄2 }); wx.ready(function () { //在此輸入各種API //分享到朋友圈 wx.onMenuShareTimeline({ title: dataForWeixin.title, // 分享標(biāo)題 link: dataForWeixin.url, // 分享鏈接 imgUrl: dataForWeixin.MsgImg, // 分享圖標(biāo) success: function () { // 用戶確認(rèn)分享后執(zhí)行的回調(diào)函數(shù) }, cancel: function () { // 用戶取消分享后執(zhí)行的回調(diào)函數(shù) } }); //分享給朋友 wx.onMenuShareAppMessage({ title: dataForWeixin.title, // 分享標(biāo)題 desc: dataForWeixin.desc, // 分享描述 link: dataForWeixin.url, // 分享鏈接 imgUrl: dataForWeixin.TLImg, // 分享圖標(biāo) type: '', // 分享類型,music、video或link,不填默認(rèn)為link dataUrl: '', // 如果type是music或video,則要提供數(shù)據(jù)鏈接,默認(rèn)為空 success: function () { // 用戶確認(rèn)分享后執(zhí)行的回調(diào)函數(shù) }, cancel: function () { // 用戶取消分享后執(zhí)行的回調(diào)函數(shù) } }); //QQ wx.onMenuShareQQ({ title: dataForWeixin.title, // 分享標(biāo)題 desc: dataForWeixin.desc, // 分享描述 link: dataForWeixin.url, // 分享鏈接 imgUrl: dataForWeixin.MsgImg,// 分享圖標(biāo) success: function () { // 用戶確認(rèn)分享后執(zhí)行的回調(diào)函數(shù) }, cancel: function () { // 用戶取消分享后執(zhí)行的回調(diào)函數(shù) } }); //QQ微博 wx.onMenuShareWeibo({ title: dataForWeixin.title, // 分享標(biāo)題 desc: dataForWeixin.desc, // 分享描述 link: dataForWeixin.url, // 分享鏈接 imgUrl: dataForWeixin.TLImg, // 分享圖標(biāo) success: function () { // 用戶確認(rèn)分享后執(zhí)行的回調(diào)函數(shù) }, cancel: function () { // 用戶取消分享后執(zhí)行的回調(diào)函數(shù) } }); // config信息驗(yàn)證后會(huì)執(zhí)行ready方法,所有接口調(diào)用都必須在config接口獲得結(jié)果之后,config是一個(gè)客戶端的異步操作, //所以如果需要在頁(yè)面加載時(shí)就調(diào)用相關(guān)接口,則須把相關(guān)接口放在ready函數(shù)中調(diào)用來(lái)確保正確執(zhí)行。對(duì)于用戶觸發(fā)時(shí)才調(diào)用的接口,則可以直接調(diào)用,不需要放在ready函數(shù)中。 }); wx.error(function (res) { //alert(res); // config信息驗(yàn)證失敗會(huì)執(zhí)行error函數(shù),如簽名過(guò)期導(dǎo)致驗(yàn)證失敗,具體錯(cuò)誤信息可以打開(kāi)config的debug模式查看,也可以在返回的res參數(shù)中查看,對(duì)于SPA可以在這里更新簽名。 }); </script>
More WeChat Development-Jssdk call sharing example For related articles, please pay attention to the PHP Chinese website!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
Grass Wonder Build Guide | Uma Musume Pretty Derby
4 weeks ago
By Jack chen
Roblox: 99 Nights In The Forest - All Badges And How To Unlock Them
3 weeks ago
By DDD
Uma Musume Pretty Derby Banner Schedule (July 2025)
4 weeks ago
By Jack chen
Windows Security is blank or not showing options
4 weeks ago
By 下次還敢
RimWorld Odyssey Temperature Guide for Ships and Gravtech
3 weeks ago
By Jack chen

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)