2017-10-01 15:59:37 +08:00
|
|
|
|
using ClientsLibrary.FileSupport;
|
|
|
|
|
|
using CommonLibrary;
|
2017-07-12 21:00:00 +08:00
|
|
|
|
using HslCommunication;
|
|
|
|
|
|
using HslCommunication.BasicFramework;
|
|
|
|
|
|
using HslCommunication.Enthernet;
|
2017-09-18 17:35:59 +08:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
2017-07-12 21:00:00 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Windows.Forms;
|
2017-11-22 14:18:35 +08:00
|
|
|
|
using HslCommunication.Core;
|
2017-07-12 21:00:00 +08:00
|
|
|
|
|
|
|
|
|
|
namespace ClientsLibrary
|
|
|
|
|
|
{
|
2017-09-17 21:59:45 +08:00
|
|
|
|
|
|
|
|
|
|
/********************************************************************************
|
|
|
|
|
|
*
|
|
|
|
|
|
* 时间:2017年9月17日 16:09:05
|
|
|
|
|
|
* 用户头像类,负责更换头像,下载头像,以及初始化登录操作
|
2017-09-18 17:35:59 +08:00
|
|
|
|
* 说明:类头像类还没有代码重构好,加载部分块还有部分重复代码,也因为.Net3.5这部分的代码异步比较难处理
|
2017-09-17 21:59:45 +08:00
|
|
|
|
*
|
|
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-12 21:00:00 +08:00
|
|
|
|
/// <summary>
|
2017-10-14 21:21:33 +08:00
|
|
|
|
/// 头像管理器,负责所有小头像数据的缓存
|
2017-07-12 21:00:00 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class UserPortrait
|
|
|
|
|
|
{
|
2017-09-15 22:41:53 +08:00
|
|
|
|
#region Constructor
|
2017-10-10 21:58:53 +08:00
|
|
|
|
|
2017-09-15 22:41:53 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 实例化一个新的头像管理类对象
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="filePath">头像存储的文件夹路径</param>
|
2017-10-10 21:58:53 +08:00
|
|
|
|
/// <param name="loadPicSmall">加载头像的委托</param>
|
2017-10-14 21:21:33 +08:00
|
|
|
|
public UserPortrait(string filePath)
|
2017-07-12 21:00:00 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (!System.IO.Directory.Exists(filePath))
|
|
|
|
|
|
{
|
|
|
|
|
|
System.IO.Directory.CreateDirectory(filePath);
|
|
|
|
|
|
}
|
|
|
|
|
|
FileSavePath = filePath;
|
2017-10-10 21:58:53 +08:00
|
|
|
|
|
2017-07-12 21:00:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-09-15 22:41:53 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
2017-09-18 17:35:59 +08:00
|
|
|
|
#region Download Portraint
|
|
|
|
|
|
|
2017-09-20 21:55:43 +08:00
|
|
|
|
|
2017-09-15 22:41:53 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 下载大尺寸的头像并打开它,该方法适合线程池
|
|
|
|
|
|
/// </summary>
|
2017-07-12 21:00:00 +08:00
|
|
|
|
public void ThreadPoolDownloadSizeLarge()
|
|
|
|
|
|
{
|
|
|
|
|
|
string path = FileSavePath;
|
2017-09-17 21:59:45 +08:00
|
|
|
|
string path300 = path + @"\" + PortraitSupport.LargePortrait;
|
|
|
|
|
|
|
2017-09-18 17:35:59 +08:00
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(UserClient.UserAccount.LargePortraitMD5))
|
|
|
|
|
|
{
|
|
|
|
|
|
// 服务器不存在头像
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (System.IO.File.Exists(path300))
|
|
|
|
|
|
{
|
|
|
|
|
|
// 验证文件MD5码是否一致
|
|
|
|
|
|
string currentMd5 = SoftBasic.CalculateFileMD5(path300);
|
|
|
|
|
|
|
|
|
|
|
|
if (UserClient.UserAccount.LargePortraitMD5 == currentMd5)
|
|
|
|
|
|
{
|
|
|
|
|
|
System.Diagnostics.Process.Start(path300);
|
|
|
|
|
|
// 避免屏幕闪烁
|
|
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 验证不一致时需要利用客户端文件引擎去下载文件
|
2017-09-17 21:59:45 +08:00
|
|
|
|
OperateResult operateResult = UserClient.Net_File_Client.DownloadFile(
|
|
|
|
|
|
PortraitSupport.LargePortrait,
|
|
|
|
|
|
"Files",
|
|
|
|
|
|
"Portrait",
|
|
|
|
|
|
UserClient.UserAccount.UserName,
|
|
|
|
|
|
null,
|
|
|
|
|
|
path300
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(operateResult.IsSuccess)
|
2017-07-12 21:00:00 +08:00
|
|
|
|
{
|
2017-09-17 21:59:45 +08:00
|
|
|
|
System.Diagnostics.Process.Start(path300);
|
2017-07-12 21:00:00 +08:00
|
|
|
|
}
|
2017-09-17 21:59:45 +08:00
|
|
|
|
|
2017-09-18 17:35:59 +08:00
|
|
|
|
// 避免屏幕闪烁
|
2017-07-12 21:00:00 +08:00
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-09-15 22:41:53 +08:00
|
|
|
|
#endregion
|
2017-09-17 21:59:45 +08:00
|
|
|
|
|
2017-10-14 21:21:33 +08:00
|
|
|
|
#region Pricvate Method
|
|
|
|
|
|
|
|
|
|
|
|
private void SetSmallPortrait(string name,Bitmap bitmap)
|
2017-09-17 21:59:45 +08:00
|
|
|
|
{
|
2017-10-14 21:21:33 +08:00
|
|
|
|
hybirdLock.Enter();
|
|
|
|
|
|
|
|
|
|
|
|
if(dictSmallPortrait.ContainsKey(name))
|
|
|
|
|
|
{
|
|
|
|
|
|
dictSmallPortrait[name] = bitmap;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
dictSmallPortrait.Add(name, bitmap);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
hybirdLock.Leave();
|
2017-09-17 21:59:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-14 21:21:33 +08:00
|
|
|
|
private Bitmap DownloadSmallPortraitByName(string name)
|
2017-09-17 21:59:45 +08:00
|
|
|
|
{
|
2017-10-14 21:21:33 +08:00
|
|
|
|
OperateResult result = UserClient.Net_File_Client.DownloadFile(
|
|
|
|
|
|
PortraitSupport.SmallPortrait,
|
|
|
|
|
|
"Files",
|
|
|
|
|
|
"Portrait",
|
|
|
|
|
|
name,
|
|
|
|
|
|
null,
|
|
|
|
|
|
out Bitmap bitmap);
|
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
|
{
|
|
|
|
|
|
SetSmallPortrait(name, bitmap);
|
|
|
|
|
|
return bitmap;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return Properties.Resources.person_1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void UpdateSmallPortraitByName(string name)
|
|
|
|
|
|
{
|
|
|
|
|
|
DownloadSmallPortraitByName(name);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Bitmap GetSmallPortraitByUserName(string name)
|
|
|
|
|
|
{
|
|
|
|
|
|
Bitmap bitmap = null;
|
|
|
|
|
|
hybirdLock.Enter();
|
|
|
|
|
|
if (dictSmallPortrait.ContainsKey(name))
|
|
|
|
|
|
{
|
|
|
|
|
|
bitmap = dictSmallPortrait[name];
|
|
|
|
|
|
}
|
|
|
|
|
|
hybirdLock.Leave();
|
|
|
|
|
|
|
|
|
|
|
|
if (bitmap != null) return bitmap;
|
|
|
|
|
|
|
|
|
|
|
|
return DownloadSmallPortraitByName(name);
|
2017-09-17 21:59:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2017-09-15 22:41:53 +08:00
|
|
|
|
#region Private Members
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 文件的路径
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private string FileSavePath { get; set; }
|
2017-10-14 21:21:33 +08:00
|
|
|
|
|
|
|
|
|
|
private Dictionary<string, Bitmap> dictSmallPortrait = new Dictionary<string, Bitmap>();
|
|
|
|
|
|
private SimpleHybirdLock hybirdLock = new SimpleHybirdLock();
|
2017-09-15 22:41:53 +08:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
2017-07-12 21:00:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|