242 lines
9.7 KiB
C#
242 lines
9.7 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Web;
|
||
using Newtonsoft.Json;
|
||
using Newtonsoft.Json.Linq;
|
||
using HZQR.Common;
|
||
using RedisHelp;
|
||
|
||
namespace EShang.Common
|
||
{
|
||
/// <summary>
|
||
/// 微信相关帮助类
|
||
/// </summary>
|
||
public class WeChatHelper
|
||
{
|
||
/// <summary>
|
||
/// 根据微信小程序平台提供的解密算法解密数据
|
||
/// 创建人:zhengxp
|
||
/// 创建日期:2020-03-17
|
||
/// 修改人:
|
||
/// 修改日期:yyyy-mm-dd
|
||
/// 修改备注:无
|
||
/// </summary>
|
||
/// <param name="encryptedData">加密数据</param>
|
||
/// <param name="iv">初始向量</param>
|
||
/// <param name="sessionKey">从服务端获取的SessionKey</param>
|
||
/// <returns></returns>
|
||
public static string Decrypt(string encryptedData, string iv, string sessionKey)
|
||
{
|
||
//创建解密器生成工具实例
|
||
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
|
||
//设置解密器参数
|
||
aes.Mode = CipherMode.CBC;
|
||
aes.BlockSize = 128;
|
||
aes.Padding = PaddingMode.PKCS7;
|
||
//格式化待处理字符串
|
||
byte[] byte_encryptedData = Convert.FromBase64String(encryptedData);
|
||
byte[] byte_iv = Convert.FromBase64String(iv);
|
||
byte[] byte_sessionKey = Convert.FromBase64String(sessionKey);
|
||
|
||
aes.IV = byte_iv;
|
||
aes.Key = byte_sessionKey;
|
||
//根据设置好的数据生成解密器实例
|
||
ICryptoTransform transform = aes.CreateDecryptor();
|
||
|
||
//解密
|
||
byte[] final = transform.TransformFinalBlock(byte_encryptedData, 0, byte_encryptedData.Length);
|
||
|
||
//生成结果
|
||
string result = Encoding.UTF8.GetString(final);
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取AccessToken
|
||
/// 创建人:zhengxp
|
||
/// 创建日期:2020-03-17
|
||
/// 修改人:
|
||
/// 修改日期:yyyy-mm-dd
|
||
/// 修改备注:无
|
||
/// </summary>
|
||
/// <param name="_AppID"></param>
|
||
/// <param name="_AppSecret"></param>
|
||
/// <returns></returns>
|
||
public static string GetAccessToken(string _AppID, string _AppSecret)
|
||
{
|
||
string reString = "";
|
||
try
|
||
{
|
||
string cachId = _AppID + "_AccessToken";//缓存key
|
||
if (System.Web.HttpRuntime.Cache[cachId] != null)
|
||
{
|
||
JObject info = (JObject)HttpRuntime.Cache[cachId];
|
||
DateTime access_token_time = Convert.ToDateTime(info["access_token_time"]);
|
||
int expires_in = Convert.ToInt32(info["expires_in"]);
|
||
if (access_token_time.AddSeconds(expires_in - 5) > DateTime.Now)
|
||
{
|
||
return info["access_token "].ToString();
|
||
}
|
||
|
||
}
|
||
string url = "https://api.weixin.qq.com/cgi-bin/token?" +
|
||
"grant_type=client_credential&appid=" + _AppID + "&secret=" + _AppSecret;
|
||
//根据url创建HttpWebRequest对象
|
||
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
|
||
objRequest.Method = "get";
|
||
//读取服务器返回信息
|
||
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
|
||
//using作为语句,用于定义一个范围,在此范围的末尾将释放对象
|
||
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
|
||
{
|
||
//ReadToEnd适用于小文件的读取,一次性的返回整个文件
|
||
JObject _JObject = (JObject)JsonConvert.DeserializeObject(sr.ReadToEnd());
|
||
|
||
System.Web.HttpRuntime.Cache.Insert(cachId, _JObject, null, DateTime.MaxValue, TimeSpan.FromSeconds(10));
|
||
try
|
||
{
|
||
reString = _JObject["access_token"].ToString();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
reString = "错误代码:" + _JObject["errcode"].ToString() + ",错误原因:" +
|
||
_JObject["errmsg"].ToString() + "(" + ex.Message + ")";
|
||
}
|
||
sr.Close();
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
reString = "error:" + ex.Message;
|
||
}
|
||
return reString;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 调用微信授权接口
|
||
/// 创建人:zhengxp
|
||
/// 创建日期:2020-03-17
|
||
/// 修改人:
|
||
/// 修改日期:yyyy-mm-dd
|
||
/// 修改备注:无
|
||
/// </summary>
|
||
/// <param name="wechatAppAppId"></param>
|
||
/// <param name="appSecret"></param>
|
||
/// <param name="code"></param>
|
||
/// <returns>JObject格式,Data中包含session_key、openid、unionid)</returns>
|
||
public static JObject WeChatLogin(string wechatAppAppId, string appSecret, string code)
|
||
{
|
||
JObject info = new JObject();
|
||
try
|
||
{
|
||
string url = "https://api.weixin.qq.com/sns/jscode2session";
|
||
if (!string.IsNullOrEmpty(code))
|
||
{
|
||
string reqUrl = url + "?appid=" + wechatAppAppId + "&secret=" + appSecret +
|
||
"&js_code=" + code + "&grant_type=authorization_code";
|
||
//根据reqUrl创建HttpWebRequest对象
|
||
HttpWebRequest objResquest = (HttpWebRequest)WebRequest.Create(reqUrl);
|
||
objResquest.Method = "get";
|
||
//读取服务器返回信息
|
||
HttpWebResponse objResponse = (HttpWebResponse)objResquest.GetResponse();
|
||
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
|
||
{
|
||
string srString = sr.ReadToEnd();
|
||
//ReadToEnd适用于小文件的读取,一次的放回整个文件
|
||
JObject data = (JObject)JsonConvert.DeserializeObject(srString);
|
||
|
||
if (data.Property("errcode") != null)
|
||
{
|
||
info["ResultCode"] = data["errcode"];
|
||
info["ResultDesc"] = data["errmsg"];
|
||
}
|
||
else
|
||
{
|
||
info["ResultData"] = data;
|
||
info["ResultCode"] = 100;
|
||
info["ResultDesc"] = "请求成功";
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
SuperMap.RealEstate.Utility.ErrorLogHelper.Write(ex, "小程序登录", "");
|
||
ex = ex ?? ex.InnerException;
|
||
info["ResultCode"] = 999;
|
||
info["ResultDesc"] = "请求失败" + ex.Message;
|
||
}
|
||
return info;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 检查一段文本是否含有违法违规内容
|
||
/// </summary>
|
||
/// <param name="wechatAppAppId"></param>
|
||
/// <param name="appSecret"></param>
|
||
/// <param name="content">要检测的文本内容,长度不超过 500KB</param>
|
||
/// <returns></returns>
|
||
public static JObject WeChatMsgSecCheck(string wechatAppAppId, string appSecret, string content)
|
||
{
|
||
JObject info = new JObject();
|
||
try
|
||
{
|
||
string access_token = GetAccessToken(wechatAppAppId, appSecret);
|
||
string url = "https://api.weixin.qq.com/wxa/msg_sec_check";
|
||
info["ResultCode"] = 100;
|
||
info["ResultDesc"] = "ok";
|
||
if (!string.IsNullOrEmpty(content))
|
||
{
|
||
string reqUrl = url + "?access_token=" + access_token;
|
||
|
||
JObject jsonObj = new JObject();
|
||
jsonObj["content"] = content;
|
||
string jsondata = jsonObj.ToString();
|
||
|
||
string restr = HttpUtil.HttpUrlPost(jsondata, reqUrl);
|
||
JObject data = (JObject)JsonConvert.DeserializeObject(restr);
|
||
if (data.Property("errcode").Value.TryParseToInt() != 0)
|
||
{
|
||
info["ResultCode"] = data["errcode"];
|
||
if (data["errcode"].TryParseToInt() == 87014)
|
||
{
|
||
info["ResultDesc"] = "内容含有违法违规内容";
|
||
}
|
||
else
|
||
{
|
||
info["ResultCode"] = data["errcode"];
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
SuperMap.RealEstate.Utility.ErrorLogHelper.Write(ex, "检查一段文本是否含有违法违规内容", "");
|
||
ex = ex ?? ex.InnerException;
|
||
info["ResultCode"] = 999;
|
||
info["ResultDesc"] = "请求失败" + ex.Message;
|
||
}
|
||
return info;
|
||
}
|
||
}
|
||
}
|
||
|
||
public class WechatUserInfo
|
||
{
|
||
public string openId { get; set; }
|
||
public string nickName { get; set; }
|
||
public string gender { get; set; }
|
||
public string city { get; set; }
|
||
public string province { get; set; }
|
||
public string country { get; set; }
|
||
public string avatarUrl { get; set; }
|
||
public string unionId { get; set; }
|
||
}
|