|
|
|
@ -44,58 +44,68 @@ namespace BWP.B3WeChat.Utils |
|
|
|
|
|
|
|
public static B3WeChatConfig config = new B3WeChatConfig(); |
|
|
|
|
|
|
|
public static string GetToken() |
|
|
|
|
|
|
|
|
|
|
|
static JavaScriptSerializer jsonHelper = new JavaScriptSerializer(); |
|
|
|
|
|
|
|
static T GetRequest<T>(string uriStr) where T : WeChatResponseBase |
|
|
|
{ |
|
|
|
string token = string.Empty; |
|
|
|
string uriStr = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", config.AppID.Value, config.AppSecret.Value); |
|
|
|
var responseBody = GetRequestString(uriStr); |
|
|
|
T obj = jsonHelper.Deserialize<T>(responseBody); |
|
|
|
if (obj.IsError) |
|
|
|
{ |
|
|
|
throw new Exception(string.Format("{0}:{1}", obj.errcode, obj.errmsg)); |
|
|
|
} |
|
|
|
return obj; |
|
|
|
} |
|
|
|
|
|
|
|
private static string GetRequestString(string url) |
|
|
|
{ |
|
|
|
WebClient client = new WebClient(); |
|
|
|
string responseBody = string.Empty; |
|
|
|
try |
|
|
|
var data = client.DownloadData(url); |
|
|
|
return Encoding.UTF8.GetString(data); |
|
|
|
} |
|
|
|
|
|
|
|
static T PostRequest<T>(string url, string json) where T : WeChatResponseBase |
|
|
|
{ |
|
|
|
byte[] requestBuffer = Encoding.UTF8.GetBytes(json); |
|
|
|
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url)); |
|
|
|
webRequest.Method = "POST"; |
|
|
|
webRequest.ContentType = "application/x-www-form-urlencoded"; |
|
|
|
webRequest.ContentLength = requestBuffer.Length; |
|
|
|
using (var requestStream = webRequest.GetRequestStream()) |
|
|
|
{ |
|
|
|
byte[] bytes = client.DownloadData(uriStr); |
|
|
|
responseBody = Encoding.UTF8.GetString(bytes); |
|
|
|
JavaScriptSerializer jsonHelper = new JavaScriptSerializer(); |
|
|
|
TokenObject obj = jsonHelper.Deserialize<TokenObject>(responseBody); |
|
|
|
if (obj.IsError) |
|
|
|
{ |
|
|
|
throw new Exception(string.Format("{0}:{1}", obj.errcode, obj.errmsg)); |
|
|
|
} |
|
|
|
if (obj != null) |
|
|
|
{ |
|
|
|
token = obj.access_token; |
|
|
|
} |
|
|
|
requestStream.Write(requestBuffer, 0, requestBuffer.Length); |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
|
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); |
|
|
|
var data = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd(); |
|
|
|
var obj = jsonHelper.Deserialize<T>(data); |
|
|
|
if (obj.IsError) |
|
|
|
{ |
|
|
|
throw e; |
|
|
|
throw new Exception(string.Format("{0}:{1}", obj.errcode, obj.errmsg)); |
|
|
|
} |
|
|
|
return token; |
|
|
|
return obj; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static string GetToken() |
|
|
|
{ |
|
|
|
string token = string.Empty; |
|
|
|
string url = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", config.AppID.Value, config.AppSecret.Value); |
|
|
|
var res = GetRequest<TokenObject>(url); |
|
|
|
return res.access_token; |
|
|
|
} |
|
|
|
|
|
|
|
public static WeiChartServerList GetIPList() |
|
|
|
{ |
|
|
|
WeiChartServerList result = new WeiChartServerList(); |
|
|
|
string uriStr = string.Format("https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token={0}", TOKEN); |
|
|
|
WebClient client = new WebClient(); |
|
|
|
try |
|
|
|
{ |
|
|
|
byte[] bytes = client.DownloadData(uriStr); |
|
|
|
string responseBody = Encoding.UTF8.GetString(bytes); |
|
|
|
JavaScriptSerializer jsonHelper = new JavaScriptSerializer(); |
|
|
|
result = jsonHelper.Deserialize<WeiChartServerList>(responseBody); |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
|
{ |
|
|
|
throw e; |
|
|
|
} |
|
|
|
return result; |
|
|
|
return GetRequest<WeiChartServerList>(uriStr); |
|
|
|
} |
|
|
|
|
|
|
|
public static SendTemplateMessageResult SendTemplateMessage(string openID, string templateID, Dictionary<string, ValueColor> dic, string url = "") |
|
|
|
{ |
|
|
|
string uriStr = string.Format("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={0}", TOKEN); |
|
|
|
|
|
|
|
JavaScriptSerializer jsonHelper = new JavaScriptSerializer(); |
|
|
|
SendTemplateMessageResult result = new SendTemplateMessageResult(); |
|
|
|
MeassageBody body = new MeassageBody(); |
|
|
|
@ -104,34 +114,30 @@ namespace BWP.B3WeChat.Utils |
|
|
|
body.url = url; |
|
|
|
body.data = dic; |
|
|
|
string postData = jsonHelper.Serialize(body); |
|
|
|
string data = string.Empty; |
|
|
|
try |
|
|
|
{ |
|
|
|
byte[] byteArray = Encoding.UTF8.GetBytes(postData); |
|
|
|
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(uriStr)); |
|
|
|
webRequest.Method = "post"; |
|
|
|
webRequest.ContentType = "application/x-www-form-urlencoded"; |
|
|
|
webRequest.ContentLength = byteArray.Length; |
|
|
|
System.IO.Stream newStream = webRequest.GetRequestStream(); |
|
|
|
newStream.Write(byteArray, 0, byteArray.Length); |
|
|
|
newStream.Close(); |
|
|
|
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); |
|
|
|
data = new System.IO.StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd(); |
|
|
|
result = jsonHelper.Deserialize<SendTemplateMessageResult>(data); |
|
|
|
if (result.IsError) |
|
|
|
{ |
|
|
|
throw new Exception(string.Format("{0}:{1}", result.errcode, result.errmsg)); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
|
{ |
|
|
|
throw e; |
|
|
|
} |
|
|
|
return result; |
|
|
|
return PostRequest<SendTemplateMessageResult>(uriStr, postData); |
|
|
|
} |
|
|
|
|
|
|
|
public static void SetCustomMenu(string menuJson) |
|
|
|
{ |
|
|
|
var url = string.Format("https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}", TOKEN); |
|
|
|
PostRequest<WeChatResponseBase>(url, menuJson); |
|
|
|
} |
|
|
|
|
|
|
|
public static string GetCustomMenu() |
|
|
|
{ |
|
|
|
var url = string.Format("https://api.weixin.qq.com/cgi-bin/menu/get?access_token={0}", TOKEN); |
|
|
|
return GetRequestString(url); |
|
|
|
} |
|
|
|
|
|
|
|
public static void DelteCustomMenu() |
|
|
|
{ |
|
|
|
var url = string.Format("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token={0}",TOKEN); |
|
|
|
GetRequest<WeChatResponseBase>(url); |
|
|
|
} |
|
|
|
|
|
|
|
public static List<string> GetOpenIDList() |
|
|
|
{ |
|
|
|
|
|
|
|
string openid = string.Empty; |
|
|
|
string uriStr = string.Empty; |
|
|
|
List<string> lstOpenID = new List<string>(); |
|
|
|
|