using Cksoft.Data; using Cksoft.Data.Repository; using Cksoft.Unity; using Cksoft.Unity.Log4NetConfig; using DllUfpDal; using DllUfpEntity; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Text; namespace DllUfpBll { [Route("ufp/api/[controller]/[action]")] [Authorize] public class BtnFuncController : ControllerBase { [HttpGet] public LayuiModel Get(string filter, int pageIndex = 1, int pageSize = 10, string sortField = "FCode", string sortOrder = "ascend") { if (sortOrder == "descend") { sortOrder = "desc"; } else { sortOrder = "asc"; } int start, end; start = (pageIndex - 1) * pageSize + 1; end = start + pageSize; using (IDatabase db = DbFactory.Base("ufp")) { db.BeginTrans(); var dal = new BtnFuncDal(db); var total = dal.GetCount(filter); string errorinfo = string.Empty; var roles = dal.Get(start, pageSize, sortOrder, sortField, filter, errorinfo); return new LayuiModel { code = 1, count = total, data = roles, msg = "" }; } } [HttpGet] public BtnFunc GetSingle(string id) { using (IDatabase db = DbFactory.Base("ufp")) { var dal = new BtnFuncDal(db); return dal.Get(id); } } [HttpPost] public string Add([FromBody] BtnFunc func) { string usercode = Request.Headers["usercode"]; using (IDatabase db = DbFactory.Base("ufp")) { var dal = new BtnFuncDal(db); if (func.ID == 0) { int count = dal.Add(func, usercode); if (count > 0) { LogHelper.LogFatal("新增BtnFunc-->" + Json.ToJson(func), "用户操作", usercode); } return count.ToString(); } else { int count = dal.Update(func, usercode); if (count > 0) { LogHelper.LogFatal("修改BtnFunc-->" + Json.ToJson(func), "用户操作", usercode); } return count.ToString(); } } } [HttpPost] public string Delete([FromBody] int id) { IDatabase db = null; string errormsg = string.Empty; try { db = DbFactory.Base("ufp"); var dal = new BtnFuncDal(db); db.BeginTrans(); var model = dal.Get(id.ToString()); var modelsec = dal.getRoleFunc(id); var res = dal.Delete(id, ref errormsg); if (res < 0) { db.Rollback(); return errormsg; } db.Commit(); LogHelper.LogFatal("删除BtnFunc-->:" + Json.ToJson(model) + ";RoleFunc" + Json.ToJson(modelsec), "用户操作", Request.Headers["usercode"]); return "1"; } catch (Exception e) { errormsg = e.Message; return errormsg; } finally { if (db != null) db.Close(); } } [HttpGet] public IEnumerable GetAuthedBtns(int roleId) { using (IDatabase db = DbFactory.Base("ufp")) { var dal = new FunctionDal(db); return dal.GetAuthedBtns(roleId); } } [HttpPost] public string SetBtns([FromBody] AuthorizeModel model) { string usercode = Request.Headers["usercode"]; IDatabase db = null; try { db = DbFactory.Base("ufp"); db.BeginTrans(); var dal = new BtnFuncDal(db); var modelfir = dal.getRoleFuncByRoleId(model.Single); if (dal.DeleteRoleFuncs(model.Multity.Removes, model.Single) < 0) { db.Rollback(); return JsonConvert.SerializeObject(new { code = -1, msg = "删除未授权按钮时出错" }); } if (dal.SetBtnFunction(model.Multity.Adds, model.Single, usercode) < 0) { db.Rollback(); return JsonConvert.SerializeObject(new { code = -1, msg = "新增菜单授权时出错" }); } db.Commit(); LogHelper.LogFatal("AuthorizeModel-->" + Json.ToJson(model), "用户操作", usercode); LogHelper.LogFatal("删除RoleFuncByRole-->" + Json.ToJson(modelfir), "用户操作", usercode); return JsonConvert.SerializeObject(new { code = 1, msg = "" }); } catch (Exception e) { db.Rollback(); return JsonConvert.SerializeObject(new { code = -1, msg = e.Message }); } finally { db.Close(); } } /// /// 判断是否有操作权限 /// /// public EapResponse GetIsPermitted(string btnCode) { string errorinfo = string.Empty; string userCode = Request.Headers["usercode"]; using (IDatabase ufpDb = DbFactory.Base("ufp")) { var btnFuncDal = new BtnFuncDal(ufpDb); var res = btnFuncDal.IsPermitted(userCode, btnCode, ref errorinfo); return new EapResponse { Code = res ? 1 : 0 }; } } } }