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
get { return "wx38e054ee42b054f4"; }
}
/// <summary>
/// 甲小鹤AppId
/// </summary>
public static string JiaXiaoHeAppId {
get { return "wx0f4b451960f8ef29"; }
}
/// <summary>
/// 甲小鹤AppSecret
/// </summary>
public static string JiaXiaoHeAppSecret
{
get { return "62332ef58d16280fc05613fc61ecff1d"; }
}
/// <summary>
/// 甲鹤小程序AppSecret
/// </summary>
......
......@@ -5,6 +5,8 @@ using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Edu.Common.Plugin;
using Newtonsoft.Json.Linq;
namespace Edu.Common
{
......@@ -695,31 +697,24 @@ namespace Edu.Common
/// <param name="iv"></param>
/// <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();
//-----------------
//设置 cipher 格式 AES-128-CBC
rijalg.KeySize = 128;
rijalg.Padding = PaddingMode.PKCS7;
rijalg.Mode = CipherMode.CBC;
rijalg.Key = Convert.FromBase64String(key);
rijalg.IV = Convert.FromBase64String(iv);
byte[] encryptedData = Convert.FromBase64String(encryptedDataStr);
//解密
ICryptoTransform decryptor = rijalg.CreateDecryptor(rijalg.Key, rijalg.IV);
string result;
using (MemoryStream msDecrypt = new MemoryStream(encryptedData))
string phoneNum = "";
byte[] encryData = Convert.FromBase64String(encryptedData); // strToToHexByte(text);
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Key = Convert.FromBase64String(Session_key); // Encoding.UTF8.GetBytes(AesKey);
rijndaelCipher.IV = Convert.FromBase64String(iv);// Encoding.UTF8.GetBytes(AesIV);
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.PKCS7;
ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
byte[] plainText = transform.TransformFinalBlock(encryData, 0, encryData.Length);
string result = Encoding.Default.GetString(plainText);
if (!string.IsNullOrEmpty(result))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
result = srDecrypt.ReadToEnd();
}
}
var jObj = JObject.Parse(result);
phoneNum = jObj.GetStringValue("phoneNumber");
}
return result;
return phoneNum;
}
}
}
\ No newline at end of file
......@@ -4,6 +4,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
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";
resultInfo = Common.Plugin.HttpHelper.HttpGet(url);
resultInfo = Common.Plugin.HttpHelper.HttpGet(url);
if (resultInfo != null && !string.IsNullOrEmpty(resultInfo))
{
userInfo = JsonConvert.DeserializeObject<result>(resultInfo);
......@@ -117,6 +118,39 @@ namespace Edu.Common.Plugin
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
}
#region 实体类
/// <summary>
/// 微信小程序验证返回结果
/// </summary>
......@@ -185,10 +217,11 @@ namespace Edu.Common.Plugin
/// 错误提示信息
/// </summary>
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
}
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
......@@ -306,6 +306,10 @@ namespace Edu.WebApi.Controllers.APP
UserIcon = model.UserIcon,
EnterPhone = Cache.User.UserReidsCache.GetUserLoginInfo(studentModel.EnterID).UserMobile,
ApiRequestFromEnum = Common.Enum.ApiRequestFromEnum.AppletStudent,
JHMallBaseId = Config.JHMallBaseId,
JHTenantId = Config.JHTenantId,
JHMallUserId = model?.Id ?? 0,
JHMallUserOpenId = Config.JHMallUserOpenId,
UploadConfig = new
{
uploadConfig?.Bucket,
......@@ -337,43 +341,23 @@ namespace Edu.WebApi.Controllers.APP
string code = parms.GetStringValue("code");
string encryptedData = parms.GetStringValue("encryptedData");
string iv = parms.GetStringValue("iv");
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 = "";
result res = new result();
try
{
string j = GetUsersHelper.GetUrltoHtml(url, type);//获取微信服务器返回字符串
//将字符串转换为json格式
jo = (JObject)JsonConvert.DeserializeObject(j);
Common.Plugin.result res = new Common.Plugin.result
res = Common.Plugin.WeChatHelper.GetWechatMobile(encryptedData, code, iv);
}
catch
{
try
{
//微信服务器验证成功
openid = jo["openid"].ToString(),
session_key = jo["session_key"].ToString()
};
if (!string.IsNullOrWhiteSpace(jo["openid"].ToString()))
res = Common.Plugin.WeChatHelper.GetWechatMobile(encryptedData, code, iv);
}
catch (Exception ex)
{
if (!String.IsNullOrEmpty(encryptedData) && !string.IsNullOrEmpty(iv))
{
//解析手机号码
_telPhone = Common.EncryptionHelper.AES_decrypt(encryptedData, jo["session_key"].ToString(), iv);
}
Common.Plugin.LogHelper.Write(ex, "GetGuestWeiXinMobile");
}
}
catch (Exception ex)
{
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);
return ApiResult.Success("", res);
}
......
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