Browse Source

增加设置自定义菜单方法

master
yashen 9 years ago
parent
commit
41d60d0af2
5 changed files with 68 additions and 72 deletions
  1. +1
    -1
      B3WeChat/Entities/ErrorObject.cs
  2. +1
    -11
      B3WeChat/Entities/SendTemplateMessageResult.cs
  3. +1
    -1
      B3WeChat/Entities/TokenObject.cs
  4. +1
    -1
      B3WeChat/Entities/WeiChartServerList.cs
  5. +64
    -58
      B3WeChat/Utils/InOutMessageUtil.cs

+ 1
- 1
B3WeChat/Entities/ErrorObject.cs View File

@ -5,7 +5,7 @@ using System.Web;
namespace BWP.B3WeChat.Entities
{
public class ErrorObject
public class WeChatResponseBase
{
public int errcode { get; set; }
public string errmsg { get; set; }


+ 1
- 11
B3WeChat/Entities/SendTemplateMessageResult.cs View File

@ -8,19 +8,9 @@ namespace BWP.B3WeChat.Entities
/// <summary>
/// 发送模板信息返回结果
/// </summary>
public class SendTemplateMessageResult
public class SendTemplateMessageResult:WeChatResponseBase
{
public int errcode { get; set; }
public string errmsg { get; set; }
public int msgid { get; set; }
public bool IsError
{
get
{
return errcode > 0;
}
}
}
}

+ 1
- 1
B3WeChat/Entities/TokenObject.cs View File

@ -8,7 +8,7 @@ namespace BWP.B3WeChat.Entities
/// <summary>
/// Token对象
/// </summary>
public class TokenObject:ErrorObject
public class TokenObject:WeChatResponseBase
{
public string access_token { get; set; }
public string expires_in { get; set; }


+ 1
- 1
B3WeChat/Entities/WeiChartServerList.cs View File

@ -5,7 +5,7 @@ using System.Web;
namespace BWP.B3WeChat.Entities
{
public class WeiChartServerList
public class WeiChartServerList:WeChatResponseBase
{
public List<string> ip_list { get; set; }
}

+ 64
- 58
B3WeChat/Utils/InOutMessageUtil.cs View File

@ -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>();


Loading…
Cancel
Save