asmrobot
2019-08-31 013a5eb0380063717241a9d6705557d9a59adbfe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using WeChatIn;
using WeChatIn.Entity;
using WeChatIn.Messages;
using ZTImage.Log;
 
namespace RichCreator.Server.Providers
{
    /// <summary>
    /// 微信提供
    /// </summary>
    public class WechatProvider
    {
        private WechatProvider()
        { }
 
 
 
        /// <summary>
        /// 微信客户端
        /// </summary>
        private WeChatClient client;
        private bool isLogin=false;
        private WeChatUser me;
 
        /// <summary>
        /// 向微信发送消息
        /// </summary>
        /// <param name="user">微信备注名</param>
        /// <param name="content">内容</param>
        public void SendMessage(string user, string content)
        {
            if (!isLogin)
            {
                return;
            }
            WeChatUser wuser = this.client.GetUserByRemarkName(user);
            if (user == null)
            {
                return;
            }
            this.SendMessage(wuser.UserName, content);
        }
        
        /// <summary>
        /// 登陆微信
        /// </summary>
        public void LoginWechat()
        {
            LoginWechat(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "qrcode.png"));
        }
 
        /// <summary>
        /// 登陆微信
        /// </summary>
        /// <param name="qrcodeSaveDir"></param>
        public void LoginWechat(string qrcodeSaveDir)
        {
            this.client = new WeChatClient();
            
            this.client.OnLogined = (c) =>
            {
                Trace.Info("login success,concat:" + c.Contacts.Count + ",nick:" + c.Me.NickName + ",username:" + c.Me.UserName);
                isLogin = true;
                this.client.OnReceive = OnReceiveMessage;
                me = this.client.GetUserByRemarkName("xiaozhuan");
            };
            
            this.client.Start(qrcodeSaveDir);
        }
 
        /// <summary>
        /// 收到消息回调
        /// </summary>
        /// <param name="msg"></param>
        private void OnReceiveMessage(Message msg)
        {
            if (me == null)
            {
                return;
            }
 
            if (msg.From == client.Me.UserName)
            {
                return;
            }
 
            if (msg.MessageType==MessageType.Text&&msg.From == me.UserName)
            {
                //来自小砖,执行命令
                ExecuteCommand(msg.Content);
                return;
            }
 
            string content = string.Empty;
            string showName = client.GetUserByUserName(msg.From).ShowName;
            if (msg.MessageType == MessageType.Text)
            {
                content = $"收到“{showName}”的消息,{msg.Content}";
            }
            else
            {
                content = $"收到“{showName}”的消息,消息类型:{msg.MessageType.ToString()}";
            }
            client.SendMessage(me.UserName, content);
        }
 
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="command"></param>
        private void ExecuteCommand(string command)
        {
            Trace.Info($"命令:{command}");
        }
 
        #region singleton
        private static WechatProvider instance;
        private static object lockHelper = new object();
        public static WechatProvider Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (lockHelper)
                    {
                        if (instance == null)
                        {
                            instance = new WechatProvider();
                        }
                    }
                }
                return instance;
            }
        }
        
        #endregion
    }
}