BaseController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Web;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.AspNetCore.Http;
  10. using Microsoft.Extensions.Logging;
  11. using Microsoft.Extensions.Options;
  12. using MySystem.Models;
  13. using Library;
  14. namespace MySystem.Areas.Admin.Controllers
  15. {
  16. public class BaseController : Controller
  17. {
  18. public IHttpContextAccessor _accessor;
  19. public readonly ILogger<BaseController> _logger;
  20. public readonly IOptions<Setting> _setting;
  21. public Setting AppSetting;
  22. public string ApiKey = "G6H7@J8%";
  23. public Models.WebCMSEntities db = new Models.WebCMSEntities();
  24. public string Host = Library.ConfigurationManager.AppSettings["Host"].ToString();
  25. public string ShareHost = "";
  26. public string SourceHost = Library.ConfigurationManager.AppSettings["SourceHost"].ToString();
  27. public string Database = Library.ConfigurationManager.AppSettings["Database"].ToString();
  28. public string defaultImage = Library.ConfigurationManager.AppSettings["Host"].ToString() + "/skin/app/default/static/images/default.jpg";
  29. //jwt参数
  30. public string JwtSecret = Library.ConfigurationManager.AppSettings["JwtSecret"].ToString();
  31. public string JwtIss = Library.ConfigurationManager.AppSettings["JwtIss"].ToString();
  32. public string JwtAud = Library.ConfigurationManager.AppSettings["JwtAud"].ToString();
  33. public int SysId;
  34. public int SysRealRole;
  35. public string SysUserName = "";
  36. public string SysRealName = "";
  37. public string RightInfo = "";
  38. public BaseController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting)
  39. {
  40. _accessor = accessor;
  41. _logger = logger;
  42. _setting = setting;
  43. AppSetting = setting.Value;
  44. Host = AppSetting.Host;
  45. Database = AppSetting.Database;
  46. if (function.GetSession(_accessor.HttpContext, "IsLogin") != "1")
  47. {
  48. function.WriteCookie(_accessor.HttpContext, "SysUserName", "");
  49. function.WriteCookie(_accessor.HttpContext, "SysRealName", "");
  50. function.WriteCookie(_accessor.HttpContext, "SysId", "0");
  51. function.WriteCookie(_accessor.HttpContext, "SysRealRole", "0");
  52. function.WriteSession(_accessor.HttpContext, "RightInfo", "");
  53. if (_accessor.HttpContext.Request.Path.Value.ToLower().Contains("admin"))
  54. {
  55. _accessor.HttpContext.Response.WriteAsync("<script>window.top.location.href='/Admin/Home/Login';</script>");
  56. }
  57. }
  58. else
  59. {
  60. SysId = int.Parse(function.CheckInt(function.GetCookie(_accessor.HttpContext, "SysId")));
  61. SysRealRole = int.Parse(function.CheckInt(function.GetCookie(_accessor.HttpContext, "SysRealRole")));
  62. SysUserName = function.GetCookie(_accessor.HttpContext, "SysUserName");
  63. SysRealName = function.GetCookie(_accessor.HttpContext, "SysRealName");
  64. SysAdminRole role = db.SysAdminRole.FirstOrDefault(m => m.Id == SysRealRole);
  65. if (role != null)
  66. {
  67. RightInfo = role.RightInfo;
  68. function.WriteSession(_accessor.HttpContext, "RightInfo", role.RightInfo);
  69. }
  70. if (_accessor.HttpContext.Request.Path.Value.ToLower().Contains("admin"))
  71. {
  72. if (!CheckAdminRight())
  73. {
  74. _accessor.HttpContext.Response.WriteAsync("<script>window.top.location.href='/Admin/Home/Login';</script>");
  75. }
  76. }
  77. }
  78. //db.Configuration.ProxyCreationEnabled = false;
  79. }
  80. #region 判断权限
  81. public bool CheckAdminRight()
  82. {
  83. string url = _accessor.HttpContext.Request.Path.Value;
  84. if(url.Contains("/RightDic/")) return true;
  85. if(url.Contains("/OperateRightList/")) return true;
  86. if(url.Contains("/PageUpdateInfo/")) return true;
  87. if(url.Contains("/FileUpdateInfo/")) return true;
  88. if(url.Contains("/AppBottomNavs/")) return true;
  89. if(url.Contains("/UploadParams/")) return true;
  90. if(url.Contains("/SystemSet/")) return true;
  91. if(url.Contains("Task")) return true;
  92. if(url.Contains("ProfitObject")) return true;
  93. if (url.Contains("/KqProductBrand/"))
  94. {
  95. url = url.Replace("/KqProductBrand/", "/KqProducts/");
  96. }
  97. if (url.Contains("/KqProductOrgs/"))
  98. {
  99. url = url.Replace("/KqProductOrgs/", "/KqProducts/");
  100. }
  101. if (url.Contains("/KqProductRuleSet/"))
  102. {
  103. url = url.Replace("/KqProductRuleSet/", "/KqProducts/");
  104. }
  105. if (url.Contains("/UserProfit/"))
  106. {
  107. url = url.Replace("/UserProfit/", "/Users/");
  108. }
  109. if (url.Contains("/Product"))
  110. {
  111. url = "/Admin/Products/Index";
  112. }
  113. if (url.Contains("/MerchantAddInfo/"))
  114. {
  115. url = url.Replace("/MerchantAddInfo/", "/MerchantInfo/");
  116. }
  117. if (url.Contains("/MerchantParamSet/"))
  118. {
  119. url = url.Replace("/MerchantParamSet/", "/MerchantInfo/");
  120. }
  121. if (url.Contains("/PosCouponRecord/"))
  122. {
  123. url = url.Replace("/PosCouponRecord/", "/PosCouponOrders/");
  124. }
  125. if (!string.IsNullOrEmpty(url))
  126. {
  127. url = url.Substring(0, url.LastIndexOf("/"));
  128. }
  129. string[] RightInfo = function.GetSession(_accessor.HttpContext, "RightInfo").Split(',');
  130. bool check = db.RightDic.Any(m => m.Url.Contains(url) && RightInfo.Contains(m.Id));
  131. return check;
  132. }
  133. #endregion
  134. #region 根据数字获取订单状态名称
  135. public string getOrderStatus(int status)
  136. {
  137. string result = "";
  138. switch (status)
  139. {
  140. case -1:
  141. result = "已取消";
  142. break;
  143. case 0:
  144. result = "未付款";
  145. break;
  146. case 1:
  147. result = "待发货";
  148. break;
  149. case 2:
  150. result = "待确认";
  151. break;
  152. case 3:
  153. result = "已完成";
  154. break;
  155. case 4:
  156. result = "已评价";
  157. break;
  158. case 5:
  159. result = "退款中";
  160. break;
  161. case 6:
  162. result = "已退款";
  163. break;
  164. default: break;
  165. }
  166. return result;
  167. }
  168. #endregion
  169. #region 根据数字获取报名状态名称
  170. public string getSignUpStatus(int status)
  171. {
  172. string result = "";
  173. switch (status)
  174. {
  175. case -1:
  176. result = "已取消";
  177. break;
  178. case 0:
  179. result = "未付款";
  180. break;
  181. case 1:
  182. result = "已付款";
  183. break;
  184. default: break;
  185. }
  186. return result;
  187. }
  188. #endregion
  189. #region 根据数字获取创客等级名称
  190. public string getUserLevel(int level)
  191. {
  192. string result = "";
  193. // UserLevelSet set = db.UserLevelSet.FirstOrDefault(m => m.Id == level);
  194. // if (set != null)
  195. // {
  196. // result = set.Name;
  197. // }
  198. return result;
  199. }
  200. #endregion
  201. #region 根据数字获取配送方式
  202. public string getMeterMode(int num)
  203. {
  204. string result = "";
  205. switch (num)
  206. {
  207. case 1:
  208. result = "快递";
  209. break;
  210. case 2:
  211. result = "EMS";
  212. break;
  213. case 3:
  214. result = "平邮";
  215. break;
  216. default:
  217. break;
  218. }
  219. return result;
  220. }
  221. #endregion
  222. #region 根据数字获取文章显示类型
  223. public string getShowTypeName(int ShowType)
  224. {
  225. string result = "";
  226. switch (ShowType)
  227. {
  228. case 2:
  229. result = "专题";
  230. break;
  231. case 3:
  232. result = "图文(大)";
  233. break;
  234. case 4:
  235. result = "文本";
  236. break;
  237. case 5:
  238. result = "图集";
  239. break;
  240. case 6:
  241. result = "直播";
  242. break;
  243. case 7:
  244. result = "视频";
  245. break;
  246. case 8:
  247. result = "音频";
  248. break;
  249. case 9:
  250. result = "图文(小)";
  251. break;
  252. default: break;
  253. }
  254. return result;
  255. }
  256. #endregion
  257. #region 获取文章内链
  258. public string getLocalUrl(int ShowType, int Id)
  259. {
  260. string result = "";
  261. switch (ShowType)
  262. {
  263. case 2:
  264. result = "subject?ArticleId=" + Id;
  265. break;
  266. case 5:
  267. result = "atlas-detail?ArticleId=" + Id;
  268. break;
  269. case 6:
  270. result = "live-detail?ArticleId=" + Id;
  271. break;
  272. case 7:
  273. result = "video-detail?ArticleId=" + Id;
  274. break;
  275. default:
  276. result = "article-detail?ArticleId=" + Id;
  277. break;
  278. }
  279. return result;
  280. }
  281. #endregion
  282. #region 获取文章内链
  283. public string getSchemeUrl(int ShowType, int Id)
  284. {
  285. string result = "";
  286. switch (ShowType)
  287. {
  288. case 2:
  289. result = "安卓:ggsj://com.ggsj.ggsj?id=subject?ArticleId=" + Id;
  290. result += "<br>IOS:ggsj://?id=subject?ArticleId=" + Id;
  291. break;
  292. case 5:
  293. result = "安卓:ggsj://com.ggsj.ggsj?id=atlas-detail?ArticleId=" + Id;
  294. result += "<br>IOS:ggsj://?id=atlas-detail?ArticleId=" + Id;
  295. break;
  296. case 6:
  297. result = "安卓:ggsj://com.ggsj.ggsj?id=live-detail?ArticleId=" + Id;
  298. result += "<br>IOS:ggsj://?id=live-detail?ArticleId=" + Id;
  299. break;
  300. case 7:
  301. result = "安卓:ggsj://com.ggsj.ggsj?id=video-detail?ArticleId=" + Id;
  302. result += "<br>IOS:ggsj://?id=video-detail?ArticleId=" + Id;
  303. break;
  304. default:
  305. result = "安卓:ggsj://com.ggsj.ggsj?id=article-detail?ArticleId=" + Id;
  306. result += "<br>IOS:ggsj://?id=article-detail?ArticleId=" + Id;
  307. break;
  308. }
  309. return result;
  310. }
  311. #endregion
  312. #region 两点距离
  313. public double GetDistanceNumber(string start, string end)
  314. {
  315. if (!string.IsNullOrEmpty(start) && !string.IsNullOrEmpty(end))
  316. {
  317. string[] startpos = start.Split(',');
  318. string[] endpos = end.Split(',');
  319. double lng1 = double.Parse(startpos[0]);
  320. double lat1 = double.Parse(startpos[1]);
  321. double lng2 = double.Parse(endpos[0]);
  322. double lat2 = double.Parse(endpos[1]);
  323. double radLat1 = rad(lat1);
  324. double radLat2 = rad(lat2);
  325. double a = radLat1 - radLat2;
  326. double b = rad(lng1) - rad(lng2);
  327. double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)));
  328. s = s * EARTH_RADIUS;
  329. s = Math.Round(s * 10000) / 10000;
  330. return s;
  331. }
  332. return 10000000;
  333. }
  334. private double rad(double d)
  335. {
  336. return d * Math.PI / 180.0;
  337. }
  338. private double EARTH_RADIUS = 6378.137;
  339. #endregion
  340. #region 记录后台操作日志
  341. public void AddSysLog(string Id, string Table, string Operation)
  342. {
  343. if (!string.IsNullOrEmpty(Id))
  344. {
  345. string[] idlist = Id.Split(new char[] { ',' });
  346. foreach (string subid in idlist)
  347. {
  348. db.SysLog.Add(new SysLog()
  349. {
  350. CreateDate = DateTime.Now,
  351. CreateMan = SysUserName,
  352. Contents = SysUserName + "," + Operation + "," + "了" + Table + ",Id:" + subid,
  353. });
  354. }
  355. db.SaveChanges();
  356. }
  357. }
  358. public void AddSysLog(int Id, string Table, string Operation)
  359. {
  360. db.SysLog.Add(new SysLog()
  361. {
  362. CreateDate = DateTime.Now,
  363. CreateMan = SysUserName,
  364. Contents = SysUserName + "," + Operation + "," + "了" + Table + ",Id:" + Id,
  365. });
  366. db.SaveChanges();
  367. }
  368. #endregion
  369. #region 接口通用DES解密
  370. public string DesDecrypt(string content)
  371. {
  372. content = HttpUtility.UrlDecode(content);
  373. return dbconn.DesDecrypt(content, "*ga34|^7");
  374. }
  375. #endregion
  376. }
  377. }