BaseController.cs 8.4 KB

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