123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using DllUfpEntity;
- using DllUfpDal;
- using System.Collections;
- using Cksoft.Data;
- using Cksoft.Data.Repository;
- using Microsoft.AspNetCore.Mvc;
- using System.Linq;
- using Microsoft.AspNetCore.Authorization;
- using Newtonsoft.Json;
- using DllUfpEntity.Dto;
- using Cksoft.Unity.Log4NetConfig;
- using Cksoft.Unity;
- using System.Threading.Tasks;
- using DllUfpUtil;
- namespace DllUfpBll
- {
- [Route("ufp/api/[controller]/[action]")]
- //[Authorize]
- [ApiController]
- public class FunctionController : ControllerBase
- {
- /// <summary>
- /// 获取根菜单
- /// </summary>
- /// <returns></returns>
- public UfpResponse<EapFunction> GetFunctionRoots(int sysId)
- {
- //int sysId = Convert.ToInt32(Request.Headers["sysid"]);
- IDatabase db = null;
- try
- {
- db = DbFactory.Base("ufp");
- var dal = new FunctionDal(db);
- string errorinfo = string.Empty;
- var roots = dal.GetRoots(sysId, errorinfo);
- var response = new UfpResponse<EapFunction>
- {
- Code = 1,
- data = roots,
- Msg = errorinfo
- };
- if (roots == null || roots.Count() <= 0)
- {
- response.Code = -1;
- }
- // db.Commit();
- return response;
- }
- catch (Exception e)
- {
- // db.Rollback();
- return new UfpResponse<EapFunction>()
- {
- Code = -1,
- Msg = e.Message
- };
- }
- finally
- {
- if (db != null)
- db.Close();
- }
- }
- /// <summary>
- /// 获取子菜单
- /// </summary>
- /// <param name="imputds"></param>
- /// <param name="errorinfo"></param>
- /// <param name="error"></param>
- /// <returns></returns>
- public UfpResponse<EapFunction> GetSubFunctions(int parentId)
- {
- int sysId = Convert.ToInt32(Request.Headers["sysid"]);
- IEnumerable<EapFunction> subFunctions = null;
- using (IDatabase db = DbFactory.Base("ufp"))
- {
- var dal = new FunctionDal(db);
- string errorinfo = string.Empty;
- subFunctions = dal.GetSubFunctions(sysId, parentId, errorinfo);
- var response = new UfpResponse<EapFunction>
- {
- Code = 1,
- data = subFunctions,
- Msg = errorinfo
- };
- if (subFunctions == null || subFunctions.Count() <= 0)
- {
- response.Code = -1;
- }
- return response;
- }
- }
- /// <summary>
- /// 根据角色Id获取树菜单
- /// </summary>
- /// <param name="roleId"></param>
- /// <returns></returns>
- [HttpGet]
- public UfpResponse<FuncTree> GetFuncTree(int roleId)
- {
- // int sysId = Convert.ToInt32(Request.Headers["sysid"]);
- using (IDatabase db = DbFactory.Base("ufp"))
- {
- var funcDal = new FunctionDal(db);
- int sysId = funcDal.GetSysIdByRoleId(roleId);
- return new UfpResponse<FuncTree>()
- {
- Code = 1,
- data = funcDal.GetFuncTreesAll(sysId),
- Msg = ""
- };
- }
- }
- public EapFunction GetFunction(int id)
- {
- using (IDatabase db = DbFactory.Base("ufp"))
- {
- var dal = new FunctionDal(db);
- return dal.GetFunction(id);
- }
- }
- [HttpPost]
- public string Add([FromBody] EapFunction function)
- {
- var usercode = HttpContext.Request.Headers["userCode"];
- int res = 0;
- IDatabase db = null;
- try
- {
- db = DbFactory.Base("ufp");
- db.BeginTrans();
- var dal = new FunctionDal(db);
- function.ModCode = usercode;
- function.ModTime = DateTime.Now;
- if (function.ID == 0)
- {
- function.RecCode = usercode;
- function.RecTime = function.ModTime;
- res = dal.Insert(function, usercode);
- if (res < 0)
- {
- db.Rollback();
- return "-1";
- }
- }
- else
- {
- res = dal.Update(function, usercode);
- if (res < 0)
- {
- db.Rollback();
- return "-1";
- }
- }
- db.Commit();
- if (function.ID == 0)
- {
- LogHelper<EapFunction>.LogFatal("新增Function-->" + Json.ToJson(function), "用户操作", usercode);
- }
- else
- {
- LogHelper<EapFunction>.LogFatal("修改Function-->" + Json.ToJson(function), "用户操作", usercode);
- }
- return res.ToString();
- }
- catch (Exception e)
- {
- db.Rollback();
- return "-1";
- }
- finally
- {
- if (db != null)
- db.Close();
- }
- }
- [HttpPost]
- public string Delete([FromBody] int id)
- {
- using (IDatabase db = DbFactory.Base("ufp"))
- {
- var dal = new FunctionDal(db);
- var model = dal.GetFunction(id);
- if (dal.Delete(id) > 0)
- {
- LogHelper<EapFunction>.LogFatal("删除Function-->:" + Json.ToJson(model), "用户操作", Request.Headers["usercode"]);
- return "1";
- }
- else
- {
- return "-1";
- }
- }
- }
- [HttpPost]
- public string SetFunction([FromBody] AuthorizeModel model)
- {
- string usercode = Request.Headers["usercode"];
- IDatabase db = null;
- try
- {
- db = DbFactory.Base("ufp");
- db.BeginTrans();
- var dal = new FunctionDal(db);
- if (dal.DeleteRoleFuncs(model.Multity.Removes, model.Single) < 0)
- {
- db.Rollback();
- return JsonConvert.SerializeObject(new
- {
- code = -1,
- msg = "删除未授权菜单时出错"
- });
- }
- if (dal.SetFunction(model.Multity.Adds, model.Single, usercode) < 0)
- {
- db.Rollback();
- return JsonConvert.SerializeObject(new
- {
- code = -1,
- msg = "新增菜单授权时出错"
- });
- }
- db.Commit();
- return JsonConvert.SerializeObject(new
- {
- code = 1,
- msg = ""
- });
- }
- catch (Exception e)
- {
- db.Rollback();
- return JsonConvert.SerializeObject(new
- {
- code = -1,
- msg = e.Message
- });
- }
- finally
- {
- db.Close();
- }
- }
- [HttpGet]
- public IEnumerable<int> GetAlreadyAuthFuncIds(int roleId, int type)
- {
- using (IDatabase db = DbFactory.Base("ufp"))
- {
- var dal = new FunctionDal(db);
- return dal.GetAuthedFunIds(roleId, type);
- }
- }
- [HttpGet]
- public IEnumerable<int> GetFuncIds(string filter)
- {
- using (IDatabase db = DbFactory.Base("ufp"))
- {
- var dal = new FunctionDal(db);
- return dal.GetFuncIds(filter);
- }
- }
- /// <summary>
- /// 获取首页菜单
- /// </summary>
- /// <returns></returns>
- [HttpGet]
- public UfpResponse<Menu> GetMenus()
- {
- string usercode = Request.Headers["usercode"];
- int sysId = Convert.ToInt32(Request.Headers["sysId"]);
- using (IDatabase db = DbFactory.Base("ufp"))
- {
- var dal = new FunctionDal(db);
- var menus = dal.GetMenusByUserCode(sysId, usercode);
- if (menus == null || menus.Count() <= 0)
- {
- return new UfpResponse<Menu>()
- {
- Code = -1,
- data = null,
- Msg = "获取菜单失败"
- };
- }
- return new UfpResponse<Menu>
- {
- Code = 1,
- data = menus,
- Msg = ""
- };
- }
- }
- [HttpGet]
- public async Task<UfpResponse<AlainMenu>> GetMenusForAlain(string token)
- {
- var userinfo = await OAuthHelper.GetUserInfo(token);
- int sysId = Convert.ToInt32(Request.Headers["sysId"]);
- using (IDatabase db = DbFactory.Base("ufp"))
- {
- var dal = new FunctionDal(db);
- var menus = dal.GetMenusByUserCodeForAlain(sysId, userinfo.UserAccount);
- if (menus == null || menus.Count() <= 0)
- {
- return new UfpResponse<AlainMenu>()
- {
- Code = -1,
- data = null,
- Msg = "获取菜单失败"
- };
- }
- return new UfpResponse<AlainMenu>
- {
- Code = 1,
- data = menus,
- Msg = ""
- };
- }
- }
- /// <summary>
- /// 根据接收到的工号和菜单地址判断当前用户是否有访问该菜单的权限
- /// </summary>
- /// <param name="usercode"></param>
- /// <param name="url"></param>
- /// <returns></returns>
- [HttpGet]
- public EapResponse IsAusthorized(string usercode, string url)
- {
- int sysId = Convert.ToInt32(Request.Headers["sysid"]);
- var res = new EapResponse { Code = 1, Msg = string.Empty };
- using (IDatabase db = DbFactory.Base("ufp"))
- {
- var dal = new FunctionDal(db);
- var isAuthed = dal.IsAuthorized(sysId, usercode, url) > 0;
- if (!isAuthed)
- {
- res.Code = -1;
- }
- return res;
- }
- }
- [HttpGet]
- public EapResponse GetMean(int id)
- {
- using (IDatabase db = DbFactory.Base("ufp"))
- {
- var dal = new FunctionDal(db);
- return dal.GetMean( id);
- }
- }
- }
- }
|