using Newtonsoft.Json.Linq;
using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Resources;
using System.Drawing;
using Newtonsoft.Json;
using Gs.DevApp.Entity;
using System.Windows.Forms;
using static System.Windows.Forms.Control;
using DevExpress.XtraEditors;
using DevExpress.XtraTab;
using System.Text.RegularExpressions;
namespace Gs.DevApp.ToolBox
{
///
/// 通用类
///
public class UtilityHelper
{
private static string WebApiUrl = System.Configuration.ConfigurationSettings.AppSettings.Get("WebApiUrl").ToString();
///
/// httpPost访问服务
///
/// 服务地址
/// 方法名称
/// 参数
///
public static string HttpPost(string url, string meth, string param)
{
if (string.IsNullOrEmpty(url))
url = WebApiUrl;
url += meth;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json";
request.Headers.Add("token", GetBasicAuthTicket());
request.Accept = "*/*";
request.Timeout = 15000;
request.AllowAutoRedirect = false;
StreamWriter requestStream = null;
WebResponse response = null;
string responseStr = null;
try
{
requestStream = new StreamWriter(request.GetRequestStream());
requestStream.Write(param);
requestStream.Close();
response = request.GetResponse();
if (response != null)
{
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
responseStr = reader.ReadToEnd();
//File.WriteAllText(Server.MapPath("~/") + @"\test.txt", responseStr);
reader.Close();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
request = null;
requestStream = null;
response = null;
}
return responseStr;
}
///
/// 根据图片名读取资源文件,不带后缀名
///
///
/// 1为大图
///
public static Image GetImgFromResource(string imageName, int lay)
{
// 获取当前程序集
Assembly assembly = Assembly.GetExecutingAssembly();
// 创建资源管理器来访问资源
ResourceManager resourceManager = new ResourceManager("Gs.DevApp.Properties.Resources", assembly);
// 尝试获取图片资源
try
{
Image image = resourceManager.GetObject(imageName) as Image;
if (image != null)
{
return image;
}
}
catch (Exception ex)
{
}
Image image2 = resourceManager.GetObject(lay == 1 ? "chartsshowlegend_32x32" : "linktoprevious_16x16") as Image;
return image2;
}
///
/// 生成token
///
///
public static string GetBasicAuthTicket()
{
string userGuid = string.IsNullOrEmpty(LoginInfoModel.CurrentUser.LoginUserGuid) ? Guid.NewGuid().ToString() : LoginInfoModel.CurrentUser.LoginUserGuid;
string orgGuid = string.IsNullOrEmpty(LoginInfoModel.CurrentUser.LoginOrgGuid) ? Guid.NewGuid().ToString() : LoginInfoModel.CurrentUser.LoginOrgGuid;
string token = (userGuid + "~" + orgGuid);
return token;
}
///
/// 标准json串返回ReturnModel-->table,
///
///
///
public static ReturnModel GetTableByJson(string strReturn)
{
ReturnModel rto = new ReturnModel();
JObject json = JObject.Parse(strReturn);
rto.rtnCode = int.Parse(json["rtnCode"].ToString());
rto.rtnMsg = json["rtnMsg"].ToString();
rto.rtnData = new PageListModel();
rto.rtnData.pages = int.Parse(json["rtnData"]["pages"].ToString());
rto.rtnData.total = int.Parse(json["rtnData"]["total"].ToString());
rto.rtnData.everyPageSize = int.Parse(json["rtnData"]["everyPageSize"].ToString());
JArray array = new JArray();
var d = json["rtnData"]["list"];
foreach (var a in d)
{
array.Add(a);
}
DataTable dt = JsonConvert.DeserializeObject(array.ToString());
rto.rtnData.list = dt;
return rto;
}
///
/// 标准json串返回ReturnModel->字符串,
///
///
///
public static ReturnModel GetDataByJson(string strReturn)
{
ReturnModel rto = new ReturnModel();
JObject json = JObject.Parse(strReturn);
rto.rtnCode = int.Parse(json["rtnCode"].ToString());
rto.rtnMsg = json["rtnMsg"].ToString();
rto.rtnData = json["rtnData"];
return rto;
}
///
/// 标准json串返回ReturnModel-->table,
///
///
///
public static ReturnModel GetNoPageTableByJson(string strReturn)
{
ReturnModel rto = new ReturnModel();
JObject json = JObject.Parse(strReturn);
rto.rtnCode = int.Parse(json["rtnCode"].ToString());
rto.rtnMsg = json["rtnMsg"].ToString();
rto.rtnData = new DataTable();
JArray array = new JArray();
var d = json["rtnData"];
foreach (var a in d)
{
array.Add(a);
}
DataTable dt = JsonConvert.DeserializeObject(array.ToString());
rto.rtnData = dt;
return rto;
}
///
/// 读取默认页大小
///
///
public static int GetPageSize()
{
return 50;
// return int.Parse(System.Configuration.ConfigurationSettings.AppSettings.Get("PageSize").ToString());
}
///
/// 根据对象批量设置文本值
///
/// controls:为groupBox1.Controls/panel1.Controls
/// 对像
/// 是否可编辑
public static void SetValueByObj(ControlCollection controls, dynamic dynamicObject, Boolean isEdt)
{
foreach (JProperty property in dynamicObject.Properties())
{
string strName = property.Name;
string strVal = property.Value.ToString();
try
{
// // 如果value是一个对象,可以递归遍历
// if (property.Value is JObject)
// {
// JObject nestedObject = (JObject)property.Value;
// foreach (JProperty nestedProperty in nestedObject.Properties())
// {
// Console.WriteLine("\tName: {0}, Value: {1}", nestedProperty.Name, nestedProperty.Value);
// }
// }
Control[] cols = controls.Find("txt_" + strName, true);
if (cols.Length > 0)
{
Control colType = cols[0];
//LookUpEdit
if (colType is LookUpEdit)
{
LookUpEdit txt = colType as LookUpEdit;
if (txt != null)
{
txt.EditValue = strVal;
// txt.Text = strVal;
}
txt.Enabled = isEdt;
continue;
}
//下拉
if (colType is ComboBoxEdit)
{
ComboBoxEdit txt = colType as ComboBoxEdit;
if (txt.Properties.TextEditStyle == DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor)
txt.SelectedIndex = int.Parse(strVal);
else
txt.Text = strVal;
txt.Enabled = isEdt;
continue;
}
//文本
if (colType is TextEdit)
{
TextEdit txt = colType as TextEdit;
if (txt != null)
txt.Text = strVal;
txt.Enabled = isEdt;
continue;
}
//数字卡
if (colType is NumericUpDown)
{
NumericUpDown txt = colType as NumericUpDown;
if (txt != null)
txt.Text = strVal;
txt.Enabled = isEdt;
continue;
}
//单选
if (colType is CheckEdit)
{
CheckEdit txt = colType as CheckEdit;
if (txt != null)
txt.Checked = (strVal.ToString() == "1" ? true : false);
txt.Enabled = isEdt;
continue;
}
}
}
catch (Exception ex)
{
MessageBox.Show(strName + ex.Message);
}
}
}
///
/// 清空容器里面的控件
///
/// controls:为groupBox1.Controls/panel1.Controls
/// 是否可编辑
public static void CleanValue(ControlCollection controls, Boolean isEdt)
{
foreach (Control ctrl in controls)
{
ctrl.Enabled = isEdt;
//多行文本
if (ctrl is MemoEdit)
{
MemoEdit txt = ctrl as MemoEdit;
txt.Text = "";
txt.Enabled = isEdt;
continue;
}
//下拉
if (ctrl is ComboBoxEdit)
{
ComboBoxEdit txt = ctrl as ComboBoxEdit;
if (txt.Properties.TextEditStyle == DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor)
txt.SelectedIndex = 0;
else
txt.Text = "";
txt.Enabled = isEdt;
continue;
}
//文本
if (ctrl is TextEdit)
{
ctrl.Text = "";
continue;
}
}
}
///
/// 禁用或启用容器里面的控件
///
/// controls:为groupBox1.Controls/panel1.Controls
/// 是否可编辑
public static void ChangeEnable(ControlCollection controls, Boolean isEdt)
{
foreach (Control ctrl in controls)
{
//文本
if (ctrl is TextEdit)
{
ctrl.Enabled = isEdt;
continue;
}
//数字卡
if (ctrl is NumericUpDown)
{
ctrl.Enabled = isEdt;
continue;
}
}
}
///
///切换选项卡
///
/// 选项卡容器
/// 从0开始,如果是999,则全部可用
public static void ChangeTab(XtraTabControl tabControl, int idx)
{
if (idx == 999)
{
for (int i = 0; i < tabControl.TabPages.Count; i++)
{
tabControl.TabPages[i].PageEnabled = true;
}
tabControl.SelectedTabPageIndex = tabControl.TabPages.Count - 1;
return;
}
for (int i = 0; i < tabControl.TabPages.Count; i++)
{
tabControl.TabPages[i].PageEnabled = false;
}
tabControl.TabPages[idx].PageEnabled = true;
tabControl.SelectedTabPageIndex = idx;
}
public static void TreeViewCheck(TreeViewEventArgs e)
{
try
{
if (e.Node.Nodes.Count > 0)
{
bool NoFalse = true;
foreach (TreeNode tn in e.Node.Nodes)
{
if (tn.Checked == false)
{
NoFalse = false;
}
}
if (e.Node.Checked == true || NoFalse)
{
foreach (TreeNode tn in e.Node.Nodes)
{
if (tn.Checked != e.Node.Checked)
{
tn.Checked = e.Node.Checked;
}
}
}
}
if (e.Node.Parent != null && e.Node.Parent is TreeNode)
{
bool ParentNode = true;
foreach (TreeNode tn in e.Node.Parent.Nodes)
{
if (tn.Checked == false)
{
ParentNode = false;
}
}
if (e.Node.Parent.Checked != ParentNode && (e.Node.Checked == false || e.Node.Checked == true && e.Node.Parent.Checked == false))
{
e.Node.Parent.Checked = ParentNode;
}
}
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 转驼峰命名
///
///
///
public static string ToCamelCase(string input)
{
if (string.IsNullOrEmpty(input))
return input;
// 匹配非字母数字字符后的第一个字母,并将其转换为大写
return Regex.Replace(
input,
"([a-z])([A-Z])",
"$1$2",
RegexOptions.CultureInvariant
).Trim();
}
}
}