利用.net4.0的dynamic特性制造的超级简单的微信SDK
生活随笔
收集整理的這篇文章主要介紹了
利用.net4.0的dynamic特性制造的超级简单的微信SDK
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.基礎支持API
/*-------------------------------------------------------------------------- * BasicAPI.cs*Auth:deepleo * Date:2013.12.31 * Email:2586662969@qq.com *--------------------------------------------------------------------------*/using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Http; using Codeplex.Data; using System.IO;namespace Deepleo.Weixin.SDK {/// <summary>/// 對應微信API的 "基礎支持"/// </summary>public class BasicAPI{/// <summary>/// 檢查簽名是否正確:/// http://mp.weixin.qq.com/wiki/index.php?title=%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97/// </summary>/// <param name="signature"></param>/// <param name="timestamp"></param>/// <param name="nonce"></param>/// <param name="token">AccessToken</param>/// <returns>/// true: check signature success/// false: check failed, 非微信官方調用!/// </returns>public static bool CheckSignature(string signature, string timestamp, string nonce, string token, out string ent){var arr = new[] { token, timestamp, nonce }.OrderBy(z => z).ToArray();var arrString = string.Join("", arr);var sha1 = System.Security.Cryptography.SHA1.Create();var sha1Arr = sha1.ComputeHash(Encoding.UTF8.GetBytes(arrString));StringBuilder enText = new StringBuilder();foreach (var b in sha1Arr){enText.AppendFormat("{0:x2}", b);}ent = enText.ToString();return signature == enText.ToString();}/// <summary>/// 獲取AccessToken/// http://mp.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96access_token/// </summary>/// <param name="grant_type"></param>/// <param name="appid"></param>/// <param name="secrect"></param>/// <returns>access_toke</returns>public static dynamic GetAccessToken( string appid, string secrect){var url = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type={0}&appid={1}&secret={2}", "client_credential", appid, secrect);var client = new HttpClient();var result = client.GetAsync(url).Result;if (!result.IsSuccessStatusCode) return string.Empty;var token = DynamicJson.Parse(result.Content.ReadAsStringAsync().Result);return token;}/// <summary>/// 上傳多媒體文件/// http://mp.weixin.qq.com/wiki/index.php?title=%E4%B8%8A%E4%BC%A0%E4%B8%8B%E8%BD%BD%E5%A4%9A%E5%AA%92%E4%BD%93%E6%96%87%E4%BB%B6/// 1.上傳的媒體文件限制:///圖片(image) : 1MB,支持JPG格式///語音(voice):1MB,播放長度不超過60s,支持MP4格式///視頻(video):10MB,支持MP4格式///縮略圖(thumb):64KB,支持JPG格式///2.媒體文件在后臺保存時間為3天,即3天后media_id失效/// </summary>/// <param name="token"></param>/// <param name="type"></param>/// <param name="file"></param>/// <returns>media_id</returns>public static string UploadMedia(string token, string type, string file){var url = string.Format("http://api.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}&filename={2}", token, type, Path.GetFileName(file));var client = new HttpClient();var result = client.PostAsync(url, new StreamContent(new FileStream(file, FileMode.Open, FileAccess.Read)));if (!result.Result.IsSuccessStatusCode) return string.Empty;var media = DynamicJson.Parse(result.Result.Content.ReadAsStringAsync().Result);return media.media_id;}} } View Code2.接收消息
/*-------------------------------------------------------------------------- * AcceptMessageAPI.cs*Auth:deepleo * Date:2013.12.31 * Email:2586662969@qq.com *--------------------------------------------------------------------------*/using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Dynamic; using System.Xml.Linq; using System.Xml; using Codeplex.Data;namespace Deepleo.Weixin.SDK {/// <summary>/// 對應微信API的 "接收消息"/// </summary>public class AcceptMessageAPI{/// <summary>/// 解析微信服務器推送的消息/// http://mp.weixin.qq.com/wiki/index.php?title=%E6%8E%A5%E6%94%B6%E6%99%AE%E9%80%9A%E6%B6%88%E6%81%AF/// http://mp.weixin.qq.com/wiki/index.php?title=%E6%8E%A5%E6%94%B6%E4%BA%8B%E4%BB%B6%E6%8E%A8%E9%80%81/// </summary>/// <param name="message"></param>/// <returns></returns>public static WeixinMessage Parse(string message){var msg = new WeixinMessage();msg.Body = new DynamicXml(message);string msgType = msg.Body.MsgType.Value;switch (msgType){case "text":msg.Type = WeixinMessageType.Text;break;case "image":msg.Type = WeixinMessageType.Image;break;case "voice":msg.Type = WeixinMessageType.Voice;break;case "video":msg.Type = WeixinMessageType.Video;break;case "location":msg.Type = WeixinMessageType.Location;break;case "link":msg.Type = WeixinMessageType.Link;break;case "event":msg.Type = WeixinMessageType.Event;break;default: throw new Exception("does not support this message type:" + msgType);}return msg;}} } View Code3.發送消息
/*-------------------------------------------------------------------------- * SendMessageAPI.cs*Auth:deepleo * Date:2013.12.31 * Email:2586662969@qq.com *--------------------------------------------------------------------------*/using System; using System.Collections.Generic; using System.Linq; using System.Text; using Codeplex.Data; using System.Net; using System.Net.Http;namespace Deepleo.Weixin.SDK {/// <summary>/// 對應微信API的 "發送消息”/// </summary>public class SendMessageAPI{/// <summary>/// 被動回復消息/// </summary>/// <param name="message">微信服務器推送的消息</param>/// <param name="executor">用戶自定義的消息執行者</param>/// <returns></returns>public static string Relay(WeixinMessage message, IWeixinExecutor executor){return executor.Execute(message);}/// <summary>/// 主動發送客服消息/// http://mp.weixin.qq.com/wiki/index.php?title=%E5%8F%91%E9%80%81%E5%AE%A2%E6%9C%8D%E6%B6%88%E6%81%AF/// 當用戶主動發消息給公眾號的時候/// 開發者在一段時間內(目前為24小時)可以調用客服消息接口,在24小時內不限制發送次數。/// </summary>/// <param name="token"></param>/// <param name="msg">json格式的消息,具體格式請參考微信官方API</param>/// <returns></returns>public static bool Send(string token, string msg){var client = new HttpClient();var task = client.PostAsync(string.Format("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}", token), new StringContent(msg)).Result;return task.IsSuccessStatusCode;}} } View Code4.用戶管理
/*-------------------------------------------------------------------------- * UserAdminAPI.cs*Auth:deepleo * Date:2013.12.31 * Email:2586662969@qq.com *--------------------------------------------------------------------------*/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Http; using Codeplex.Data;namespace Deepleo.Weixin.SDK {/// <summary>/// 對應微信API的 "用戶管理"/// 注意:/// 1.以下API未實現 :/// "分組管理接口",“網頁授權獲取用戶基本信息”/// 2.獲取用戶"地理位置"API 見AcceptMessageAPI實現,/// “地理位置”獲取方式有兩種:一種是僅在進入會話時上報一次,一種是進入會話后每隔5秒上報一次,公眾號可以在公眾平臺網站中設置。/// </summary>public class UserAdminAPI{/// <summary>/// 獲取用戶基本信息/// </summary>/// <param name="token"></param>/// <param name="openId"></param>/// <returns></returns>public static dynamic GetInfo(string token, string openId){var client = new HttpClient();var result = client.GetAsync(string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}", token, openId)).Result;if (!result.IsSuccessStatusCode) return null;return DynamicJson.Parse(result.Content.ReadAsStringAsync().Result);}/// <summary>/// 獲取訂閱者信息/// </summary>/// <param name="token"></param>/// <returns></returns>public static dynamic GetSubscribes(string token){var client = new HttpClient();var result = client.GetAsync(string.Format("https://api.weixin.qq.com/cgi-bin/user/get?access_token={0}", token)).Result;if (!result.IsSuccessStatusCode) return null;return DynamicJson.Parse(result.Content.ReadAsStringAsync().Result);}/// <summary>/// 獲取訂閱者信息/// </summary>/// <param name="token"></param>/// <param name="nextOpenId"></param>/// <returns></returns>public static dynamic GetSubscribes(string token, string nextOpenId){var client = new HttpClient();var result = client.GetAsync(string.Format("https://api.weixin.qq.com/cgi-bin/user/get?access_token={0}&next_openid={1}", token, nextOpenId)).Result;if (!result.IsSuccessStatusCode) return null;return DynamicJson.Parse(result.Content.ReadAsStringAsync().Result);}} } View Code5.自定義菜單
/*-------------------------------------------------------------------------- * CustomMenu.cs*Auth:deepleo * Date:2013.12.31 * Email:2586662969@qq.com *--------------------------------------------------------------------------*/using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Http; using Codeplex.Data;namespace Deepleo.Weixin.SDK {/// <summary>/// 對應微信API的 "自定義菜單”/// 注意:自定義菜單事件推送接口見:AcceptMessageAPI/// </summary>public class CustomMenuAPI{/// <summary>/// 自定義菜單創建接口/// </summary>/// <param name="token"></param>/// <param name="content"></param>/// <returns></returns>public static bool Create(string token, string content){var client = new HttpClient();var result = client.PostAsync(string.Format("https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}", token), new StringContent(content)).Result;return DynamicJson.Parse(result.Content.ReadAsStringAsync().Result).errcode == 0;}/// <summary>/// 自定義菜單查詢接口/// </summary>/// <param name="token"></param>/// <returns></returns>public static dynamic Query(string token){var client = new HttpClient();var result = client.GetAsync(string.Format("https://api.weixin.qq.com/cgi-bin/menu/get?access_token={0}", token)).Result;if (!result.IsSuccessStatusCode) return null;return DynamicJson.Parse(result.Content.ReadAsStringAsync().Result);}/// <summary>/// 自定義菜單刪除接口/// </summary>/// <param name="token"></param>/// <returns></returns>public static bool Delete(string token){var client = new HttpClient();var result = client.GetAsync(string.Format("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token={0}", token)).Result;if (!result.IsSuccessStatusCode) return false;return DynamicJson.Parse(result.Content.ReadAsStringAsync().Result).errmsg == "ok";}} } View Code?詳細:https://github.com/night-king/weixinSDK
?關于DynamicJson和Dynamicxml請參考:上一篇文章:http://www.cnblogs.com/deepleo/p/weixinSDK.html
QQ互助交流群:173564082
轉載于:https://www.cnblogs.com/deepleo/p/dynamicWeixinSDK.html
總結
以上是生活随笔為你收集整理的利用.net4.0的dynamic特性制造的超级简单的微信SDK的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Opencv学习笔记(六)SURF学习笔
- 下一篇: yui压缩js文件