BaseController.cs 4.8 KB

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