396 lines
19 KiB
C#
396 lines
19 KiB
C#
using ICSharpCode.SharpZipLib.Zip;
|
||
using Mini.Common;
|
||
using Mini.Model.ViewModel;
|
||
using Mini.Services.ww;
|
||
using Mini.Web.Areas.Admin.Controllers;
|
||
using Mini.Web.WebHelper;
|
||
using Newtonsoft.Json;
|
||
using Newtonsoft.Json.Linq;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Web;
|
||
|
||
namespace Mini.Web.ThreadClass
|
||
{
|
||
public class MessagePack
|
||
{
|
||
public static Dictionary<string, string> GenerateDownFile = new Dictionary<string, string>();//当前执行下载的文件
|
||
public string folderName { get; set; }
|
||
public string isgroup { get; set; }
|
||
public string userid { get; set; }
|
||
public string uname { get; set; }
|
||
public string corp { get; set; }
|
||
public string cs_nickname { get; set; }
|
||
public string cs_username { get; set; }
|
||
//public Iww_hhuser_Service _iww_hhuser_service { get; set; }
|
||
|
||
public string groupuser { get; set; }
|
||
/// <summary>
|
||
/// 当前地址
|
||
/// </summary>
|
||
public string selfurl { get; set; }
|
||
/// <summary>
|
||
/// 是否隐藏图片二维码
|
||
/// </summary>
|
||
public string IsHiddenEWM { get; set; }
|
||
|
||
public void GenerateZip()
|
||
{
|
||
try
|
||
{
|
||
FastZip fastZip = new FastZip();
|
||
|
||
//if (isgroup == "true")
|
||
//{
|
||
// Ww_MsgRoom model = _iww_hhuser_service.GetRoom(corp, cs_username);
|
||
// cs_nickname = string.IsNullOrEmpty(model.roomname) ? "群:" + model.roomid : "群:" + model.roomname;
|
||
// List<GroupUser> info = _iww_hhuser_service.GetGroupUser(corp, model);
|
||
// groupuser = Common.JsonHelper.ObjDivertToJson(info);
|
||
//}
|
||
|
||
var webPath = FileUnit.GetBaseDirectory();//站点目录
|
||
var tmpPath = Path.Combine(webPath, "temp");//临时文件目录
|
||
var tempUserPath = Path.Combine(tmpPath, folderName);//当前temp目录
|
||
if (Directory.Exists(tempUserPath))
|
||
{
|
||
Directory.Delete(tempUserPath, true);
|
||
}
|
||
string zipPath = Path.Combine(tmpPath, folderName + ".zip");
|
||
if (System.IO.File.Exists(zipPath))//如果存在zip文件,则就地解压出来重复利用
|
||
{
|
||
fastZip.ExtractZip(zipPath, tempUserPath, "static/file");
|
||
}
|
||
|
||
string zipName = string.Format("{0} 和 {1} 的聊天记录", uname, cs_nickname);
|
||
|
||
var templatePath = Path.Combine(webPath, "Template2");//模板目录
|
||
var facePath = Path.Combine(webPath, "wwwroot", "Face");//表情文件
|
||
|
||
var tempFacePath = Path.Combine(tempUserPath, "static/Face");
|
||
var temphtmlPath = Path.Combine(tempUserPath, "message.html");//模板主文件
|
||
var tempfilePath = Path.Combine(tempUserPath, "static/file");//所有的文件目录
|
||
|
||
if (!Directory.Exists(tempfilePath))//创建临时目录
|
||
{
|
||
Directory.CreateDirectory(tempfilePath);
|
||
}
|
||
if (!Directory.Exists(tempUserPath))//创建临时目录
|
||
{
|
||
Directory.CreateDirectory(tempUserPath);
|
||
}
|
||
if (!Directory.Exists(tempFacePath))//创建临时目录
|
||
{
|
||
Directory.CreateDirectory(tempFacePath);
|
||
}
|
||
CopyFolder(templatePath, tempUserPath);
|
||
CopyFolder(facePath, tempFacePath);
|
||
|
||
var exportTemplate = string.Empty;
|
||
using (StreamReader sr = new StreamReader(temphtmlPath))
|
||
{
|
||
exportTemplate = sr.ReadToEnd();
|
||
sr.Close();
|
||
}
|
||
string httpurl = Mini.Common.Utility.GetSettingByKey("HHMessageFile");//外网地址
|
||
string innerhttp = Mini.Common.Utility.GetSettingByKey("InnerFile");//内网地址
|
||
var url = "";
|
||
if (isgroup == "false")
|
||
{
|
||
url = innerhttp + "userjson?start=2017-06-08&end=2050-01-01&corpid=" + corp + "&user=" + userid + "&customer=" + cs_username + "&i=" + Guid.NewGuid().ToString();
|
||
}
|
||
else
|
||
{
|
||
url = innerhttp + "roomjson?corpid=" + corp + "&roomid=" + cs_username + "&i=" + Guid.NewGuid().ToString();
|
||
}
|
||
var recordurl = innerhttp + "recordjson?corpid=" + corp + "&user=" + userid + "&customer=" + cs_username + "&i=" + Guid.NewGuid().ToString();
|
||
string messageData = OutHHuserController.RequestGet(url);
|
||
string recordData = OutHHuserController.RequestGet(recordurl);
|
||
exportTemplate = exportTemplate
|
||
.Replace("{{htmltitle}}", "企业微信聊天记录")
|
||
.Replace("{{isgroup}}", isgroup)
|
||
.Replace("{{userid}}", userid)
|
||
.Replace("{{uname}}", uname)
|
||
.Replace("{{corp}}", corp)
|
||
.Replace("{{cs_username}}", cs_username)
|
||
.Replace("{{cs_nickname}}", ManagerPhoneHelper.FormatPhoneUserName(cs_nickname))
|
||
.Replace("{{isShowPhone}}", ManagerPhoneHelper.GetIsShowPhone())
|
||
.Replace("{{MessageData}}", messageData)
|
||
.Replace("{{RecordData}}", recordData)
|
||
.Replace("{{httpurl}}", httpurl)
|
||
.Replace("{{group}}", groupuser)
|
||
;
|
||
JArray messageJson = (JArray)JsonConvert.DeserializeObject(messageData);//json信息
|
||
JArray recordJson = (JArray)JsonConvert.DeserializeObject(recordData);//json信息
|
||
|
||
string downpath = "";
|
||
|
||
Dictionary<string, string> filedic = new Dictionary<string, string>();//不做重复下载
|
||
DirectoryInfo dicinfo = new DirectoryInfo(tempfilePath);
|
||
foreach (FileInfo item in dicinfo.GetFiles())//将file的历史文件添加到当前 集合中去,下次不做下载
|
||
{
|
||
if (!filedic.ContainsKey(item.Name))
|
||
filedic.Add(item.Name, "");
|
||
}
|
||
|
||
foreach (JToken item in messageJson.Children())
|
||
{
|
||
string msgtype = item["msgtype"].ToString();
|
||
if (msgtype == "image" || msgtype == "emotion")
|
||
{
|
||
string nfilename = item["nfile"].ToString();
|
||
if (!filedic.ContainsKey(nfilename))
|
||
{
|
||
downpath = innerhttp + "mediafile?corpid=" + corp + "&fname=" + nfilename + "";
|
||
if (msgtype == "image" && IsHiddenEWM == "1")
|
||
{
|
||
//downpath = $"{selfurl}/Admin/HHuser/GetNoEWMImgePath?url=" + HttpUtility.UrlEncode(downpath);
|
||
DownNowEWMImg(downpath, Path.Combine(tempfilePath, nfilename));
|
||
filedic.Add(nfilename, "");
|
||
}
|
||
else
|
||
{
|
||
DownFile(downpath, Path.Combine(tempfilePath, nfilename));
|
||
filedic.Add(nfilename, "");
|
||
}
|
||
|
||
}
|
||
}
|
||
else if (msgtype == "voice")
|
||
{
|
||
string nfilename = item["nfile"].ToString();
|
||
string mp3filename = nfilename.Replace(".amr", ".mp3");
|
||
if (!filedic.ContainsKey(mp3filename))
|
||
{
|
||
downpath = innerhttp + "playamr?corpid=" + corp + "&fname=" + nfilename + "";
|
||
DownFile(downpath, Path.Combine(tempfilePath, mp3filename));
|
||
filedic.Add(mp3filename, "");
|
||
}
|
||
}
|
||
//else if (msgtype == "video")//视频暂时不下载
|
||
//{
|
||
// string nfilename = item["nfile"].ToString();
|
||
// if (!filedic.ContainsKey(nfilename))
|
||
// {
|
||
// downpath = innerhttp + "mediafile?corpid=" + corp + "&fname=" + nfilename + "";
|
||
// DownFile(downpath, Path.Combine(tempfilePath, nfilename));
|
||
// filedic.Add(nfilename, "");
|
||
// }
|
||
//}
|
||
else if (msgtype == "file")
|
||
{
|
||
string nfilename = item["nfile"].ToString();
|
||
if (!filedic.ContainsKey(nfilename))
|
||
{
|
||
downpath = innerhttp + "mediafile?corpid=" + corp + "&fname=" + nfilename + "";
|
||
DownFile(downpath, Path.Combine(tempfilePath, nfilename));
|
||
filedic.Add(nfilename, "");
|
||
}
|
||
}
|
||
else if (msgtype == "meeting_voice_call")
|
||
{
|
||
JObject contentx = (JObject)Newtonsoft.Json.Linq.JObject.Parse(item["ext"].ToString());
|
||
string name = contentx["voiceid"].ToString() + ".mp3";
|
||
if (!filedic.ContainsKey(name))
|
||
{
|
||
downpath = innerhttp + "mediafile?corpid=" + corp + "&fname=" + name + "";
|
||
DownFile(downpath, Path.Combine(tempfilePath, name));
|
||
filedic.Add(name, "");
|
||
}
|
||
}
|
||
else if (msgtype == "chatrecord")
|
||
{
|
||
JObject contentx = (JObject)Newtonsoft.Json.Linq.JObject.Parse(item["ext"].ToString());
|
||
foreach (var item2 in contentx["chatrecord"]["item"].Children())
|
||
{
|
||
string type = item2["type"].ToString();
|
||
if (type == "ChatRecordImage")
|
||
{
|
||
JObject contentx2 = (JObject)Newtonsoft.Json.Linq.JObject.Parse(item2["content"].ToString());
|
||
string nfilename = contentx2["md5sum"].ToString() + ".jpg";
|
||
if (!filedic.ContainsKey(nfilename))
|
||
{
|
||
downpath = innerhttp + "mediafile?corpid=" + corp + "&fname=" + nfilename + "";
|
||
DownFile(downpath, Path.Combine(tempfilePath, nfilename));
|
||
filedic.Add(nfilename, "");
|
||
}
|
||
}
|
||
else if (type == "ChatRecordVoice")
|
||
{
|
||
JObject contentx2 = (JObject)Newtonsoft.Json.Linq.JObject.Parse(item2["content"].ToString());
|
||
string nfilename = contentx2["md5sum"].ToString() + ".amr";
|
||
string mp3filename = nfilename.Replace(".amr", ".mp3");
|
||
if (!filedic.ContainsKey(mp3filename))
|
||
{
|
||
downpath = innerhttp + "playamr?corpid=" + corp + "&fname=" + nfilename + "";
|
||
DownFile(downpath, Path.Combine(tempfilePath, mp3filename));
|
||
filedic.Add(mp3filename, "");
|
||
}
|
||
}
|
||
//else if (type == "ChatRecordVideo")
|
||
//{
|
||
// JObject contentx2 = (JObject)Newtonsoft.Json.Linq.JObject.Parse(item2["content"].ToString());
|
||
// string nfilename = contentx2["md5sum"].ToString() + ".mp4";
|
||
// if (!filedic.ContainsKey(nfilename))
|
||
// {
|
||
// downpath = innerhttp + "mediafile?corpid=" + corp + "&fname=" + nfilename + "";
|
||
// DownFile(downpath, Path.Combine(tempfilePath, nfilename));
|
||
// filedic.Add(nfilename, "");
|
||
// }
|
||
//}
|
||
else if (type == "ChatRecordFile")
|
||
{
|
||
JObject contentx2 = (JObject)Newtonsoft.Json.Linq.JObject.Parse(item2["content"].ToString());
|
||
string nfilename = contentx2["md5sum"].ToString() + "." + contentx2["fileext"].ToString();
|
||
if (!filedic.ContainsKey(nfilename))
|
||
{
|
||
downpath = innerhttp + "mediafile?corpid=" + corp + "&fname=" + nfilename + "";
|
||
DownFile(downpath, Path.Combine(tempfilePath, nfilename));
|
||
filedic.Add(nfilename, "");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
foreach (JToken item in recordJson.Children())//通话列表
|
||
{
|
||
string nfilename = item["filename"].ToString();
|
||
if (!filedic.ContainsKey(nfilename))
|
||
{
|
||
downpath = innerhttp + "mediafile?corpid=" + corp + "&fname=" + nfilename + "";
|
||
DownFile(downpath, Path.Combine(tempfilePath, nfilename));
|
||
filedic.Add(nfilename, "");
|
||
}
|
||
}
|
||
|
||
|
||
using (StreamWriter sw = new StreamWriter(temphtmlPath, false, Encoding.GetEncoding("UTF-8")))
|
||
{
|
||
sw.WriteLine(exportTemplate);
|
||
sw.Flush();
|
||
sw.Close();
|
||
}
|
||
if (System.IO.File.Exists(zipPath))
|
||
{
|
||
System.IO.File.Delete(zipPath);
|
||
}
|
||
|
||
fastZip.CreateZip(zipPath, tempUserPath, true, "");
|
||
Directory.Delete(tempUserPath, true);
|
||
if (GenerateDownFile.ContainsKey(folderName))//删除
|
||
GenerateDownFile.Remove(folderName);
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogHelper.Error(e.ToString());
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 下载AMR文件
|
||
/// </summary>
|
||
private void DownLoadAmr(string remoteUri, string localurl)
|
||
{
|
||
try
|
||
{
|
||
string url = Utility.GetNewUrlData(remoteUri, "", Encoding.UTF8);//获取重定向的地址
|
||
DownFile(url, localurl);
|
||
}
|
||
catch (Exception E)
|
||
{
|
||
LogHelper.Error(E.ToString());
|
||
}
|
||
}
|
||
private static char[] constant = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
|
||
public static string GenerateRandomNumber(int Length)
|
||
{
|
||
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62); Random rd = new Random(); for (int i = 0; i < Length; i++) { newRandom.Append(constant[rd.Next(62)]); }
|
||
return newRandom.ToString();
|
||
}
|
||
private void DownFile(string remoteUri, string localurl)
|
||
{
|
||
try
|
||
{
|
||
remoteUri += "&guid=" + GenerateRandomNumber(8);
|
||
LogHelper.Error("此时地址:" + remoteUri);
|
||
Uri downUri = new Uri(remoteUri);
|
||
HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(downUri);
|
||
using (Stream inputStream = hwr.GetResponse().GetResponseStream())
|
||
{
|
||
using (FileStream flieStream = new FileStream(localurl, FileMode.Create))
|
||
{
|
||
inputStream.CopyTo(flieStream);
|
||
}
|
||
}
|
||
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogHelper.Error(e.ToString());//下载失败
|
||
}
|
||
}
|
||
|
||
private void DownNowEWMImg(string remoteUri, string localurl)
|
||
{
|
||
try
|
||
{
|
||
remoteUri += "&guid=" + GenerateRandomNumber(8);
|
||
Uri downUri = new Uri(remoteUri);
|
||
HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(downUri);
|
||
using (Stream inputStream = hwr.GetResponse().GetResponseStream())
|
||
{
|
||
using (FileStream flieStream = new FileStream(localurl, FileMode.Create))
|
||
{
|
||
inputStream.CopyTo(flieStream);
|
||
}
|
||
}
|
||
LogHelper.Error("此时地址:" + localurl);
|
||
string erwm = MangerEWM.QcodeString(localurl);
|
||
if (!string.IsNullOrEmpty(erwm))//发现有二维码,就进行数据替换文件
|
||
{
|
||
File.Delete(localurl);
|
||
var webPath = FileUnit.GetBaseDirectory();//站点目录
|
||
var cantlookimg = Path.Combine(webPath, "wwwroot", "Content", "Images", "cantlook.jpg");//临时文件目录
|
||
File.Copy(cantlookimg, localurl);
|
||
LogHelper.Error("拷贝数据:"+ cantlookimg);
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogHelper.Error(e.ToString());//下载失败
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 复制源文件夹下的所有内容到新文件夹
|
||
/// </summary>
|
||
/// <param name="sources">源文件夹路径</param>
|
||
/// <param name="dest">新文件夹路径</param>
|
||
static void CopyFolder(string sources, string dest)
|
||
{
|
||
DirectoryInfo dinfo = new DirectoryInfo(sources);
|
||
//注,这里面传的是路径,并不是文件,所以不能包含带后缀的文件
|
||
foreach (FileSystemInfo f in dinfo.GetFileSystemInfos())
|
||
{
|
||
//目标路径destName = 新文件夹路径 + 源文件夹下的子文件(或文件夹)名字
|
||
//Path.Combine(string a ,string b) 为合并两个字符串
|
||
string destName = Path.Combine(dest, f.Name);
|
||
if (f is FileInfo)
|
||
{
|
||
//如果是文件就复制
|
||
System.IO.File.Copy(f.FullName, destName, true);//true代表可以覆盖同名文件
|
||
}
|
||
else
|
||
{
|
||
//如果是文件夹就创建文件夹,然后递归复制
|
||
Directory.CreateDirectory(destName);
|
||
CopyFolder(f.FullName, destName);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|