You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
229 lines
7.5 KiB
229 lines
7.5 KiB
1 month ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using LitJson;
|
||
|
using SimpleHttpServer;
|
||
|
using System.Reflection;
|
||
|
using Sog;
|
||
|
|
||
|
namespace Operation
|
||
|
{
|
||
|
public static class Resolver
|
||
|
{
|
||
|
private static Dictionary<int, Permission> Permissions = new Dictionary<int, Permission>();
|
||
|
|
||
|
private static Dictionary<string, Menu> menus = new Dictionary<string, Menu>();
|
||
|
|
||
|
//无需登陆可直接请求的接口:注册、登陆、外部api等
|
||
|
public static List<KeyValuePair<string,string>> Ignored = new List<KeyValuePair<string,string>>();
|
||
|
|
||
|
public static List<string> logRequestList = new List<string>();
|
||
|
|
||
|
public static List<string> AllRequestList = new List<string>();
|
||
|
|
||
|
|
||
|
private static void CheckPermissionCode()
|
||
|
{
|
||
|
int[] permissionCodes = (int[])Enum.GetValues(typeof(PermissionCode));
|
||
|
bool hasDuplicates = permissionCodes.GroupBy(x => x).Any(g => g.Count() > 1);
|
||
|
if (hasDuplicates)
|
||
|
{
|
||
|
throw new InvalidOperationException("permission code duplicates,please check");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void Register(string httpApiCmd,
|
||
|
Func<string, JsonData, HttpResponse, HttpRequest, HttpQueryParams, uint, int> httpApiFunction)
|
||
|
{
|
||
|
if (httpApiFunction == null)
|
||
|
{
|
||
|
Ignored.Add(new KeyValuePair<string, string>(key:httpApiCmd,value:""));
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (AllRequestList.Contains(httpApiCmd))
|
||
|
{
|
||
|
throw new InvalidOperationException();
|
||
|
}
|
||
|
|
||
|
AllRequestList.Add(httpApiCmd);
|
||
|
var method = httpApiFunction.Method;
|
||
|
var declaringType = method.DeclaringType;
|
||
|
string parent = "";
|
||
|
if (declaringType != null)
|
||
|
{
|
||
|
var menuMapping = declaringType.GetCustomAttribute<MenuMapping>();
|
||
|
if (menuMapping != null)
|
||
|
{
|
||
|
if (menuMapping.refc == null)
|
||
|
{
|
||
|
var name = menuMapping.Name;
|
||
|
var order = menuMapping.Order;
|
||
|
var icon = menuMapping.Icon;
|
||
|
var permission = menuMapping.Pcode;
|
||
|
parent = name;
|
||
|
AddParentMenu(name, "", icon, order, permission);
|
||
|
AddPermission(name, "", permission);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
var refcMenuMapping = menuMapping.refc.GetCustomAttribute<MenuMapping>();
|
||
|
if (refcMenuMapping == null)
|
||
|
{
|
||
|
throw new InvalidOperationException();
|
||
|
}
|
||
|
|
||
|
var name = refcMenuMapping.Name;
|
||
|
var order = refcMenuMapping.Order;
|
||
|
var icon = refcMenuMapping.Icon;
|
||
|
var permission = refcMenuMapping.Pcode;
|
||
|
parent = name;
|
||
|
AddParentMenu(name, "", icon, order, permission);
|
||
|
AddPermission(name, "", permission);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var attr = method.GetCustomAttribute<RequestMapping>();
|
||
|
if (attr != null)
|
||
|
{
|
||
|
var toMenu = attr.isMenu;
|
||
|
var name = attr.Name;
|
||
|
var pCode = (int)attr.Permission;
|
||
|
if (toMenu)
|
||
|
{
|
||
|
AddSubMenu(parent, name, httpApiCmd, "", 1, pCode);
|
||
|
}
|
||
|
|
||
|
AddPermission(name, httpApiCmd, pCode);
|
||
|
if (attr.toLog)
|
||
|
{
|
||
|
logRequestList.Add(httpApiCmd);
|
||
|
}
|
||
|
|
||
|
if (attr.Ignored)
|
||
|
{
|
||
|
Ignored.Add(new KeyValuePair<string, string>(key:httpApiCmd,value:name));
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Ignored.Add(new KeyValuePair<string, string>(key:httpApiCmd,value:""));
|
||
|
TraceLog.Debug("Resolver.Register http request permission not found, url={0}", httpApiCmd);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void AddParentMenu(string name, string url, string icon, int order, int permission)
|
||
|
{
|
||
|
if (menus.ContainsKey(name))
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
Menu menu = new Menu();
|
||
|
menu.Url = url;
|
||
|
menu.Name = name;
|
||
|
menu.Order = order;
|
||
|
menu.ParentId = 0;
|
||
|
menu.Icon = icon;
|
||
|
menu.Permission = permission;
|
||
|
menus[name] = menu;
|
||
|
}
|
||
|
|
||
|
public static void AddSubMenu(string parent, string name, string url, string icon, int order, int permission)
|
||
|
{
|
||
|
if (!menus.ContainsKey(parent))
|
||
|
{
|
||
|
throw new InvalidOperationException("Resolver.AddSubMenu not found parent menu parentId=" + parent);
|
||
|
}
|
||
|
|
||
|
var pMenu = menus[parent];
|
||
|
Menu menu = new Menu();
|
||
|
menu.Url = url;
|
||
|
menu.Name = name;
|
||
|
menu.Order = order;
|
||
|
menu.ParentId = 0;
|
||
|
menu.Icon = icon;
|
||
|
menu.Permission = permission;
|
||
|
pMenu.subMenus.Add(menu);
|
||
|
}
|
||
|
|
||
|
public static void AddPermission(string name, string url, int code)
|
||
|
{
|
||
|
if (Permissions.ContainsKey(code))
|
||
|
{
|
||
|
if (url != "")
|
||
|
{
|
||
|
Permissions[code].Urls.Add(url);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
int pocde = 0;
|
||
|
Type enumType = typeof(PermissionCode);
|
||
|
Array enumValues = Enum.GetValues(enumType);
|
||
|
foreach (var p in enumValues)
|
||
|
{
|
||
|
FieldInfo fieldInfo = enumType.GetField(p.ToString());
|
||
|
PermissionMapping customAttribute = (PermissionMapping)Attribute.GetCustomAttribute(fieldInfo,
|
||
|
typeof(PermissionMapping));
|
||
|
if ((int)p == code && customAttribute != null)
|
||
|
{
|
||
|
pocde = customAttribute.PCode;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Permission permission = new Permission();
|
||
|
permission.Name = name;
|
||
|
permission.Id = code;
|
||
|
permission.ParentId = pocde;
|
||
|
if (url != "")
|
||
|
{
|
||
|
permission.Urls.Add(url);
|
||
|
}
|
||
|
|
||
|
Permissions[code] = permission;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
public static bool IsIgnored(string httpCmd)
|
||
|
{
|
||
|
return Ignored.Any(pair => pair.Key.Equals(httpCmd));
|
||
|
}
|
||
|
|
||
|
public static bool IsWritLog(string httpCmd)
|
||
|
{
|
||
|
return logRequestList.Contains(httpCmd);
|
||
|
}
|
||
|
|
||
|
public static string GetUrlName(string url)
|
||
|
{
|
||
|
foreach (var pair in Permissions)
|
||
|
{
|
||
|
var urls = pair.Value.Urls;
|
||
|
foreach (var u in urls)
|
||
|
{
|
||
|
if (u.Equals(url))
|
||
|
{
|
||
|
return pair.Value.Name;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
//刷新菜单权限数据
|
||
|
public static void Flusher()
|
||
|
{
|
||
|
CheckPermissionCode();
|
||
|
AdminSvc.InitMenus(menus);
|
||
|
|
||
|
AdminSvc.InitPermissions(Permissions);
|
||
|
// Permissions.Clear();
|
||
|
// menus.Clear();
|
||
|
}
|
||
|
}
|
||
|
}
|