From efad979b6fae76fb37a4de7e94e6bac0a85cb72c Mon Sep 17 00:00:00 2001 From: JLChen <551775569@qq.com> Date: 星期二, 30 十一月 2021 13:14:28 +0800 Subject: [PATCH] 2021-11-30 1.修复乐橙Android,没调用拒接接口问题。 --- HDL_ON/Common/ImageUtlis.cs | 262 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 220 insertions(+), 42 deletions(-) diff --git a/HDL_ON/Common/ImageUtlis.cs b/HDL_ON/Common/ImageUtlis.cs index 88f00e1..918483d 100644 --- a/HDL_ON/Common/ImageUtlis.cs +++ b/HDL_ON/Common/ImageUtlis.cs @@ -1,11 +1,8 @@ 锘縰sing System; using Shared; -using HDL_ON; -using HDL_ON.UI.CSS; using HDL_ON.DAL.Server; -using HDL_ON.UI; -using System.Threading; -using HDL_ON.Entity; +using System.IO; +using HDL_ON.Common; namespace HDL_ON { @@ -41,6 +38,107 @@ } #endregion + + #region 鏂囦欢璇诲啓 + /// <summary> + /// 鏍圭洰褰� + /// </summary> + string RootPath = Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "/"; + /// <summary> + /// 鏂囦欢鏄惁瀛樺湪 + /// </summary> + /// <param name="filePath">File path.</param> + public bool Exists(string filePath) + { + return File.Exists(Path.Combine(RootPath, filePath)); + } + + /// <summary> + /// Writes the file by bytes. + /// </summary> + /// <returns><c>true</c>, if file by bytes was writed, <c>false</c> otherwise.</returns> + /// <param name="fileName">File name.</param> + /// <param name="bytes">Bytes.</param> + public bool WriteFileByBytes(string fileName, byte[] bytes) + { + if (fileName == null || (fileName = fileName.Trim()) == ("")) + { + return false; + } + + FileStream fs = null; + + try + { + fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Create, FileAccess.Write); + fs.Write(bytes, 0, bytes.Length); + fs.Flush(); + MainPage.Log("SaveFile:" + fileName); + return true; + } + catch (Exception ex) + { + MainPage.Log("FileUtiles Code 113:" + ex.ToString()); + return false; + } + finally + { + try + { + if (fs != null) + { + fs.Close(); + } + } + catch (Exception ex) + { + MainPage.Log("FileUtils Code 121 :" + ex.ToString()); + } + } + } + public byte[] ReadFile(string fileName) + { + FileStream fs = null; + try + { + if (File.Exists(Path.Combine(RootPath, fileName))) + { + fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Open, FileAccess.Read); + } + else if (File.Exists(fileName)) + { + fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); + } + else + { + return new byte[0]; + } + byte[] bytes = new byte[fs.Length]; + fs.Read(bytes, 0, bytes.Length); + return bytes; + } + catch + { + return new byte[0]; + } + finally + { + try + { + if (fs != null) + { + fs.Close(); + } + } + catch + { + + } + } + } + + #endregion + #region 鎴块棿銆佸満鏅� 鍥剧墖涓婁紶鍜屼笅杞� /// <summary> /// 涓婁紶鍥剧墖 @@ -65,6 +163,7 @@ /// <summary> /// 閫氳繃鍥剧墖璺緞涓嬭浇鍥剧墖 + /// 閽堝浣忓畢锛屼娇鐢ㄤ綇瀹呭煙鍚嶆湇鍔″櫒 /// </summary> public byte[] GetImageDownloadUrl(string imageKey) { @@ -73,6 +172,31 @@ imageKey = imageKey, }); var revertObj = HttpUtil.RequestHttpsPostFroHome(NewAPI.API_POST_GetImageUrl, requestJson); + if (revertObj.Code == StateCode.SUCCESS) + { + if (revertObj.Data != null) + { + if (!string.IsNullOrEmpty(revertObj.Data.ToString())) + { + var url = revertObj.Data.ToString(); + return HttpUtil.HttpsDownload(url); + } + } + } + return null; + } + + /// <summary> + /// 閫氳繃鍥剧墖璺緞涓嬭浇鍥剧墖 + /// 閽堝鐢ㄦ埛澶村儚鎸囧畾鐢ㄦ埛璺緞绛� + /// </summary> + public byte[] GetImageDownloadUrlWithRegionUrl(string imageKey, string regionUrl) + { + var requestJson = HttpUtil.GetSignRequestJson(new GetImageUrlObj() + { + imageKey = imageKey, + }); + var revertObj = HttpUtil.RequestHttpsPost(NewAPI.API_POST_GetImageUrl, requestJson, regionUrl); if (revertObj.Code == StateCode.SUCCESS) { if (revertObj.Data != null) @@ -106,19 +230,19 @@ { imageView.ImageBytes = null; imageView.ImagePath = imageKey; - Utlis.WriteLine("imageKey 鍔犺浇鏈湴鍥剧墖鎴愬姛"); + //Utlis.WriteLine("imageKey 鍔犺浇鏈湴鍥剧墖鎴愬姛"); } } else { //2.鑷畾涔夊浘鐗囷紝妫�娴嬫槸鍚︽湰鍦扮紦瀛樿繃锛屾病鏈夊垯浜戠涓嬭浇鐒跺悗缂撳瓨 - if (!ignoreCache && FileUtils.Exists(imageKey)) + if (!ignoreCache && Exists(imageKey)) { //2.1 鏈湴鏈夌紦瀛樺苟涓斾笉蹇界暐缂撳瓨锛岀洿鎺ユ湰鍦拌鍙� if (imageView != null) { - imageView.ImageBytes = FileUtils.ReadFile(imageKey); - Utlis.WriteLine("imageKey 鍔犺浇鑷畾涔夌紦瀛樺浘鐗囨垚鍔�"); + imageView.ImageBytes = ReadFile(imageKey); + //Utlis.WriteLine("imageKey 鍔犺浇鑷畾涔夌紦瀛樺浘鐗囨垚鍔�"); } } else @@ -129,21 +253,21 @@ byte[] imageBytes = GetImageDownloadUrl(imageKey); if (imageBytes != null) { - FileUtils.WriteFileByBytes(imageKey, imageBytes); + WriteFileByBytes(imageKey, imageBytes); //2.3 涓嬭浇鎴愬姛锛屾樉绀哄苟缂撳瓨鏈湴 Application.RunOnMainThread(() => { if (imageView != null) { imageView.ImageBytes = imageBytes; - Utlis.WriteLine("imageKey 鍔犺浇浜戠涓嬭浇鍥剧墖鎴愬姛"); + //Utlis.WriteLine("imageKey 鍔犺浇浜戠涓嬭浇鍥剧墖鎴愬姛"); } }); } else { //2.4 涓嬭浇鏄惁锛屾槸鍚︿娇鐢ㄩ粯璁ゅ浘鐗� - Utlis.WriteLine("imageKey 鍔犺浇浜戠鍥剧墖澶辫触"); + //Utlis.WriteLine("imageKey 鍔犺浇浜戠鍥剧墖澶辫触"); } }); } @@ -177,19 +301,19 @@ { //imageView.ImageBytes = null; frameLayout.BackgroundImagePath = imageKey; - Utlis.WriteLine("imageKey 鍔犺浇鏈湴鍥剧墖鎴愬姛"); + //Utlis.WriteLine("imageKey 鍔犺浇鏈湴鍥剧墖鎴愬姛"); } } else { //2.鑷畾涔夊浘鐗囷紝妫�娴嬫槸鍚︽湰鍦扮紦瀛樿繃锛屾病鏈夊垯浜戠涓嬭浇鐒跺悗缂撳瓨 - if (!ignoreCache && FileUtils.Exists(imageKey)) + if (!ignoreCache && Exists(imageKey)) { //2.1 鏈湴鏈夌紦瀛樺苟涓斾笉蹇界暐缂撳瓨锛岀洿鎺ユ湰鍦拌鍙� if (frameLayout != null) { frameLayout.BackgroundImagePath = imageKey; - Utlis.WriteLine("imageKey 鍔犺浇鑷畾涔夌紦瀛樺浘鐗囨垚鍔�"); + //Utlis.WriteLine("imageKey 鍔犺浇鑷畾涔夌紦瀛樺浘鐗囨垚鍔�"); } } else @@ -200,21 +324,21 @@ byte[] imageBytes = GetImageDownloadUrl(imageKey); if (imageBytes != null) { - FileUtils.WriteFileByBytes(imageKey, imageBytes); + WriteFileByBytes(imageKey, imageBytes); //2.3 涓嬭浇鎴愬姛锛屾樉绀哄苟缂撳瓨鏈湴 Application.RunOnMainThread(() => { if (frameLayout != null) { frameLayout.BackgroundImagePath = imageKey; - Utlis.WriteLine("imageKey 鍔犺浇浜戠涓嬭浇鍥剧墖鎴愬姛"); + //Utlis.WriteLine("imageKey 鍔犺浇浜戠涓嬭浇鍥剧墖鎴愬姛"); } }); } else { //2.4 涓嬭浇鏄惁锛屾槸鍚︿娇鐢ㄩ粯璁ゅ浘鐗� - Utlis.WriteLine("imageKey 鍔犺浇浜戠鍥剧墖澶辫触"); + //Utlis.WriteLine("imageKey 鍔犺浇浜戠鍥剧墖澶辫触"); } }); } @@ -229,54 +353,108 @@ } } - /// <summary> - /// 鑾峰彇鎷兼帴鐢ㄦ埛澶村儚鐨処mageKey瑙勫垯 - /// </summary> - /// <returns></returns> - public string GetHeadImageKey(string userId) - { - return "ON" + "_" + userId + "_" + HEADIMAGE; - } + /// <summary> - /// 鐢熸垚澶村儚涓婁紶瑙勫垯鍙傛暟 + /// 2021-01-28 + /// 鍔犺浇绗笁鏂瑰搧鐗宨con /// </summary> - /// <returns></returns> - public UploadImageObj GetHeadImageUploadKeyObj(string userId) + /// <param name="imageKey">鍥剧墖璺緞銆佷簯绔笅杞藉浘鐗囩殑鏍囪瘑key</param> + /// <param name="imageView">闇�瑕佹樉绀哄浘鐗囩殑ImageView</param> + /// <param name="ignoreCache">鏄惁闇�瑕佸拷鐣ョ紦瀛�</param> + public void Load3tyBrandIconImages(string brandName, string imageKey, ImageView imageView) { - return new UploadImageObj() + try { - prefix = "ON", - uid = userId, - fileName = HEADIMAGE, - }; + System.Threading.Tasks.Task.Run(() => + { + //byte[] imageBytes = GetImageDownloadUrl(imageKey); + //if (imageBytes != null) + //{ + // WriteFileByBytes(imageKey, imageBytes); + // //2.3 涓嬭浇鎴愬姛锛屾樉绀哄苟缂撳瓨鏈湴 + // Application.RunOnMainThread(() => { + // if (imageView != null) + // { + // imageView.ImageBytes = imageBytes; + // //Utlis.WriteLine("imageKey 鍔犺浇浜戠涓嬭浇鍥剧墖鎴愬姛"); + // } + // }); + //} + + //var ddd = new Common.DownLoadImageAsync(imageKey,"1","1"); + //ddd.StartDownloadImageAsync(); + + Action action = () => { + Application.RunOnMainThread(() => + { + imageView.ImagePath = $"{brandName}.png"; + }); + }; + FileUtlis.Files.DownLoadImage($"{brandName}.png", imageKey, action); + + }); + } + catch (Exception ex) + { + Utlis.WriteLine("imageKey 鍔犺浇鍥剧墖寮傚父 catch"); + } + finally + { + } } + + + // /// <summary> + ///// 鑾峰彇鎷兼帴鐢ㄦ埛澶村儚鐨処mageKey瑙勫垯 + ///// </summary> + ///// <returns></returns> + //public string GetHeadImageKey(string userId) + // { + // return "ON" + "_" + userId + "_" + HEADIMAGE; + // } + + ///// <summary> + ///// 鐢熸垚澶村儚涓婁紶瑙勫垯鍙傛暟 + ///// </summary> + ///// <returns></returns> + //public UploadImageObj GetHeadImageUploadKeyObj(string userId) + //{ + // return new UploadImageObj() + // { + // prefix = "Icon" + Utlis.GetTimestamp(), + // uid = userId, + // fileName = HEADIMAGE, + // }; + //} /// <summary> /// 涓嬭浇鐢ㄦ埛澶村儚 /// </summary> - /// <param name="userId"></param> - /// <param name="ignoreCache">鏄惁蹇界暐缂撳瓨</param> + /// <param name="imageKey"></param> + /// <param name="ignoreCache">蹇界暐缂撳瓨</param> + /// <param name="regionUrl">鎸囧畾鏈嶅姟鍣ㄥ煙鍚�,浼犵┖涓轰娇鐢ㄥ綋鍓嶇敤鎴峰湴鍧�</param> /// <returns></returns> - public byte[] DownHeadImage(string userId, bool ignoreCache = false) + public byte[] DownHeadImageByImageKey(string imageKey, bool ignoreCache = false, string regionUrl = "") { try { - var imageKey = GetHeadImageKey(userId); + if (string.IsNullOrEmpty(imageKey)) return null; + byte[] imageBytes; - if (!ignoreCache && FileUtils.Exists(imageKey)) + if (!ignoreCache && Exists(imageKey)) { //鏈湴鏈夌紦瀛� - imageBytes = FileUtils.ReadFile(imageKey); + imageBytes = ReadFile(imageKey); } else { //浜戠涓嬭浇 - imageBytes = GetImageDownloadUrl(imageKey); + imageBytes = GetImageDownloadUrlWithRegionUrl(imageKey, regionUrl); if(imageBytes != null) { //淇濆瓨鍒版湰鍦� - FileUtils.WriteFileByBytes(imageKey, imageBytes); + WriteFileByBytes(imageKey, imageBytes); } } -- Gitblit v1.8.0