BaseController.cs 15 KB

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