using JCSoft.WX.Framework.Api; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Common.Wechat; using Common; using System.Data; using Common.Model; using ZcPeng.PublicLibrary; using CoreEntity.Entity; using CoreEntity.DAL; using System.Collections.Concurrent; using Newtonsoft.Json.Converters; using Newtonsoft.Json; using System.Data.SqlClient; using Microsoft.Extensions.Primitives; using Microsoft.Extensions.Caching.Memory; using Jwt; using PublicLibrary.Model; using Newtonsoft.Json.Linq; using SupplierWeb.Codes.mvc; using SupplierWeb.Codes.Auth; using Common.Config; using SupplierWeb.Commonss; using System.Web; using System.Runtime.Serialization.Json; using System.Text; using System.IO; using System.Runtime.Serialization; namespace SupplierWeb.Controllers { [Route("web/auth")] public class PermissionController : BaseController { public PermissionController(IMemoryCache cache, IApiClient client) : base(cache, client) { } private ConcurrentDictionary roleMap = new ConcurrentDictionary(); /// 供应商,采购,供应商业务员挂靠,资料管理员,财务,仓库 等角色权限列表 /// 权限列表 //[HttpGet, Route("index")] //public ActionResult index(string[] filters, Int32 pageIndex, Int32 pageSize, // string sortField, Int32 sortDirection, string[] sumFields, string callback) //{ // var r = this.indexp(filters, pageIndex, pageSize, // sortField, sortDirection, sumFields, null); // return Content(callback + "(" + JsonConvert.SerializeObject(r.Value) // + ")"); //} /// 供应商,采购,供应商业务员挂靠,资料管理员,财务,仓库 等角色权限列表 /// 权限列表 [HttpPost, Route("index")] public JsonResult indexp(QueryFilter[] filters, Int32 pageIndex, Int32 pageSize, string sortField, Int32 sortDirection, string[] sumFields, [FromBody]dynamic data) { if (data != null) { //Newtonsoft.Json.Linq.JArray filters = data.filters.ToObject(); pageIndex = data.pageIndex; pageSize = data.pageSize; sortField = data.sortField; sortDirection = data.sortDirection; sumFields = data.sumFields.ToObject(); } #region 获取权限列表 DataTable dt = new DataTable(); string result; IList permss = new List(0); List parameters = new List(); string filterstr = QueryFilter.getFilterSqlParam(filters, out parameters, new Permission(), "A."); string direct = " desc "; if (sortDirection != 1) direct = " asc"; int start = (pageIndex - 1) * pageSize; int end = (start + 1 + pageSize); string commandText0 = "select * from "; string commandText1 = "(" + "select A.*,row_number() over" + "( order by " + sortField + " " + direct + " ) as rownum from " + Config.TablePrefix + "Permission as A " + " where 1=1 " + //" and A.CreationPerson = '" + userids + "'" + filterstr + ")AAA "; string commandText2 = " where AAA.rownum>" + start + " and AAA.rownum<" + end; string commandText3 = commandText0 + commandText1 + commandText2;//-tanyang bool success = DataAccess.GetValues(commandText3, ref dt, parameters.ToArray(), out result); #endregion if (dt != null && dt.Rows.Count > 0) { // 把DataTable转换为IList permss = ModelConvertHelper.ConvertToModel(dt); #region 获取权限的角色列表,角色列表 //角色列表 IList roles = RoleDAL.GetRoles(); // 把DataTable转换为IList if (roleMap.Count < roles.Count) { foreach (Role role in roles) { roleMap.TryAdd(role.Id, role); } } foreach (Permission perms in permss) { string direct1 = " desc "; dt = DataAccess.GetDataTable(Config.TablePrefix + "RoleRelatePermission", "Id", "*", "PermissionId=" + perms.Id + " and IsDelete = 0 ", "", "Id" + direct1, 1, 100, out var msg); // 把DataTable转换为IList if (dt != null && dt.Rows.Count > 0) { IList permRelates = ModelConvertHelper.ConvertToModel(dt); //List listRoles = new List(); List superior_list = new List(0); foreach (RoleRelatePermission permsr in permRelates) { //Role role; //if (roleMap.TryGetValue(permsr.RoleId, out role)) //{ // listRoles.Add(role); //} superior_list.Add(permsr.RoleId); } //perms.RoleList = listRoles; perms.Roleids = string.Join(",", superior_list.ToArray()); } } #endregion } string result1; long totalcount = DataAccess.GetRowCountDefine("select count(Id) from " + commandText1, parameters.ToArray(), out result1); IsoDateTimeConverter timejson = new IsoDateTimeConverter { DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss" }; //IList menus = Permission.Convert(permss); var jsonData = JsonConvert.SerializeObject(permss, timejson); return Json(new { items = JsonConvert.DeserializeObject(jsonData), sum = new { }, totalCount = totalcount }); } [AuthPermission] [HttpPost, Route("add")] public JsonResult addp( string PermissionName, string PermissionUrl, string AuthType, Int32 ActionType, string Icon , string ParentId, string Roleids, [FromBody]dynamic data , Int32 IsDelete = 0 ) { if (data != null) { //Newtonsoft.Json.Linq.JArray PermissionName = data.PermissionName; PermissionUrl = data.PermissionUrl; AuthType = data.AuthType; if (ActionType != 0) { ActionType = data.ActionType; } Icon = data.Icon; ParentId = data.ParentId; Roleids = data.Roleids; IsDelete = (data.IsDelete == null) ? 0 : data.IsDelete; } string[] RoleIdss = Roleids.Split(","); int[] RoleIdis = Array.ConvertAll(RoleIdss, s => Convert.ToInt32(s)); #region 添加权限节点 string commandText = "INSERT INTO " + Config.TablePrefix + "Permission (permissionname,permissionurl," + "AuthType,ActionType," + "Icon,ParentId," + "isdelete)" + " VALUES (@permissionname,@permissionurl,@AuthType,@ActionType,@Icon,@ParentId,@isdelete)"; string result; //准备参数 List> parameters = new List>(); parameters.Add(new List() { "permissionname", PermissionName }); parameters.Add(new List() { "permissionurl", PermissionUrl }); parameters.Add(new List() { "AuthType", AuthType }); parameters.Add(new List() { "ActionType", ActionType }); parameters.Add(new List() { "Icon", Icon }); parameters.Add(new List() { "ParentId", ParentId != null ? Convert.ToInt32(ParentId) : 0 }); parameters.Add(new List() { "isdelete", IsDelete }); List parameters1 = DataAccess.ToParameters(parameters); int success = DataAccess.ExecuteCommand(commandText, parameters1, out result); #endregion #region 添加角色 int id = BaseDAL.GetId(Config.TablePrefix + "Permission"); RoleDAL.SaveRolesRelatePermission(id, RoleIdis); #endregion return Json(new { success = success }); } ////编辑权限 //[HttpGet, Route("edit")] //public ActionResult edit(Int32 Id, // string PermissionName, string PermissionUrl, string Icon // , string ParentId, string Roleids // , string callback, Int32 IsDelete // ) //{ // var r = this.editp(Id, PermissionName, PermissionUrl, Icon // , ParentId, Roleids, null // , IsDelete); // return Content(callback + "(" + JsonConvert.SerializeObject(r.Value) // + ")"); //} [AuthPermission] [HttpPost, Route("edit")] public JsonResult editp(Int32 Id, string PermissionName, string PermissionUrl, string AuthType, string Icon, string ParentId, string Roleids, [FromBody]dynamic data , Int32 IsDelete ) { if (data != null) { //Newtonsoft.Json.Linq.JArray Id = data.Id; PermissionName = data.PermissionName; PermissionUrl = data.PermissionUrl; AuthType = data.AuthType; Icon = data.Icon; ParentId = data.ParentId; Roleids = data.Roleids; IsDelete = data.IsDelete == null ? 0 : data.IsDelete; } string[] RoleIdss = Roleids.Split(","); int[] RoleIdis = Array.ConvertAll(RoleIdss, s => Convert.ToInt32(s)); #region 编辑用户 string commandText = "UPDATE " + Config.TablePrefix + "Permission " + " SET permissionname = @permissionname,"; if (PermissionUrl != null) commandText += "permissionurl = @permissionurl,"; commandText += " ParentId = @ParentId" + ",Icon = @Icon" + " ,isdelete = @isdelete" + ",AuthType = @AuthType" + " WHERE id= @Id"; string result; //准备参数 List> parameters = new List>(); parameters.Add(new List() { "permissionname", PermissionName }); parameters.Add(new List() { "permissionurl", PermissionUrl }); parameters.Add(new List() { "AuthType", AuthType }); parameters.Add(new List() { "ParentId", ParentId }); parameters.Add(new List() { "Icon", Icon }); parameters.Add(new List() { "isdelete", IsDelete }); parameters.Add(new List() { "Id", Id }); List parameters1 = DataAccess.ToParameters(parameters); int success = DataAccess.ExecuteCommand(commandText, parameters1, out result); #endregion #region 角色 RoleDAL.SaveRolesRelatePermission(Id, RoleIdis); #endregion return Json(new { success = success }); } ////删除权限 [AuthPermission] [HttpGet, Route("delete/{0}")] public ActionResult Delete(Int32 Id, string callback) { string commandText = "DELETE FROM " + Config.TablePrefix + "Permission WHERE Id = @Id"; //准备参数 List> parameters = new List>(); parameters.Add(new List() { "Id", Id }); string result = ""; List parameters1 = DataAccess.ToParameters(parameters); int success = DataAccess.ExecuteCommand(commandText, parameters1, out result); return Content(callback + "({" + success + "})"); } ////删除权限 [AuthPermission] [HttpPost, Route("delete")] public ActionResult Delete(Int32 Id) { string commandText = "DELETE FROM " + Config.TablePrefix + "Permission WHERE Id = @Id"; //准备参数 List> parameters = new List>(); parameters.Add(new List() { "Id", Id }); string result = ""; List parameters1 = DataAccess.ToParameters(parameters); int success = DataAccess.ExecuteCommand(commandText, parameters1, out result); return Content("{success:" + success + "}"); } [HttpPost] [Route("Esserch")] public ActionResult Esserch([FromBody]dynamic data) { //data={"query":"select a.AppreciationPrice from product a limit 2"}; //data = "{"+"query"+":"+"select a.AppreciationPrice from product a limit 2"+"}"; data = new { query = "select a.AppreciationPrice from product a limit 20" }; var json = JsonConvert.SerializeObject(data); var str = Util.ElasticQuery("_sql?format=json", "POST", json); //long totalcount = DataAccess.GetRowCountDefine("select count(Id) from " + commandText1, parameters.ToArray(), out result1); Root es = new Root(); List aa = new List(); Root stobj = (Root)PermissionController.FromJSON(str); for (int i = 0; i < stobj.rows.Count; i++) { ES bb = new ES(); bb.name = "AA"+i; bb.price = stobj.rows[i][0].ToString(); aa.Add(bb); } var jsonData = JsonConvert.SerializeObject(aa); //return new JSONNetResult(JsonConvert.DeserializeObject(str)); return Json(new { items = JsonConvert.DeserializeObject(jsonData) }); } public class ES { public string name { get; set; } public string price { get; set; } } public class ColumnsItem { public string name { get; set; } public string type { get; set; } } public class Root { /// /// /// public List columns { get; set; } /// /// /// public List> rows { get; set; } } public static T FromJSON(string input) { try { return JsonConvert.DeserializeObject(input); } catch (Exception ex) { string a = ex.Message; return default(T); } } ////权限列表 [HttpGet, Route("menus")] public ActionResult menus(string type,string ParentId) { HttpRequest request = HttpContext.Request; StringValues oo; string staffid = ""; request.Headers.TryGetValue("Sso-Token", out oo); if (oo.Count > 0 && oo.ToArray()[0] != "") { staffid = oo.ToArray()[0]; } //取token var token = (string)_cache.Get(staffid); var secret = TokenConfig.SecretKey; Dictionary data; Object roleid; Int64 roleid1; string jsonData = ""; if (token != null) { try { data = JsonWebToken.DecodeToObject>(token, secret); data.TryGetValue("roleid", out roleid); roleid1 = (Int64)roleid; var permissions = RoleDAL.GetPermissions(roleid1, " and A.AuthType in('"+ type + "')", ParentId); var options = Permission.Convert(permissions); jsonData = JsonConvert.SerializeObject(options); } catch (SignatureVerificationException) { // Given token is either expired or hashed with an unsupported algorithm. } } return Content(jsonData ); } [HttpGet, Route("menusTree")] public JsonResult menusTree(string type) { string roleIdstr = getStaff("roleid"); var sql = $"select A.PermissionName as label,cast(ParentId as varchar) as parentValue,Id as value " + " from " + Config.TablePrefix + "Permission A" + " where A.Id in (select B.PermissionId from mem_RoleRelatePermission B" + " where B.roleid = @roleid and B.permissionId = A.Id and isdelete <> 1 )" + " and A.AuthType in('" + type + "') " + " and A.isdelete = 0 "; DataTable dt = new DataTable(); var param = new List(); param.Add(new SqlParameter("roleId", long.Parse(roleIdstr))); DataAccess.GetValues(sql, ref dt, param.ToArray(), out var msg); IList result = new List(); if (dt != null && dt.Rows.Count > 0) { result = ModelConvertHelper.ConvertToModel(dt); } return Json(new { result, msg }); } ////父权限列表 //[HttpGet, Route("parents/{0}")] //public ActionResult parents(string callback) //{ // var permss = RoleDAL.GetTopPermissions(); // IList