Przeglądaj źródła

xls:品牌logo文件key值格式调整

xulisong 6 lat temu
rodzic
commit
4252e0644e

+ 1 - 0
MBI/FirmLib/Com.FirmLib.UI/Com.FirmLib.UI.csproj

@@ -125,6 +125,7 @@
     <Compile Include="Common\IdNameItem.cs" />
     <Compile Include="Common\InfoPointTemplateManager.cs" />
     <Compile Include="Common\TryCatchWrapper.cs" />
+    <Compile Include="Common\UrlFileKeyParse.cs" />
     <Compile Include="Extension\MessageTipExtension.cs" />
     <Compile Include="Common\Model\AssetShowItem.cs" />
     <Compile Include="Common\Model\VenderItem.cs" />

+ 70 - 0
MBI/FirmLib/Com.FirmLib.UI/Common/UrlFileKeyParse.cs

@@ -0,0 +1,70 @@
+/*-------------------------------------------------------------------------
+ * 功能描述:UrlFileKeyParse
+ * 作者:xulisong
+ * 创建时间: 2019/4/28 10:26:25
+ * 版本号:v1.0
+ *  -------------------------------------------------------------------------*/
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using FirmHttpDao;
+using Newtonsoft.Json;
+using SAGA.DotNetUtils.Data;
+
+namespace Com.FirmLib.UI
+{
+    public class UrlFileKeyParse
+    {
+        /// <summary>
+        /// 创建文件key
+        /// </summary>
+        /// <param name="urlFile"></param>
+        /// <returns></returns>
+        public static string CreatKey(UrlFile urlFile)
+        {
+            if (urlFile == null)
+                return string.Empty;
+            return JsonConvert.SerializeObject(urlFile);
+        }
+        /// <summary>
+        /// 解析文件key
+        /// </summary>
+        /// <param name="urlKey"></param>
+        /// <returns></returns>
+        public static UrlFile ParseKey(string urlKey)
+        {
+            UrlFile urlFile = null;
+            try
+            {
+                urlFile = JsonConvert.DeserializeObject<UrlFile>(urlKey);
+            }
+            catch (Exception)
+            {
+            }
+            if (urlFile == null)
+            {
+                urlFile = new UrlFile();
+                urlFile.FileType = "image";
+                urlFile.Key = urlKey;
+                urlFile.SystemId = BllHttpSetting.Instance.Default.SystemId;
+            }
+            return urlFile;
+        }
+        /// <summary>
+        /// 创建UrlFile文件
+        /// </summary>
+        /// <param name="fileKey"></param>
+        /// <returns></returns>
+        public static UrlFile CreateImageUrlFile(string fileKey)
+        {
+            UrlFile urlFile = new UrlFile();
+            urlFile.FileType = "image";
+            urlFile.Key = fileKey;
+            urlFile.SystemId = BllHttpSetting.Instance.Default.SystemId;
+            return urlFile;
+        }
+    }
+}

+ 46 - 47
MBI/FirmLib/Com.FirmLib.UI/Manufacturer/VmBrandEditor.cs

@@ -8,26 +8,22 @@
 ////////////////////////////////////////////////////////////////////////////////
 
 using System;
-using System.Collections.Generic;
 using System.IO;
 using System.Linq;
-using System.Net;
 using System.Net.Cache;
 using System.Text;
-using System.Threading.Tasks;
 using System.Windows;
-using System.Windows.Controls;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using Com.FirmLib.Bll;
 using Com.FirmLib.Entity;
 using Com.FirmLib.UI.Manufacturer.Model;
 using FirmHttpDao;
-using FWindSoft.Data;
 using FWindSoft.DataFramework;
 using FWindSoft.MVVM;
 using FWindSoft.Wpf;
 using Microsoft.Win32;
+using SAGA.DotNetUtils.Data;
 
 namespace Com.FirmLib.UI.Manufacturer
 {
@@ -38,10 +34,9 @@ namespace Com.FirmLib.UI.Manufacturer
         {
             Init();
         }
-
         private void Init()
         {
-            LogoKey = new NameValueItem<string>();
+            InputFile = new InputFile<UrlFile>();
             //为空可能引发遮罩异常
             ImageBrush = new ImageBrush();
         }
@@ -55,7 +50,6 @@ namespace Com.FirmLib.UI.Manufacturer
                 ManufacturerId = para.ManufacturerId;
                 BrandId = para.Id;
                 BranchName = para.Name;
-                //TODO 图片信息处理
             }
             LoadBrandItem(ManufacturerId, BrandId);
         }
@@ -76,9 +70,9 @@ namespace Com.FirmLib.UI.Manufacturer
             }
         }
         /// <summary>
-        /// 图片编码
+        /// 上传文件输入
         /// </summary>
-        public NameValueItem<string> LogoKey { get;private set; }
+        public InputFile<UrlFile> InputFile { get;private set; }
 
         private ImageBrush m_ImageBrush;
         /// <summary>
@@ -107,8 +101,6 @@ namespace Com.FirmLib.UI.Manufacturer
         /// </summary>
         public string BrandId { get; private set; }
         #region 命令相关
-
-
         /// <summary>
         /// 上传图片
         /// </summary>
@@ -134,11 +126,10 @@ namespace Com.FirmLib.UI.Manufacturer
                     }
                     BitmapSource imageSource = (BitmapSource)isConverter.ConvertFrom(bytes);
                     ImageBrush = null;
-                    //ImageBrush =
-                    //    new ImageBrush((ImageSource)isConverter.ConvertFrom(bytes));
                     ImageBrush = new ImageBrush(ImageUtil.GetCenterCropped(imageSource));
-                    LogoKey.Value = fileName;
-                    LogoKey.Tag = bytes;
+                    InputFile.Path = fileName;
+                    InputFile.Content = bytes;
+                    InputFile.AcceptContent();
                 }
             }
             catch (Exception ex)
@@ -184,7 +175,6 @@ namespace Com.FirmLib.UI.Manufacturer
         [Command]
         public void AddProductCommand(object parameter)
         {
-
             try
             {
                 if (!AddBrand())
@@ -203,8 +193,6 @@ namespace Com.FirmLib.UI.Manufacturer
         {
             return IsValidated;
         }
-
-
         /// <summary>
         /// 删除命令
         /// </summary>
@@ -212,7 +200,6 @@ namespace Com.FirmLib.UI.Manufacturer
         [Command]
         public void DeleteCommand(object parameter)
         {
-
             try
             {
                 if (!DeleteBrand())
@@ -231,7 +218,6 @@ namespace Com.FirmLib.UI.Manufacturer
         {
             return true;
         }
-
         #endregion
         #region 私有方法
         /// <summary>
@@ -287,7 +273,6 @@ namespace Com.FirmLib.UI.Manufacturer
             return result;
         } 
 
-
         //管理图片信息
         public void LoadBrandItem(string venderId,string brandId)
         {
@@ -296,13 +281,12 @@ namespace Com.FirmLib.UI.Manufacturer
             var brandItem=BllFactory<BrandBll>.Instance.GetItem(venderId, brandId);
             if (brandItem == null)
                 return;
-            LogoKey.Name=brandItem.LogoKey;
-            if (!string.IsNullOrEmpty(brandItem.LogoKey))
+            InputFile.Key=brandItem.LogoKey;
+            if (!string.IsNullOrWhiteSpace(brandItem.LogoKey))
             {
-                //todo 调用接口加载图片信息
-
-                string imageUrl = BllFactory<FileBll>.Instance.CreateImageUrl(BllHttpSetting.Instance.Default, brandItem.LogoKey);
-
+                var urlFile = UrlFileKeyParse.ParseKey(InputFile.Key);
+                InputFile.KeyEntity = urlFile;
+                string imageUrl = BllFactory<FileBll>.Instance.CreateImageUrl(FileUrlValidateFactory.Create(urlFile.SystemId),urlFile.Key);
                 BitmapSource bitmap = null;
                 try
                 {
@@ -323,7 +307,6 @@ namespace Com.FirmLib.UI.Manufacturer
                      bitmap = new BitmapImage(new Uri(imageUrl), new RequestCachePolicy(RequestCacheLevel.Reload));
                 }
                 ImageBrush = null;
-               // ImageBrush = new ImageBrush(bitmap);
                 ImageBrush = new ImageBrush(bitmap);
             }
 
@@ -331,27 +314,43 @@ namespace Com.FirmLib.UI.Manufacturer
 
         public string GetLogoKey()
         {
-            /*
-            name 存储键值,value 存路径
-            */
-            var bytes = LogoKey.Tag as byte[];
-            if (bytes == null)
-            {
-                //如果没有图片的加载路径,则返回默认的键值(空,或者修改前)
-                return LogoKey.Name ?? string.Empty;
-            }
-            //如果存在路径信息,则需要上传图片,键值为null的话创建新的键值
-            string logoKey = LogoKey.Name;
-            if (string.IsNullOrEmpty(logoKey))
+            if (InputFile.UpdateStatus == 0)
             {
-                logoKey = BllFactory<FileBll>.Instance.CreateKey(ManufacturerId);
+                return InputFile.Key ?? string.Empty;
             }
-            var flag=BllFactory<FileBll>.Instance.UploadImage(BllHttpSetting.Instance.Default,logoKey, bytes);
-            if (!flag)
+            string fileKey = InputFile.KeyEntity?.Key;         
+            if (InputFile.Content != null)
             {
-                MessageTip.Show(this.GetWindow(), BllFactory<FileBll>.Instance.ErrorMessage ?? string.Empty);
-            }
-            return logoKey;
+                if (string.IsNullOrWhiteSpace(fileKey))
+                {
+                    fileKey = new TimeKeyBuilder().CreateKey();
+                    #region 扩展名处理
+                    if (!string.IsNullOrEmpty(InputFile.Path))
+                    {
+                        try
+                        {
+                            fileKey += Path.GetExtension(InputFile.Path);
+                        }
+                        catch (Exception e)
+                        {
+                        }
+                    }
+                    #endregion
+                }
+
+                var urlFile = UrlFileKeyParse.CreateImageUrlFile(fileKey);
+                var flag = BllFactory<FileBll>.Instance.UploadImage(FileUrlValidateFactory.Create(urlFile.SystemId), urlFile.Key, InputFile.Content);
+                if (!flag)
+                {
+                    MessageTip.Show(this.GetWindow(), BllFactory<FileBll>.Instance.ErrorMessage ?? string.Empty);
+                }
+                else
+                {
+                    InputFile.KeyEntity = urlFile;
+                }
+                
+            }           
+            return UrlFileKeyParse.CreatKey(InputFile.KeyEntity);
         }
 
         #endregion

+ 2 - 0
MBI/FirmLib/Com.FirmLib/Bll/FileBll.cs

@@ -8,6 +8,7 @@ using System.Linq;
 using System.Net.Http;
 using System.Text;
 using System.Threading.Tasks;
+using SAGA.DotNetUtils.Data;
 
 namespace Com.FirmLib.Bll
 {
@@ -116,6 +117,7 @@ namespace Com.FirmLib.Bll
         }
         #endregion
 
+
         #region 文件相关
         /// <summary>
         /// 上传文件

+ 18 - 0
MBI/FirmLib/Com.FirmLib/FileUrlValidate.cs

@@ -17,4 +17,22 @@ namespace Com.FirmLib
         /// </summary>
         public string Secret { get; set; }
     }
+
+
+    public class FileUrlValidateFactory
+    {
+        /// <summary>
+        /// 创建文件操作token
+        /// </summary>
+        /// <param name="systemId"></param>
+        /// <returns></returns>
+        public static FileUrlValidate Create(string systemId)
+        {
+            FileUrlValidate validate = new FileUrlValidate();
+            validate.SystemId = systemId;
+            validate.Secret= "dev" == systemId ? "123" : "123";
+            return validate;
+        }
+    }
+
 }

+ 87 - 0
MBI/SAGA.DotNetUtils/Data/InputFile.cs

@@ -0,0 +1,87 @@
+/*-------------------------------------------------------------------------
+ * 功能描述:InputFile
+ * 作者:xulisong
+ * 创建时间: 2019/4/26 16:56:45
+ * 版本号:v1.0
+ *  -------------------------------------------------------------------------*/
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SAGA.DotNetUtils.Data
+{
+    /// <summary>
+    /// 输入文件类【封装了上传,下载的相关逻辑操作】
+    /// </summary>
+    public class InputFile
+    {
+        #region 描述
+        /*
+         * 根据需求不同,可以使用Path或Content;
+         * 当对象发生变化时,比如对已有绑定对象的修改,
+         * 可以手工指定是使用Path还是使用Content去读写
+         */
+        #endregion
+
+        public InputFile()
+        {
+            UpdateStatus = 0;
+        }
+        /// <summary>
+        /// 文件名称(可以和path中名称不同)
+        /// </summary>
+        public string Name { get; set; }
+        /// <summary>
+        /// 文件key值,与持久化平台对接使用(可以扩展成有格式的字符串自行解析)
+        /// </summary>
+        public string Key { get; set; }
+        /// <summary>
+        /// 文件地址
+        /// </summary>
+        public string Path { get; set; }
+        /// <summary>
+        /// 文件内容
+        /// </summary>
+        public byte[] Content { get; set; }
+
+        /// <summary>
+        /// 修改状态(0不变,1使用path变化,2使用Content变化)
+        /// </summary>
+        public int UpdateStatus
+        {
+            get;private set;
+        }
+
+        #region stats相关操作
+        public void InitStats()
+        {
+            UpdateStatus = 0;
+        }
+        /// <summary>
+        /// 接收路径变化
+        /// </summary>
+        public void AcceptPath()
+        {
+            UpdateStatus = 1;
+        }
+        /// <summary>
+        /// 接收内容变化
+        /// </summary>
+        public void AcceptContent()
+        {
+            UpdateStatus = 2;
+        }
+        #endregion
+    }
+
+    public class InputFile<T>:InputFile
+    {
+        /// <summary>
+        /// key的实体信息
+        /// </summary>
+        public T KeyEntity { get; set; }
+    }
+}

+ 61 - 0
MBI/SAGA.DotNetUtils/Data/TimeKeyBuilder.cs

@@ -0,0 +1,61 @@
+/*-------------------------------------------------------------------------
+ * 功能描述:TimeKeyBuilder
+ * 作者:xulisong
+ * 创建时间: 2019/4/28 9:57:50
+ * 版本号:v1.0
+ *  -------------------------------------------------------------------------*/
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SAGA.DotNetUtils.Data
+{
+    /// <summary>
+    /// 事件key值生成器
+    /// </summary>
+    public class TimeKeyBuilder
+    {
+        public string TimeFormat { get; private set; }
+        public int SeedCount { get; private set; }
+        public TimeKeyBuilder()
+        {
+            TimeFormat = "yyyyMMddHHmmssfff";
+            SeedCount = 10000;
+        }
+        public TimeKeyBuilder(string timeFormat, int sendCount)
+        {
+            if (sendCount < 1)
+            {
+                throw new ArgumentException($"{nameof(sendCount)}不能小于1");
+            }
+            TimeFormat = timeFormat;
+            SeedCount = sendCount;
+        }
+        public virtual string CreateKey()
+        {
+            var fileName = DateTime.Now.ToString(TimeFormat);
+            Random rnd = new Random(Guid.NewGuid().GetHashCode());
+            var randomStr = rnd.Next(1, SeedCount).ToString().PadLeft(GetDigit(SeedCount-1), '0');
+            return $"{fileName}{randomStr}";
+
+        }
+        /// <summary>
+        /// 获取整数的位数
+        /// </summary>
+        /// <param name="inputValue"></param>
+        /// <returns></returns>
+        private  int GetDigit( int inputValue)
+        {
+            inputValue = Math.Abs(inputValue);
+            int digit;
+            for (digit = 0; inputValue > 0; digit++)
+            {
+                inputValue /= 10;
+            }
+            return Math.Max(digit, 1);
+        }
+    }
+}

+ 44 - 0
MBI/SAGA.DotNetUtils/Data/UrlFile.cs

@@ -0,0 +1,44 @@
+/*-------------------------------------------------------------------------
+ * 功能描述:UrlFile
+ * 作者:xulisong
+ * 创建时间: 2019/4/26 16:08:31
+ * 版本号:v1.0
+ *  -------------------------------------------------------------------------*/
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Newtonsoft.Json;
+
+namespace SAGA.DotNetUtils.Data
+{
+    /// <summary>
+    /// 网络文件信息
+    /// </summary>
+    public class UrlFile
+    {
+        /// <summary>
+        /// 文件显示名称
+        /// </summary>
+        [JsonProperty("name")]
+        public string Name { get; set; }
+        /// <summary>
+        /// 定位文件服务器key值
+        /// </summary>
+        [JsonProperty("key")]
+        public string Key { get; set; }
+        /// <summary>
+        /// 文件类型
+        /// </summary>
+        [JsonProperty("type")]
+        public string FileType { get; set; }
+        /// <summary>
+        /// 文件所属平台
+        /// </summary>
+        [JsonProperty("systemId")]
+        public string SystemId { get; set; }
+     
+    }
+}

+ 4 - 0
MBI/SAGA.DotNetUtils/SAGA.DotNetUtils.csproj

@@ -297,10 +297,13 @@
     <Compile Include="Data\FlagDecorator.cs" />
     <Compile Include="Data\InitAttribute.cs" />
     <Compile Include="Data\InitObejct.cs" />
+    <Compile Include="Data\InputFile.cs" />
     <Compile Include="Data\MessageMapItem.cs" />
     <Compile Include="Data\NumberGenerater.cs" />
     <Compile Include="Data\SingleFactory.cs" />
     <Compile Include="Data\SingleInstance.cs" />
+    <Compile Include="Data\TimeKeyBuilder.cs" />
+    <Compile Include="Data\UrlFile.cs" />
     <Compile Include="DB\SQLiteHelper.cs" />
     <Compile Include="DB\SQLiteUpdateUtil.cs" />
     <Compile Include="DB\TableNameAttribute.cs" />
@@ -391,6 +394,7 @@
     <Compile Include="Service\ServiceManager.cs" />
     <Compile Include="Service\ServiceWrapper.cs" />
     <Compile Include="Utilities\CommonUtil.cs" />
+    <Compile Include="Utilities\FileKeyUtil.cs" />
     <Compile Include="Utilities\GuidUtil.cs" />
     <Compile Include="Utilities\CompressUtil.cs" />
     <Compile Include="Utilities\FileUp.cs" />

+ 19 - 0
MBI/SAGA.DotNetUtils/Utilities/FileKeyUtil.cs

@@ -0,0 +1,19 @@
+/*-------------------------------------------------------------------------
+ * 功能描述:FileKeyUtil
+ * 作者:xulisong
+ * 创建时间: 2019/4/28 8:58:05
+ * 版本号:v1.0
+ *  -------------------------------------------------------------------------*/
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SAGA.DotNetUtils.Utilities
+{
+    public class FileKeyUtil
+    {
+    }
+}