Browse Source

修改获取手机

master
wtp 2 years ago
parent
commit
57b3866d2b
  1. 48
      src/Znyc.Dispatching.Application/User/Services/UserService.cs
  2. 47
      src/Znyc.Dispatching.Core/Util/Extensions.cs
  3. 93
      src/Znyc.Dispatching.Core/Util/MiniProgramUtil.cs
  4. 69
      src/Znyc.Dispatching.Core/Znyc.Dispatching.Core.xml

48
src/Znyc.Dispatching.Application/User/Services/UserService.cs

@ -20,6 +20,7 @@ using Znyc.Dispatching.Application.Dto.Input;
using Znyc.Dispatching.Core; using Znyc.Dispatching.Core;
using Znyc.Dispatching.Core.Entitys; using Znyc.Dispatching.Core.Entitys;
using Znyc.Dispatching.Core.Extension; using Znyc.Dispatching.Core.Extension;
using Znyc.Dispatching.Core.Util;
using Znyc.Dispatching.WeChat.Core.CommonService.TemplateMessage.WxOpen; using Znyc.Dispatching.WeChat.Core.CommonService.TemplateMessage.WxOpen;
namespace Znyc.Dispatching.Application namespace Znyc.Dispatching.Application
@ -195,30 +196,31 @@ namespace Znyc.Dispatching.Application
var key = Convert.FromBase64String(jsCode2JsonResult.session_key);//第一步获取到的session_key var key = Convert.FromBase64String(jsCode2JsonResult.session_key);//第一步获取到的session_key
var iv = Convert.FromBase64String(input.DecryptPhoneAddInput.Iv); var iv = Convert.FromBase64String(input.DecryptPhoneAddInput.Iv);
Console.WriteLine("encryptedData:" + encryptedData); var phoneInfo = MiniProgramUtil.AESDecrypt(input.DecryptPhoneAddInput.EncryptedData, jsCode2JsonResult.session_key, input.DecryptPhoneAddInput.Iv);
Console.WriteLine("key:" + key); phone = phoneInfo.PurePhoneNumber;
Console.WriteLine("iv:" + iv); Console.WriteLine(phoneInfo.PurePhoneNumber);
var decryptor = rijndael.CreateDecryptor(key, iv); //var decryptor = rijndael.CreateDecryptor(key, iv);
Console.WriteLine("decryptor:" + decryptor);
using (var msDecrypt = new MemoryStream(encryptedData)) //using (var msDecrypt = new MemoryStream(encryptedData))
{ //{
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) // using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{ // {
Console.WriteLine("csDecrypt:"+ csDecrypt); // Console.WriteLine("csDecrypt:"+ csDecrypt);
using (var srDecrypt = new StreamReader(csDecrypt)) // using (var srDecrypt = new StreamReader(csDecrypt))
{ // {
var plaintext = srDecrypt.ReadToEnd(); // var plaintext = srDecrypt.ReadToEnd();
var json = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(plaintext); // Console.WriteLine("plaintext:"+ plaintext);
Console.WriteLine("json:"+json); // var json = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(plaintext);
var phoneNumber = json.phoneNumber; // Console.WriteLine("json:"+json);
var purePhoneNumber = json.purePhoneNumber; // var phoneNumber = json.phoneNumber;
// var purePhoneNumber = json.purePhoneNumber;
//至此,成功获取到手机号 // //至此,成功获取到手机号
phone = Convert.ToString(purePhoneNumber); // phone = Convert.ToString(purePhoneNumber);
} // }
} // }
} //}
} }
catch (Exception ex) catch (Exception ex)
{ {

47
src/Znyc.Dispatching.Core/Util/Extensions.cs

@ -0,0 +1,47 @@

using System;
using Microsoft.AspNetCore.Http;
namespace Znyc.Dispatching.Core.Util
{
/// <summary>
/// 验证扩展类
/// </summary>
public static partial class Extensions
{
/// <summary>
/// 检查 Object 是否为 NULL
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsEmpty(this object value)
{
return value == null || string.IsNullOrEmpty(value.ToString());
}
/// <summary>
/// 检查 Object 是否为 NULL 或者 0
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsNullOrZero(this object value)
{
return value == null || value.ToString().Trim() == "0";
}
/// <summary>
/// 检查是否为 AJAX 请求
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static bool IsAjaxRequest(this HttpRequest request)
{
if (request == null)
throw new ArgumentNullException(nameof(request));
if (request.Headers != null)
return request.Headers["X-Requested-With"] == "XMLHttpRequest";
return false;
}
}
}

93
src/Znyc.Dispatching.Core/Util/MiniProgramUtil.cs

@ -0,0 +1,93 @@
using Furion.JsonSerialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace Znyc.Dispatching.Core.Util
{
/// <summary>
/// 小程序工具类
/// </summary>
public class MiniProgramUtil
{
/// <summary>
/// 解密数据获取小程序用户信息
/// </summary>
/// <param name="encryptedDataStr">包括敏感数据在内的完整用户信息的加密数据</param>
/// <param name="session_key">auth.code2Session获取的session_key</param>
/// <param name="iv">加密算法的初始向量</param>
/// <returns>如遇异常返回 ""</returns>
public static TelUserInfoModel AESDecrypt(string encryptedDataStr, string session_key, string iv)
{
var rlt = BasicAESDecrypt(encryptedDataStr, session_key, iv);
return !rlt.IsEmpty() ? JSON.Deserialize<TelUserInfoModel>(rlt) : null;
}
/// <summary>
/// 基础解析
/// </summary>
/// <param name="encryptedDataStr">包括敏感数据在内的完整用户信息的加密数据</param>
/// <param name="session_key">auth.code2Session获取的session_key</param>
/// <param name="iv">加密算法的初始向量</param>
/// <returns></returns>
private static string BasicAESDecrypt(string encryptedDataStr, string session_key, string iv)
{
var databyte = System.Text.Encoding.Default.GetBytes(encryptedDataStr);
// 16进制转换成byte
var encryptedData = Convert.FromBase64String(encryptedDataStr.Replace(" ", ""));
var managed = new RijndaelManaged
{
Key = Convert.FromBase64String(session_key),
IV = Convert.FromBase64String(iv),
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
};
var transform = managed.CreateDecryptor();
var finalBlock = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
var result = Encoding.Default.GetString(finalBlock);
return result;
}
public static byte[] GetByteArray(string shex)
{
string[] ssArray = shex.Split(' ');
List<byte> bytList = new List<byte>();
foreach (var s in ssArray)
{ //将十六进制的字符串转换成数值
bytList.Add(Convert.ToByte(s, 16));
} //返回字节数组
return bytList.ToArray();
}
}
/// <summary>
/// 小程序登录解密后的用户信息Model
/// </summary>
public class TelUserInfoModel
{
/// <summary>
/// 手机号
/// </summary>
[JsonPropertyName("phoneNumber")]
public string PhoneNumber { get; set; }
/// <summary>
/// 纯手机号
/// </summary>
[JsonPropertyName("purePhoneNumber")]
public string PurePhoneNumber { get; set; }
/// <summary>
/// 区号
/// </summary>
[JsonPropertyName("countryCode")]
public string CountryCode { get; set; }
}
}

69
src/Znyc.Dispatching.Core/Znyc.Dispatching.Core.xml

@ -5343,6 +5343,75 @@
<param name="path">保存地址</param> <param name="path">保存地址</param>
<returns></returns> <returns></returns>
</member> </member>
<member name="T:Znyc.Dispatching.Core.Util.Extensions">
<summary>
验证扩展类
</summary>
</member>
<member name="M:Znyc.Dispatching.Core.Util.Extensions.IsEmpty(System.Object)">
<summary>
检查 Object 是否为 NULL
</summary>
<param name="value"></param>
<returns></returns>
</member>
<member name="M:Znyc.Dispatching.Core.Util.Extensions.IsNullOrZero(System.Object)">
<summary>
检查 Object 是否为 NULL 或者 0
</summary>
<param name="value"></param>
<returns></returns>
</member>
<member name="M:Znyc.Dispatching.Core.Util.Extensions.IsAjaxRequest(Microsoft.AspNetCore.Http.HttpRequest)">
<summary>
检查是否为 AJAX 请求
</summary>
<param name="request"></param>
<returns></returns>
</member>
<member name="T:Znyc.Dispatching.Core.Util.MiniProgramUtil">
<summary>
小程序工具类
</summary>
</member>
<member name="M:Znyc.Dispatching.Core.Util.MiniProgramUtil.AESDecrypt(System.String,System.String,System.String)">
<summary>
解密数据获取小程序用户信息
</summary>
<param name="encryptedDataStr">包括敏感数据在内的完整用户信息的加密数据</param>
<param name="session_key">auth.code2Session获取的session_key</param>
<param name="iv">加密算法的初始向量</param>
<returns>如遇异常返回 ""</returns>
</member>
<member name="M:Znyc.Dispatching.Core.Util.MiniProgramUtil.BasicAESDecrypt(System.String,System.String,System.String)">
<summary>
基础解析
</summary>
<param name="encryptedDataStr">包括敏感数据在内的完整用户信息的加密数据</param>
<param name="session_key">auth.code2Session获取的session_key</param>
<param name="iv">加密算法的初始向量</param>
<returns></returns>
</member>
<member name="T:Znyc.Dispatching.Core.Util.TelUserInfoModel">
<summary>
小程序登录解密后的用户信息Model
</summary>
</member>
<member name="P:Znyc.Dispatching.Core.Util.TelUserInfoModel.PhoneNumber">
<summary>
手机号
</summary>
</member>
<member name="P:Znyc.Dispatching.Core.Util.TelUserInfoModel.PurePhoneNumber">
<summary>
纯手机号
</summary>
</member>
<member name="P:Znyc.Dispatching.Core.Util.TelUserInfoModel.CountryCode">
<summary>
区号
</summary>
</member>
<member name="T:Znyc.Dispatching.Core.OSSClientUtil"> <member name="T:Znyc.Dispatching.Core.OSSClientUtil">
<summary> <summary>
阿里云oss文件上传工具类 阿里云oss文件上传工具类

Loading…
Cancel
Save