BaseController.cs 17 KB

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