643 lines
33 KiB
C#
643 lines
33 KiB
C#
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.Net;
|
||
using System.Runtime.InteropServices;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
|
||
namespace PCRobot
|
||
{
|
||
public class PCRobotClient
|
||
{
|
||
public static void Test()
|
||
{
|
||
|
||
}
|
||
|
||
public static void Rcv(string str)
|
||
{
|
||
try
|
||
{
|
||
var head = str.Substring(0, str.IndexOf(" "));
|
||
var cmd = (PCRobotCMD)Enum.Parse(typeof(PCRobotCMD), head);
|
||
str = CsharpHttpHelper.HttpHelper.URLDecode(str.Replace(head + " ", ""));
|
||
var msg = JsonConvert.DeserializeObject<WechatReceiveMsg>(str);
|
||
switch (cmd)
|
||
{
|
||
case PCRobotCMD.sendTxt:
|
||
WechatAPI.Wx_SendTextMsg(WechatAPI.Client_Handle, msg.FromUsername, WechatAPI.Client_Wxid, msg.FromMessage);
|
||
break;
|
||
case PCRobotCMD.sendFile:
|
||
//文件接口
|
||
WechatAPI.Wx_SendFileMsg(WechatAPI.Client_Handle, 3, msg.FromUsername, msg.FromMessage);
|
||
break;
|
||
case PCRobotCMD.sendImg:
|
||
byte[] ImageByte = Convert.FromBase64String(msg.FromMessage);
|
||
string temp = System.Environment.GetEnvironmentVariable("TEMP");
|
||
//DirectoryInfo info = new DirectoryInfo(temp);
|
||
string uFile = temp + "//" + Guid.NewGuid().ToString() + ".png";
|
||
File.WriteAllBytes(uFile, ImageByte);
|
||
//图片接口
|
||
WechatAPI.Wx_SendFileMsg(WechatAPI.Client_Handle, 3, msg.FromUsername, uFile);
|
||
break;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine(ex.Message);
|
||
}
|
||
|
||
}
|
||
SocketClient skc;
|
||
Action<string> lc;
|
||
public PCRobotClient(SocketClient skc, Action<string> lc)
|
||
{
|
||
this.skc = skc;
|
||
this.lc = lc;
|
||
}
|
||
|
||
public PCRobotClient()
|
||
{
|
||
|
||
}
|
||
|
||
|
||
private static PCRobotClient Client;
|
||
public static PCRobotClient GetClient()
|
||
{
|
||
if (Client == null) Client = new PCRobotClient();
|
||
return Client;
|
||
}
|
||
[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;
|
||
private static readonly object SequenceLock = new object();
|
||
public static WeChatAPI WechatAPI { get; private set; }
|
||
public void IniWechat()
|
||
{
|
||
WechatAPI = new WeChatAPI();
|
||
WechatAPI.sc = new KstAPI.dele_Server_CallBack(Server_CallBack);
|
||
WechatAPI.cc = new KstAPI.dele_Client_CallBack(Client_CallBack);
|
||
|
||
WechatAPI.Socket_Port = WechatAPI.Socket_Init(Marshal.GetFunctionPointerForDelegate(WechatAPI.sc), Marshal.GetFunctionPointerForDelegate(WechatAPI.cc));
|
||
|
||
if (WechatAPI.Socket_Port == 0) throw new Exception("通信端口创建失败,请重试!");
|
||
|
||
//Console.WriteLine("[初始化完成]" + Environment.NewLine + "服务端口:" + wxAPI.Socket_Port.ToString() + Environment.NewLine + "通信句柄:" + wxAPI.Socket_Handle.ToString());
|
||
|
||
KstAPI.kst_DLLConnect(201906061, WechatAPI.Socket_Port);
|
||
}
|
||
// private static readonly object SequenceLock = new object();
|
||
HPSocketCS.TcpPackServer HP_Server = new HPSocketCS.TcpPackServer();
|
||
NMHelper nmHeler = new NMHelper();
|
||
NMCore nmCore;
|
||
|
||
public IntPtr StartWechat()
|
||
{
|
||
var path = CsharpHttpHelper.HttpExtend.MapPath("WeChat");
|
||
string szDllPath = System.IO.Directory.GetCurrentDirectory() + "\\NanMuCore.dll";
|
||
DwPid = (IntPtr)NMHelper.NM_CreateWeChatProcess(path, "WeChat.exe", szDllPath, nmHeler.Socket_Port);
|
||
//KstAPI.kst_CreateWx(path, "WeChat.exe", "kst.dll",WechatAPI.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)
|
||
{
|
||
}
|
||
|
||
}
|
||
private void Client_CallBack(int so, int sPoint, int type, int buf, int buflen)
|
||
{
|
||
|
||
}
|
||
|
||
private void Server_CallBack(int sPoint, int so, int type, int buf, int buflen, int cso)
|
||
{
|
||
try
|
||
{
|
||
if (type == KstAPI.通信_连接成功)
|
||
{
|
||
WechatAPI.Wx_Init(so, "C#-20190605");
|
||
WechatAPI.Client_Handle = so;
|
||
}
|
||
else if (type == KstAPI.通信_连接断开)
|
||
{
|
||
//int uIndex = FindRowIndex(LOGIN_LIST, so.ToString(), 4);
|
||
//if (uIndex != -1)
|
||
//{
|
||
//if (so == wxAPI.Client_Handle)
|
||
//{
|
||
// LOGIN_TXT_SOCKET.Text = "[未选择]";
|
||
// LOGIN_TXT_NICK.Text = "[未选择]";
|
||
// LOGIN_TXT_WXID.Text = "[未选择]";
|
||
// PIC_HEADIMG.Image = null;
|
||
// wxAPI.Client_Handle = 0;
|
||
// wxAPI.Client_Wxid = "";
|
||
//}
|
||
//LOGIN_LIST.Items.RemoveAt(uIndex);
|
||
//}
|
||
}
|
||
else if (type == KstAPI.通信_数据到达)
|
||
{
|
||
byte[] s_buf = new byte[buflen + 1];
|
||
KstAPI.RtlMoveMemory(s_buf, buf, buflen);
|
||
string s_Recv = Encoding.Default.GetString(s_buf).TrimEnd('\0');
|
||
|
||
//Log(s_Recv);
|
||
|
||
JObject recvJson = JObject.Parse(s_Recv);
|
||
int RecvType = (int)recvJson["packtype"];
|
||
|
||
if (RecvType == KstAPI.回调_二维码)
|
||
{
|
||
string B64_QRCode = recvJson["qrcode"].ToString();
|
||
byte[] imageBytes = Convert.FromBase64String(B64_QRCode);
|
||
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
|
||
ms.Write(imageBytes, 0, imageBytes.Length);
|
||
//PIC_HEADIMG.Image = System.Drawing.Image.FromStream(ms, true);
|
||
}
|
||
else if (RecvType == KstAPI.回调_登录成功)
|
||
{
|
||
lock (SequenceLock)
|
||
{
|
||
ListViewItem UserItem = new ListViewItem();
|
||
|
||
UserItem.Text = recvJson["nick"].ToString();
|
||
UserItem.SubItems.Add(recvJson["wxid"].ToString());
|
||
UserItem.SubItems.Add(recvJson["wxno"].ToString());
|
||
UserItem.SubItems.Add(recvJson["himg"].ToString());
|
||
UserItem.SubItems.Add(so.ToString());
|
||
WechatAPI.Client_Nick= recvJson["nick"].ToString();
|
||
WechatAPI.Client_Wxid = recvJson["wxid"].ToString();
|
||
lc?.Invoke(WechatAPI.Client_Nick + $"({ WechatAPI.Client_Wxid})");
|
||
|
||
//LOGIN_LIST.Items.Add(UserItem);
|
||
}
|
||
}
|
||
else if (RecvType == KstAPI.回调_退出登陆)
|
||
{
|
||
//int uIndex = FindRowIndex(LOGIN_LIST, so.ToString(), 4);
|
||
//if (uIndex != -1)
|
||
//{
|
||
// if (so == wxAPI.Client_Handle)
|
||
// {
|
||
// LOGIN_TXT_SOCKET.Text = "[未选择]";
|
||
// LOGIN_TXT_NICK.Text = "[未选择]";
|
||
// LOGIN_TXT_WXID.Text = "[未选择]";
|
||
// PIC_HEADIMG.Image = null;
|
||
// wxAPI.Client_Handle = 0;
|
||
// wxAPI.Client_Wxid = "";
|
||
// }
|
||
// LOGIN_LIST.Items.RemoveAt(uIndex);
|
||
//}
|
||
}
|
||
else if (RecvType == KstAPI.回调_好友列表)
|
||
{
|
||
JArray uList = recvJson.Value<JArray>("list");
|
||
//FRIEND_LIST.BeginUpdate();
|
||
//FRIEND_GH_LIST.BeginUpdate();
|
||
//GROUP_LIST.BeginUpdate();
|
||
for (int i = 0; i < uList.Count; i++)
|
||
{
|
||
ListViewItem UserItem = new ListViewItem();
|
||
|
||
WeChatAPI.UserInfo uInfo = new WeChatAPI.UserInfo();
|
||
uInfo.Type = (int)recvJson["list"][i]["Type"];
|
||
uInfo.List = (int)recvJson["list"][i]["List"];
|
||
uInfo.Wxid = recvJson["list"][i]["Wxid"].ToString();
|
||
uInfo.WxNo = recvJson["list"][i]["WxNo"].ToString();
|
||
uInfo.Nick = recvJson["list"][i]["Nick"].ToString();
|
||
uInfo.Nick = KstAPI.DeUnicode(uInfo.Nick);
|
||
uInfo.Mark = recvJson["list"][i]["Mark"].ToString();
|
||
uInfo.Mark = KstAPI.DeUnicode(uInfo.Mark);
|
||
uInfo.HImg = recvJson["list"][i]["HImg"].ToString();
|
||
|
||
if (uInfo.Type == 8 || uInfo.Type == 24 || uInfo.Type == 29)
|
||
{//公众号
|
||
UserItem.Text = uInfo.Nick;
|
||
UserItem.SubItems.Add(uInfo.Wxid);
|
||
UserItem.SubItems.Add(uInfo.WxNo);
|
||
UserItem.SubItems.Add(uInfo.HImg);
|
||
//FRIEND_GH_LIST.Items.Add(UserItem);
|
||
}
|
||
else if (uInfo.Wxid.LastIndexOf("@chatroom") != -1)
|
||
{//群
|
||
if (uInfo.Nick == "")
|
||
uInfo.Nick = "群聊";
|
||
UserItem.Text = uInfo.Nick;
|
||
UserItem.SubItems.Add(uInfo.Wxid);
|
||
UserItem.SubItems.Add(uInfo.WxNo);
|
||
UserItem.SubItems.Add(uInfo.List.ToString());
|
||
UserItem.SubItems.Add(uInfo.HImg);
|
||
//GROUP_LIST.Items.Add(UserItem);
|
||
}
|
||
else if (uInfo.List != 2)
|
||
{//好友
|
||
UserItem.Text = uInfo.Nick;
|
||
UserItem.SubItems.Add(uInfo.Mark);
|
||
UserItem.SubItems.Add(uInfo.Wxid);
|
||
UserItem.SubItems.Add(uInfo.WxNo);
|
||
UserItem.SubItems.Add(uInfo.HImg);
|
||
//FRIEND_LIST.Items.Add(UserItem);
|
||
}
|
||
}
|
||
//FRIEND_LIST.EndUpdate();
|
||
//FRIEND_GH_LIST.EndUpdate();
|
||
//GROUP_LIST.EndUpdate();
|
||
|
||
//string OutLog = "[刷新好友列表][" + recvJson["mywxid"].ToString() + "]" + Environment.NewLine +
|
||
//"好友数:" + FRIEND_LIST.Items.Count.ToString() + Environment.NewLine +
|
||
//"公众号:" + FRIEND_GH_LIST.Items.Count.ToString() + Environment.NewLine +
|
||
//"群聊数:" + GROUP_LIST.Items.Count.ToString();
|
||
//Log(OutLog);
|
||
//Console.WriteLine(OutLog);
|
||
|
||
|
||
}
|
||
else if (RecvType == KstAPI.回调_群员列表)
|
||
{
|
||
JArray uList = recvJson.Value<JArray>("list");
|
||
|
||
//GROUP_MEMBER_LIST.BeginUpdate();
|
||
for (int i = 0; i < uList.Count; i++)
|
||
{
|
||
ListViewItem UserItem = new ListViewItem();
|
||
|
||
UserItem.Text = KstAPI.DeUnicode(recvJson["list"][i]["Nick"].ToString());
|
||
UserItem.SubItems.Add(KstAPI.DeUnicode(recvJson["list"][i]["Mark"].ToString()));
|
||
UserItem.SubItems.Add(recvJson["list"][i]["Wxid"].ToString());
|
||
UserItem.SubItems.Add(recvJson["list"][i]["HImg"].ToString());
|
||
|
||
//GROUP_MEMBER_LIST.Items.Add(UserItem);
|
||
}
|
||
//GROUP_MEMBER_LIST.EndUpdate();
|
||
}
|
||
else if (RecvType == KstAPI.回调_接收消息)
|
||
{
|
||
WeChatAPI.MsgInfo mInfo = new WeChatAPI.MsgInfo();
|
||
mInfo.RecvWxid = recvJson["mywxid"].ToString();
|
||
mInfo.Type = (int)recvJson["type"];
|
||
mInfo.Time = Timestamp2DataTime((int)recvJson["time"]);
|
||
mInfo.IsSend = (int)recvJson["isme"];
|
||
mInfo.LocalMsgID = (int)recvJson["msgid"];
|
||
mInfo.ServerMsgID = recvJson["smsgid"].ToString();
|
||
mInfo.Wxid_1 = recvJson["wxid1"].ToString();
|
||
mInfo.Wxid_2 = recvJson["wxid2"].ToString();
|
||
mInfo.Msg = Encoding.Default.GetString(Convert.FromBase64String(recvJson["msg"].ToString()));
|
||
mInfo.Msg = KstAPI.DeUnicode(mInfo.Msg);
|
||
mInfo.Source = Encoding.Default.GetString(Convert.FromBase64String(recvJson["source"].ToString()));
|
||
|
||
if (mInfo.Type==1)
|
||
{
|
||
if (skc != null)
|
||
{
|
||
WechatReceiveMsg msg = new WechatReceiveMsg();
|
||
msg.Cmd = PCRobotCMD.rcvTxt;
|
||
msg.RobotUsername = recvJson["mywxid"].ToString();
|
||
msg.RobotUsernick = WechatAPI.Client_Nick;
|
||
|
||
msg.RobotType = RobotType.客户端微信;
|
||
msg.FromMessageType = WechatMsgType.文本;
|
||
msg.FromUsername = recvJson["wxid1"].ToString();
|
||
if (msg.FromUsername.Contains("@chatroom"))
|
||
{
|
||
msg.FromGroupid= recvJson["wxid1"].ToString();
|
||
}
|
||
mInfo.Msg = Encoding.Default.GetString(Convert.FromBase64String(recvJson["msg"].ToString()));
|
||
mInfo.Msg = KstAPI.DeUnicode(mInfo.Msg);
|
||
msg.FromMessage = mInfo.Msg;
|
||
msg.FromUsernick = "";
|
||
skc.Send(msg);
|
||
}
|
||
}
|
||
|
||
string OutLog;
|
||
if (mInfo.IsSend == 0)
|
||
OutLog = "[接收消息]";
|
||
else
|
||
OutLog = "[发送消息]";
|
||
|
||
OutLog += "[" + mInfo.Time + "][" + mInfo.RecvWxid + "]" + Environment.NewLine +
|
||
"Type:" + mInfo.Type.ToString() + Environment.NewLine +
|
||
"消息ID:" + mInfo.LocalMsgID.ToString() + Environment.NewLine +
|
||
"服务ID:" + mInfo.ServerMsgID + Environment.NewLine +
|
||
"Wxid_1:" + mInfo.Wxid_1 + Environment.NewLine +
|
||
"Wxid_2:" + mInfo.Wxid_2 + Environment.NewLine +
|
||
//"消息内容:" + mInfo.Msg + " | " + temmsg + Environment.NewLine +
|
||
"消息内容:" + mInfo.Msg + Environment.NewLine +
|
||
"附加内容:" + mInfo.Source;
|
||
|
||
|
||
//Log(OutLog);
|
||
Console.WriteLine(OutLog);
|
||
|
||
if (mInfo.Type == 37)//好友请求
|
||
{
|
||
//if (CHK_ADDFRIEND.Checked == true)//通过好友请求
|
||
//{
|
||
// string Add_v1 = StrBetween(mInfo.Msg, "encryptusername=\"", "\" fromnickname=");
|
||
// string Add_v2 = StrBetween(mInfo.Msg, "ticket=\"", "\" opcode=");
|
||
// wxAPI.Wx_AddFriend(so, 3, 14, Add_v1, Add_v2, "");
|
||
//}
|
||
}
|
||
else if (mInfo.Type == 49 && mInfo.Msg.IndexOf("微信转账") != -1 && mInfo.Msg.IndexOf("收到转账") != -1)
|
||
{
|
||
//if (CHK_MONEY.Checked == true)//接收转账
|
||
//{
|
||
// string sState = StrBetween(mInfo.Msg, "<paysubtype>", "</paysubtype>");
|
||
// string sTranID = StrBetween(mInfo.Msg, "transferid><![CDATA[", "]]></transferid>");
|
||
// string sTime = StrBetween(mInfo.Msg, "<invalidtime><![CDATA[", "]]></invalidtime>");
|
||
// if (sState == "1" && mInfo.IsSend == 0)
|
||
// wxAPI.Wx_GetMoney(so, mInfo.Wxid_1, sTranID, int.Parse(sTime));
|
||
|
||
//}
|
||
}
|
||
else if (mInfo.Type == 49 && mInfo.Msg.IndexOf("邀请你加入群聊") != -1 && mInfo.IsSend == 0)
|
||
{
|
||
//if (CHK_INVITE.Checked == true)//自动通过群聊邀请
|
||
//{
|
||
// string inviteURL = StrBetween(mInfo.Msg, "<url><![CDATA[", "]]></url>");
|
||
// if (inviteURL != "")
|
||
// wxAPI.Wx_GetA8Key(so, 1, mInfo.Wxid_1, inviteURL);
|
||
//}
|
||
}
|
||
}
|
||
else if (RecvType == KstAPI.回调_语音消息 ||
|
||
RecvType == KstAPI.回调_视频消息 ||
|
||
RecvType == KstAPI.回调_文件消息|| RecvType == KstAPI.回调_图片消息)
|
||
{
|
||
WeChatAPI.MsgInfo mInfo = new WeChatAPI.MsgInfo();
|
||
mInfo.RecvWxid = recvJson["mywxid"].ToString();
|
||
mInfo.Type = (int)recvJson["type"];
|
||
mInfo.Time = Timestamp2DataTime((int)recvJson["time"]);
|
||
mInfo.IsSend = (int)recvJson["isme"];
|
||
mInfo.LocalMsgID = (int)recvJson["msgid"];
|
||
mInfo.ServerMsgID = recvJson["smsgid"].ToString();
|
||
mInfo.Wxid_1 = recvJson["wxid1"].ToString();
|
||
mInfo.Wxid_2 = recvJson["wxid2"].ToString();
|
||
mInfo.Msg = Encoding.Default.GetString(Convert.FromBase64String(recvJson["msg"].ToString()));
|
||
if (RecvType == KstAPI.回调_语音消息)
|
||
{
|
||
byte[] silkByte = Convert.FromBase64String(recvJson["source"].ToString());
|
||
//mInfo.Source = SaveFile(mInfo.RecvWxid, mInfo.Wxid_1, "VOICE", mInfo.ServerMsgID, "silk", silkByte);
|
||
}
|
||
else if (RecvType == KstAPI.回调_图片消息)
|
||
{
|
||
//byte[] ImageByte = Convert.FromBase64String(recvJson["source"].ToString());
|
||
//mInfo.Source = SaveFile(mInfo.RecvWxid, mInfo.Wxid_1, "IMAGE", mInfo.ServerMsgID, "jpg", ImageByte);
|
||
|
||
//string MaxPicPath = mInfo.Source.Replace(mInfo.ServerMsgID, mInfo.ServerMsgID + "_MAX");
|
||
|
||
//if (skc != null && skc.IsConnected)
|
||
//{
|
||
// WechatReceiveMsg msg = new WechatReceiveMsg();
|
||
// msg.Cmd = ClientCMD.rcvImg;
|
||
|
||
// msg.FromMessage = recvJson["source"].ToString();
|
||
// msg.FromUsernick = "";
|
||
// skc.Send(msg);
|
||
//}
|
||
//if (CHK_PIC.Checked == true)
|
||
// //wxAPI.DownLoadPic(so, mInfo.Msg, MaxPicPath);
|
||
// wxAPI.DownLoadPic(so, mInfo.Msg, "C:\\rra\\MAX_.jpg");
|
||
}
|
||
else
|
||
{
|
||
mInfo.Source = recvJson["source"].ToString();
|
||
}
|
||
|
||
|
||
string OutLog;
|
||
if (mInfo.IsSend == 0)
|
||
OutLog = "[接收消息]";
|
||
else
|
||
OutLog = "[发送消息]";
|
||
|
||
OutLog += "[" + mInfo.Time + "][" + mInfo.RecvWxid + "]" + Environment.NewLine +
|
||
"Type:" + mInfo.Type.ToString() + Environment.NewLine +
|
||
"消息ID:" + mInfo.LocalMsgID.ToString() + Environment.NewLine +
|
||
"服务ID:" + mInfo.ServerMsgID + Environment.NewLine +
|
||
"Wxid_1:" + mInfo.Wxid_1 + Environment.NewLine +
|
||
"Wxid_2:" + mInfo.Wxid_2 + Environment.NewLine +
|
||
"消息内容:" + mInfo.Msg + Environment.NewLine +
|
||
"文件路径:" + mInfo.Source;
|
||
//Log(OutLog);
|
||
Console.WriteLine(OutLog);
|
||
}
|
||
else if (RecvType == KstAPI.回调_添加好友)
|
||
{
|
||
string RecvWxid = recvJson["mywxid"].ToString();
|
||
int uType = (int)recvJson["id"];
|
||
int uSource = (int)recvJson["scene"];
|
||
string uWxid = recvJson["wxid"].ToString();
|
||
//Log("[添加好友][" + RecvWxid + "]" + Environment.NewLine + "返回类型:" + uType.ToString() + Environment.NewLine +
|
||
Console.WriteLine("[添加好友][" + RecvWxid + "]" + Environment.NewLine + "返回类型:" + uType.ToString() + Environment.NewLine +
|
||
"添加来源:" + uSource.ToString() + Environment.NewLine +
|
||
"添加Wxid:" + uWxid);
|
||
if (uType == -44)//需要验证
|
||
{
|
||
//wxAPI.Wx_AddFriend(so, 2, uSource, uWxid, "", FRI_TXT_MSG.Text);
|
||
}
|
||
}
|
||
else if (RecvType == KstAPI.回调_下载信息)
|
||
{
|
||
WeChatAPI.UserInfo uInfo = new WeChatAPI.UserInfo();
|
||
uInfo.Wxid = recvJson["info"]["Wxid"].ToString();
|
||
uInfo.WxNo = recvJson["info"]["WxNo"].ToString();
|
||
uInfo.Nick = recvJson["info"]["Nick"].ToString();
|
||
uInfo.Nick = KstAPI.DeUnicode(uInfo.Nick);
|
||
uInfo.V1 = recvJson["info"]["v1"].ToString();
|
||
uInfo.HImg = recvJson["info"]["HImg"].ToString();
|
||
uInfo.Sex = uInfo.GetSex((int)recvJson["info"]["Sex"]);
|
||
uInfo.Address = recvJson["info"]["Address"].ToString();
|
||
string OutLog = "[详细信息][" + recvJson["mywxid"].ToString() + "]" + Environment.NewLine +
|
||
"Wxid:" + uInfo.Wxid + Environment.NewLine +
|
||
"微信号:" + uInfo.WxNo + Environment.NewLine +
|
||
"昵称:" + uInfo.Nick + Environment.NewLine +
|
||
"性别:" + uInfo.Sex + Environment.NewLine +
|
||
"地区:" + uInfo.Address + Environment.NewLine +
|
||
"v1:" + uInfo.V1 + Environment.NewLine +
|
||
"头像:" + uInfo.HImg;
|
||
//Log(OutLog);
|
||
Console.WriteLine(OutLog);
|
||
}
|
||
else if (RecvType == KstAPI.回调_建群返回)
|
||
{
|
||
string cdata = recvJson["data"].ToString();
|
||
string RecvWxid = recvJson["mywxid"].ToString();
|
||
if (cdata == "Everything is OK")
|
||
{
|
||
string OutLog = "[建群返回][" + RecvWxid + "]" + Environment.NewLine;
|
||
string GroupID = recvJson["gid"].ToString();
|
||
OutLog += "群ID:" + GroupID + Environment.NewLine + "成员:" + Environment.NewLine;
|
||
JArray mList = recvJson.Value<JArray>("list");
|
||
for (int i = 0; i < mList.Count; i++)
|
||
{
|
||
string uWxid = recvJson["list"][i]["wxid"].ToString();
|
||
string uState = recvJson["list"][i]["status"].ToString();
|
||
if (uState == "1")
|
||
OutLog += "成功 --> " + uWxid + Environment.NewLine;
|
||
else
|
||
OutLog += "失败 --> " + uWxid + Environment.NewLine;
|
||
}
|
||
//Log(OutLog);
|
||
Console.WriteLine(OutLog);
|
||
}
|
||
else
|
||
//Log("[建群返回][" + RecvWxid + "]" + Environment.NewLine + "建群失败!");
|
||
Console.WriteLine("[建群返回][" + RecvWxid + "]" + Environment.NewLine + "建群失败!");
|
||
}
|
||
else if (RecvType == KstAPI.回调_A8Key)
|
||
{
|
||
string RecvWxid = recvJson["mywxid"].ToString();
|
||
string LastURL = recvJson["lasturl"].ToString();
|
||
string NowURL = recvJson["url"].ToString();
|
||
//Log("[GetA8Key][" + RecvWxid + "]" + Environment.NewLine + "访问链接:" + LastURL + Environment.NewLine + "返回链接:" + NowURL);
|
||
Console.WriteLine("[GetA8Key][" + RecvWxid + "]" + Environment.NewLine + "访问链接:" + LastURL + Environment.NewLine + "返回链接:" + NowURL);
|
||
if (NowURL.IndexOf("addchatroombyinvite") != -1)
|
||
{
|
||
HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(NowURL);
|
||
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 = NowURL;
|
||
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 = StrBetween(hLocation, "weixin://jump/mainframe/", "@chatroom");
|
||
//Log("[入群成功][" + RecvWxid + "]" + Environment.NewLine + "群号:" + GroupID + "@chatroom");
|
||
Console.WriteLine("[入群成功][" + RecvWxid + "]" + Environment.NewLine + "群号:" + GroupID + "@chatroom");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
else if (RecvType == KstAPI.回调_聊天对象)
|
||
{
|
||
WeChatAPI.UserInfo uInfo = new WeChatAPI.UserInfo();
|
||
uInfo.Wxid = recvJson["wxid"].ToString();
|
||
uInfo.WxNo = recvJson["wxno"].ToString();
|
||
uInfo.Nick = recvJson["nick"].ToString();
|
||
uInfo.Nick = KstAPI.DeUnicode(uInfo.Nick);
|
||
uInfo.Mark = recvJson["mark"].ToString();
|
||
uInfo.Mark = KstAPI.DeUnicode(uInfo.Mark);
|
||
uInfo.HImg = recvJson["himg"].ToString();
|
||
|
||
string OutLog = "[聊天对象][" + recvJson["mywxid"].ToString() + "]" + Environment.NewLine +
|
||
"Wxid:" + uInfo.Wxid + Environment.NewLine +
|
||
"微信号:" + uInfo.WxNo + Environment.NewLine +
|
||
"昵称:" + uInfo.Nick + Environment.NewLine +
|
||
"备注:" + uInfo.Mark + Environment.NewLine +
|
||
"头像:" + uInfo.HImg;
|
||
//Log(OutLog);
|
||
Console.WriteLine(OutLog);
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
// MessageBox.Show(ex.Message, "启动失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
Console.WriteLine("Server_CallBack ERROR:"+ex.Message);
|
||
}
|
||
}
|
||
|
||
private string StrBetween(string sText, string sLeft, string sRight)
|
||
{
|
||
try
|
||
{
|
||
int i = sText.IndexOf(sLeft) + sLeft.Length;
|
||
string temp = sText.Substring(i, sText.IndexOf(sRight, i) - i);
|
||
return temp;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "启动失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
return string.Empty;
|
||
}
|
||
|
||
private string Timestamp2DataTime(long Timestamp)
|
||
{
|
||
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 8, 0, 0, 0);
|
||
dtDateTime = dtDateTime.AddSeconds(Timestamp).ToLocalTime();
|
||
return dtDateTime.ToString("yyyy-MM-dd HH:mm:ss");
|
||
}
|
||
|
||
//public void SendSocket(ClientCMD CMD, object json)
|
||
//{
|
||
|
||
//}
|
||
//public void SendSocket(ClientCMD CMD)
|
||
//{
|
||
// this.SendSocket(CMD,string.Empty);
|
||
//}
|
||
//WeChatAPI wxAPI = new WeChatAPI();
|
||
//public void ServerRequest()
|
||
//{
|
||
// XxType XxType = new XxType();
|
||
// string msg = "";
|
||
// string wxid = "";
|
||
// switch (XxType)
|
||
// {
|
||
// case XxType.语音消息:
|
||
// break;
|
||
// case XxType.文件消息:
|
||
|
||
// break;
|
||
// case XxType.视频消息:
|
||
// break;
|
||
// case XxType.图片消息:
|
||
// break;
|
||
// case XxType.文字消息:
|
||
// string uMessage = KstAPI.EnUnicode(msg);
|
||
// WechatAPI.Wx_SendTextMsg(WechatAPI.Client_Handle, wxid, uMessage, uMessage);
|
||
// break;
|
||
// }
|
||
|
||
|
||
//}
|
||
|
||
|
||
}
|
||
}
|