using System.Text; using System.Web; using JNPF.Extras.CollectiveOAuth.Enums; using Newtonsoft.Json; namespace JNPF.Extras.CollectiveOAuth.Utils; public static class GlobalAuthUtil { /// /// 把字典集合拼接成&符号链接的字符串. /// /// 参数字典. /// public static string parseMapToString(Dictionary dicParams) { StringBuilder builder = new StringBuilder(); if (dicParams.Count > 0) { builder.Append(string.Empty); int i = 0; foreach (KeyValuePair item in dicParams) { if (i > 0) builder.Append("&"); builder.AppendFormat("{0}={1}", item.Key, Convert.ToString(item.Value)); i++; } } return builder.ToString(); } /// /// 是否为http协议. /// /// 待验证的url. /// true: http协议, false: 非http协议. public static bool isHttpProtocol(string url) { if (string.IsNullOrWhiteSpace(url)) return false; return url.StartsWith("http://"); } /// /// 是否为https协议. /// /// 待验证的url. /// true: https协议, false: 非https协议. public static bool isHttpsProtocol(string url) { if (string.IsNullOrWhiteSpace(url)) return false; return url.StartsWith("https://"); } /// /// 是否为本地主机(域名). /// /// 待验证的url. /// true: 本地主机(域名), false: 非本地主机(域名). public static bool isLocalHost(string url) { return string.IsNullOrWhiteSpace(url) || url.Contains("127.0.0.1") || url.Contains("localhost"); } /// /// 获取用户的实际性别,常规网站. /// /// 用户第三方标注的原始性别. /// 用户性别. public static AuthUserGender getRealGender(string originalGender) { if (null == originalGender || Convert.ToInt32(AuthUserGender.UNKNOWN).ToString().Equals(originalGender)) { return AuthUserGender.UNKNOWN; } string[] males = { "m", "男", "1", "male" }; if (males.ToList().Contains(originalGender.ToLower())) { return AuthUserGender.MALE; } return AuthUserGender.FEMALE; } /// /// 获取微信平台用户的实际性别,0表示未定义,1表示男性,2表示女性. /// /// 用户第三方标注的原始性别. /// 用户性别. public static AuthUserGender getWechatRealGender(string originalGender) { if (string.IsNullOrWhiteSpace(originalGender) || "0".Equals(originalGender)) { return AuthUserGender.UNKNOWN; } return getRealGender(originalGender); } /// /// url编码. /// /// url. /// public static string urlEncode(string value) { if (value == null) return string.Empty; try { return System.Web.HttpUtility.UrlEncode(value); } catch (Exception e) { throw new Exception("Failed To Encode Uri", e); } } /// /// url解码. /// /// url. /// public static string urlDecode(string value) { if (value == null) return string.Empty; try { return HttpUtility.UrlDecode(value); // utf-8 解码 } catch (Exception e) { throw new Exception("Failed To Decode Uri", e); } } /// /// 字符串转换成枚举. /// /// 对象. /// 类型. /// 对象. public static T enumFromString(string type) { if (type.IsNullOrEmpty()) throw new Exception($"没有找到授权类型: {type}"); try { T result = (T)Enum.Parse(typeof(T), type); // utf-8 解码 return result; } catch (Exception e) { throw new Exception($"授权类型解析失败: {type}", e); } } /// /// json字符串转换为字典集合. /// /// /// public static List> parseListObject(this string jsonStr) { var retDic = new List>(); if (!string.IsNullOrWhiteSpace(jsonStr)) { try { retDic = JsonConvert.DeserializeObject>>(jsonStr); } catch (Exception ex) { } } return retDic; } /// /// json字符串转换为字典集合. /// /// /// public static Dictionary parseObject(this string jsonStr) { var retDic = new Dictionary(); if (!string.IsNullOrWhiteSpace(jsonStr)) { try { retDic = JsonConvert.DeserializeObject>(jsonStr); } catch (Exception ex) { } } return retDic; } /// /// 将URL参数解析为Map(也可以解析Post中的键值对参数). /// /// 参数字符串(或者带参数的Path). /// 参数Map. public static Dictionary parseUrlObject(this string paramsStr) { Dictionary res = new Dictionary(); try { if (paramsStr.IsNullOrWhiteSpace()) { return res; } // 去掉Path部分 int pathEndPos = paramsStr.IndexOf('?'); if (pathEndPos > -1) { paramsStr = paramsStr.Substring(pathEndPos + 1); } return parseStringObject(paramsStr); } catch (Exception e) { return res; } } /// /// string字符串转map,str格式为 {@code xxx=xxx&xxx=xxx}. /// /// 待转换的字符串. /// 字段对象. public static Dictionary parseStringObject(this string accessTokenStr) { Dictionary res = new Dictionary(); if (accessTokenStr.Contains("&")) { string[] fields = accessTokenStr.Split("&"); foreach (var field in fields) { if (field.Contains("=")) { string[] keyValue = field.Split("="); res.Add(urlDecode(keyValue[0]), keyValue.Length == 2 ? urlDecode(keyValue[1]) : null); } } } return res; } /// /// 把字典集合拼接成&符号链接的字符串. /// /// /// public static string spellParams(this Dictionary dicParams) { StringBuilder builder = new StringBuilder(); if (dicParams.Count > 0) { builder.Append(string.Empty); int i = 0; foreach (KeyValuePair item in dicParams) { if (i > 0) builder.Append("&"); builder.AppendFormat("{0}={1}", item.Key, Convert.ToString(item.Value)); i++; } } return builder.ToString(); } /// /// object的字典集合. /// /// /// /// public static string getString(this Dictionary dic, string key) { if (dic == null) return string.Empty; if (dic.ContainsKey(key)) { return Convert.ToString(dic[key]); } else { return string.Empty; } } /// /// 获取参数Int32类型. /// /// /// /// public static int getInt32(this Dictionary request, string paramName) { var paramValue = request.getString(paramName); if (!string.IsNullOrWhiteSpace(paramValue)) { try { return Convert.ToInt32(paramValue); } catch (Exception ex) { return -1; } } return -1; } /// /// 获取参数Int64类型. /// /// /// /// public static long getLong(this Dictionary request, string paramName) { var paramValue = request.getString(paramName); if (!string.IsNullOrWhiteSpace(paramValue)) { try { return Convert.ToInt64(paramValue); } catch (Exception ex) { return -1; } } return -1; } /// /// 获取参数Bool类型. /// /// /// /// public static bool getBool(this Dictionary request, string paramName) { var paramValue = request.getString(paramName); if (!string.IsNullOrWhiteSpace(paramValue)) { try { return Convert.ToBoolean(paramValue); } catch (Exception ex) { return false; } } return false; } /// /// 获取参数字符串并且转换为字典集合. /// /// /// /// public static Dictionary getJSONObject(this Dictionary request, string paramName) { var paramValue = request.getString(paramName); if (!string.IsNullOrWhiteSpace(paramValue)) { try { return paramValue.parseObject(); } catch (Exception ex) { return new Dictionary(); } } return new Dictionary(); } /// /// 获取参数字符串并且转换为字典集合. /// /// /// /// public static List> getJSONArray(this Dictionary request, string paramName) { var paramValue = request.getString(paramName); if (!string.IsNullOrWhiteSpace(paramValue)) { try { return paramValue.parseListObject(); } catch (Exception ex) { return new List>(); } } return new List>(); } /// /// 获取参数字符串类型. /// /// /// /// /*public static string getString(this Dictionary request, string paramName) { var paramValue = request.getString(paramName); if (!string.IsNullOrWhiteSpace(paramValue)) { try { return Convert.ToString(paramValue); } catch (Exception ex) { return null; } } return null; }*/ /// /// 获取参数日期类型. /// /// /// /// public static DateTime? GetParamDateTime(this Dictionary request, string paramName) { var paramValue = request.getString(paramName); if (!string.IsNullOrWhiteSpace(paramValue)) { try { return Convert.ToDateTime(paramValue); } catch (Exception ex) { return null; } } return null; } /// /// 获取参数double类型. /// /// /// /// public static double? GetParamDouble(this Dictionary request, string paramName) { var paramValue = request.getString(paramName); if (!string.IsNullOrWhiteSpace(paramValue)) { try { return Convert.ToDouble(paramValue); } catch (Exception ex) { return null; } } return null; } /// /// 获取参数Decimal类型. /// /// /// /// public static decimal? GetParamDecimal(this Dictionary request, string paramName) { var paramValue = request.getString(paramName); if (!string.IsNullOrWhiteSpace(paramValue)) { try { return Convert.ToDecimal(paramValue); } catch (Exception ex) { return null; } } return null; } /// /// 字典排序,asc排序. /// /// 需排序的字典对象 /// true=正序,反之倒序 public static Dictionary Sort(this Dictionary dic, bool isAsc = true) { Dictionary rdic = new Dictionary(); if (dic.Count > 0) { List> lst = new List>(dic); lst.Sort(delegate (KeyValuePair s1, KeyValuePair s2) { if (isAsc) { return string.CompareOrdinal(s1.Key, s2.Key); } else { return string.CompareOrdinal(s2.Key, s1.Key); } }); foreach (KeyValuePair kvp in lst) rdic.Add(kvp.Key, kvp.Value); } return rdic; } }