BaseController.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. protected string RemoteIp
  40. {
  41. get
  42. {
  43. var context = (OwinContext)Request.Properties.First().Value;
  44. return context.Request.RemoteIpAddress;
  45. }
  46. }
  47. /// <summary>
  48. /// 返回成功
  49. /// </summary>
  50. /// <param name="data">返回的数据</param>
  51. /// <returns></returns>
  52. protected AjaxResult Success(object data = null)
  53. {
  54. AjaxResult res = new AjaxResult
  55. {
  56. code = 200,
  57. msg = "请求成功!",
  58. data = data
  59. };
  60. return res;
  61. }
  62. /// <summary>
  63. /// 返回成功
  64. /// </summary>
  65. /// <param name="msg">返回的消息</param>
  66. /// <param name="data">返回的数据</param>
  67. /// <returns></returns>
  68. protected AjaxResult Success(object data, string msg = "请求成功!")
  69. {
  70. AjaxResult res = new AjaxResult
  71. {
  72. code = 200,
  73. msg = msg,
  74. data = data
  75. };
  76. return res;
  77. }
  78. /// <summary>
  79. /// 返回成功
  80. /// </summary>
  81. /// <param name="msg">返回的消息</param>
  82. /// <param name="data">返回的数据</param>
  83. /// <returns></returns>
  84. protected AjaxResult<T> Success<T>(T data, string msg = "请求成功!")
  85. {
  86. AjaxResult<T> res = new AjaxResult<T>
  87. {
  88. code = 200,
  89. msg = msg,
  90. data = data
  91. };
  92. return res;
  93. }
  94. /// <summary>
  95. /// 返回错误
  96. /// </summary>
  97. /// <param name="msg">错误提示</param>
  98. /// <returns></returns>
  99. protected AjaxResult Error(string msg)
  100. {
  101. AjaxResult res = new AjaxResult
  102. {
  103. code = 0,
  104. msg = msg,
  105. data = null
  106. };
  107. return res;
  108. }
  109. /// <summary>
  110. /// 返回错误
  111. /// </summary>
  112. /// <param name="msg">错误提示</param>
  113. /// <returns></returns>
  114. protected AjaxResult<T> Error<T>(string msg)
  115. {
  116. AjaxResult<T> res = new AjaxResult<T>
  117. {
  118. code = 0,
  119. msg = msg,
  120. data = default
  121. };
  122. return res;
  123. }
  124. }
  125. }