using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HuizhongLibrary.Weixin { public class WeixinMessage { public string touser { get; set; } public string template_id { get; set; } public string url { get; set; } public List data { get; set; } public WeixinMessage() { data = new List(); } public string ToJsonString() { StringBuilder sb = new StringBuilder(); sb.Append("{"); sb.Append("\"touser\":\"" + this.touser + "\","); sb.Append("\"template_id\":\"" + this.template_id + "\","); sb.Append("\"url\":\"" + this.url + "\","); sb.Append("\"data\":{"); foreach (var item in data) { sb.Append(item.ToJsonString()); int index = data.IndexOf(item); if (index < data.Count-1) sb.Append(","); } sb.Append("}"); sb.Append("}"); return sb.ToString(); } } public class WeixinMessageData { public string Name { get; set; } public string value { get; set; } public string color { get; set; } public WeixinMessageData(string Name,string value) { this.Name = Name; this.value = value; this.color = ""; } public WeixinMessageData(string Name, string value,string color) { this.Name = Name; this.value = value; this.color = color; } public string ToJsonString() { StringBuilder sb = new StringBuilder(); sb.Append("\""+this.Name+"\":{"); sb.Append("\"value\":\"" + this.value + "\","); sb.Append("\"color\":\"" + this.color + "\""); sb.Append("}"); return sb.ToString(); } } public class WeixinEnterpriseMessage { public string touser { get; set; } public string toparty { get; set; } public string totag { get; set; } public string msgtype { get; set; } public int agentid { get; set; } public string content { get; set; } public int safe { get; set; } public List articles { get; set; } public WeixinEnterpriseMessage() { touser = "@all"; agentid = 3; msgtype = "news"; safe = 0; articles = new List(); } public string ToJsonString() { StringBuilder sb = new StringBuilder(); sb.Append("{"); if (string.IsNullOrEmpty(touser) == false) sb.Append("\"touser\":\"" + this.touser + "\","); if (string.IsNullOrEmpty(toparty) == false) sb.Append("\"toparty\":\"" + this.toparty + "\","); if (string.IsNullOrEmpty(totag) == false) sb.Append("\"totag\":\"" + this.totag + "\","); sb.Append("\"msgtype\":\"" + this.msgtype + "\","); sb.Append("\"agentid\":" + this.agentid + ","); if (msgtype == "text") { sb.Append("\"text\":{"); sb.Append("\"content\":\"" + this.content + "\","); sb.Append("},"); } if (msgtype == "news") { sb.Append("\"news\":{"); sb.Append("\"articles\":["); foreach (var item in articles) { sb.Append(item.ToJsonString()); int index = articles.IndexOf(item); if (index < articles.Count-1) sb.Append(","); } sb.Append("]"); sb.Append("},"); } if (msgtype == "textcard") { sb.Append("\"textcard\":"); sb.Append(articles[0].ToJsonString()); sb.Append(","); } sb.Append("\"safe\":" + this.safe); sb.Append("}"); return sb.ToString(); } } public class WeixinEnterpriseArticle { public string title { get; set; } public string description { get; set; } public string url { get; set; } public string btntxt { get; set; } public WeixinEnterpriseArticle() { btntxt = "更多"; } public string ToJsonString() { StringBuilder sb = new StringBuilder(); sb.Append("{"); sb.Append("\"title\":\"" + this.title + "\","); sb.Append("\"description\":\"" + this.description + "\","); sb.Append("\"url\":\"" + this.url + "\","); sb.Append("\"btntxt\":\"" + this.btntxt + "\""); sb.Append("}"); return sb.ToString(); } } public class WeixinArticleMessage { public string thumb_media_id { get; set; } public string author { get; set; } public string title { get; set; } public string content_source_url { get; set; } public string content { get; set; } public int show_cover_pic { get; set; } public string ToJsonString() { StringBuilder sb = new StringBuilder(); sb.Append("{"); sb.Append("\"articles\":[{"); sb.Append("\"thumb_media_id\":\"" + this.thumb_media_id + "\","); sb.Append("\"author\":\"" + this.author + "\","); sb.Append("\"title\":\"" + this.title + "\","); sb.Append("\"content_source_url\":\"" + this.content_source_url + "\","); sb.Append("\"content\":\"" + this.content + "\","); sb.Append("\"show_cover_pic\":" + this.show_cover_pic); sb.Append("}"); sb.Append("]"); sb.Append("}"); return sb.ToString(); } } }