using MiniWebApi.Network; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Web; using Vinno.IUS.Common.Log; namespace Vinno.vCloud.Disk { public static class ApiExtends { private static ViewEngine viewEngine = new ViewEngine(); /// /// 验证是否为空,空返回true /// /// /// /// /// /// /// public static bool VerificationIsEmpty(this WebApiHttpContext context, string param, string paramName, string contentType = "html", string pageName = "PageError.html") { bool verificationResult = false; var errorMsg = string.Format("Param {0} is empty", paramName); if (string.IsNullOrWhiteSpace(param)) { context.PageResponse(contentType, pageName, new { ErrorMsg = errorMsg }); verificationResult = true; } return verificationResult; } /// /// 验证是否包含特殊字符,包含返回true /// /// /// /// /// /// /// public static bool VerificationSpecialChar(this WebApiHttpContext context, string param, string paramName, string contentType = "html", string pageName = "PageError.html") { bool verificationResult = false; var errorMsg = string.Format("Param {0} are not allowed to have special characters", paramName); if (Regex.IsMatch(param, "[^0-9a-zA-Z]+")) { context.PageResponse(contentType, pageName, new { ErrorMsg = errorMsg }); verificationResult = true; } return verificationResult; } /// /// 验证是否小于0 ,小于0返回true; /// /// /// /// /// /// /// public static bool VerificationLessThanZero(this WebApiHttpContext context, string param, string paramName, string contentType = "html", string pageName = "PageError.html") { bool verificationResult = false; var errorMsg = string.Format("Param {0} must greater than zero", paramName); double doubleTotalAmount; double.TryParse(param, out doubleTotalAmount); if (doubleTotalAmount <= 0) { context.PageResponse(contentType, pageName, new { ErrorMsg = errorMsg }); verificationResult = true; } return verificationResult; } /// /// 写入响应数据 /// /// /// /// /// /// public static void PageResponse(this WebApiHttpContext context, string contentType, string pageName, object model) { switch (contentType) { case "html": string html = Encoding.UTF8.GetString(viewEngine.GetSource(pageName, contentType, model).ToArray()); context.Response.ContentType = "text/html"; context.Response.Write(Encoding.UTF8.GetBytes(html)); break; case "json": context.Response.Json(model); break; } } /// /// 获取Get参数 /// /// /// /// /// public static string GetParam(this WebApiHttpContext context, string keyName, string defaultValue = "") { var param = context.Request.QueryString.Get(keyName).XssEncode(); return !string.IsNullOrWhiteSpace(param) ? param : defaultValue; } /// /// 获取Post参数 /// /// /// public static Dictionary GetParams(this WebApiHttpContext context) { try { var postContent = context.Request.GetInputStreamValue(); Dictionary keyValuePairs = new Dictionary(); var uriParams = postContent; if (uriParams != null) { var paramsArr = uriParams.Split('&'); foreach (string param in paramsArr) { var keyValue = param.Split('='); var key = HttpUtility.UrlDecode(keyValue[0]).Replace("[]", ""); var value = keyValue.Length > 1 ? HttpUtility.UrlDecode(keyValue[1]) : string.Empty; if (keyValuePairs.Keys.Contains(key)) { if (keyValuePairs[key] is List pairs) { pairs.Add(value); } else { List newPairs = new List(); var convertValue = keyValuePairs[key] as string; newPairs.Add(convertValue); newPairs.Add(value); keyValuePairs[key] = newPairs; } } else { keyValuePairs.Add(key, value); } } } return keyValuePairs; } catch (Exception ex) { Logger.WriteLineError("GetParams error:" + ex.Message + "|" + ex.StackTrace); return new Dictionary(); } } /// /// 获取Dictionary参数 /// /// /// /// /// public static string GetStringValue(this Dictionary keyValuePairs, string keyName, string defaultValue = "") { return GetValue(keyValuePairs, keyName, defaultValue); } /// /// 获取Dictionary参数 /// /// /// /// /// public static List GetArrayValue(this Dictionary keyValuePairs, string keyName) { return GetArrayValueData(keyValuePairs, keyName); } /// /// 获取Dictionary参数 /// /// /// /// /// private static List GetArrayValueData(Dictionary keyValuePairs, string keyName) { var list = GetValue(keyValuePairs, keyName, new List()); if (list == null || list.Count == 0) { var onlyValue = GetValue(keyValuePairs, keyName, string.Empty); if (!onlyValue.IsEmpty()) { list = new List { onlyValue }; } } return list; } /// /// 获取Dictionary参数 /// /// /// /// /// public static T GetValue(this Dictionary keyValuePairs, string keyName, T defaultValue = null) where T : class { T paramValue = defaultValue ?? default(T); try { paramValue = (keyValuePairs[keyName] as T); if (paramValue == null) { paramValue = defaultValue; } } catch(Exception ex) { Logger.WriteLineError("GetValue error:" + ex.Message + "|" + ex.StackTrace); } return paramValue; } /// /// 获取SortedDictionary参数 /// /// /// /// /// public static string GetValue(this SortedDictionary keyValuePairs, string keyName, string defaultValue = "") { string paramValue = defaultValue; try { paramValue = keyValuePairs[keyName].ToStringEx().XssEncode(); if (string.IsNullOrWhiteSpace(paramValue)) { paramValue = defaultValue; } } catch (Exception ex) { Logger.WriteLineError("GetValue error:" + ex.Message + "|" + ex.StackTrace); } return paramValue; } /// /// ToString扩展函数 /// /// /// /// /// public static string ToStringEx(this T obj, string defaultValue = "") { if (obj == null) { return defaultValue; } return obj.ToString(); } } }