整理代码,更改头像部分移动到我的信息中,服务器适配

This commit is contained in:
dathlin
2017-09-18 17:35:59 +08:00
parent 78c04eadfb
commit 57f5eb83a6
21 changed files with 1050 additions and 486 deletions

View File

@@ -2,6 +2,7 @@
using HslCommunication;
using HslCommunication.BasicFramework;
using HslCommunication.Enthernet;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Drawing;
@@ -17,6 +18,7 @@ namespace ClientsLibrary
*
* 时间2017年9月17日 16:09:05
* 用户头像类,负责更换头像,下载头像,以及初始化登录操作
* 说明:类头像类还没有代码重构好,加载部分块还有部分重复代码,也因为.Net3.5这部分的代码异步比较难处理
*
*********************************************************************************/
@@ -82,7 +84,41 @@ namespace ClientsLibrary
{
ffo.ShowDialog();
}
DownloadUserPortraint();
// 上传文件MD5码
string SmallPortraitMD5 = SoftBasic.CalculateFileMD5(path32);
string LargePortraitMD5 = SoftBasic.CalculateFileMD5(path300);
JObject json = new JObject
{
{ UserAccount.UserNameText, new JValue(UserClient.UserAccount.UserName) },
{ UserAccount.SmallPortraitText, new JValue(SmallPortraitMD5) },
{ UserAccount.LargePortraitText, new JValue(LargePortraitMD5) }
};
OperateResultString result = UserClient.Net_simplify_client.ReadFromServer(
CommonHeadCode.SimplifyHeadCode.MD5,
json.ToString());
if(result.IsSuccess)
{
if (result.Content.Substring(0, 2) == "成功")
{
UserClient.UserAccount.SmallPortraitMD5 = SmallPortraitMD5;
UserClient.UserAccount.LargePortraitMD5 = LargePortraitMD5;
// 成功上传MD5码
LoadUserSmallPortraint();
}
else
{
MessageBox.Show("上传头像失败!原因:" + result.Content);
}
}
else
{
MessageBox.Show("上传头像失败!原因:" + result.Message);
}
}
}
}
@@ -90,71 +126,120 @@ namespace ClientsLibrary
#endregion
#region Load Portraint
/// <summary>
/// 加载小尺寸的头像操作,可以放到主线程
/// </summary>
public void LoadUserSmallPortraint()
{
// 先获取服务器端的MD5码
string fileMd5 = UserClient.UserAccount.SmallPortraitMD5;
if(string.IsNullOrEmpty(fileMd5))
{
// 服务器端没有文件,加载结束
return;
}
// 获取本地MD5
string fileName = FileSavePath + @"\" + PortraitSupport.SmallPortrait;
if (System.IO.File.Exists(fileName))
{
// 本地存在文件
string currentMd5 = SoftBasic.CalculateFileMD5(fileName);
// 对比验证
if (fileName == currentMd5)
{
// 加载本地文件
LoadPic?.Invoke(fileName);
return;
}
}
// 本地不存在文件或校验失败,需要重新下载
OperateResult result = UserClient.Net_File_Client.DownloadFile(PortraitSupport.SmallPortrait,
"Files",
"Portrait",
UserClient.UserAccount.UserName,
null,
fileName);
if(result.IsSuccess)
{
// 下载成功
LoadPic?.Invoke(fileName);
}
else
{
MessageBox.Show("头像从服务器下载失败,错误原因:" + result.Message);
}
}
/// <summary>
/// 加载大尺寸的头像方法,需要放到线程池中操作
/// </summary>
/// <param name="largeLoadAction"></param>
public void LoadUserLargePortraint(Action<string> largeLoadAction)
{
// 先获取服务器端的MD5码
string fileMd5 = UserClient.UserAccount.LargePortraitMD5;
if (string.IsNullOrEmpty(fileMd5))
{
// 服务器端没有文件,加载结束
return;
}
// 获取本地MD5
string fileName = FileSavePath + @"\" + PortraitSupport.LargePortrait;
if (System.IO.File.Exists(fileName))
{
// 本地存在文件,先进行加载,如果运算不一致,再重新加载
largeLoadAction?.Invoke(fileName);
string currentMd5 = null;
try
{
currentMd5 = SoftBasic.CalculateFileMD5(fileName);
}
catch(Exception ex)
{
MessageBox.Show("计算文件MD5码错误" + ex.Message);
return;
}
// 对比验证
if (fileMd5 == currentMd5)
{
return;
}
}
// 本地不存在文件或校验失败,需要重新下载
OperateResult result = UserClient.Net_File_Client.DownloadFile(PortraitSupport.LargePortrait,
"Files",
"Portrait",
UserClient.UserAccount.UserName,
null,
fileName);
if (result.IsSuccess)
{
// 下载成功
largeLoadAction?.Invoke(fileName);
}
else
{
MessageBox.Show("头像从服务器下载失败,错误原因:" + result.Message);
}
}
#endregion
#region Download Portraint
/// <summary>
/// 加载头像信息,检查本地是否有相应的文件,没有则向服务器请求下载新的头像,然后加载,应放到线程池中执行
/// </summary>
public void DownloadUserPortraint()
{
string path = FileSavePath;
//获取服务器文件名称
OperateResultString result = UserClient.Net_simplify_client.ReadFromServer(CommonHeadCode.SimplifyHeadCode., UserClient.UserAccount.UserName);
if (result.IsSuccess)
{
if (result.Content[0] == 'Y')
{
//服务器存在头像
string fileName = path + @"\" + PortraitSupport.SmallPortrait;
string FileMd5 = result.Content.Substring(1);
if (System.IO.File.Exists(fileName))
{
//文件文件
string currentMd5 = SoftBasic.CalculateFileMD5(fileName);
if (currentMd5 == FileMd5)
{
//加载本地头像
LoadPic?.Invoke(fileName);
}
else
{
//头像已经换了
DownloadUserPortraint(path);
}
}
else
{
//客户端不存在头像
DownloadUserPortraint(path);
}
}
else
{
//服务器不存在头像,本次加载结束
}
}
}
/// <summary>
/// 下载小尺寸的头像并使用委托加载它
/// </summary>
/// <param name="path"></param>
public void DownloadUserPortraint(string path)
{
OperateResultString result = UserClient.Net_simplify_client.ReadFromServer(CommonHeadCode.SimplifyHeadCode., UserClient.UserAccount.UserName);
if (result.IsSuccess)
{
if (result.Content[0] == 'Y')
{
byte[] data = Convert.FromBase64String(result.Content.Substring(1));
string path32 = path + @"\" + PortraitSupport.SmallPortrait;
System.IO.File.WriteAllBytes(path32, data);
LoadPic?.Invoke(path32);
}
}
}
/// <summary>
/// 下载大尺寸的头像并打开它,该方法适合线程池
/// </summary>
@@ -163,7 +248,28 @@ namespace ClientsLibrary
string path = FileSavePath;
string path300 = path + @"\" + PortraitSupport.LargePortrait;
// 利用客户端文件引擎去下载文件
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;
}
}
// 验证不一致时需要利用客户端文件引擎去下载文件
OperateResult operateResult = UserClient.Net_File_Client.DownloadFile(
PortraitSupport.LargePortrait,
"Files",
@@ -179,6 +285,7 @@ namespace ClientsLibrary
System.Diagnostics.Process.Start(path300);
}
// 避免屏幕闪烁
Thread.Sleep(1000);
}