BaseController.cs 8.7 KB

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