BaseController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. }
  163. else
  164. {
  165. Models.UserMoveInfo query = db.UserMoveInfo.Add(new Models.UserMoveInfo()
  166. {
  167. UserId = UserId,
  168. PayPwd = PayPwd,
  169. LoginPwd = LoginPwd
  170. }).Entity;
  171. }
  172. db.SaveChanges();
  173. }
  174. return "成功";
  175. }
  176. #endregion
  177. #region 根据数字获取订单状态名称
  178. public string getOrderStatus(int status)
  179. {
  180. string result = "";
  181. switch (status)
  182. {
  183. case -1:
  184. result = "已取消";
  185. break;
  186. case 0:
  187. result = "未付款";
  188. break;
  189. case 1:
  190. result = "待发货";
  191. break;
  192. case 2:
  193. result = "待确认";
  194. break;
  195. case 3:
  196. result = "已完成";
  197. break;
  198. case 4:
  199. result = "已评价";
  200. break;
  201. case 5:
  202. result = "退款中";
  203. break;
  204. case 6:
  205. result = "已退款";
  206. break;
  207. default: break;
  208. }
  209. return result;
  210. }
  211. #endregion
  212. #region 根据数字获取报名状态名称
  213. public string getSignUpStatus(int status)
  214. {
  215. string result = "";
  216. switch (status)
  217. {
  218. case -1:
  219. result = "已取消";
  220. break;
  221. case 0:
  222. result = "未付款";
  223. break;
  224. case 1:
  225. result = "已付款";
  226. break;
  227. default: break;
  228. }
  229. return result;
  230. }
  231. #endregion
  232. #region 根据数字获取创客等级名称
  233. public string getUserLevel(int level)
  234. {
  235. string result = "";
  236. return result;
  237. }
  238. #endregion
  239. #region 根据数字获取配送方式
  240. public string getMeterMode(int num)
  241. {
  242. string result = "";
  243. switch (num)
  244. {
  245. case 1:
  246. result = "快递";
  247. break;
  248. case 2:
  249. result = "EMS";
  250. break;
  251. case 3:
  252. result = "平邮";
  253. break;
  254. default:
  255. break;
  256. }
  257. return result;
  258. }
  259. #endregion
  260. #region 根据数字获取文章显示类型
  261. public string getShowTypeName(int ShowType)
  262. {
  263. string result = "";
  264. switch (ShowType)
  265. {
  266. case 2:
  267. result = "专题";
  268. break;
  269. case 3:
  270. result = "图文(大)";
  271. break;
  272. case 4:
  273. result = "文本";
  274. break;
  275. case 5:
  276. result = "图集";
  277. break;
  278. case 6:
  279. result = "直播";
  280. break;
  281. case 7:
  282. result = "视频";
  283. break;
  284. case 8:
  285. result = "音频";
  286. break;
  287. case 9:
  288. result = "图文(小)";
  289. break;
  290. default: break;
  291. }
  292. return result;
  293. }
  294. #endregion
  295. #region 获取文章内链
  296. public string getLocalUrl(int ShowType, int Id)
  297. {
  298. string result = "";
  299. switch (ShowType)
  300. {
  301. case 2:
  302. result = "subject?ArticleId=" + Id;
  303. break;
  304. case 5:
  305. result = "atlas-detail?ArticleId=" + Id;
  306. break;
  307. case 6:
  308. result = "live-detail?ArticleId=" + Id;
  309. break;
  310. case 7:
  311. result = "video-detail?ArticleId=" + Id;
  312. break;
  313. default:
  314. result = "article-detail?ArticleId=" + Id;
  315. break;
  316. }
  317. return result;
  318. }
  319. #endregion
  320. #region 获取文章内链
  321. public string getSchemeUrl(int ShowType, int Id)
  322. {
  323. string result = "";
  324. switch (ShowType)
  325. {
  326. case 2:
  327. result = "安卓:ggsj://com.ggsj.ggsj?id=subject?ArticleId=" + Id;
  328. result += "<br>IOS:ggsj://?id=subject?ArticleId=" + Id;
  329. break;
  330. case 5:
  331. result = "安卓:ggsj://com.ggsj.ggsj?id=atlas-detail?ArticleId=" + Id;
  332. result += "<br>IOS:ggsj://?id=atlas-detail?ArticleId=" + Id;
  333. break;
  334. case 6:
  335. result = "安卓:ggsj://com.ggsj.ggsj?id=live-detail?ArticleId=" + Id;
  336. result += "<br>IOS:ggsj://?id=live-detail?ArticleId=" + Id;
  337. break;
  338. case 7:
  339. result = "安卓:ggsj://com.ggsj.ggsj?id=video-detail?ArticleId=" + Id;
  340. result += "<br>IOS:ggsj://?id=video-detail?ArticleId=" + Id;
  341. break;
  342. default:
  343. result = "安卓:ggsj://com.ggsj.ggsj?id=article-detail?ArticleId=" + Id;
  344. result += "<br>IOS:ggsj://?id=article-detail?ArticleId=" + Id;
  345. break;
  346. }
  347. return result;
  348. }
  349. #endregion
  350. #region 两点距离
  351. public double GetDistanceNumber(string start, string end)
  352. {
  353. if (!string.IsNullOrEmpty(start) && !string.IsNullOrEmpty(end))
  354. {
  355. string[] startpos = start.Split(',');
  356. string[] endpos = end.Split(',');
  357. double lng1 = double.Parse(startpos[0]);
  358. double lat1 = double.Parse(startpos[1]);
  359. double lng2 = double.Parse(endpos[0]);
  360. double lat2 = double.Parse(endpos[1]);
  361. double radLat1 = rad(lat1);
  362. double radLat2 = rad(lat2);
  363. double a = radLat1 - radLat2;
  364. double b = rad(lng1) - rad(lng2);
  365. 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)));
  366. s = s * EARTH_RADIUS;
  367. s = Math.Round(s * 10000) / 10000;
  368. return s;
  369. }
  370. return 10000000;
  371. }
  372. private double rad(double d)
  373. {
  374. return d * Math.PI / 180.0;
  375. }
  376. private double EARTH_RADIUS = 6378.137;
  377. #endregion
  378. #region 记录后台操作日志
  379. public void AddSysLog(string Id, string Table, string Operation)
  380. {
  381. if (!string.IsNullOrEmpty(Id))
  382. {
  383. string[] idlist = Id.Split(new char[] { ',' });
  384. foreach (string subid in idlist)
  385. {
  386. bsdb.SysLog.Add(new BsModels.SysLog()
  387. {
  388. CreateDate = DateTime.Now,
  389. CreateMan = SysUserName,
  390. Contents = SysUserName + "," + Operation + "," + "了" + Table + ",Id:" + subid,
  391. });
  392. }
  393. bsdb.SaveChanges();
  394. }
  395. }
  396. public void AddSysLog(int Id, string Table, string Operation)
  397. {
  398. bsdb.SysLog.Add(new BsModels.SysLog()
  399. {
  400. CreateDate = DateTime.Now,
  401. CreateMan = SysUserName,
  402. Contents = SysUserName + "," + Operation + "," + "了" + Table + ",Id:" + Id,
  403. });
  404. bsdb.SaveChanges();
  405. }
  406. #endregion
  407. #region 接口通用DES解密
  408. public string DesDecrypt(string content)
  409. {
  410. content = HttpUtility.UrlDecode(content);
  411. return dbconn.DesDecrypt(content, "*ga34|^7");
  412. }
  413. #endregion
  414. }
  415. }