|
- using Learun.Application.Base.SystemModule;
- using Learun.Application.Organization;
- using Learun.Application.TwoDevelopment.LR_Desktop;
- using Learun.Util;
- using Nancy;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Security.Cryptography;
- using System.Text;
-
- namespace Learun.Application.WebApi.Modules
- {
- public class VisitmanageApi : BaseApi
- {
- public VisitmanageApi()
- : base("/learun/visitmanage")
- {
-
- Post["/save"] = SaveForm;
- Post["/getweixinaccess_token"] = GetWeixinAccess_token;
- }
- private VisitManageBLL visitIBLL = new VisitManageBLL();
- private UserIBLL userIbll = new UserBLL();
- private WeChatConfigIBLL weChatConfigIbll = new WeChatConfigBLL();
-
- /// <summary>
- /// 保存实体数据(新增、修改)
- /// <param name="_"></param>
- /// <summary>
- /// <returns></returns>
- public Response SaveForm(dynamic _)
- {
- ReqFormEntity parameter = this.GetReqData<ReqFormEntity>();
- VisitManageEntity entity = parameter.strEntity.ToObject<VisitManageEntity>();
- visitIBLL.SaveEntity(parameter.keyValue, entity);
- return Success("保存成功!");
- }
-
-
- #region 微信openid获取
-
- public Response GetWeixinAccess_token(dynamic _)
- {
- var entity = weChatConfigIbll.GetEnableEntity();
- string appid = entity?.APPId;
- string secret = entity?.secret;
- //string appid = "wx9bba72a124c32b58";// WeChatConfigentity.APPId;
- //string secret = "3553f4244898c20e387ccc4d039b683e";// WeChatConfigentity.secret;
-
- string code = Request.Query["code"];
- var responsejson = HttpGet("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code");
- if (!string.IsNullOrEmpty(responsejson))
- {
- return Success(JsonConvert.DeserializeObject(responsejson));
- //var weixintokenobj = JsonConvert.DeserializeObject<dynamic>(responsejson);
- //if (string.IsNullOrEmpty(weixintokenobj.errcode))
- //{
- //}
- //else
- //{
- // return Fail("微信授权失败,请重试。");
- //}
- }
- return Fail("微信授权失败,请重试。");
- }
- /// <summary>
- /// 发起一个HTTP请求(以GET方式)
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- public string HttpGet(string url)
- {
- WebRequest myWebRequest = WebRequest.Create(url);
- WebResponse myWebResponse = myWebRequest.GetResponse();
- Stream ReceiveStream = myWebResponse.GetResponseStream();
- string responseStr = "";
- if (ReceiveStream != null)
- {
- StreamReader reader = new StreamReader(ReceiveStream, Encoding.UTF8);
- responseStr = reader.ReadToEnd();
- reader.Close();
- }
- myWebResponse.Close();
- return responseStr;
- }
- /// <summary>
- /// 获取当前时间戳
- /// </summary>
- /// <param name="bflag">为真时获取10位时间戳,为假时获取13位时间戳.bool bflag = true</param>
- /// <returns></returns>
- public static string GetTimeStamp(bool bflag)
- {
- TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
- string ret = string.Empty;
- if (bflag)
- ret = Convert.ToInt64(ts.TotalSeconds).ToString();
- else
- ret = Convert.ToInt64(ts.TotalMilliseconds).ToString();
-
- return ret;
- }
- // <summary>
- /// SHA1 加密,返回大写字符串
- /// </summary>
- /// <param name="content">需要加密字符串</param>
- /// <returns>返回40位UTF8 大写</returns>
- public static string SHA1(string content)
- {
- return SHA1(content, Encoding.UTF8);
- }
- /// <summary>
- /// SHA1 加密,返回大写字符串
- /// </summary>
- /// <param name="content">需要加密字符串</param>
- /// <param name="encode">指定加密编码</param>
- /// <returns>返回40位大写字符串</returns>
- public static string SHA1(string content, Encoding encode)
- {
- try
- {
- SHA1 sha1 = new SHA1CryptoServiceProvider();
- byte[] bytes_in = encode.GetBytes(content);
- byte[] bytes_out = sha1.ComputeHash(bytes_in);
- sha1.Dispose();
- string result = BitConverter.ToString(bytes_out);
- result = result.Replace("-", "");
- return result;
- }
- catch (Exception ex)
- {
- throw new Exception("SHA1加密出错:" + ex.Message);
- }
- }
- /// <summary>
- /// 获取IP
- /// </summary>
- /// <returns></returns>
- private string GetIP()
- {
- //string ip = string.Empty;
- //if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"]))
- // ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
- //if (string.IsNullOrEmpty(ip))
- // ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);
- //return ip;
- string userIP = "未获取用户IP";
-
- try
- {
- if (System.Web.HttpContext.Current == null
- || System.Web.HttpContext.Current.Request == null
- || System.Web.HttpContext.Current.Request.ServerVariables == null)
- {
- return "";
- }
-
- string CustomerIP = "";
-
- //CDN加速后取到的IP simone 090805
- CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
- if (!string.IsNullOrEmpty(CustomerIP))
- {
- return CustomerIP;
- }
-
- CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
-
- if (!string.IsNullOrEmpty(CustomerIP))
- {
- return CustomerIP;
- }
-
- if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
- {
- CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
-
- if (CustomerIP == null)
- {
- CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
- }
- }
- else
- {
- CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
- }
-
- if (string.Compare(CustomerIP, "unknown", true) == 0 || string.IsNullOrEmpty(CustomerIP))
- {
- return System.Web.HttpContext.Current.Request.UserHostAddress;
- }
- return CustomerIP;
- }
- catch { }
-
- return userIP;
- }
- #endregion
-
- #region 私有类
-
- /// <summary>
- /// 表单实体类
- /// <summary>
- private class ReqFormEntity
- {
- public string keyValue { get; set; }
- public string strEntity { get; set; }
- }
- #endregion
- }
- }
|