Commit eef3e03c authored by liudong1993's avatar liudong1993

Merge branch 'master' of http://gitlab.oytour.com/Kui2/education

parents e9ded4b4 02325bd6
...@@ -807,6 +807,21 @@ namespace Edu.Common ...@@ -807,6 +807,21 @@ namespace Edu.Common
get { return "wx38e054ee42b054f4"; } get { return "wx38e054ee42b054f4"; }
} }
/// <summary>
/// 甲小鹤AppId
/// </summary>
public static string JiaXiaoHeAppId {
get { return "wx0f4b451960f8ef29"; }
}
/// <summary>
/// 甲小鹤AppSecret
/// </summary>
public static string JiaXiaoHeAppSecret
{
get { return "62332ef58d16280fc05613fc61ecff1d"; }
}
/// <summary> /// <summary>
/// 甲鹤小程序AppSecret /// 甲鹤小程序AppSecret
/// </summary> /// </summary>
......
...@@ -5,6 +5,8 @@ using System.Linq; ...@@ -5,6 +5,8 @@ using System.Linq;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Edu.Common.Plugin;
using Newtonsoft.Json.Linq;
namespace Edu.Common namespace Edu.Common
{ {
...@@ -695,31 +697,24 @@ namespace Edu.Common ...@@ -695,31 +697,24 @@ namespace Edu.Common
/// <param name="iv"></param> /// <param name="iv"></param>
/// <returns></returns> /// <returns></returns>
public static string AES_decrypt(string encryptedDataStr, string key, string iv) public static string AES_decrypt(string encryptedData, string Session_key, string iv)
{ {
RijndaelManaged rijalg = new RijndaelManaged(); string phoneNum = "";
//----------------- byte[] encryData = Convert.FromBase64String(encryptedData); // strToToHexByte(text);
//设置 cipher 格式 AES-128-CBC RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijalg.KeySize = 128; rijndaelCipher.Key = Convert.FromBase64String(Session_key); // Encoding.UTF8.GetBytes(AesKey);
rijalg.Padding = PaddingMode.PKCS7; rijndaelCipher.IV = Convert.FromBase64String(iv);// Encoding.UTF8.GetBytes(AesIV);
rijalg.Mode = CipherMode.CBC; rijndaelCipher.Mode = CipherMode.CBC;
rijalg.Key = Convert.FromBase64String(key); rijndaelCipher.Padding = PaddingMode.PKCS7;
rijalg.IV = Convert.FromBase64String(iv); ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
byte[] encryptedData = Convert.FromBase64String(encryptedDataStr); byte[] plainText = transform.TransformFinalBlock(encryData, 0, encryData.Length);
//解密 string result = Encoding.Default.GetString(plainText);
ICryptoTransform decryptor = rijalg.CreateDecryptor(rijalg.Key, rijalg.IV); if (!string.IsNullOrEmpty(result))
string result;
using (MemoryStream msDecrypt = new MemoryStream(encryptedData))
{ {
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) var jObj = JObject.Parse(result);
{ phoneNum = jObj.GetStringValue("phoneNumber");
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
result = srDecrypt.ReadToEnd();
}
}
} }
return result; return phoneNum;
} }
} }
} }
\ No newline at end of file
...@@ -4,6 +4,7 @@ using System; ...@@ -4,6 +4,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Security.Cryptography;
using System.Text; using System.Text;
namespace Edu.Common.Plugin namespace Edu.Common.Plugin
...@@ -101,7 +102,7 @@ namespace Edu.Common.Plugin ...@@ -101,7 +102,7 @@ namespace Edu.Common.Plugin
{ {
//请求路径 //请求路径
string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + AppId + "&secret=" + AppSecret + "&js_code=" + Code + "&grant_type=authorization_code"; string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + AppId + "&secret=" + AppSecret + "&js_code=" + Code + "&grant_type=authorization_code";
resultInfo = Common.Plugin.HttpHelper.HttpGet(url); resultInfo = Common.Plugin.HttpHelper.HttpGet(url);
if (resultInfo != null && !string.IsNullOrEmpty(resultInfo)) if (resultInfo != null && !string.IsNullOrEmpty(resultInfo))
{ {
userInfo = JsonConvert.DeserializeObject<result>(resultInfo); userInfo = JsonConvert.DeserializeObject<result>(resultInfo);
...@@ -117,6 +118,39 @@ namespace Edu.Common.Plugin ...@@ -117,6 +118,39 @@ namespace Edu.Common.Plugin
return userInfo; return userInfo;
} }
/// <summary>
/// 获取微信授权手机号码
/// </summary>
/// <param name="encryptedData"></param>
/// <param name="code"></param>
/// <param name="ivStr"></param>
/// <returns></returns>
public static result GetWechatMobile(string encryptedData, string code, string iv)
{
string Appid = Common.Config.JiaXiaoHeAppId;
var Secret = Common.Config.JiaXiaoHeAppSecret;
string grant_type = "authorization_code";
//向微信服务端 使用登录凭证 code 获取 session_key 和 openid
string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + Appid + "&secret=" + Secret + "&js_code=" + code + "&grant_type=" + grant_type;
string type = "utf-8";
GetUsersHelper GetUsersHelper = new GetUsersHelper();
result res = new Common.Plugin.result();
string j = GetUsersHelper.GetUrltoHtml(url, type);//获取微信服务器返回字符串
//将字符串转换为json格式
JObject jo = JObject.Parse(j);
//微信服务器验证成功
res.openid = jo.GetStringValue("openid");
res.session_key = jo.GetStringValue("session_key");
if (!string.IsNullOrWhiteSpace(res.openid))
{
if (!string.IsNullOrEmpty(encryptedData) && !string.IsNullOrEmpty(iv))
{
//解析手机号码
res.phoneNumber = Common.EncryptionHelper.AES_decrypt(encryptedData, res.session_key, iv);
}
}
return res;
}
} }
...@@ -154,8 +188,6 @@ namespace Edu.Common.Plugin ...@@ -154,8 +188,6 @@ namespace Edu.Common.Plugin
} }
#region 实体类
/// <summary> /// <summary>
/// 微信小程序验证返回结果 /// 微信小程序验证返回结果
/// </summary> /// </summary>
...@@ -185,10 +217,11 @@ namespace Edu.Common.Plugin ...@@ -185,10 +217,11 @@ namespace Edu.Common.Plugin
/// 错误提示信息 /// 错误提示信息
/// </summary> /// </summary>
public string errmsg { get; set; } public string errmsg { get; set; }
}
/// <summary>
/// 电话号码
/// </summary>
public string phoneNumber { get; set; }
}
#endregion }
\ No newline at end of file
}
...@@ -240,11 +240,11 @@ namespace Edu.WebApi.Controllers.APP ...@@ -240,11 +240,11 @@ namespace Edu.WebApi.Controllers.APP
} }
else else
{ {
if (!string.IsNullOrEmpty(model.UnionId))//UnionId是否为空,为空则绑定手机号与UnionId if (!string.IsNullOrEmpty(model.OpenId))//UnionId是否为空,为空则绑定手机号与UnionId
{ {
if (UnionId != model.UnionId) if (OpenId != model.OpenId)
{ {
return ApiResult.Failed("手机号与绑定的微信账户不一致", new { Error = 0 }); //return ApiResult.Failed("手机号与绑定的微信账户不一致", new { Error = 0 });
} }
} }
else else
...@@ -306,6 +306,10 @@ namespace Edu.WebApi.Controllers.APP ...@@ -306,6 +306,10 @@ namespace Edu.WebApi.Controllers.APP
UserIcon = model.UserIcon, UserIcon = model.UserIcon,
EnterPhone = Cache.User.UserReidsCache.GetUserLoginInfo(studentModel.EnterID).UserMobile, EnterPhone = Cache.User.UserReidsCache.GetUserLoginInfo(studentModel.EnterID).UserMobile,
ApiRequestFromEnum = Common.Enum.ApiRequestFromEnum.AppletStudent, ApiRequestFromEnum = Common.Enum.ApiRequestFromEnum.AppletStudent,
JHMallBaseId = Config.JHMallBaseId,
JHTenantId = Config.JHTenantId,
JHMallUserId = model?.Id ?? 0,
JHMallUserOpenId = Config.JHMallUserOpenId,
UploadConfig = new UploadConfig = new
{ {
uploadConfig?.Bucket, uploadConfig?.Bucket,
...@@ -337,43 +341,23 @@ namespace Edu.WebApi.Controllers.APP ...@@ -337,43 +341,23 @@ namespace Edu.WebApi.Controllers.APP
string code = parms.GetStringValue("code"); string code = parms.GetStringValue("code");
string encryptedData = parms.GetStringValue("encryptedData"); string encryptedData = parms.GetStringValue("encryptedData");
string iv = parms.GetStringValue("iv"); string iv = parms.GetStringValue("iv");
result res = new result();
string Appid = Common.Config.AppID;
var Secret = Common.Config.AppSecret;
string grant_type = "authorization_code";
//向微信服务端 使用登录凭证 code 获取 session_key 和 openid
string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + Appid + "&secret=" + Secret + "&js_code=" + code + "&grant_type=" + grant_type;
string type = "utf-8";
GetUsersHelper GetUsersHelper = new GetUsersHelper();
JObject jo = null;
string _telPhone = "";
try try
{ {
string j = GetUsersHelper.GetUrltoHtml(url, type);//获取微信服务器返回字符串 res = Common.Plugin.WeChatHelper.GetWechatMobile(encryptedData, code, iv);
//将字符串转换为json格式 }
jo = (JObject)JsonConvert.DeserializeObject(j); catch
Common.Plugin.result res = new Common.Plugin.result {
try
{ {
//微信服务器验证成功 res = Common.Plugin.WeChatHelper.GetWechatMobile(encryptedData, code, iv);
openid = jo["openid"].ToString(), }
session_key = jo["session_key"].ToString() catch (Exception ex)
};
if (!string.IsNullOrWhiteSpace(jo["openid"].ToString()))
{ {
if (!String.IsNullOrEmpty(encryptedData) && !string.IsNullOrEmpty(iv)) Common.Plugin.LogHelper.Write(ex, "GetGuestWeiXinMobile");
{
//解析手机号码
_telPhone = Common.EncryptionHelper.AES_decrypt(encryptedData, jo["session_key"].ToString(), iv);
}
} }
} }
catch (Exception ex) return ApiResult.Success("", res);
{
Common.Plugin.LogHelper.Write(ex, string.Format("GetGuestWeiXinMobile:msg:{0},request:{1},URL:{2}", Common.Plugin.JsonHelper.Serialize(jo), RequestParm.Msg.ToString(), url));
return ApiResult.Failed("获取失败");
}
return ApiResult.Success("", _telPhone);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment