BaseController.cs 17 KB

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