namespace SendWechatTempInfo
{
public class Wechat: ForguncyApi
{
static string AppID = "微信ID";
static string AppSecret = "微信密钥";
static string _lastAccessToken;
static DateTime _lastAccessTokenTimeOut;
[Post] /*此处第一个post*/
public void SendTemplateMessage()
{
var task = this.Context.Request.ReadFormAsync();
task.Wait();
var form = task.Result;
var openid = form["openid"];
var first = form["first"];
var keyword1 = form["keyword1"];
var keyword2 = form["keyword2"];
var keyword3 = form["keyword3"];
var keyword4 = form["keyword4"];
var remark = form["remark"];
var templateMessageid = "消息ID";
var url = form["url"];
var color = "#1C86EE";
var colorb = "#525252";
var msg = new TemplateMsg { touser = openid, template_id = templateMessageid, url = url };
msg.data = new Dictionary<string, TemplateDataItem>();
msg.data.Add("first", new TemplateDataItem { value = first, color = color });
msg.data.Add("keyword1", new TemplateDataItem { value = keyword1, color = color });
msg.data.Add("keyword2", new TemplateDataItem { value = keyword2, color = color });
msg.data.Add("keyword3", new TemplateDataItem { value = keyword3, color = color });
msg.data.Add("keyword4", new TemplateDataItem { value = keyword4, color = color });
msg.data.Add("remark", new TemplateDataItem { value = remark, color = colorb });
var data = JsonConvert.SerializeObject(msg);
string postUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + GetAccessToken();
var response = new PostData().Post(postUrl, data);
if (string.IsNullOrEmpty(response))
{
Trace.Write("openWeixin get user info failed.(1), result: " + response);
}
}
[Post] /*此处第二个post*/
public void SendTemplateMessage1()
{
var task = this.Context.Request.ReadFormAsync();
task.Wait();
var form = task.Result;
var openid = form["openid"];
var first = form["first"];
var keyword1 = form["keyword1"];
var keyword2 = form["keyword2"];
var keyword3 = form["keyword3"];
var keyword4 = form["keyword4"];
var keyword5 = form["keyword5"];
var remark = form["remark"];
var templateMessageid = "消息ID";
var url = form["url"];
var color = "#1C86EE";
var colorb = "#525252";
var msg = new TemplateMsg { touser = openid, template_id = templateMessageid, url = url };
msg.data = new Dictionary<string, TemplateDataItem>();
msg.data.Add("first", new TemplateDataItem { value = first, color = color });
msg.data.Add("keyword1", new TemplateDataItem { value = keyword1, color = color });
msg.data.Add("keyword2", new TemplateDataItem { value = keyword2, color = color });
msg.data.Add("keyword3", new TemplateDataItem { value = keyword3, color = color });
msg.data.Add("keyword4", new TemplateDataItem { value = keyword4, color = color });
msg.data.Add("keyword5", new TemplateDataItem { value = keyword5, color = color });
msg.data.Add("remark", new TemplateDataItem { value = remark, color = colorb });
var data = JsonConvert.SerializeObject(msg);
string postUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + GetAccessToken();
var response = new PostData().Post(postUrl, data);
if (string.IsNullOrEmpty(response))
{
Trace.Write("openWeixin get user info failed.(1), result: " + response);
}
}
/*下面的代码 都不用动,复制了一部分*/
protected string GetAccessToken()
{
if (!string.IsNullOrEmpty(_lastAccessToken))
{
if (DateTime.Now < _lastAccessTokenTimeOut)
{
return _lastAccessToken;
}
}
string result = "";
try
{
PostData postData = new PostData();
result = postData.Get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + AppID + "&secret=" + AppSecret);
var jd = JsonConvert.DeserializeObject(result) as JObject;
_lastAccessToken = jd["access_token"].ToString();
_lastAccessTokenTimeOut = DateTime.Now.AddSeconds((int)jd["expires_in"]);
return _lastAccessToken;
}
catch (Exception)
{
Trace.WriteLine("GetAccessToken Failed");
if (result != null)
{
Trace.WriteLine(result);
}
|