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