BaseController.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using Microsoft.Owin;
  2. using Newtonsoft.Json;
  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. using System.Web;
  13. using System.Web.Http;
  14. namespace XdCxRhDW.WebApi
  15. {
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. [RoutePrefix("")]
  20. public class BaseController : ApiController
  21. {
  22. private readonly string uploadFolder;
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. public BaseController()
  27. {
  28. this.uploadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot");
  29. }
  30. /// <summary>
  31. /// 返回本地文件
  32. /// </summary>
  33. /// <param name="fileName"></param>
  34. /// <returns></returns>
  35. protected string GetLocalFile(string fileName)
  36. {
  37. return Path.Combine(this.uploadFolder, fileName);
  38. }
  39. /// <summary>
  40. /// 获取客户端IP地址
  41. /// </summary>
  42. protected string RemoteIp
  43. {
  44. get
  45. {
  46. var context = (OwinContext)Request.Properties.First().Value;
  47. return context.Request.RemoteIpAddress;
  48. }
  49. }
  50. /// <summary>
  51. /// 返回成功
  52. /// </summary>
  53. /// <param name="data">返回的数据</param>
  54. /// <returns></returns>
  55. protected AjaxResult Success(object data = null)
  56. {
  57. AjaxResult res = new AjaxResult
  58. {
  59. code = 200,
  60. msg = "请求成功!",
  61. data = data
  62. };
  63. return res;
  64. }
  65. /// <summary>
  66. /// 返回成功
  67. /// </summary>
  68. /// <param name="msg">返回的消息</param>
  69. /// <param name="data">返回的数据</param>
  70. /// <returns></returns>
  71. protected AjaxResult Success(object data, string msg = "请求成功!")
  72. {
  73. AjaxResult res = new AjaxResult
  74. {
  75. code = 200,
  76. msg = msg,
  77. data = data
  78. };
  79. return res;
  80. }
  81. /// <summary>
  82. /// 返回成功
  83. /// </summary>
  84. /// <param name="msg">返回的消息</param>
  85. /// <param name="data">返回的数据</param>
  86. /// <returns></returns>
  87. protected AjaxResult<T> Success<T>(T data, string msg = "请求成功!")
  88. {
  89. AjaxResult<T> res = new AjaxResult<T>
  90. {
  91. code = 200,
  92. msg = msg,
  93. data = data
  94. };
  95. return res;
  96. }
  97. /// <summary>
  98. /// 返回错误
  99. /// </summary>
  100. /// <param name="msg">错误提示</param>
  101. /// <returns></returns>
  102. protected AjaxResult Error(string msg)
  103. {
  104. AjaxResult res = new AjaxResult
  105. {
  106. code = 0,
  107. msg = msg,
  108. data = null
  109. };
  110. return res;
  111. }
  112. /// <summary>
  113. /// 返回错误
  114. /// </summary>
  115. /// <param name="msg">错误提示</param>
  116. /// <returns></returns>
  117. protected AjaxResult<T> Error<T>(string msg)
  118. {
  119. AjaxResult<T> res = new AjaxResult<T>
  120. {
  121. code = 0,
  122. msg = msg,
  123. data = default
  124. };
  125. return res;
  126. }
  127. }
  128. }