old_flsystem/PCRobot/PCWechat/Dazong/NMHelper.cs

329 lines
11 KiB
C#

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net;
using System.Drawing;
namespace NM_WeChat
{
public class NMHelper
{
public int Socket_Handle { get; set; }
public int Socket_Port { get; set; }
public int Socket_ConnId { get; set; }
public string Socket_MyWxid { get; set; }
public string Socket_GroupID { get; set; }
public struct nmWxidInfo
{
public int Type { get; set; }
public int List { get; set; }
public string Wxid { get; set; }
public string WxNo { get; set; }
public string Nick { get; set; }
public string Mark { get; set; }
public string V1 { get; set; }
public string HeadIMG { get; set; }
public string Sex { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
public string Config { get; set; }
public string GetSex(int uSex)
{
if (uSex == 1)
return "男";
else if (uSex == 2)
return "女";
else
return "未知(" + uSex.ToString() + ")";
}
}
public struct nmMsgInfo
{
public string RecvWxid { get; set; }
public int Type { get; set; }
public int State { get; set; }
public int Time { get; set; }
public int IsSend { get; set; }
public int LocalMsgID { get; set; }
public string ServerMsgID { get; set; }
public string Wxid_1 { get; set; }
public string Nick_1 { get; set; }
public string Wxid_2 { get; set; }
public string Nick_2 { get; set; }
public string Msg { get; set; }
public string Source { get; set; }
public string FilePath { get; set; }
}
public const int Recv_QRCode = 100000;
public const int Recv_Login = 100001;
public const int Recv_Logout = 100002;
public const int Recv_Msg = 100003;
public const int Recv_SelectTalker = 100004;
public const int NM_GetContactList = 200000;
public const int NM_GetGroupMember = 200001;
public const int NM_GetWxidInfo = 200002;
public const int NM_GetA8Key = 200003;
public const int NM_SendText = 300001;
public const int NM_SendPic = 300002;
public const int NM_SendGif = 300003;
public const int NM_SendFile = 300004;
public const int NM_SendCard = 300005;
public const int NM_SendMap = 300006;
public const int NM_SendURL = 300007;
public const int NM_SendWeChatApp = 300008;
public const int NM_RevokeMsg = 300009;
public const int NM_DesRevoke = 300010;
public const int NM_ForwardAny = 300012;
public const int NM_SendVoice = 300013;
public const int NM_AddFriend = 400001;
public const int NM_PassApply = 400002;
public const int NM_DelFriend = 400003;
public const int NM_SetFriendMark = 400004;
public const int NM_DelGHFriend = 400005;
public const int NM_CreateGroup = 500001;
public const int NM_QuitGroup = 500002;
public const int NM_InvitePull = 500003;
public const int NM_InviteURL = 500004;
public const int NM_KickGroupMember = 500005;
public const int NM_SetGroupName = 500006;
public const int NM_SetGroupNotice = 500007;
public const int NM_SetMyGroupNick = 500008;
public const int NM_GetFavList = 600001;
public const int NM_SendFavMsg = 600002;
public const int NM_GetCircles = 700001;
public const int NM_SendCircle = 700002;
public const int NM_CircleUpLoadImage = 700003;
public const int NM_CircleComment = 700004;
public const int NM_CircleLink = 700005;
public const int NM_DelCircle = 700006;
public const int NM_GetCircleDetails = 700008;
public const int NM_DelCircleComment = 700009;
public const int NM_GetMoney = 900001;
public const int NM_StateOpt = 900002;
public const int NM_SetObject = 900003;
public const int NM_GetImageByCDN = 900004;
[DllImport("Bin\\NanMuHelper.dll")]
public static extern bool NM_GetWeChatPath(byte[] Path, int PathSize);
[DllImport("Bin\\NanMuHelper.dll")]
public static extern int NM_CreateWeChatProcess(string ExePath, string ExeName, string DllPath, int lPort);
[DllImport("Bin\\NanMuHelper.dll")]
public static extern int NM_LinkWeChat(int dwPsd, int dwPort, int dwUseLastPort, int dwProcessID);
public bool GetInt(JObject Json, string ObjectName, out int lResult)
{
bool bRet = false;
lResult = 0;
JToken JtObject;
if (Json.TryGetValue(ObjectName, out JtObject) == true)
{
if (JtObject.Type == JTokenType.Integer)
{
bRet = int.TryParse(JtObject.ToString(), out lResult);
if (bRet == false)
{
uint uResult = 0;
bRet = uint.TryParse(JtObject.ToString(), out uResult);
if (bRet == true)
{
lResult = (int)uResult;
}
}
}
}
return bRet;
}
public int GetIntEx(JObject Json, string ObjectName)
{
int lResult = 0;
JToken JtObject;
if (Json.TryGetValue(ObjectName, out JtObject) == true)
{
if (JtObject.Type == JTokenType.Integer)
{
if (int.TryParse(JtObject.ToString(), out lResult) == false)
{
uint uResult = 0;
if (uint.TryParse(JtObject.ToString(), out uResult) == true)
{
lResult = (int)uResult;
}
}
}
}
return lResult;
}
public JArray GetArray(JObject Json, string ObjectName)
{
string lResult = string.Empty;
JToken JtObject;
if (Json.TryGetValue(ObjectName, out JtObject))
{
if (JtObject.Type == JTokenType.Array)
{
return (JArray)Json[ObjectName];
}
}
return null;
}
public string GetString(JObject Json, string ObjectName, bool isDecode = true)
{
string lResult = string.Empty;
JToken JtObject;
if (Json.TryGetValue(ObjectName, out JtObject))
{
if (JtObject.Type == JTokenType.String)
{
if (isDecode)
{
if (!string.IsNullOrWhiteSpace(JtObject.ToString()))
{
try
{
byte[] ResultBytes = Convert.FromBase64String(JtObject.ToString());
lResult = System.Text.UnicodeEncoding.Unicode.GetString(ResultBytes);
}
catch (Exception)
{
return JtObject.ToString();
}
}
else
return string.Empty;
}
else
lResult = JtObject.ToString();
}
}
return lResult;
}
public string JToken_GetString(JToken Jt, string ObjectName, bool isDecode = true)
{
string lResult = string.Empty;
if (Jt != null)
{
try
{
lResult = GetString((JObject)Jt, ObjectName, isDecode);
}
catch (Exception)
{ }
}
return lResult;
}
public int FindRowIndex(ListView lv, string text, int columnIndex)
{
foreach (ListViewItem li in lv.Items)
{
if (li.SubItems[columnIndex].Text == text)
{
return li.Index;
}
}
return -1;
}
public string Timestamp2DataTime(long Timestamp)
{
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
dtDateTime = dtDateTime.AddSeconds(Timestamp).ToLocalTime();
return dtDateTime.ToString("yyyy-MM-dd HH:mm:ss");
}
public string SaveFile(string mWxid, string uWxid, string fName, string uName, string uExt, byte[] fData)
{
string filePath = Directory.GetCurrentDirectory() + "\\" + mWxid + "\\" + fName + "\\" + uWxid;
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
filePath += "\\" + uName + "." + uExt;
FileStream fs = new FileStream(filePath, FileMode.Create);
fs.Write(fData, 0, fData.Length);
fs.Flush();
fs.Close();
return filePath;
}
public string StringMid(string Src, string Left, string Right)
{
string lRusult = string.Empty;
if (Src != string.Empty)
{
int dwLeft = Src.IndexOf(Left);
if (dwLeft != -1)
{
dwLeft += Left.Length;
int dwRight = Src.IndexOf(Right, dwLeft);
if (dwRight != -1)
{
lRusult = Src.Substring(dwLeft, dwRight - dwLeft);
}
}
}
return lRusult;
}
public Image ShowPicFromURL(string PicUrl)
{
Image Img = null;
if (PicUrl.Length > 10)
{
if (PicUrl.Substring(0, 4) == "http")
{
WebRequest Request = (WebRequest)HttpWebRequest.Create(PicUrl);
WebResponse Resp = Request.GetResponse();
Stream stream = Resp.GetResponseStream();
Img = Image.FromStream(stream);
stream.Close();
Resp.Close();
}
}
return Img;
}
public string GetFavType(int Type)
{
if (Type == 1)
return "文本";
else if (Type == 2)
return "图片";
else if (Type == 3)
return "语音";
else if (Type == 4)
return "视频";
else if (Type == 5)
return "链接";
else if (Type == 6)
return "定位";
else if (Type == 8)
return "文件";
else if (Type == 14)
return "聊天记录";
else if (Type == 18)
return "笔记";
else
return "未知(" + Type.ToString() + ")";
}
}
}