ConfigRead.cs 7.64 KB
using Hh.Mes.Common.Json;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Text;

namespace Hh.Mes.Common.config
{
    /// <summary>
    ///  配置读取 ,公共变量 支持内存缓存 
    /// </summary>
    public class ConfigRead
    {
        #region 属性
        private static volatile ConfigRead _configRead = null;
        private readonly AppsettingsHelp _appsettingsHelp;

        /// <summary>
        /// ConnectionStrings
        /// </summary>
        private ConnectionStrings  appsetConnection { get; set; }  
        
        private Dictionary<string, string> OperType { get; set; }

        /// <summary>
        /// Gets the instance.
        /// </summary>
        public static ConfigRead GetInstance
        {
            get
            {
                if (_configRead == null)
                {
                    lock (typeof(ConfigRead))
                    {
                        if (_configRead == null)
                        {
                            _configRead = new ConfigRead();
                        }
                    }
                }
                return _configRead;
            }
        }

        /// <summary>
        /// BaseInfoCache.json  sql
        /// </summary>
        private Dictionary<string, string> baseSql { get; set; }
        #endregion

        private ConfigRead()
        {
            _appsettingsHelp = new AppsettingsHelp();
        }

        #region method


        /// <summary>
        /// ConnectionStrings 密码已解密
        /// </summary>
        /// <returns></returns>
        public ConnectionStrings GetAppsetConnection()
        {
            try
            {
                if (appsetConnection == null)
                {
                    appsetConnection = _appsettingsHelp.GetAppsettings<ConnectionStrings>("ConnectionStrings");
                    var  stringBuilder = new SqlConnectionStringBuilder(appsetConnection.BaseDBContext);
                    stringBuilder.Password = Encryption.Decrypt(stringBuilder.Password);
                    appsetConnection.BaseDBContext= stringBuilder.ConnectionString;
                    if (!string.IsNullOrEmpty(appsetConnection.AopAllowed))
                    {
                        var temp = appsetConnection.AopAllowed.Split(',', StringSplitOptions.RemoveEmptyEntries);
                        appsetConnection.allowed = temp;
                    }
                }
            }
            catch
            {
                throw new Exception("ConfigRead 类 ,GetAppsetConnection 方法读取数据库连接失败");
            }
            return appsetConnection;
        }



        /// <summary>
        /// OperLogFilter  OperType
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> GetOperType()
        {
            if (OperType == null)
            {
                OperType = new Dictionary<string, string>
                {
                    {"Index", "查看"},
                    {"Ins", "增加"},
                    {"Upd", "更新"},
                    {"DelByIds", "删除"}
                };
            }
            return OperType;
        }


        #region  BaseInfoCache
        /// <summary>
        /// GetBaseInfoSql
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> GetBaseInfoSql()
        {
            if (baseSql == null || baseSql.Count == 0)
            {
                baseSql = new Dictionary<string, string>();
                string filePath = Path.Combine(AppContext.BaseDirectory, "BaseInfoCache.json");
                string content = ReadFileString(filePath);
                var tempData = JsonHelper.JsonConvertObject<BaseInfoCls>(content);
                for (int i = 0; i < tempData.baseInfo.Count; i++)
                {
                    baseSql.Add(tempData.baseInfo[i].key, tempData.baseInfo[i].sql);
                }
            }
            return baseSql;
        }


        /// <summary>
        /// 读取文件 返回string
        /// </summary>
        protected string ReadFileString(string filePath)
        {
            string content = "";
            using (StreamReader sr = new StreamReader(filePath, Encoding.Default))
            {
                content = sr.ReadToEnd();
            }
            return content;
        } 
        #endregion


        #endregion
    }

    /// <summary>
    /// appsettings.json  ConnectionStrings 节点类
    /// </summary>
    public class ConnectionStrings
    {
        public string BaseDBContext { get; set; }

        public string InterfaceDBContext { get; set; }

        public string RedisIp { get; set; }


        public int HttpsPort { get; set; }

        public int HttpPort { get; set; }

        //当前环境是http还是https  默认值是http
        public string HttpOrHttps{ get; set; }  

        public int ReportPort { get; set; }

        public string LoginTitle { get; set; }

        public string FaceBaseUrl { get; set; }
        public string ServerIPList { get; set; }

        public string CerPwd { get; set; }

        public string CerPath { get; set; }

        /// <summary>
        /// 是否是开发环境  false是开发环境,true是正式环境
        /// 开发者程序禁止修改
        ///</summary>
        public bool IsDevelopment { get; set; }

        /// <summary>
        /// AOP 不拦截
        /// </summary>
        public string AopAllowed { get; set; }

        /// <summary>
        ///重置密码
        /// </summary>
        public string ResetPwd { get; set; }
        


        /// <summary>
        /// aop 判断使用次字段
        /// </summary>
        public string[] allowed { get; set; }


        /// <summary>
        /// aop 判断使用次字段
        /// </summary>
        public string[] ext { get; set; }


        /// <summary>
        /// 扩展1 
        /// </summary>
        public dynamic Extend1 { get; set; }


        /// <summary>
        /// 扩展2
        /// </summary>
        public dynamic Extend2 { get; set; }


        /// <summary>
        /// 扩展3
        /// </summary>
        public dynamic Extend3 { get; set; }


        /// <summary>
        /// 扩展4
        /// </summary>
        public dynamic Extend4 { get; set; }

        /// <summary>
        /// 扩展5
        /// </summary>
        public dynamic Extend5 { get; set; }
        /// <summary>
        /// 扩展6
        /// </summary>
        public dynamic Extend6 { get; set; }
        /// <summary>
        /// 扩展7
        /// </summary>
        public dynamic Extend7 { get; set; }
        /// <summary>
        /// 扩展8
        /// </summary>
        public dynamic Extend8{ get; set; }


        #region 配置文件AppCustomSettings 扩展字段
        public dynamic AppCustomExtend1 { get; set; }
        public dynamic AppCustomExtend2 { get; set; }
        public dynamic AppCustomExtend3 { get; set; }
        public dynamic AppCustomExtend4 { get; set; }

        public dynamic AppCustomExtend5 { get; set; }
        public dynamic AppCustomExtend6 { get; set; }
        public dynamic AppCustomExtend7 { get; set; }
        public dynamic AppCustomExtend8 { get; set; }
        #endregion
    }

    #region BaseInfoCache.json 缓存类
    internal class BaseInfoCls
    {
        /// <summary>
        /// 
        /// </summary>
        public List<BaseInfoItem> baseInfo { get; set; }
    }

    internal class BaseInfoItem
    {
        /// <summary>
        /// 
        /// </summary>
        public string key { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string sql { get; set; }
    } 
    #endregion
}