Browse Source

fix MD5 生成头像文件名称

zhangtaotao 6 years ago
parent
commit
0c9620a4bf

+ 30 - 0
WeChatCore/Common/MethodsHelper.cs

@@ -3,6 +3,7 @@ using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
+using System.Security.Cryptography;
 using System.Text;
 using System.Threading.Tasks;
 
@@ -67,6 +68,35 @@ namespace WeChatCore.Common
             return "e" + sb.ToString();
         }
         /// <summary>
+        /// 生成MD5
+        /// </summary>
+        /// <param name="source"></param>
+        /// <returns></returns>
+        public static string EncryptWithMD5(string source)
+        {
+            byte[] sor = Encoding.UTF8.GetBytes(source);
+            MD5 md5 = MD5.Create();
+            byte[] result = md5.ComputeHash(sor);
+            StringBuilder strbul = new StringBuilder(40);
+            for (int i = 0; i < result.Length; i++)
+            {
+                strbul.Append(result[i].ToString("x2"));//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
+
+            }
+            return strbul.ToString();
+        }
+        /// <summary>
+        /// 下载头像的保存
+        /// </summary>
+        /// <param name="FilePath"></param>
+        /// <returns></returns>
+        public static string HeadImageSaveFile(string FilePath,string NickName)
+        {
+            if (!Directory.Exists(Environment.CurrentDirectory + "\\" + FilePath))
+                Directory.CreateDirectory(Environment.CurrentDirectory + "\\" + FilePath);
+            return EncryptWithMD5(NickName);
+        }
+        /// <summary>
         /// 生成本地MP3文件
         /// </summary>
         /// <param name="UserName">当前用户名</param>

+ 17 - 1
WeChatCore/Entity/MemberListItemEntity.cs

@@ -229,15 +229,31 @@ namespace WeChatCore.Entity
                 catch (Exception ex)
                 {
                     LogWriter.Write("设置头像发生异常" + DisplayNameDef, LogPathDefine.ExceptionLogPath);
+                    try
+                    {
+                        File.SetAttributes(HeadUrlDef, FileAttributes.Normal);
+                        File.Delete(HeadUrlDef);
+                        DownloadImage();
+                    }
+                    catch (Exception e)
+                    {
+                        LogWriter.Write("设置头像发生异常" + e.Message, LogPathDefine.ExceptionLogPath);
+                    }
                     return new BitmapImage(new Uri("pack://application:,,,/WeChatCore;component/Resource/DefultHeader.png"));
                 }
             }
         }
         public void DownloadImage()
         {
+            string uuid = MethodsHelper.HeadImageSaveFile(DirectoryDefine.HeaderImagePath, DisplayNameDef);
+            if (File.Exists(DirectoryDefine.HeaderImagePath + "\\" + uuid + ".jpg"))
+            {
+                HeadUrlDef = Environment.CurrentDirectory + "\\" + DirectoryDefine.HeaderImagePath + "\\" + uuid + ".jpg";
+                return;
+            }
             if (string.IsNullOrWhiteSpace(HeadUrlDef))
             {
-                string uuid = MethodsHelper.MsgSaveFile(DirectoryDefine.HeaderImagePath);
+
                 HttpHelper.HttpMethods.GetFile(UrlDefine.RootUrl + HeadImgUrl, Environment.CurrentDirectory + "\\" + DirectoryDefine.HeaderImagePath + "\\" + uuid + ".jpg", CommonDefine.Cookies);
                 Application.Current.Dispatcher.Invoke(new Action(() =>
                 {

+ 1 - 0
WeChatCore/Entity/MsgEntity.cs

@@ -78,5 +78,6 @@ namespace WeChatCore.Entity
         /// 是否设置自动回复
         /// </summary>
         public bool IsCanAutoReply { get; set; }
+
     }
 }