BaseController.cs 6.9 KB

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