mac
2024-07-25 3f6685c77beeb12baf840733fb890860f4c26e7c
HDL_ON/Common/ImageUtlis.cs
@@ -3,6 +3,9 @@
using HDL_ON.DAL.Server;
using System.IO;
using HDL_ON.Common;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace HDL_ON
{
@@ -147,9 +150,9 @@
        {
            var requestJson = HttpUtil.GetSignRequestJson(uploadImageObj);
            var revertObj = HttpUtil.RequestHttpsPostFroHome(NewAPI.API_POST_UploadImage, requestJson);
            if(revertObj.Code == StateCode.SUCCESS)
            if (revertObj.Code == StateCode.SUCCESS)
            {
                if(revertObj.Data != null)
                if (revertObj.Data != null)
                {
                    return revertObj.Data.ToString();
                }
@@ -160,6 +163,17 @@
            }
            return "";
        }
        /// <summary>
        /// 获取场景默认图库列表
        /// </summary>
        /// <returns></returns>
        public ResponsePackNew GetSceneDefaultIconList()
        {
            var d = new Dictionary<string, object>();
            var requestJson = HttpUtil.GetSignRequestJson(d);
            var revertObj = HttpUtil.RequestHttpsPostFroHome(NewAPI.Api_Post_GetSceneDefaultIconList, requestJson);
            return revertObj;
        }
        /// <summary>
        /// 通过图片路径下载图片
@@ -167,19 +181,26 @@
        /// </summary>
        public byte[] GetImageDownloadUrl(string imageKey)
        {
            var requestJson = HttpUtil.GetSignRequestJson(new GetImageUrlObj()
            if (imageKey.StartsWith("https:") || imageKey.StartsWith("http:"))
            {
                imageKey = imageKey,
            });
            var revertObj = HttpUtil.RequestHttpsPostFroHome(NewAPI.API_POST_GetImageUrl, requestJson);
            if (revertObj.Code == StateCode.SUCCESS)
                return HttpUtil.HttpsDownload(imageKey);
            }
            else
            {
                if (revertObj.Data != null)
                var requestJson = HttpUtil.GetSignRequestJson(new GetImageUrlObj()
                {
                    if (!string.IsNullOrEmpty(revertObj.Data.ToString()))
                    imageKey = imageKey,
                });
                var revertObj = HttpUtil.RequestHttpsPostFroHome(NewAPI.API_POST_GetImageUrl, requestJson);
                if (revertObj.Code == StateCode.SUCCESS)
                {
                    if (revertObj.Data != null)
                    {
                        var url = revertObj.Data.ToString();
                        return HttpUtil.HttpsDownload(url);
                        if (!string.IsNullOrEmpty(revertObj.Data.ToString()))
                        {
                            var url = revertObj.Data.ToString();
                            return HttpUtil.HttpsDownload(url);
                        }
                    }
                }
            }
@@ -260,7 +281,6 @@
                                    if (imageView != null)
                                    {
                                        imageView.ImageBytes = imageBytes;
                                        //Utlis.WriteLine("imageKey 加载云端下载图片成功");
                                    }
                                });
                            }
@@ -282,6 +302,34 @@
            }
        }
        string strKey = "abcdefgh";//注意:这里的密钥sKey必须能转为8个byte,即输入密钥为8半角个字符或者4个全角字符或者4个汉字的字符串
        string strIV = "ijklmnop";
        // 加密
        private string Encrypt(string _strQ)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(_strQ);
            MemoryStream ms = new MemoryStream();
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            CryptoStream encStream = new CryptoStream(ms, des.CreateEncryptor(Encoding.UTF8.GetBytes(strKey), Encoding.UTF8.GetBytes(strIV)), CryptoStreamMode.Write);
            encStream.Write(buffer, 0, buffer.Length);
            encStream.FlushFinalBlock();
            return Convert.ToBase64String(ms.ToArray());
        }
        // 解密
        private string Decrypt(string _strQ)
        {
            byte[] buffer = Convert.FromBase64String(_strQ);
            MemoryStream ms = new MemoryStream();
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            CryptoStream encStream = new CryptoStream(ms, des.CreateDecryptor(Encoding.UTF8.GetBytes(strKey), Encoding.UTF8.GetBytes(strIV)), CryptoStreamMode.Write);
            encStream.Write(buffer, 0, buffer.Length);
            encStream.FlushFinalBlock();
            return Encoding.UTF8.GetString(ms.ToArray());
        }
        /// <summary>
        /// 2020-12-03
        /// 检测加载图片
@@ -294,6 +342,13 @@
        {
            try
            {
                var imageUrl = imageKey;
                if (imageUrl.StartsWith("http"))
                {
                    imageKey = Encrypt(imageKey).Replace("/","123");
                }
                //1.本地默认图库图片,直接加载本地
                if (imageKey.Contains("Classification/Room/Roombg") || imageKey.Contains("Intelligence/Gallery/scenebg"))
                {
@@ -321,7 +376,7 @@
                        //2.2 本地没缓存,开启线程云端下载然后缓存
                        System.Threading.Tasks.Task.Run(() =>
                        {
                            byte[] imageBytes = GetImageDownloadUrl(imageKey);
                            byte[] imageBytes = GetImageDownloadUrl(imageUrl);
                            if (imageBytes != null)
                            {
                                WriteFileByBytes(imageKey, imageBytes);
@@ -331,14 +386,14 @@
                                    if (frameLayout != null)
                                    {
                                        frameLayout.BackgroundImagePath = imageKey;
                                        //Utlis.WriteLine("imageKey 加载云端下载图片成功");
                                        Utlis.WriteLine("imageKey 加载云端下载图片成功");
                                    }
                                });
                            }
                            else
                            {
                                //2.4 下载是否,是否使用默认图片
                                //Utlis.WriteLine("imageKey 加载云端图片失败");
                                Utlis.WriteLine("imageKey 加载云端图片失败");
                            }
                        });
                    }
@@ -440,7 +495,7 @@
            try
            {
                if (string.IsNullOrEmpty(imageKey)) return null;
                byte[] imageBytes;
                if (!ignoreCache && Exists(imageKey))
                {
@@ -451,12 +506,12 @@
                {
                    //云端下载
                    imageBytes = GetImageDownloadUrlWithRegionUrl(imageKey, regionUrl);
                    if(imageBytes != null)
                    if (imageBytes != null)
                    {
                        //保存到本地
                        WriteFileByBytes(imageKey, imageBytes);
                    }
                }
                return imageBytes;
            }
@@ -466,6 +521,47 @@
            }
        }
        /// <summary>
        /// 下载用户头像
        /// </summary>
        /// <param name="imageUrl"></param>
        /// <param name="ignoreCache">忽略缓存</param>
        /// <param name="regionUrl">指定服务器域名,传空为使用当前用户地址</param>
        /// <returns></returns>
        public byte[] DownImageByImageUrl(string imageUrl, bool ignoreCache = false)
        {
            try
            {
                if (string.IsNullOrEmpty(imageUrl)) return null;
                byte[] imageBytes;
                if (!ignoreCache && Exists(imageUrl))
                {
                    //本地有缓存
                    imageBytes = ReadFile(imageUrl);
                }
                else
                {
                    //下载
                    imageBytes = HttpUtil.HttpsDownload(imageUrl);
                    if (imageBytes != null)
                    {
                        //保存到本地
                        WriteFileByBytes(imageUrl, imageBytes);
                    }
                }
                return imageBytes;
            }
            catch
            {
                return null;
            }
        }
        #endregion
    }
}