BaseController.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.Linq;
  3. using System.Web;
  4. using System.Text.RegularExpressions;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.Extensions.Logging;
  8. using Microsoft.Extensions.Options;
  9. using MySystem.MainModels;
  10. using Library;
  11. namespace MySystem.Areas.Api.Controllers
  12. {
  13. public class BaseController : Controller
  14. {
  15. public IHttpContextAccessor _accessor;
  16. public readonly ILogger<BaseController> _logger;
  17. public readonly IOptions<Setting> _setting;
  18. public Setting AppSetting;
  19. public WebCMSEntities maindb = new WebCMSEntities();
  20. public MainModels2.WebCMSEntities maindb2 = new MainModels2.WebCMSEntities();
  21. public MiniModels.WebCMSEntities minidb = new MiniModels.WebCMSEntities();
  22. public Models.WebCMSEntities bsdb = new Models.WebCMSEntities();
  23. public OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  24. public KxsMainModels.WebCMSEntities kxsdb = new KxsMainModels.WebCMSEntities();
  25. public string ApiKey = "G6H7@J8%";
  26. public string Host = Library.ConfigurationManager.AppSettings["Host"].ToString();
  27. public string SourceHost = Library.ConfigurationManager.AppSettings["SourceHost"].ToString();
  28. public string SpHost = Library.ConfigurationManager.AppSettings["SpHost"].ToString();
  29. public string OssHost = Library.ConfigurationManager.AppSettings["OssHost"].ToString();
  30. public string OssKey = Library.ConfigurationManager.AppSettings["OSSKey"].ToString();
  31. public string OssSecret = Library.ConfigurationManager.AppSettings["OSSSecret"].ToString();
  32. public string OssEndpoint = Library.ConfigurationManager.AppSettings["OSSEndpoint"].ToString();
  33. public string Database = Library.ConfigurationManager.AppSettings["Database"].ToString();
  34. public string defaultImage = Library.ConfigurationManager.AppSettings["Host"].ToString() + "/skin/app/default/static/images/header.png";
  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. public int SysId;
  40. public int SysRealRole;
  41. public string SysUserName = "";
  42. public string SysRealName = "";
  43. public string RightInfo = "";
  44. public BaseController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting)
  45. {
  46. _accessor = accessor;
  47. _logger = logger;
  48. _setting = setting;
  49. AppSetting = setting.Value;
  50. Host = AppSetting.Host;
  51. Database = AppSetting.Database;
  52. }
  53. #region 接口通用DES解密
  54. public string DesDecrypt(string content)
  55. {
  56. content = HttpUtility.UrlDecode(content);
  57. return dbconn.DesDecrypt(content, "*ga34|^7");
  58. }
  59. #endregion
  60. #region 根据数字获取订单状态名称
  61. public string getOrderStatus(int status)
  62. {
  63. string result = "";
  64. switch (status)
  65. {
  66. case -1:
  67. result = "已取消";
  68. break;
  69. case 0:
  70. result = "未付款";
  71. break;
  72. case 1:
  73. result = "待发货";
  74. break;
  75. case 2:
  76. result = "待确认";
  77. break;
  78. case 3:
  79. result = "已完成";
  80. break;
  81. case 4:
  82. result = "已评价";
  83. break;
  84. case 5:
  85. result = "退款中";
  86. break;
  87. case 6:
  88. result = "已退款";
  89. break;
  90. default: break;
  91. }
  92. return result;
  93. }
  94. #endregion
  95. #region 根据数字获取报名状态名称
  96. public string getSignUpStatus(int status)
  97. {
  98. string result = "";
  99. switch (status)
  100. {
  101. case -1:
  102. result = "已取消";
  103. break;
  104. case 0:
  105. result = "未付款";
  106. break;
  107. case 1:
  108. result = "已付款";
  109. break;
  110. default: break;
  111. }
  112. return result;
  113. }
  114. #endregion
  115. #region 根据数字获取配送方式
  116. public string getMeterMode(int num)
  117. {
  118. string result = "";
  119. switch (num)
  120. {
  121. case 1:
  122. result = "快递";
  123. break;
  124. case 2:
  125. result = "EMS";
  126. break;
  127. case 3:
  128. result = "平邮";
  129. break;
  130. default:
  131. break;
  132. }
  133. return result;
  134. }
  135. #endregion
  136. #region 两点距离
  137. public double GetDistanceNumber(string start, string end)
  138. {
  139. if (!string.IsNullOrEmpty(start) && !string.IsNullOrEmpty(end))
  140. {
  141. string[] startpos = start.Split(',');
  142. string[] endpos = end.Split(',');
  143. double lng1 = double.Parse(startpos[0]);
  144. double lat1 = double.Parse(startpos[1]);
  145. double lng2 = double.Parse(endpos[0]);
  146. double lat2 = double.Parse(endpos[1]);
  147. double radLat1 = rad(lat1);
  148. double radLat2 = rad(lat2);
  149. double a = radLat1 - radLat2;
  150. double b = rad(lng1) - rad(lng2);
  151. 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)));
  152. s = s * EARTH_RADIUS;
  153. s = Math.Round(s * 10000) / 10000;
  154. return s;
  155. }
  156. return 10000000;
  157. }
  158. private double rad(double d)
  159. {
  160. return d * Math.PI / 180.0;
  161. }
  162. private double EARTH_RADIUS = 6378.137;
  163. #endregion
  164. #region 默认姓名
  165. public string DefaultRealName(MainModels.Users user)
  166. {
  167. return string.IsNullOrEmpty(user.RealName) ? "码牌" + user.MakerCode : user.RealName;
  168. }
  169. #endregion
  170. #region 默认图片
  171. public string DefaultPic(string picpath)
  172. {
  173. if (!string.IsNullOrEmpty(picpath))
  174. {
  175. if (picpath.StartsWith("http"))
  176. {
  177. return picpath;
  178. }
  179. if(picpath.Contains("/v2/") || picpath.Contains("/upload/"))
  180. {
  181. return OssHost + picpath;
  182. }
  183. return SourceHost + picpath;
  184. }
  185. return OssHost + "/default.png";
  186. }
  187. #endregion
  188. #region 创客学堂/晨会图片
  189. public string StudyOrMeetDefaultPic(string picpath)
  190. {
  191. if (!string.IsNullOrEmpty(picpath))
  192. {
  193. if (picpath.StartsWith("http"))
  194. {
  195. return picpath;
  196. }
  197. if(picpath.Contains("/v2/"))
  198. {
  199. return OssHost + picpath;
  200. }
  201. return SourceHost + picpath;
  202. }
  203. return "";
  204. }
  205. #endregion
  206. #region 解析编辑器中的图片
  207. public string CheckContentImage(string content)
  208. {
  209. if (string.IsNullOrEmpty(content))
  210. {
  211. return "";
  212. }
  213. MatchCollection mc = Regex.Matches(content, "<img.*?/>");
  214. foreach (Match match in mc)
  215. {
  216. Match m = Regex.Match(match.Value, "src=\".*?\"");
  217. if (m.Success)
  218. {
  219. string srcString = m.Value;
  220. string picPath = srcString.Replace("src=\"", "").Replace("\"", "");
  221. if(!picPath.StartsWith("http"))
  222. {
  223. if(picPath.Contains("/v2/") || picPath.Contains("/up/"))
  224. {
  225. picPath = OssHost + picPath;
  226. }
  227. else
  228. {
  229. picPath = SourceHost + picPath;
  230. }
  231. content = content.Replace(srcString, "src=\"" + picPath + "\"");
  232. }
  233. }
  234. }
  235. return content;
  236. }
  237. #endregion
  238. }
  239. }