759 lines
36 KiB
C#
759 lines
36 KiB
C#
using HPSocketCS;
|
||
using Newtonsoft.Json;
|
||
using Newtonsoft.Json.Linq;
|
||
using PCRobot.Pack;
|
||
using PCRobot.WechatApi;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Diagnostics;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Runtime.InteropServices;
|
||
using System.Text;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
using static PCRobot.WechatApi.NMHelper;
|
||
|
||
namespace PCRobot
|
||
{
|
||
public class NmClient
|
||
{
|
||
private static readonly object SequenceLock = new object();
|
||
HPSocketCS.TcpPackServer HP_Server = new HPSocketCS.TcpPackServer();
|
||
NMHelper nmHeler = new NMHelper();
|
||
public NMCore nmCore { get; set; }
|
||
public string NewLine;
|
||
|
||
public static bool isReady { get; set; }
|
||
|
||
Action<nmWxidInfo> LgCall;
|
||
|
||
nmWxidInfo curWx;
|
||
|
||
public void IniWechat(SocketClient skc, Action<nmWxidInfo> LgCall)
|
||
{
|
||
if (!isReady)
|
||
{
|
||
NewLine = Environment.NewLine;
|
||
this.skc = skc;
|
||
this.LgCall = LgCall;
|
||
HP_Server.OnAccept += new ServerEvent.OnAcceptEventHandler(OnAccept);
|
||
HP_Server.OnReceive += new ServerEvent.OnReceiveEventHandler(OnReceive);
|
||
HP_Server.OnClose += new ServerEvent.OnCloseEventHandler(OnClose);
|
||
HP_Server.MaxPackSize = 0x3FFFFF;
|
||
HP_Server.IpAddress = "127.0.0.1";
|
||
HP_Server.Port = 0;
|
||
if (HP_Server.Start() == true)
|
||
{
|
||
isReady = true;
|
||
|
||
nmCore = new NMCore(HP_Server);
|
||
string szIPAddress = string.Empty;
|
||
ushort dwPort = 0;
|
||
HP_Server.GetListenAddress(ref szIPAddress, ref dwPort);
|
||
if (dwPort == 0)
|
||
{
|
||
//MessageBox.Show("通信端口创建失败,请重试!", "错误");
|
||
//Environment.Exit(0);
|
||
}
|
||
nmHeler.Socket_Port = dwPort;
|
||
WriteLog("[初始化]" + NewLine + "绑定地址:" + szIPAddress + NewLine + "通信端口:" + nmHeler.Socket_Port.ToString());
|
||
}
|
||
else
|
||
{
|
||
//MessageBox.Show("创建通信服务失败,请重试!", "错误");
|
||
//Environment.Exit(0);
|
||
}
|
||
//byte[] bytPath = new byte[260];
|
||
//if (NMHelper.NM_GetWeChatPath(bytPath, 260) == true)
|
||
//{
|
||
// string Path = System.Text.Encoding.GetEncoding("GB2312").GetString(bytPath, 0, bytPath.Length);
|
||
// text_WeChatPath.Text = Path;
|
||
//}
|
||
|
||
NMHelper.NM_LinkWeChat(201912121, nmHeler.Socket_Port, 0, 0);
|
||
}
|
||
|
||
|
||
}
|
||
|
||
HandleResult OnAccept(IServer sender, IntPtr connId, IntPtr pClient)
|
||
{
|
||
nmCore.NM_Init((int)connId, "C#-275569550");
|
||
return HandleResult.Ok;
|
||
}
|
||
|
||
HandleResult OnReceive(IServer sender, IntPtr connId, byte[] bytes)
|
||
{
|
||
string s_Recv = Encoding.Default.GetString(bytes);
|
||
JObject RecvJson = JObject.Parse(s_Recv);
|
||
int RecvType;
|
||
if (nmHeler.Json_GetInt(RecvJson, "PackType", out RecvType) == true)
|
||
{
|
||
if (RecvType == NMHelper.Recv_QRCode)
|
||
{
|
||
Event_QRCode(RecvJson);
|
||
}
|
||
else if (RecvType == NMHelper.Recv_Login)
|
||
{
|
||
Event_Login((int)connId, RecvJson);
|
||
}
|
||
else if (RecvType == NMHelper.Recv_Logout)
|
||
{
|
||
Event_Logout((int)connId, RecvJson);
|
||
}
|
||
else if (RecvType == NMHelper.Recv_Msg)
|
||
{
|
||
Event_RecvMsg((int)connId, RecvJson);
|
||
}
|
||
else if (RecvType == NMHelper.Recv_SelectTalker)
|
||
{
|
||
Event_SelectTalker((int)connId, RecvJson);
|
||
}
|
||
else if (RecvType == NMHelper.NM_GetContactList)
|
||
{
|
||
Event_RecvContactList((int)connId, RecvJson);
|
||
}
|
||
else if (RecvType == NMHelper.NM_GetGroupMember)
|
||
{
|
||
Event_RecvGroupMember((int)connId, RecvJson);
|
||
}
|
||
else if (RecvType == NMHelper.NM_GetWxidInfo)
|
||
{
|
||
Event_RecvWxidInfo((int)connId, RecvJson);
|
||
}
|
||
else if (RecvType == NMHelper.NM_AddFriend)
|
||
{
|
||
Event_AddFriend((int)connId, RecvJson);
|
||
}
|
||
else if (RecvType == NMHelper.NM_CreateGroup)
|
||
{
|
||
Event_CreateGroup((int)connId, RecvJson);
|
||
}
|
||
else if (RecvType == NMHelper.NM_GetFavList)
|
||
{
|
||
Event_GetFavList((int)connId, RecvJson);
|
||
}
|
||
else if (RecvType == NMHelper.NM_GetA8Key)
|
||
{
|
||
Event_GetA8Key((int)connId, RecvJson);
|
||
}
|
||
else if (RecvType == NMHelper.NM_InvitePull)
|
||
{
|
||
Event_GroupInvite((int)connId, RecvJson);
|
||
}
|
||
else if (RecvType == NMHelper.NM_QuitGroup)
|
||
{
|
||
Event_GroupQuit((int)connId, RecvJson);
|
||
}
|
||
}
|
||
return HandleResult.Ok;
|
||
}
|
||
|
||
HandleResult OnClose(IServer sender, IntPtr connId, SocketOperation enOperation, int errorCode)
|
||
{
|
||
Event_Logout((int)connId, new JObject());
|
||
return HandleResult.Ok;
|
||
}
|
||
|
||
|
||
SocketClient skc;
|
||
|
||
|
||
/// <summary>
|
||
/// 通信事件_接收登录二维码
|
||
/// </summary>
|
||
/// <param name="RecvJson"></param>
|
||
void Event_QRCode(JObject RecvJson)
|
||
{
|
||
int dwPid = 0;
|
||
nmHeler.Json_GetInt(RecvJson, "ProcessId", out dwPid);
|
||
string B64_QRCode = nmHeler.Json_GetString(RecvJson, "QRCode", false);
|
||
if (B64_QRCode != string.Empty)
|
||
{
|
||
byte[] imageBytes = Convert.FromBase64String(B64_QRCode);
|
||
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
|
||
ms.Write(imageBytes, 0, imageBytes.Length);
|
||
//pictureBox1.Image = System.Drawing.Image.FromStream(ms, true);
|
||
WriteLog("[二维码]" + NewLine + "接收来自进程(PID:" + dwPid.ToString() + ")的登录二维码");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 通信事件_登录成功
|
||
/// </summary>
|
||
/// <param name="ConnID"></param>
|
||
/// <param name="RecvJson"></param>
|
||
void Event_Login(int ConnID, JObject RecvJson)
|
||
{
|
||
lock (SequenceLock)
|
||
{
|
||
NMHelper.nmWxidInfo WxidInfo = new NMHelper.nmWxidInfo();
|
||
WxidInfo.Nick = nmHeler.Json_GetString(RecvJson, "Nick", true);
|
||
WxidInfo.Wxid = nmHeler.Json_GetString(RecvJson, "Wxid", true);
|
||
WxidInfo.WxNo = nmHeler.Json_GetString(RecvJson, "WxNo", true);
|
||
WxidInfo.HeadIMG = nmHeler.Json_GetString(RecvJson, "HeadIMG", true);
|
||
WxidInfo.handle = (int)DwPid;
|
||
WxidInfo.socketHandle = ConnID;
|
||
curWx = WxidInfo;
|
||
LgCall(curWx);
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 通信事件_退出登录
|
||
/// </summary>
|
||
/// <param name="ConnID"></param>
|
||
/// <param name="RecvJson"></param>
|
||
void Event_Logout(int ConnID, JObject RecvJson)
|
||
{
|
||
//int dwRowIndex = nmHeler.FindRowIndex(list_Login, ConnID.ToString(), 0);
|
||
//if (dwRowIndex != -1)
|
||
//{
|
||
// if (ConnID == nmHeler.Socket_ConnId)
|
||
// {
|
||
// nmHeler.Socket_ConnId = 0;
|
||
// groupBox1.Text = "登录列表";
|
||
// }
|
||
// string szNick = list_Login.Items[dwRowIndex].SubItems[1].Text;
|
||
// string szWxid = list_Login.Items[dwRowIndex].SubItems[2].Text;
|
||
// list_Login.Items.RemoveAt(dwRowIndex);
|
||
// WriteLog("[微信退出]" + NewLine + "昵称:" + szNick + NewLine + "Wxid:" + szWxid);
|
||
//}
|
||
}
|
||
/// <summary>
|
||
/// 通信事件_接收消息
|
||
/// </summary>
|
||
/// <param name="ConnID"></param>
|
||
/// <param name="RecvJson"></param>
|
||
void Event_RecvMsg(int ConnID, JObject RecvJson)
|
||
{
|
||
NMHelper.nmMsgInfo MsgInfo = new NMHelper.nmMsgInfo();
|
||
MsgInfo.RecvWxid = nmHeler.Json_GetString(RecvJson, "RecvWxid", true);
|
||
MsgInfo.Type = nmHeler.Json_GetIntEx(RecvJson, "Type");
|
||
MsgInfo.State = nmHeler.Json_GetIntEx(RecvJson, "State");
|
||
MsgInfo.Time = nmHeler.Json_GetIntEx(RecvJson, "Time");
|
||
MsgInfo.IsSend = nmHeler.Json_GetIntEx(RecvJson, "Sender");
|
||
MsgInfo.LocalMsgID = nmHeler.Json_GetIntEx(RecvJson, "LocalID");
|
||
MsgInfo.ServerMsgID = nmHeler.Json_GetString(RecvJson, "ServerID", false);
|
||
MsgInfo.Wxid_1 = nmHeler.Json_GetString(RecvJson, "Wxid_1", true);
|
||
MsgInfo.Nick_1 = nmHeler.Json_GetString(RecvJson, "Nick_1", true);
|
||
MsgInfo.Wxid_2 = nmHeler.Json_GetString(RecvJson, "Wxid_2", true);
|
||
MsgInfo.Nick_2 = nmHeler.Json_GetString(RecvJson, "Nick_2", true);
|
||
MsgInfo.Msg = nmHeler.Json_GetString(RecvJson, "Msg", true);
|
||
MsgInfo.Source = nmHeler.Json_GetString(RecvJson, "Source", true);
|
||
|
||
string outLog = (MsgInfo.IsSend > 0 ? "[发出消息]" : "[接收消息]") + MsgInfo.RecvWxid + NewLine;
|
||
outLog += "类型:" + MsgInfo.Type.ToString() + " 状态:" + MsgInfo.State.ToString() + " 时间:" + nmHeler.Timestamp2DataTime(MsgInfo.Time) + NewLine;
|
||
outLog += "本地ID:" + MsgInfo.LocalMsgID.ToString() + " 服务器ID:" + MsgInfo.ServerMsgID + NewLine;
|
||
outLog += "Wxid1:" + MsgInfo.Wxid_1 + (MsgInfo.Nick_1 == String.Empty ? string.Empty : "(" + MsgInfo.Nick_1 + ")") + NewLine;
|
||
outLog += "Wxid2:" + MsgInfo.Wxid_2 + (MsgInfo.Nick_2 == String.Empty ? string.Empty : "(" + MsgInfo.Nick_2 + ")") + NewLine;
|
||
outLog += "消息内容:" + MsgInfo.Msg + NewLine + "附加消息:" + MsgInfo.Source + NewLine;
|
||
|
||
if (MsgInfo.Type == 43 || MsgInfo.Type == 49)
|
||
{
|
||
string FilePath = nmHeler.Json_GetString(RecvJson, "File", true);
|
||
if (FilePath != string.Empty)
|
||
{
|
||
outLog += "文件路径:" + FilePath;
|
||
}
|
||
}
|
||
else if (MsgInfo.Type == 3 || MsgInfo.Type == 34)
|
||
{
|
||
string FileData = nmHeler.Json_GetString(RecvJson, "File", false);
|
||
if (FileData != string.Empty)
|
||
{
|
||
byte[] FileBytes = Convert.FromBase64String(FileData);
|
||
string FilePath = nmHeler.SaveFile(MsgInfo.RecvWxid, MsgInfo.Wxid_1, (MsgInfo.Type == 3 ? "IMAGE" : "VOICE"), MsgInfo.ServerMsgID, (MsgInfo.Type == 3 ? "jpg" : "silk"), FileBytes);
|
||
outLog += "文件路径:" + FilePath;
|
||
|
||
//if (MsgInfo.Type == 3 && chk_MaxPicture.Checked == true && MsgInfo.IsSend == 0)
|
||
//{
|
||
// string MaxFilePath = FilePath.Replace(MsgInfo.ServerMsgID, MsgInfo.ServerMsgID + "_MAX");
|
||
// nmCore.NM_GetImageByCDN(ConnID, MsgInfo.Msg, MaxFilePath);
|
||
//}
|
||
}
|
||
}
|
||
WriteLog(outLog+"@####");
|
||
|
||
//自动通过好友
|
||
if (MsgInfo.Type == 37)
|
||
{
|
||
Console.WriteLine("@@@@@ = " + MsgInfo.Msg);
|
||
|
||
nmCore.NM_PassApply(ConnID, MsgInfo.Msg);
|
||
|
||
|
||
}
|
||
if (MsgInfo.Type == 1)
|
||
{
|
||
Console.WriteLine("####### = " + MsgInfo.Msg);
|
||
if (skc != null && MsgInfo.IsSend == 0)
|
||
{
|
||
WechatReceiveMsg msg = new WechatReceiveMsg();
|
||
msg.Cmd = PCRobotCMD.rcvTxt;
|
||
msg.IsSend = MsgInfo.IsSend == 1 ? true : false;
|
||
msg.RobotUsername = curWx.Wxid;
|
||
msg.RobotUsernick = curWx.Nick;
|
||
|
||
msg.RobotType = RobotType.客户端微信;
|
||
msg.FromMessageType = WechatMsgType.文本;
|
||
|
||
|
||
|
||
if (MsgInfo.Wxid_1.Contains("@chatroom"))
|
||
{
|
||
msg.FromGroupid = MsgInfo.Wxid_1;
|
||
|
||
msg.FromUsername = MsgInfo.Wxid_2;
|
||
msg.FromUsernick = MsgInfo.Wxid_2;
|
||
}
|
||
else if (MsgInfo.Wxid_2.Contains("@chatroom"))
|
||
{
|
||
msg.FromGroupid = MsgInfo.Wxid_2;
|
||
|
||
msg.FromUsername = MsgInfo.Wxid_1;
|
||
msg.FromUsernick = MsgInfo.Wxid_1;
|
||
}
|
||
else
|
||
{
|
||
msg.FromUsername = MsgInfo.Wxid_1;
|
||
msg.FromUsernick = MsgInfo.Wxid_1;
|
||
}
|
||
msg.FromMessage = MsgInfo.Msg;
|
||
msg.FromUsernick = "";
|
||
skc.Send(msg);
|
||
}
|
||
}
|
||
|
||
//自动填充消息内容,用于撤回消息
|
||
if (MsgInfo.IsSend == 1 && ConnID == nmHeler.Socket_ConnId)
|
||
{
|
||
//text_Recvke_Wxid.Text = MsgInfo.Wxid_1;
|
||
//text_Recvke_Sid.Text = MsgInfo.ServerMsgID;
|
||
//text_Recvke_Message.Text = MsgInfo.Msg;
|
||
}
|
||
//转账自动收款
|
||
if (MsgInfo.IsSend == 0 && MsgInfo.Type == 49)
|
||
{
|
||
//if (MsgInfo.Msg.IndexOf("微信转账") != -1 && MsgInfo.Msg.IndexOf("收到转账") != -1)
|
||
//{
|
||
// string State = nmHeler.StringMid(MsgInfo.Msg, "<paysubtype>", "</paysubtype>");
|
||
// string Transid = nmHeler.StringMid(MsgInfo.Msg, "transferid><![CDATA[", "]]></transferid>");
|
||
// int dwTime = 0;
|
||
// int.TryParse(nmHeler.StringMid(MsgInfo.Msg, "<invalidtime><![CDATA[", "]]></invalidtime>"), out dwTime);
|
||
// if (State == "1" && Transid != string.Empty)
|
||
// {
|
||
// nmCore.NM_GetMoney(ConnID, MsgInfo.Wxid_1, Transid, dwTime);
|
||
// }
|
||
//}
|
||
}
|
||
//名片消息自动填充数据
|
||
if (MsgInfo.IsSend == 0 && MsgInfo.Type == 42 && ConnID == nmHeler.Socket_ConnId)
|
||
{
|
||
//string v1 = nmHeler.StringMid(MsgInfo.Msg, "username=\"", "\" nickname=");
|
||
//string v2 = nmHeler.StringMid(MsgInfo.Msg, "antispamticket=\"", "\" /");
|
||
//text_Friend_v1.Text = v1;
|
||
//text_Friend_v2.Text = v2;
|
||
}
|
||
//自动接受群聊邀请
|
||
if (MsgInfo.IsSend == 0 && MsgInfo.Type == 49)
|
||
{
|
||
//if (MsgInfo.Msg.IndexOf("邀请你加入群聊") != -1 && MsgInfo.Msg.IndexOf("mmsupport-bin/addchatroombyinvite") != -1)
|
||
//{
|
||
// string InviteURL = nmHeler.StringMid(MsgInfo.Msg, "<url><![CDATA[", "]]></url>");
|
||
// if (InviteURL != string.Empty)
|
||
// {
|
||
// nmCore.NM_GetA8Key(ConnID, MsgInfo.Wxid_1, InviteURL);
|
||
// }
|
||
//}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 通信事件_切换聊天对象
|
||
/// </summary>
|
||
/// <param name="ConnID"></param>
|
||
/// <param name="RecvJson"></param>
|
||
void Event_SelectTalker(int ConnID, JObject RecvJson)
|
||
{
|
||
NMHelper.nmWxidInfo WxidInfo = new NMHelper.nmWxidInfo();
|
||
string RecvWxid = nmHeler.Json_GetString(RecvJson, "RecvWxid", true);
|
||
WxidInfo.Wxid = nmHeler.Json_GetString(RecvJson, "Wxid", true);
|
||
WxidInfo.WxNo = nmHeler.Json_GetString(RecvJson, "WxNo", true);
|
||
WxidInfo.Nick = nmHeler.Json_GetString(RecvJson, "Nick", true);
|
||
WxidInfo.Mark = nmHeler.Json_GetString(RecvJson, "Mark", true);
|
||
WxidInfo.HeadIMG = nmHeler.Json_GetString(RecvJson, "HeadIMG", true);
|
||
string outLog = "[切换聊天对象]" + RecvWxid + NewLine;
|
||
outLog += "Wxid:" + WxidInfo.Wxid + NewLine;
|
||
outLog += WxidInfo.WxNo == string.Empty ? string.Empty : "微信号:" + WxidInfo.Wxid + NewLine;
|
||
outLog += "昵称:" + WxidInfo.Nick + NewLine;
|
||
outLog += WxidInfo.Mark == string.Empty ? string.Empty : "备注:" + WxidInfo.Mark;
|
||
//outLog += "头像:" + WxidInfo.HeadIMG;
|
||
WriteLog(outLog);
|
||
}
|
||
|
||
void Event_RecvContactList(int ConnID, JObject RecvJson)
|
||
{
|
||
//string outLog = "[通讯录列表]" + nmHeler.Json_GetString(RecvJson, "RecvWxid", true) + NewLine;
|
||
//int dwState = nmHeler.Json_GetIntEx(RecvJson, "State");
|
||
//if (dwState == 0)
|
||
//{
|
||
// outLog += "通讯录列表读取失败!";
|
||
// WriteLog(outLog);
|
||
// return;
|
||
//}
|
||
//JArray m_List = RecvJson.Value<JArray>("List");
|
||
//if (m_List != null)
|
||
//{
|
||
// list_Friend.BeginUpdate(); list_Group.BeginUpdate(); list_GHFriend.BeginUpdate();
|
||
// list_Friend.Items.Clear(); list_Group.Items.Clear(); list_GHFriend.Items.Clear();
|
||
// for (int i = 0; i < m_List.Count; i++)
|
||
// {
|
||
// JToken JtMember = m_List.ElementAt(i);
|
||
// if (JtMember != null)
|
||
// {
|
||
// NMHelper.nmWxidInfo MemberInfo = new NMHelper.nmWxidInfo();
|
||
// MemberInfo.Type = nmHeler.Json_GetIntEx((JObject)JtMember, "Type");
|
||
// MemberInfo.List = nmHeler.Json_GetIntEx((JObject)JtMember, "List");
|
||
// MemberInfo.Wxid = nmHeler.Json_GetString((JObject)JtMember, "Wxid", true);
|
||
// MemberInfo.WxNo = nmHeler.Json_GetString((JObject)JtMember, "WxNo", true);
|
||
// MemberInfo.Nick = nmHeler.Json_GetString((JObject)JtMember, "Nick", true);
|
||
// MemberInfo.Mark = nmHeler.Json_GetString((JObject)JtMember, "Mark", true);
|
||
// MemberInfo.HeadIMG = nmHeler.Json_GetString((JObject)JtMember, "HeadIMG", true);
|
||
// ListViewItem UserItem = new ListViewItem();
|
||
|
||
// if (MemberInfo.Type == 8 || MemberInfo.Type == 24 || MemberInfo.Type == 29)
|
||
// {
|
||
// UserItem.Text = MemberInfo.Nick;
|
||
// UserItem.SubItems.Add(MemberInfo.Wxid);
|
||
// UserItem.SubItems.Add(MemberInfo.WxNo);
|
||
// UserItem.SubItems.Add(MemberInfo.HeadIMG);
|
||
// list_GHFriend.Items.Add(UserItem);
|
||
// }
|
||
// else if (MemberInfo.Wxid.LastIndexOf("@chatroom") != -1)
|
||
// {
|
||
// UserItem.Text = MemberInfo.Nick == string.Empty ? "群聊" : MemberInfo.Nick;
|
||
// UserItem.SubItems.Add(MemberInfo.Wxid);
|
||
// UserItem.SubItems.Add(MemberInfo.WxNo);
|
||
// UserItem.SubItems.Add(MemberInfo.List.ToString());
|
||
// UserItem.SubItems.Add(MemberInfo.HeadIMG);
|
||
// list_Group.Items.Add(UserItem);
|
||
// }
|
||
// else if (MemberInfo.List != 2)
|
||
// {
|
||
// UserItem.Text = MemberInfo.Nick;
|
||
// UserItem.SubItems.Add(MemberInfo.Mark);
|
||
// UserItem.SubItems.Add(MemberInfo.Wxid);
|
||
// UserItem.SubItems.Add(MemberInfo.WxNo);
|
||
// UserItem.SubItems.Add(MemberInfo.HeadIMG);
|
||
// list_Friend.Items.Add(UserItem);
|
||
// }
|
||
// }
|
||
// }
|
||
// list_Friend.EndUpdate(); list_Group.EndUpdate(); list_GHFriend.EndUpdate();
|
||
// outLog += "通讯录列表读取成功";
|
||
// outLog += ",好友:" + list_Friend.Items.Count.ToString();
|
||
// outLog += ",群聊:" + list_Group.Items.Count.ToString();
|
||
// outLog += ",好友:" + list_GHFriend.Items.Count.ToString();
|
||
// WriteLog(outLog);
|
||
//}
|
||
}
|
||
|
||
void Event_RecvGroupMember(int ConnID, JObject RecvJson)
|
||
{
|
||
//string outLog = "[群员列表]" + nmHeler.Json_GetString(RecvJson, "RecvWxid", true) + NewLine;
|
||
//int dwState = nmHeler.Json_GetIntEx(RecvJson, "State");
|
||
//if (dwState == 0)
|
||
//{
|
||
// outLog += nmHeler.Json_GetString(RecvJson, "GroupID", false) + "的群员列表读取失败!";
|
||
// WriteLog(outLog);
|
||
// return;
|
||
//}
|
||
//JArray m_List = RecvJson.Value<JArray>("List");
|
||
//if (m_List != null)
|
||
//{
|
||
// list_GroupMember.BeginUpdate(); list_GroupMember.Items.Clear();
|
||
// for (int i = 0; i < m_List.Count; i++)
|
||
// {
|
||
// JToken JtMember = m_List.ElementAt(i);
|
||
// if (JtMember != null)
|
||
// {
|
||
// NMHelper.nmWxidInfo MemberInfo = new NMHelper.nmWxidInfo();
|
||
// MemberInfo.Wxid = nmHeler.Json_GetString((JObject)JtMember, "Wxid", true);
|
||
// MemberInfo.Nick = nmHeler.Json_GetString((JObject)JtMember, "Nick", true);
|
||
// MemberInfo.Mark = nmHeler.Json_GetString((JObject)JtMember, "Mark", true);
|
||
// MemberInfo.HeadIMG = nmHeler.Json_GetString((JObject)JtMember, "HeadIMG", true);
|
||
// ListViewItem UserItem = new ListViewItem();
|
||
// UserItem.Text = MemberInfo.Nick;
|
||
// UserItem.SubItems.Add(MemberInfo.Mark);
|
||
// UserItem.SubItems.Add(MemberInfo.Wxid);
|
||
// UserItem.SubItems.Add(MemberInfo.HeadIMG);
|
||
// list_GroupMember.Items.Add(UserItem);
|
||
// }
|
||
// }
|
||
// list_GroupMember.EndUpdate();
|
||
// outLog += nmHeler.Json_GetString(RecvJson, "GroupID", true) + "的群员列表读取成功,群员数:" + list_GroupMember.Items.Count.ToString();
|
||
// WriteLog(outLog);
|
||
//}
|
||
}
|
||
|
||
void Event_RecvWxidInfo(int ConnID, JObject RecvJson)
|
||
{
|
||
//string outLog = "[下载详细信息]" + nmHeler.Json_GetString(RecvJson, "RecvWxid", true) + NewLine;
|
||
//int dwState = nmHeler.Json_GetIntEx(RecvJson, "State");
|
||
//if (dwState == -1) { outLog += "下载详细信息超时(原因:服务器无返回)!"; }
|
||
//else if (dwState == 0) { outLog += "下载详细信息失败(原因:可能是输入的Wxid不正确)!"; }
|
||
//else if (dwState == 1)
|
||
//{
|
||
// string szGroupID = nmHeler.Json_GetString(RecvJson, "GroupId", true);
|
||
// NMHelper.nmWxidInfo MemberInfo = new NMHelper.nmWxidInfo();
|
||
// MemberInfo.Wxid = nmHeler.Json_GetString(RecvJson, "Wxid", true);
|
||
// MemberInfo.WxNo = nmHeler.Json_GetString(RecvJson, "WxNo", true);
|
||
// MemberInfo.Nick = nmHeler.Json_GetString(RecvJson, "Nick", true);
|
||
// MemberInfo.V1 = nmHeler.Json_GetString(RecvJson, "v1", true);
|
||
// MemberInfo.HeadIMG = nmHeler.Json_GetString(RecvJson, "HeadIMG", true);
|
||
// MemberInfo.Address = nmHeler.Json_GetString(RecvJson, "Address", true);
|
||
// MemberInfo.Sex = MemberInfo.GetSex(nmHeler.Json_GetIntEx(RecvJson, "Sex"));
|
||
|
||
// outLog += szGroupID == string.Empty ? string.Empty : "群ID:" + szGroupID + NewLine;
|
||
// outLog += "Wxid:" + MemberInfo.Wxid + NewLine;
|
||
// outLog += MemberInfo.WxNo == string.Empty ? string.Empty : "微信号:" + MemberInfo.WxNo + NewLine;
|
||
// outLog += "昵称:" + MemberInfo.Nick + NewLine;
|
||
// outLog += MemberInfo.V1 == string.Empty ? string.Empty : "v1:" + MemberInfo.V1 + NewLine;
|
||
// outLog += "性别:" + MemberInfo.Sex + NewLine;
|
||
// outLog += MemberInfo.Address == string.Empty ? string.Empty : "地区:" + MemberInfo.Address + NewLine;
|
||
// outLog += MemberInfo.HeadIMG == string.Empty ? string.Empty : "头像:" + MemberInfo.HeadIMG + NewLine;
|
||
//}
|
||
//WriteLog(outLog);
|
||
}
|
||
|
||
void Event_AddFriend(int ConnID, JObject RecvJson)
|
||
{
|
||
//string outLog = "[添加好友]" + nmHeler.Json_GetString(RecvJson, "RecvWxid", true) + NewLine;
|
||
//int DataType = nmHeler.Json_GetIntEx(RecvJson, "DataType");
|
||
//string Data = nmHeler.Json_GetString(RecvJson, "Data", true);
|
||
//int Type = nmHeler.Json_GetIntEx(RecvJson, "Type");
|
||
//int AddType = nmHeler.Json_GetIntEx(RecvJson, "AddType");
|
||
//string Wxid = nmHeler.Json_GetString(RecvJson, "Wxid", true);
|
||
//string v2 = nmHeler.Json_GetString(RecvJson, "V2", true);
|
||
//string Message = nmHeler.Json_GetString(RecvJson, "Message", true);
|
||
|
||
//outLog += "添加返回:[" + DataType.ToString() + "]" + (DataType == 0 ? "已发送好友请求或已添加好友。" : Data) + NewLine;
|
||
//outLog += "添加类型:" + Type.ToString() + " 添加来源:" + AddType.ToString() + NewLine;
|
||
//outLog += "Wxid:" + Wxid;
|
||
//outLog += v2 == string.Empty ? string.Empty : NewLine + "v2:" + v2;
|
||
//outLog += Message == string.Empty ? string.Empty : NewLine + "附加消息:" + Message;
|
||
//WriteLog(outLog);
|
||
|
||
//if (Type == 1 && DataType == -44)
|
||
//{
|
||
// nmCore.NM_AddFriend(ConnID, 2, AddType, Wxid, v2, text_Friend_Message.Text);
|
||
//}
|
||
}
|
||
|
||
void Event_CreateGroup(int ConnID, JObject RecvJson)
|
||
{
|
||
//string outLog = "[创建群聊]" + nmHeler.Json_GetString(RecvJson, "RecvWxid", true) + NewLine;
|
||
//string Data = nmHeler.Json_GetString(RecvJson, "Data", true);
|
||
//string GroupID = nmHeler.Json_GetString(RecvJson, "GroupID", true);
|
||
//outLog += "建群返回:" + Data;
|
||
//outLog += GroupID == string.Empty ? string.Empty : NewLine + "群聊ID:" + GroupID + NewLine;
|
||
//JArray MemberList = RecvJson.Value<JArray>("List");
|
||
//if (MemberList != null)
|
||
//{
|
||
// outLog += "群员列表:";
|
||
// for (int i = 0; i < MemberList.Count; i++)
|
||
// {
|
||
// JToken JtMember = MemberList.ElementAt(i);
|
||
// if (JtMember != null)
|
||
// {
|
||
// string Wxid = nmHeler.Json_GetString((JObject)JtMember, "Wxid", true);
|
||
// string Nick = nmHeler.Json_GetString((JObject)JtMember, "Nick", true);
|
||
// int state = nmHeler.Json_GetIntEx((JObject)JtMember, "State");
|
||
// if (Wxid != string.Empty)
|
||
// {
|
||
// outLog += NewLine + "[" + state.ToString() + "]" + (state == 0 ? "[成功]" : "[失败]");
|
||
// outLog += Wxid + (Nick == string.Empty ? string.Empty : "(" + Nick + ")");
|
||
// }
|
||
// }
|
||
// }
|
||
//}
|
||
//WriteLog(outLog);
|
||
}
|
||
|
||
void Event_GetFavList(int ConnID, JObject RecvJson)
|
||
{
|
||
//string outLog = "[收藏列表]" + nmHeler.Json_GetString(RecvJson, "RecvWxid", true) + NewLine;
|
||
//JArray FavList = RecvJson.Value<JArray>("List");
|
||
//if (FavList != null)
|
||
//{
|
||
// list_Fav.Items.Clear();
|
||
// int dwCount = FavList.Count;
|
||
// if (dwCount != 0)
|
||
// {
|
||
// list_Fav.BeginUpdate();
|
||
|
||
// for (int i = 0; i < dwCount; i++)
|
||
// {
|
||
// JToken JtMember = FavList.ElementAt(i);
|
||
// if (JtMember != null)
|
||
// {
|
||
// int Type = nmHeler.Json_GetIntEx((JObject)JtMember, "Type");
|
||
// string SearchKey = nmHeler.Json_GetString((JObject)JtMember, "SearchKey", true);
|
||
// string TypeName = nmHeler.GetFavType(Type);
|
||
// ListViewItem FavItem = new ListViewItem();
|
||
// FavItem.Text = i.ToString();
|
||
// FavItem.SubItems.Add(TypeName);
|
||
// FavItem.SubItems.Add(SearchKey == string.Empty ? "[" + TypeName + "文件]" : SearchKey);
|
||
// list_Fav.Items.Add(FavItem);
|
||
// }
|
||
// }
|
||
// list_Fav.EndUpdate();
|
||
// outLog += "收藏列表读取成功,共" + dwCount.ToString() + "条记录!";
|
||
// }
|
||
// else
|
||
// {
|
||
// outLog += "没有收藏";
|
||
// }
|
||
//}
|
||
//WriteLog(outLog);
|
||
}
|
||
|
||
void Event_GetA8Key(int ConnID, JObject RecvJson)
|
||
{
|
||
//string RecvWxid = nmHeler.Json_GetString(RecvJson, "RecvWxid", true);
|
||
//string outLog = "[GetA8Key]" + RecvWxid + NewLine;
|
||
//string URL = nmHeler.Json_GetString(RecvJson, "URL", true);
|
||
//string LastURL = nmHeler.Json_GetString(RecvJson, "LastURL", true);
|
||
//outLog += "获取链接:" + URL + NewLine;
|
||
//outLog += "返回链接:" + LastURL;
|
||
//WriteLog(outLog);
|
||
|
||
//if (URL.IndexOf("mmsupport-bin/addchatroombyinvite") != -1)
|
||
//{
|
||
// HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(URL);
|
||
// Request.Method = "POST";
|
||
// Request.AllowAutoRedirect = false;
|
||
// Request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
|
||
// Request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 MicroMessenger/6.5.2.501 NetType/WIFI WindowsWechat QBCore/3.43.556.400 QQBrowser/9.0.2524.400";
|
||
// Request.Referer = URL;
|
||
// Request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8,en-us;q=0.6,en;q=0.5;q=0.4");
|
||
// HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
|
||
// string hLocation = Response.GetResponseHeader("Location").ToString();
|
||
// if (hLocation != "")
|
||
// {
|
||
// if (hLocation.IndexOf("chatroom") != -1)
|
||
// {
|
||
// string GroupID = nmHeler.StringMid(hLocation, "weixin://jump/mainframe/", "@chatroom");
|
||
// WriteLog("[接受群聊邀请]" + RecvWxid + NewLine + "群聊ID:" + GroupID + "@chatroom");
|
||
// }
|
||
// }
|
||
//}
|
||
}
|
||
|
||
void Event_GroupInvite(int ConnID, JObject RecvJson)
|
||
{
|
||
string RecvWxid = nmHeler.Json_GetString(RecvJson, "RecvWxid", true);
|
||
string outLog = "[群员动态-进群]" + RecvWxid + NewLine;
|
||
string GroupID = nmHeler.Json_GetString(RecvJson, "GroupID", true);
|
||
outLog = "群聊ID:" + GroupID + NewLine;
|
||
JArray MemberList = RecvJson.Value<JArray>("List");
|
||
if (MemberList != null)
|
||
{
|
||
for (int i = 0; i < MemberList.Count; i++)
|
||
{
|
||
JToken JtMember = MemberList.ElementAt(i);
|
||
if (JtMember != null)
|
||
{
|
||
string Wxid = nmHeler.Json_GetString((JObject)JtMember, "Wxid", true);
|
||
string Nick = nmHeler.Json_GetString((JObject)JtMember, "Nick", true);
|
||
string InvWxid = nmHeler.Json_GetString((JObject)JtMember, "InvWxid", true);
|
||
string InvNick = nmHeler.Json_GetString((JObject)JtMember, "InvNick", true);
|
||
outLog += "[" + (i + 1).ToString() + "]" + NewLine;
|
||
outLog += "进群人:" + Wxid + (Nick == string.Empty ? string.Empty : "(" + Nick + ")" + NewLine);
|
||
outLog += "邀请人:" + InvWxid + (InvNick == string.Empty ? string.Empty : "(" + InvNick + ")" + NewLine);
|
||
}
|
||
}
|
||
}
|
||
WriteLog(outLog);
|
||
}
|
||
void Event_GroupQuit(int ConnID, JObject RecvJson)
|
||
{
|
||
string RecvWxid = nmHeler.Json_GetString(RecvJson, "RecvWxid", true);
|
||
string outLog = "[群员动态-退群]" + RecvWxid + NewLine;
|
||
string GroupID = nmHeler.Json_GetString(RecvJson, "GroupID", true);
|
||
outLog += "群聊ID:" + GroupID + NewLine;
|
||
JArray MemberList = RecvJson.Value<JArray>("List");
|
||
if (MemberList != null)
|
||
{
|
||
for (int i = 0; i < MemberList.Count; i++)
|
||
{
|
||
JToken JtMember = MemberList.ElementAt(i);
|
||
if (JtMember != null)
|
||
{
|
||
string Wxid = nmHeler.Json_GetString((JObject)JtMember, "Wxid", true);
|
||
string Nick = nmHeler.Json_GetString((JObject)JtMember, "Nick", true);
|
||
|
||
outLog += "[" + (i + 1).ToString() + "]" + NewLine;
|
||
outLog += Wxid + (Nick == string.Empty ? string.Empty : "(" + Nick + ")" + NewLine);
|
||
}
|
||
}
|
||
}
|
||
WriteLog(outLog);
|
||
}
|
||
|
||
[DllImport("User32.dll", EntryPoint = "PostMessage")]
|
||
public static extern int PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
|
||
|
||
[DllImport("LIB.KTY.WC.dll", EntryPoint = "Run")]
|
||
public static extern IntPtr Run(string path, string dll, int port);
|
||
|
||
private IntPtr DwPid = IntPtr.Zero;
|
||
|
||
public int handle { get { return (int)DwPid; } }
|
||
|
||
public IntPtr StartWechat()
|
||
{
|
||
var path = CsharpHttpHelper.HttpExtend.MapPath("PCWechat\\2.8.0.112");
|
||
string szDllPath = System.IO.Directory.GetCurrentDirectory() + "\\bin\\NanMuCore.dll";
|
||
DwPid = (IntPtr)NMHelper.NM_CreateWeChatProcess(path, "WeChat.exe", szDllPath, nmHeler.Socket_Port);
|
||
return DwPid;
|
||
}
|
||
|
||
public void CloseWechat()
|
||
{
|
||
try
|
||
{
|
||
if (DwPid != IntPtr.Zero)
|
||
{
|
||
PostMessage(DwPid, 18, 0, 0);
|
||
Process[] myproc = Process.GetProcesses();
|
||
foreach (Process item in myproc)
|
||
{
|
||
try
|
||
{
|
||
if (item.Id == (int)DwPid)
|
||
{
|
||
item.Kill();
|
||
break;
|
||
}
|
||
|
||
}
|
||
catch (Exception)
|
||
{
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{
|
||
}
|
||
|
||
}
|
||
|
||
public void WriteLog(string str)
|
||
{
|
||
|
||
}
|
||
}
|
||
}
|