lu
2024-12-06 e6a9b817b0fd82dd89bf58107388edd1ab89bf11
DevApp/Gs.DevApp/ToolBox/UtilityHelper.cs
@@ -13,6 +13,7 @@
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.RichEdit.Export;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid;
@@ -25,6 +26,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using static System.Windows.Forms.Control;
namespace Gs.DevApp.ToolBox
{
@@ -64,35 +66,35 @@
        /// <param name="meth">方法名称</param>
        /// <param name="param">参数</param>
        /// <returns></returns>
        public static async Task<string> HttpPostAsync(string url, string meth, string param)
        {
            if (string.IsNullOrEmpty(url))
                url = WebApiUrl;
            url += meth;
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.Headers.Add("token", GetBasicAuthTicket());
            request.Accept = "*/*";
            request.Timeout = 15000;
            request.AllowAutoRedirect = false;
            request.ServicePoint.Expect100Continue = false;
            HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
            request.CachePolicy = noCachePolicy;
            using (Stream requestStream = await request.GetRequestStreamAsync())
            {
                byte[] dataBytes = Encoding.UTF8.GetBytes(param);
                await requestStream.WriteAsync(dataBytes, 0, dataBytes.Length);
            }
        //public static async Task<string> HttpPostAsync(string url, string meth, string param)
        //{
        //    if (string.IsNullOrEmpty(url))
        //        url = WebApiUrl;
        //    url += meth;
        //    var request = (HttpWebRequest)WebRequest.Create(url);
        //    request.Method = "POST";
        //    request.ContentType = "application/json";
        //    request.Headers.Add("token", GetBasicAuthTicket());
        //    request.Accept = "*/*";
        //    request.Timeout = 15000;
        //    request.AllowAutoRedirect = false;
        //    request.ServicePoint.Expect100Continue = false;
        //    HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
        //    request.CachePolicy = noCachePolicy;
        //    using (Stream requestStream = await request.GetRequestStreamAsync())
        //    {
        //        byte[] dataBytes = Encoding.UTF8.GetBytes(param);
        //        await requestStream.WriteAsync(dataBytes, 0, dataBytes.Length);
        //    }
            using (WebResponse response = await request.GetResponseAsync())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    return await reader.ReadToEndAsync();
                }
            }
        }
        //    using (WebResponse response = await request.GetResponseAsync())
        //    {
        //        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        //        {
        //            return await reader.ReadToEndAsync();
        //        }
        //    }
        //}
        /// <summary>
        ///     httpPost访问服务
@@ -103,24 +105,28 @@
        /// <returns></returns>
        public static string HttpPost(string url, string meth, string param)
        {
            if (string.IsNullOrEmpty(url))
                url = WebApiUrl;
            url += meth;
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.Headers.Add("token", GetBasicAuthTicket());
            request.Accept = "*/*";
            request.Timeout = 15000;
            request.AllowAutoRedirect = false;
            request.ServicePoint.Expect100Continue = false;
            HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
            request.CachePolicy = noCachePolicy;
            DevExpress.Utils.WaitDialogForm wdf = new DevExpress.Utils.WaitDialogForm("拼命加载中,请稍后..." + meth, "友情提示");
            HttpWebRequest request = null;
            StreamWriter requestStream = null;
            WebResponse response = null;
            string responseStr = null;
            try
            {
                if (string.IsNullOrEmpty(url))
                    url = WebApiUrl;
                url += meth;
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "application/json";
                request.Headers.Add("token", GetBasicAuthTicket());
                request.Accept = "*/*";
                request.Timeout = 15000;
                request.AllowAutoRedirect = false;
                request.ServicePoint.Expect100Continue = false;
                wdf.SetCaption("加载进度(10/100)");
                HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
                request.CachePolicy = noCachePolicy;
                wdf.SetCaption("加载进度(20/100)");
                requestStream = new StreamWriter(request.GetRequestStream());
                requestStream.Write(param);
                requestStream.Close();
@@ -133,9 +139,11 @@
                    //File.WriteAllText(Server.MapPath("~/") + @"\test.txt", responseStr); 
                    reader.Close();
                }
                wdf.SetCaption("加载进度(80/100)");
            }
            catch (Exception ex)
            {
                wdf.Close();
                LogHelper.Debug(url, param + ":" + ex.Message);
                throw ex;
            }
@@ -145,7 +153,8 @@
                requestStream = null;
                response = null;
            }
            wdf.SetCaption("加载进度(90/100)");
            wdf.Close();
            return responseStr;
        }
@@ -193,7 +202,14 @@
        {
            var dt = new DataTable();
            foreach (GridColumn col in gv.Columns)
                dt.Columns.Add(col.FieldName, typeof(string));
            {
                if (col.UnboundDataType == typeof(bool))
                    dt.Columns.Add(col.FieldName, typeof(System.Boolean));
                else
                {
                    dt.Columns.Add(col.FieldName, typeof(string));
                }
            }
            gc.BindingContext = new BindingContext();
            gc.DataSource = dt;
            gc.ForceInitialize();
@@ -293,13 +309,16 @@
                {
                    foreach (GridColumn colmn in gv.Columns)
                    {
                        if (colmn.Name.ToString().Contains("gvMxDel"))
                        if (colmn.Name.ToString().Contains("gvMxDel") || colmn.Name.ToString().Contains("gvMxTui"))
                        {
                            colmn.Visible = !isEdt;
                            break;
                            //  colmn.Visible = !isEdt;
                            //  break;
                            colmn.OptionsColumn.AllowEdit = true;
                        }
                        else
                            colmn.OptionsColumn.AllowEdit = !isEdt;
                    }
                    gv.OptionsBehavior.Editable = !isEdt;
                    //gv.OptionsBehavior.Editable = !isEdt;
                }
            var _btnAry = controls.Find("btnSelect", false);
            if (_btnAry.Length > 0)
@@ -565,6 +584,17 @@
                                txt.Enabled = false;
                            continue;
                        }
                        //自定组织
                        if (colType is UcLookOrg)
                        {
                            var txt = colType as UcLookOrg;
                            txt.SetIdOrCode(strVal);
                            if (txt.IsReadly == false)
                                txt.Enabled = !isEdt;
                            else
                                txt.Enabled = false;
                            continue;
                        }
                        if (colType is SimpleButton)
                        {
@@ -596,13 +626,16 @@
                {
                    foreach (GridColumn colmn in gv.Columns)
                    {
                        if (colmn.Name.ToString().Contains("gvMxDel"))
                        if (colmn.Name.ToString().Contains("gvMxDel") || colmn.Name.ToString().Contains("gvMxTui"))
                        {
                            colmn.Visible = !isEdt;
                            break;
                            //  colmn.Visible = !isEdt;
                            //  break;
                            colmn.OptionsColumn.AllowEdit = true;
                        }
                        else
                            colmn.OptionsColumn.AllowEdit = !isEdt;
                    }
                    gv.OptionsBehavior.Editable = !isEdt;
                    //gv.OptionsBehavior.Editable = !isEdt;
                }
            foreach (Control ctrl in controls)
            {
@@ -738,6 +771,14 @@
                    txt.Enabled = !isEdt;
                    continue;
                }
                //自定组织
                if (ctrl is UcLookOrg)
                {
                    var txt = ctrl as UcLookOrg;
                    txt.SetIdOrCode("-1");
                    txt.Enabled = !isEdt;
                    continue;
                }
                if (ctrl is SimpleButton)
                {
                    var txt = ctrl as SimpleButton;
@@ -763,13 +804,16 @@
                {
                    foreach (GridColumn colmn in gv.Columns)
                    {
                        if (colmn.Name.ToString().Contains("gvMxDel"))
                        if (colmn.Name.ToString().Contains("gvMxDel") || colmn.Name.ToString().Contains("gvMxTui"))
                        {
                            colmn.Visible = !isEdt;
                            break;
                            //  colmn.Visible = !isEdt;
                            //  break;
                            colmn.OptionsColumn.AllowEdit = true;
                        }
                        else
                            colmn.OptionsColumn.AllowEdit = !isEdt;
                    }
                    gv.OptionsBehavior.Editable = !isEdt;
                    //gv.OptionsBehavior.Editable = !isEdt;
                }
            foreach (Control ctrl in controls)
            {
@@ -902,6 +946,16 @@
                        txt.Enabled = false;
                    continue;
                }
                //自定组织
                if (ctrl is UcLookOrg)
                {
                    var txt = ctrl as UcLookOrg;
                    if (txt.IsReadly == false)
                        txt.Enabled = !isEdt;
                    else
                        txt.Enabled = false;
                    continue;
                }
            }
        }
@@ -1028,6 +1082,14 @@
                return 0;
            }
        }
        public static int ToBit(string str)
        {
            if (str.ToUpper() == "true".ToUpper())
                return 1;
            return 0;
        }
        /// <summary>
        /// 读取grid的当前行
        /// </summary>
@@ -1282,16 +1344,20 @@
            return _sbSqlWhere.ToString();
        }
        /// <summary>
        /// 初始化gridview
        ///
        /// </summary>
        /// <param name="gridView1"></param>
        public static void SetGridSear(GridView gridView1)
        /// <param name="gridView1">gridview</param>
        /// <param name="btnChkIco">图标按钮</param>
        /// <param name="fm">当前窗体</param>
        /// <param name="fileName">字段</param>
        /// <param name="icoName">图标路径</param>
        public static void SetGridSear(GridView gridView1, PictureBox btnChkIco = null, Form fm = null, string fileName = "checkStatus", string icoName = "")
        {
            foreach (GridColumn column in gridView1.Columns)
            {
                column.OptionsFilter.AutoFilterCondition = AutoFilterCondition.Contains;
                column.OptionsFilter.ImmediateUpdateAutoFilter = false;
                column.OptionsColumn.AllowEdit = false;
                // column.OptionsColumn.AllowEdit = false;
                if (column.Tag == null || column.Tag.ToString().Length <= 0)
                    column.OptionsFilter.AllowAutoFilter = false;
            }
@@ -1316,10 +1382,15 @@
                    e.Bounds.Top + 45, e.Bounds.Right - 5, e.Bounds.Height - 5);
                e.Graphics.DrawString(str, f, Brushes.Gray, r);
            };
            if (btnChkIco != null)
            {
                gridView1.FocusedRowChanged += (s, e) =>
                {
                    UtilityHelper.SetCheckIco(s, btnChkIco, fm, fileName, icoName);
                };
            }
        }
        #endregion
        public static bool IsNumeric(string str)
        {
@@ -1327,7 +1398,11 @@
            return regex.IsMatch(str);
        }
        /// <summary>
        /// yz_quantity_0,yz_字段_类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void SetValidatingEditor(object sender, BaseContainerValidateEditorEventArgs e)
        {
            //yz_quantity_0