BaseController.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using Microsoft.AspNetCore.Mvc;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net.Http;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace DW5S.WebApi
  12. {
  13. /// <summary>
  14. ///
  15. /// </summary>
  16. [ApiController]
  17. [Route("api/[controller]/[action]")]
  18. public class BaseController : ControllerBase
  19. {
  20. private readonly string uploadFolder;
  21. /// <summary>
  22. ///
  23. /// </summary>
  24. public BaseController()
  25. {
  26. this.uploadFolder =AppDomain.CurrentDomain.BaseDirectory;
  27. }
  28. /// <summary>
  29. /// 发布为单文件App时确保可以找到Controller程序集
  30. /// </summary>
  31. [NonAction]
  32. internal static void EnsureAssemblyLoaded()
  33. {
  34. }
  35. /// <summary>
  36. /// 返回本地文件
  37. /// </summary>
  38. /// <param name="fileName"></param>
  39. /// <returns></returns>
  40. protected string GetLocalFile(string fileName)
  41. {
  42. return Path.Combine(this.uploadFolder, fileName);
  43. }
  44. /// <summary>
  45. /// 获取客户端IP地址
  46. /// </summary>
  47. protected string RemoteIp
  48. {
  49. get
  50. {
  51. var ip = HttpContext.Connection.RemoteIpAddress?.ToString();
  52. if (ip == null || !ip.Contains("."))
  53. {
  54. return "127.0.0.1";
  55. }
  56. var idx = ip.LastIndexOf(":");
  57. if (idx >= 0)
  58. {
  59. ip = ip.Substring(idx + 1);
  60. }
  61. return ip;
  62. }
  63. }
  64. /// <summary>
  65. /// 获取客户端使用的连接端口
  66. /// </summary>
  67. protected int RemotePort
  68. {
  69. get
  70. {
  71. return HttpContext.Connection.RemotePort;
  72. }
  73. }
  74. /// <summary>
  75. /// 获取本地IP地址
  76. /// </summary>
  77. protected string LocalIp
  78. {
  79. get
  80. {
  81. var ip = HttpContext.Connection.LocalIpAddress?.ToString();
  82. if (ip == null || !ip.Contains("."))
  83. {
  84. return "127.0.0.1";
  85. }
  86. var idx = ip.LastIndexOf(":");
  87. if (idx >= 0)
  88. {
  89. ip = ip.Substring(idx + 1);
  90. }
  91. return ip;
  92. }
  93. }
  94. /// <summary>
  95. /// 获取本地使用的http端口
  96. /// </summary>
  97. protected int LocalPort
  98. {
  99. get
  100. {
  101. return HttpContext.Connection.LocalPort;
  102. }
  103. }
  104. /// <summary>
  105. /// 返回成功
  106. /// </summary>
  107. /// <param name="data">返回的数据</param>
  108. /// <returns></returns>
  109. protected AjaxResult Success(object data = null)
  110. {
  111. AjaxResult res = new AjaxResult
  112. {
  113. code = 200,
  114. msg = "请求成功!",
  115. data = data
  116. };
  117. return res;
  118. }
  119. /// <summary>
  120. /// 返回成功
  121. /// </summary>
  122. /// <param name="msg">返回的消息</param>
  123. /// <param name="data">返回的数据</param>
  124. /// <returns></returns>
  125. protected AjaxResult Success(object data, string msg = "请求成功!")
  126. {
  127. AjaxResult res = new AjaxResult
  128. {
  129. code = 200,
  130. msg = msg,
  131. data = data
  132. };
  133. return res;
  134. }
  135. /// <summary>
  136. /// 返回成功
  137. /// </summary>
  138. /// <param name="msg">返回的消息</param>
  139. /// <param name="data">返回的数据</param>
  140. /// <returns></returns>
  141. protected AjaxResult<T> Success<T>(T data, string msg = "请求成功!")
  142. {
  143. AjaxResult<T> res = new AjaxResult<T>
  144. {
  145. code = 200,
  146. msg = msg,
  147. data = data
  148. };
  149. return res;
  150. }
  151. /// <summary>
  152. /// 返回错误
  153. /// </summary>
  154. /// <param name="msg">错误提示</param>
  155. /// <returns></returns>
  156. protected AjaxResult Error(string msg)
  157. {
  158. AjaxResult res = new AjaxResult
  159. {
  160. code = 0,
  161. msg = msg,
  162. data = null
  163. };
  164. return res;
  165. }
  166. /// <summary>
  167. /// 返回错误
  168. /// </summary>
  169. /// <param name="msg">错误提示</param>
  170. /// <returns></returns>
  171. protected AjaxResult<T> Error<T>(string msg)
  172. {
  173. AjaxResult<T> res = new AjaxResult<T>
  174. {
  175. code = 0,
  176. msg = msg,
  177. data = default
  178. };
  179. return res;
  180. }
  181. }
  182. }