1. 程式人生 > >基於Senparc的二次封裝

基於Senparc的二次封裝

cti art .exe agent lin article 發送數據 https gty

前段時間用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;
        }
    }
View Code 技術分享
    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;
        }
    }
View Code 技術分享
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;
        }
    }
View Code 技術分享
    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;
        }
    }
View Code 技術分享
    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;
        }
    }
View Code 技術分享
    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;
        }
    }
View Code 技術分享
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;
        }
    }
View Code

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

基於Senparc的二次封裝