using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Ys.Core.Common; using System.Reflection; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using djy.Paas.Model; using djy.Paas.IService; using Swashbuckle.AspNetCore.Swagger; using Swashbuckle.AspNetCore.SwaggerGen; namespace djyweb_djyPaasApi.Controllers { /// /// api接口基类 /// [Route("api/[controller]")] [ApiController] [Authorize] public class ApiBase : Controller { #region http数据获取 /// /// 创建日志 /// /// 内容 /// /// /// /// protected void _LogsAdd(string Message, string GroupName = null, object DataJson = null, string ObjGid = null, object ResultJson = null) { var toolsserver = IOC.AddServer(); toolsserver.LogsAdd(Message, GroupName, DataJson, ObjGid, ResultJson); } /// /// 获取大简云设置权限 /// /// keyname标识 /// protected DJyUserAuthorityDto GetDJyUserAuthority(string KeyName) { var _djyserver = IOC.AddServer(); var rs= _djyserver.GetUserAuthority(GetLoginId,KeyName); if (rs.Status) { return rs.Data; } else { return null; } } /// /// 获取登录详情信息 /// /// protected djy.Model.User GetUserInfo(Guid? UserGid=null) { var _suser=IOC.AddServer(); if (UserGid == null) { UserGid = GetLoginId; } var rs= _suser.GetUserInfo(UserGid.ToString()); if (rs.Status) { return rs.Data; } else { return null; } } /// /// 统一获取大简云权限查询权限的userid companyid 没有权限则指定userid和companyid 为不存的guid值 /// /// /// /// 模块keyname标识 /// 0 查询查看权限 1 操作更新权限 默认 0 /// protected UserAuthorityDto GetUserAuthorityToFormDto(string KeyName,int type=0 ) { //本人的绑定UserId 全部 userid 和 compayid不做绑定 注册公司的 绑定 companyid 没有权限则指定userid和companyid 为不存的guid值 var RetrunData = new UserAuthorityDto(); var _djyserver = IOC.AddServer(); djy.Model.User User; User = null; var uuid = GetLoginId.ToString(); var userrs = _djyserver.GetUserInfo(GetLoginId.ToString()); var notguid = Guid.Parse("00000000-0000-0000-0000-000000000001"); RetrunData.CompayId = null; if (userrs.Status) { User = userrs.Data; RetrunData.CompayId = Guid.Parse(User.CompId); } if (User == null) { RetrunData.UserId = notguid; RetrunData.CompayId = notguid; } var aut = GetDJyUserAuthority(KeyName); if (aut != null) {//根据权限处理 _userid 和 _companyid 值 RetrunData.IsPower = true; var _useraut= aut.Visiblerange; if (type == 1) { _useraut = aut.Operaterange; } switch (_useraut) { case 0://全部 RetrunData.UserId = null; RetrunData.CompayId = null; break; case 1://本公司 RetrunData.UserId = null; RetrunData.CompayId = Guid.Parse(User.CompId); break; case 3://本人 RetrunData.UserId = GetLoginId; RetrunData.CompayId = null; break; case 4://无权限或或默认权限 //_uerid = notguid; //_companyid = notguid; RetrunData.UserId = notguid; RetrunData.CompayId = notguid; RetrunData.IsPower = false; break; case 7://注册公司 RetrunData.UserId = null; RetrunData.CompayId = Guid.Parse( User.CompId); break; default: RetrunData.UserId = notguid; RetrunData.CompayId = notguid; break; } } else { RetrunData.UserId = notguid; RetrunData.CompayId = notguid; RetrunData.IsPower = false; } //if (sysOptionConfig.YsWebconfig.IsDev) //{ // RetrunData.UserId = null; // RetrunData.CompayId =null; // RetrunData.IsPower = true; //} return RetrunData; } /// /// 获取登录Id /// protected Guid? GetLoginId { get { return Guid.Parse(GetClaimsValue("loginid")); } } /// /// 获取登录名称 /// protected string GetLoginName { get { return GetClaimsValue("loginname"); } } /// /// 获取登录类型 /// protected EnumUser.UserType GetLoginType { get { var type = GetClaimsValue("logintype"); if (type.Isint()) { return (EnumUser.UserType)int.Parse(type); } else { return EnumUser.UserType.All; } } } /// /// 根据key获取claims值 没有则返回null /// /// /// 是否是加密 /// protected string GetClaimsValue(string Key, bool IsDecrtypt = true) { try { var claims = HttpContext.User.Claims; var id = claims.SingleOrDefault(s => s.Type == Key); if (id == null) { return "0"; } return IsDecrtypt ? _DecryptDES(id.Value) : id.Value; } catch { return null; } } /// /// DES解密 /// /// /// protected static string _DecryptDES(string value) { return SafeTools.DecryptDES(value, sysOptionConfig.YsWebconfig.DesKey); } #endregion } /// ///api接口基类 /// /// 接口类型比如 Iservice public class ApiBase : ApiBase { /// /// /// protected IS _server = IOC.AddServer(); /// /// 执行指定的方法 /// /// 方法名称 /// 参数对象队列 /// protected object _InvokeServer(string methodName, object[] parameters) { return _server.GetType().GetMethod(methodName).Invoke(_server, parameters); } } /// /// api接口基类 /// /// 接口类型比如 Iservice /// Dto Model /// Tables数据表model public class ApiBase : ApiBase { /// /// 根据Id获取实体 /// /// /// protected virtual object _GetId(long Id) { return _InvokeServer("GetId", new object[] { Id }); } /// ///基础的创建接口 提交创建对象 /// /// /// protected virtual object _Add(T Dto) { return _InvokeServer("Add", new object[] { Dto }); } /// /// 最基础的更新接口 传递要更新的数据对象 必须有Id /// /// /// protected virtual object _Up(T Dto) { return _InvokeServer("Up", new object[] { Dto, null, null }); } /// /// 最基础的删除接口 [1,2,3] /// /// /// protected virtual object _Del(long[] Idlist) { return _InvokeServer("Del", new object[] { Idlist }); } } }