Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

214 строки
7.8 KiB

  1. using Learun.Application.Base.SystemModule;
  2. using Learun.Application.Organization;
  3. using Learun.Application.TwoDevelopment.LR_Desktop;
  4. using Learun.Util;
  5. using Nancy;
  6. using Newtonsoft.Json;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Net;
  11. using System.Security.Cryptography;
  12. using System.Text;
  13. namespace Learun.Application.WebApi.Modules
  14. {
  15. public class VisitmanageApi : BaseApi
  16. {
  17. public VisitmanageApi()
  18. : base("/learun/visitmanage")
  19. {
  20. Post["/save"] = SaveForm;
  21. Post["/getweixinaccess_token"] = GetWeixinAccess_token;
  22. }
  23. private VisitManageBLL visitIBLL = new VisitManageBLL();
  24. private UserIBLL userIbll = new UserBLL();
  25. private WeChatConfigIBLL weChatConfigIbll = new WeChatConfigBLL();
  26. /// <summary>
  27. /// 保存实体数据(新增、修改)
  28. /// <param name="_"></param>
  29. /// <summary>
  30. /// <returns></returns>
  31. public Response SaveForm(dynamic _)
  32. {
  33. ReqFormEntity parameter = this.GetReqData<ReqFormEntity>();
  34. VisitManageEntity entity = parameter.strEntity.ToObject<VisitManageEntity>();
  35. visitIBLL.SaveEntity(parameter.keyValue, entity);
  36. return Success("保存成功!");
  37. }
  38. #region 微信openid获取
  39. public Response GetWeixinAccess_token(dynamic _)
  40. {
  41. var entity = weChatConfigIbll.GetEnableEntity();
  42. string appid = entity?.APPId;
  43. string secret = entity?.secret;
  44. //string appid = "wx9bba72a124c32b58";// WeChatConfigentity.APPId;
  45. //string secret = "3553f4244898c20e387ccc4d039b683e";// WeChatConfigentity.secret;
  46. string code = Request.Query["code"];
  47. var responsejson = HttpGet("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code");
  48. if (!string.IsNullOrEmpty(responsejson))
  49. {
  50. return Success(JsonConvert.DeserializeObject(responsejson));
  51. //var weixintokenobj = JsonConvert.DeserializeObject<dynamic>(responsejson);
  52. //if (string.IsNullOrEmpty(weixintokenobj.errcode))
  53. //{
  54. //}
  55. //else
  56. //{
  57. // return Fail("微信授权失败,请重试。");
  58. //}
  59. }
  60. return Fail("微信授权失败,请重试。");
  61. }
  62. /// <summary>
  63. /// 发起一个HTTP请求(以GET方式)
  64. /// </summary>
  65. /// <param name="url"></param>
  66. /// <returns></returns>
  67. public string HttpGet(string url)
  68. {
  69. WebRequest myWebRequest = WebRequest.Create(url);
  70. WebResponse myWebResponse = myWebRequest.GetResponse();
  71. Stream ReceiveStream = myWebResponse.GetResponseStream();
  72. string responseStr = "";
  73. if (ReceiveStream != null)
  74. {
  75. StreamReader reader = new StreamReader(ReceiveStream, Encoding.UTF8);
  76. responseStr = reader.ReadToEnd();
  77. reader.Close();
  78. }
  79. myWebResponse.Close();
  80. return responseStr;
  81. }
  82. /// <summary>
  83. /// 获取当前时间戳
  84. /// </summary>
  85. /// <param name="bflag">为真时获取10位时间戳,为假时获取13位时间戳.bool bflag = true</param>
  86. /// <returns></returns>
  87. public static string GetTimeStamp(bool bflag)
  88. {
  89. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  90. string ret = string.Empty;
  91. if (bflag)
  92. ret = Convert.ToInt64(ts.TotalSeconds).ToString();
  93. else
  94. ret = Convert.ToInt64(ts.TotalMilliseconds).ToString();
  95. return ret;
  96. }
  97. // <summary>
  98. /// SHA1 加密,返回大写字符串
  99. /// </summary>
  100. /// <param name="content">需要加密字符串</param>
  101. /// <returns>返回40位UTF8 大写</returns>
  102. public static string SHA1(string content)
  103. {
  104. return SHA1(content, Encoding.UTF8);
  105. }
  106. /// <summary>
  107. /// SHA1 加密,返回大写字符串
  108. /// </summary>
  109. /// <param name="content">需要加密字符串</param>
  110. /// <param name="encode">指定加密编码</param>
  111. /// <returns>返回40位大写字符串</returns>
  112. public static string SHA1(string content, Encoding encode)
  113. {
  114. try
  115. {
  116. SHA1 sha1 = new SHA1CryptoServiceProvider();
  117. byte[] bytes_in = encode.GetBytes(content);
  118. byte[] bytes_out = sha1.ComputeHash(bytes_in);
  119. sha1.Dispose();
  120. string result = BitConverter.ToString(bytes_out);
  121. result = result.Replace("-", "");
  122. return result;
  123. }
  124. catch (Exception ex)
  125. {
  126. throw new Exception("SHA1加密出错:" + ex.Message);
  127. }
  128. }
  129. /// <summary>
  130. /// 获取IP
  131. /// </summary>
  132. /// <returns></returns>
  133. private string GetIP()
  134. {
  135. //string ip = string.Empty;
  136. //if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"]))
  137. // ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
  138. //if (string.IsNullOrEmpty(ip))
  139. // ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);
  140. //return ip;
  141. string userIP = "未获取用户IP";
  142. try
  143. {
  144. if (System.Web.HttpContext.Current == null
  145. || System.Web.HttpContext.Current.Request == null
  146. || System.Web.HttpContext.Current.Request.ServerVariables == null)
  147. {
  148. return "";
  149. }
  150. string CustomerIP = "";
  151. //CDN加速后取到的IP simone 090805
  152. CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
  153. if (!string.IsNullOrEmpty(CustomerIP))
  154. {
  155. return CustomerIP;
  156. }
  157. CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  158. if (!string.IsNullOrEmpty(CustomerIP))
  159. {
  160. return CustomerIP;
  161. }
  162. if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
  163. {
  164. CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  165. if (CustomerIP == null)
  166. {
  167. CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  168. }
  169. }
  170. else
  171. {
  172. CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  173. }
  174. if (string.Compare(CustomerIP, "unknown", true) == 0 || string.IsNullOrEmpty(CustomerIP))
  175. {
  176. return System.Web.HttpContext.Current.Request.UserHostAddress;
  177. }
  178. return CustomerIP;
  179. }
  180. catch { }
  181. return userIP;
  182. }
  183. #endregion
  184. #region 私有类
  185. /// <summary>
  186. /// 表单实体类
  187. /// <summary>
  188. private class ReqFormEntity
  189. {
  190. public string keyValue { get; set; }
  191. public string strEntity { get; set; }
  192. }
  193. #endregion
  194. }
  195. }