FunctionController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using DllUfpEntity;
  5. using DllUfpDal;
  6. using System.Collections;
  7. using Cksoft.Data;
  8. using Cksoft.Data.Repository;
  9. using Microsoft.AspNetCore.Mvc;
  10. using System.Linq;
  11. using Microsoft.AspNetCore.Authorization;
  12. using Newtonsoft.Json;
  13. using DllUfpEntity.Dto;
  14. using Cksoft.Unity.Log4NetConfig;
  15. using Cksoft.Unity;
  16. using System.Threading.Tasks;
  17. using DllUfpUtil;
  18. namespace DllUfpBll
  19. {
  20. [Route("ufp/api/[controller]/[action]")]
  21. //[Authorize]
  22. [ApiController]
  23. public class FunctionController : ControllerBase
  24. {
  25. /// <summary>
  26. /// 获取根菜单
  27. /// </summary>
  28. /// <returns></returns>
  29. public UfpResponse<EapFunction> GetFunctionRoots(int sysId)
  30. {
  31. //int sysId = Convert.ToInt32(Request.Headers["sysid"]);
  32. IDatabase db = null;
  33. try
  34. {
  35. db = DbFactory.Base("ufp");
  36. var dal = new FunctionDal(db);
  37. string errorinfo = string.Empty;
  38. var roots = dal.GetRoots(sysId, errorinfo);
  39. var response = new UfpResponse<EapFunction>
  40. {
  41. Code = 1,
  42. data = roots,
  43. Msg = errorinfo
  44. };
  45. if (roots == null || roots.Count() <= 0)
  46. {
  47. response.Code = -1;
  48. }
  49. // db.Commit();
  50. return response;
  51. }
  52. catch (Exception e)
  53. {
  54. // db.Rollback();
  55. return new UfpResponse<EapFunction>()
  56. {
  57. Code = -1,
  58. Msg = e.Message
  59. };
  60. }
  61. finally
  62. {
  63. if (db != null)
  64. db.Close();
  65. }
  66. }
  67. /// <summary>
  68. /// 获取子菜单
  69. /// </summary>
  70. /// <param name="imputds"></param>
  71. /// <param name="errorinfo"></param>
  72. /// <param name="error"></param>
  73. /// <returns></returns>
  74. public UfpResponse<EapFunction> GetSubFunctions(int parentId)
  75. {
  76. int sysId = Convert.ToInt32(Request.Headers["sysid"]);
  77. IEnumerable<EapFunction> subFunctions = null;
  78. using (IDatabase db = DbFactory.Base("ufp"))
  79. {
  80. var dal = new FunctionDal(db);
  81. string errorinfo = string.Empty;
  82. subFunctions = dal.GetSubFunctions(sysId, parentId, errorinfo);
  83. var response = new UfpResponse<EapFunction>
  84. {
  85. Code = 1,
  86. data = subFunctions,
  87. Msg = errorinfo
  88. };
  89. if (subFunctions == null || subFunctions.Count() <= 0)
  90. {
  91. response.Code = -1;
  92. }
  93. return response;
  94. }
  95. }
  96. /// <summary>
  97. /// 根据角色Id获取树菜单
  98. /// </summary>
  99. /// <param name="roleId"></param>
  100. /// <returns></returns>
  101. [HttpGet]
  102. public UfpResponse<FuncTree> GetFuncTree(int roleId)
  103. {
  104. // int sysId = Convert.ToInt32(Request.Headers["sysid"]);
  105. using (IDatabase db = DbFactory.Base("ufp"))
  106. {
  107. var funcDal = new FunctionDal(db);
  108. int sysId = funcDal.GetSysIdByRoleId(roleId);
  109. return new UfpResponse<FuncTree>()
  110. {
  111. Code = 1,
  112. data = funcDal.GetFuncTreesAll(sysId),
  113. Msg = ""
  114. };
  115. }
  116. }
  117. public EapFunction GetFunction(int id)
  118. {
  119. using (IDatabase db = DbFactory.Base("ufp"))
  120. {
  121. var dal = new FunctionDal(db);
  122. return dal.GetFunction(id);
  123. }
  124. }
  125. [HttpPost]
  126. public string Add([FromBody] EapFunction function)
  127. {
  128. var usercode = HttpContext.Request.Headers["userCode"];
  129. int res = 0;
  130. IDatabase db = null;
  131. try
  132. {
  133. db = DbFactory.Base("ufp");
  134. db.BeginTrans();
  135. var dal = new FunctionDal(db);
  136. function.ModCode = usercode;
  137. function.ModTime = DateTime.Now;
  138. if (function.ID == 0)
  139. {
  140. function.RecCode = usercode;
  141. function.RecTime = function.ModTime;
  142. res = dal.Insert(function, usercode);
  143. if (res < 0)
  144. {
  145. db.Rollback();
  146. return "-1";
  147. }
  148. }
  149. else
  150. {
  151. res = dal.Update(function, usercode);
  152. if (res < 0)
  153. {
  154. db.Rollback();
  155. return "-1";
  156. }
  157. }
  158. db.Commit();
  159. if (function.ID == 0)
  160. {
  161. LogHelper<EapFunction>.LogFatal("新增Function-->" + Json.ToJson(function), "用户操作", usercode);
  162. }
  163. else
  164. {
  165. LogHelper<EapFunction>.LogFatal("修改Function-->" + Json.ToJson(function), "用户操作", usercode);
  166. }
  167. return res.ToString();
  168. }
  169. catch (Exception e)
  170. {
  171. db.Rollback();
  172. return "-1";
  173. }
  174. finally
  175. {
  176. if (db != null)
  177. db.Close();
  178. }
  179. }
  180. [HttpPost]
  181. public string Delete([FromBody] int id)
  182. {
  183. using (IDatabase db = DbFactory.Base("ufp"))
  184. {
  185. var dal = new FunctionDal(db);
  186. var model = dal.GetFunction(id);
  187. if (dal.Delete(id) > 0)
  188. {
  189. LogHelper<EapFunction>.LogFatal("删除Function-->:" + Json.ToJson(model), "用户操作", Request.Headers["usercode"]);
  190. return "1";
  191. }
  192. else
  193. {
  194. return "-1";
  195. }
  196. }
  197. }
  198. [HttpPost]
  199. public string SetFunction([FromBody] AuthorizeModel model)
  200. {
  201. string usercode = Request.Headers["usercode"];
  202. IDatabase db = null;
  203. try
  204. {
  205. db = DbFactory.Base("ufp");
  206. db.BeginTrans();
  207. var dal = new FunctionDal(db);
  208. if (dal.DeleteRoleFuncs(model.Multity.Removes, model.Single) < 0)
  209. {
  210. db.Rollback();
  211. return JsonConvert.SerializeObject(new
  212. {
  213. code = -1,
  214. msg = "删除未授权菜单时出错"
  215. });
  216. }
  217. if (dal.SetFunction(model.Multity.Adds, model.Single, usercode) < 0)
  218. {
  219. db.Rollback();
  220. return JsonConvert.SerializeObject(new
  221. {
  222. code = -1,
  223. msg = "新增菜单授权时出错"
  224. });
  225. }
  226. db.Commit();
  227. return JsonConvert.SerializeObject(new
  228. {
  229. code = 1,
  230. msg = ""
  231. });
  232. }
  233. catch (Exception e)
  234. {
  235. db.Rollback();
  236. return JsonConvert.SerializeObject(new
  237. {
  238. code = -1,
  239. msg = e.Message
  240. });
  241. }
  242. finally
  243. {
  244. db.Close();
  245. }
  246. }
  247. [HttpGet]
  248. public IEnumerable<int> GetAlreadyAuthFuncIds(int roleId, int type)
  249. {
  250. using (IDatabase db = DbFactory.Base("ufp"))
  251. {
  252. var dal = new FunctionDal(db);
  253. return dal.GetAuthedFunIds(roleId, type);
  254. }
  255. }
  256. [HttpGet]
  257. public IEnumerable<int> GetFuncIds(string filter)
  258. {
  259. using (IDatabase db = DbFactory.Base("ufp"))
  260. {
  261. var dal = new FunctionDal(db);
  262. return dal.GetFuncIds(filter);
  263. }
  264. }
  265. /// <summary>
  266. /// 获取首页菜单
  267. /// </summary>
  268. /// <returns></returns>
  269. [HttpGet]
  270. public UfpResponse<Menu> GetMenus()
  271. {
  272. string usercode = Request.Headers["usercode"];
  273. int sysId = Convert.ToInt32(Request.Headers["sysId"]);
  274. using (IDatabase db = DbFactory.Base("ufp"))
  275. {
  276. var dal = new FunctionDal(db);
  277. var menus = dal.GetMenusByUserCode(sysId, usercode);
  278. if (menus == null || menus.Count() <= 0)
  279. {
  280. return new UfpResponse<Menu>()
  281. {
  282. Code = -1,
  283. data = null,
  284. Msg = "获取菜单失败"
  285. };
  286. }
  287. return new UfpResponse<Menu>
  288. {
  289. Code = 1,
  290. data = menus,
  291. Msg = ""
  292. };
  293. }
  294. }
  295. [HttpGet]
  296. public async Task<UfpResponse<AlainMenu>> GetMenusForAlain(string token)
  297. {
  298. var userinfo = await OAuthHelper.GetUserInfo(token);
  299. int sysId = Convert.ToInt32(Request.Headers["sysId"]);
  300. using (IDatabase db = DbFactory.Base("ufp"))
  301. {
  302. var dal = new FunctionDal(db);
  303. var menus = dal.GetMenusByUserCodeForAlain(sysId, userinfo.UserAccount);
  304. if (menus == null || menus.Count() <= 0)
  305. {
  306. return new UfpResponse<AlainMenu>()
  307. {
  308. Code = -1,
  309. data = null,
  310. Msg = "获取菜单失败"
  311. };
  312. }
  313. return new UfpResponse<AlainMenu>
  314. {
  315. Code = 1,
  316. data = menus,
  317. Msg = ""
  318. };
  319. }
  320. }
  321. /// <summary>
  322. /// 根据接收到的工号和菜单地址判断当前用户是否有访问该菜单的权限
  323. /// </summary>
  324. /// <param name="usercode"></param>
  325. /// <param name="url"></param>
  326. /// <returns></returns>
  327. [HttpGet]
  328. public EapResponse IsAusthorized(string usercode, string url)
  329. {
  330. int sysId = Convert.ToInt32(Request.Headers["sysid"]);
  331. var res = new EapResponse { Code = 1, Msg = string.Empty };
  332. using (IDatabase db = DbFactory.Base("ufp"))
  333. {
  334. var dal = new FunctionDal(db);
  335. var isAuthed = dal.IsAuthorized(sysId, usercode, url) > 0;
  336. if (!isAuthed)
  337. {
  338. res.Code = -1;
  339. }
  340. return res;
  341. }
  342. }
  343. [HttpGet]
  344. public EapResponse GetMean(int id)
  345. {
  346. using (IDatabase db = DbFactory.Base("ufp"))
  347. {
  348. var dal = new FunctionDal(db);
  349. return dal.GetMean( id);
  350. }
  351. }
  352. }
  353. }