标签:企业 开发者平台 list gem and isp 枚举 article hand
前段时间用Senparc做微信开发,但是个人感觉不太好用,所以我把Senparc的API用装饰器模式进行二次封装。
微信开发者平台文档地址:https://mp.weixin.qq.com/wiki
0x00
先上一个抽象类,对各种类型消息的抽象封装
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Senparc.Weixin.QY; using Senparc.Weixin.QY.AdvancedAPIs; using Senparc.Weixin.QY.AdvancedAPIs.Mass; using Senparc.Weixin.QY.Entities; using Senparc.Weixin; namespace WeiXin { public abstract class MsgHandler { /// <summary> /// 该消息的RID /// </summary> public String RID { get; set; } /// <summary> /// 接受者 /// </summary> public String ToUser { get; set; } /// <summary> /// 接收部门 /// </summary> public String ToParty { get; set; } /// <summary> /// 标签 /// </summary> public String ToTag { get; set; } /// <summary> /// 消息类型 /// </summary> public MsgType MsgType { get; set; } /// <summary> /// 应用ID /// </summary> public String AgentID { get; set; } /// <summary> /// 是否加密 /// </summary> public int Safe { get; set; } /// <summary> /// 发送数据 /// </summary> /// <returns></returns> public abstract MassResult SendMsg(); /// <summary> /// 写入Log表 /// </summary> public void Log(MassResult Result) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("update Msg set IsSuccess = {0} where RID = ‘{1}‘", Result.errcode == Senparc.Weixin.ReturnCode_QY.请求成功 ? 1 : (int)Result.errcode, this.RID); SqlHelper.ExecuteCommand(sb.ToString()); } } }
0x01
用枚举区分消息类型
public enum MsgType { TextMsg = 1, ImageMsg = 2, VoiceMsg = 3, VideoMsg = 4, FileMsg = 5, NewsMsg = 6, MpNewsMsg = 7 }
0x02
按照每种消息类型进行详细封装
public partial class TextMsgHandler : MsgHandler { /// <summary> /// 文本消息 /// </summary> private String Text { get; set; } /// <summary> /// 构造 /// </summary> public TextMsgHandler() { this.MsgType = MsgType.TextMsg; } /// <summary> /// 发送消息方法 /// </summary> public override MassResult SendMsg() { MassResult Result = MassApi.SendText("", ToUser, ToUser, ToTag, AgentID, Text, Safe, 10000); return Result; } }
public partial class ImageMsgHandler : MsgHandler { /// <summary> /// 媒体ID /// </summary> private String Media_ID { get; set; } /// <summary> /// 构造 /// </summary> public ImageMsgHandler() { this.MsgType = MsgType.ImageMsg; } /// <summary> /// 发送消息方法 /// </summary> public override MassResult SendMsg() { MassResult Result = MassApi.SendImage("", ToUser, ToParty, ToTag, AgentID, Media_ID, Safe, 10000); return Result; } }
public partial class VoiceMsgHandler : MsgHandler { /// <summary> /// 媒体ID /// </summary> private String Media_ID { get; set; } /// <summary> /// 构造 /// </summary> public VoiceMsgHandler() { this.MsgType = MsgType.VoiceMsg; } /// <summary> /// 发送消息方法 /// </summary> public override MassResult SendMsg() { MassResult Result = MassApi.SendVoice("", ToUser, ToParty, ToTag, AgentID, Media_ID, Safe, 10000); return Result; } }
public partial class VideoMsgHandler : MsgHandler { /// <summary> /// 视频标题 /// </summary> private String Title { get; set; } /// <summary> /// 视频描述 /// </summary> private String Description { get; set; } /// <summary> /// 媒体ID /// </summary> private String Media_ID { get; set; } /// <summary> /// 构造 /// </summary> public VideoMsgHandler() { this.MsgType = MsgType.VideoMsg; } /// <summary> /// 发送消息方法 /// </summary> public override MassResult SendMsg() { MassResult Result = MassApi.SendVideo("", ToUser, ToParty, ToTag, AgentID, Media_ID, Title, Description, Safe, 10000); return Result; } }
public partial class FileMsgHandler : MsgHandler { /// <summary> /// 媒体ID /// </summary> private String Media_ID { get; set; } /// <summary> /// 构造 /// </summary> public FileMsgHandler() { this.MsgType = MsgType.FileMsg; } /// <summary> /// 发送消息方法 /// </summary> public override MassResult SendMsg() { MassResult Result = MassApi.SendFile("", ToUser, ToParty, ToTag, AgentID, Media_ID,Safe,10000); return Result; } }
public partial class NewsMsgHandler : MsgHandler { /// <summary> /// 新闻列表 /// </summary> private List<Article> Articles { get; set; } /// <summary> /// 构造 /// </summary> private NewsMsgHandler() { this.MsgType = MsgType.NewsMsg; } /// <summary> /// 发送消息方法 /// </summary> public override MassResult SendMsg() { MassResult Result = MassApi.SendNews("", ToUser, ToParty, ToTag, AgentID, Articles, Safe, 10000); return Result; } }
public partial class MpNewsMsgHandler : MsgHandler { /// <summary> /// 新闻列表 /// </summary> private List<MpNewsArticle> MpArticles { get; set; } /// <summary> /// 构造 /// </summary> public MpNewsMsgHandler() { this.MsgType = MsgType.MpNewsMsg; } /// <summary> /// 发送消息方法 /// </summary> public override MassResult SendMsg() { MassResult Result = MassApi.SendMpNews("", ToUser, ToParty, ToTag, AgentID, MpArticles, Safe, 10000); return Result; } }
0x03
创建消息处理器类,处理类主要管理Token等与消息无直接关系的信息,比如Token隔多久取一次,如何加密企业通信SecretCode,这里是部分主要的代码,用到了装饰器模式。
public class Processor { /// <summary> /// 抽象消息 /// </summary> private MsgHandler Msg { get; set; } /// <summary> /// Token /// </summary> private String Token { get; set; } /// <summary> /// 处理器构造 /// </summary> /// <param name="msg"></param> public Processor(MsgHandler msg) { this.Msg = msg; } /// <summary> /// 发送消息 /// </summary> public void SendMsg() { MassResult Result = Msg.SendMsg();
Msg.Log(Result); } }
0x03
Demo
class Program { static void Main(string[] args) { TextMsgHandler textHandlder = new TextMsgHandler(); textHandlder.RID = Guid.NewGuid().ToString(); textHandlder.ToUser = "me"; textHandlder.ToParty = "dev"; textHandlder.ToTag = "ex"; textHandlder.AgentID = "0"; textHandlder.Text = "微信测试"; Processor P = new Processor(textHandlder); //装饰器模式 P.SendMsg(); } }
标签:企业 开发者平台 list gem and isp 枚举 article hand
原文地址:http://www.cnblogs.com/prettydone/p/6928056.html