using BizCom;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SiteCore.wechat;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TenPay;
using Utils;
namespace SiteCore.Handler
{
public partial class app
{
string mini_Appid = "wx20432512482565e9";
string mini_Secret = "07492bc9cbbc5b303e5e135b4dccd6f0";
string mini_Appid_run = "wxe62d4788908d682c";
string mini_Secret_run = "f6d0852161703ed7acdfe880895c7fbe";
string grant_type = "authorization_code";
///
/// 获取链接返回数据
///
/// 链接
/// 请求类型
///
public string GetUrltoHtml(string Url, string type)
{
try
{
System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
// Get the response instance.
System.Net.WebResponse wResp = wReq.GetResponse();
System.IO.Stream respStream = wResp.GetResponseStream();
// Dim reader As StreamReader = New StreamReader(respStream)
using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.GetEncoding(type)))
{
return reader.ReadToEnd();
}
}
catch (System.Exception ex)
{
return ex.Message;
}
}
public void getUnifiedOrder()
{
}
public void getReSign()
{
if(UrlPostParmsCheck("prepay_id"))
{
string appid = WxPayConfig.MiniAPPID;// GetPostString("appid");
string prepay_id = GetPostString("prepay_id");
WxPayData jsApiParam = new WxPayData();
jsApiParam.SetValue("appId", appid);
jsApiParam.SetValue("timeStamp", WxPayApi.GenerateTimeStamp());
jsApiParam.SetValue("nonceStr", WxPayApi.GenerateNonceStr());
jsApiParam.SetValue("package", "prepay_id=" + prepay_id);
jsApiParam.SetValue("signType", "MD5");
jsApiParam.SetValue("paySign", jsApiParam.MakeSign());
conSuccess("",jsApiParam.ToJson());
return;
//string parameters = ;
}
conError("错误的参数");
//Log.Debug(this.GetType().ToString(), "Get jsApiParam : " + parameters);
//return parameters;
}
public void getReSign2(string prepay_id)
{
string appid = WxPayConfig.MiniAPPID;// GetPostString("appid");
//string prepay_id = GetPostString("prepay_id");
WxPayData jsApiParam = new WxPayData();
jsApiParam.SetValue("appId", appid);
jsApiParam.SetValue("timeStamp", WxPayApi.GenerateTimeStamp());
jsApiParam.SetValue("nonceStr", WxPayApi.GenerateNonceStr());
jsApiParam.SetValue("package", "prepay_id=" + prepay_id);
jsApiParam.SetValue("signType", "MD5");
jsApiParam.SetValue("paySign", jsApiParam.MakeSign());
conSuccess("", jsApiParam.ToJson());
}
///
/// 根据微信小程序平台提供的解密算法解密数据
///
/// 加密数据
/// 初始向量
/// 从服务端获取的SessionKey
///
public WechatUserInfo Decrypt(string encryptedData, string iv, string sessionKey)
{
WechatUserInfo userInfo;
//创建解密器生成工具实例
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);
//反序列化结果,生成用户信息实例
userInfo = JsonConvert.DeserializeObject(result);
return userInfo;
}
public void wx_login()
{
if (UrlPostParmsCheck("code,uname,pwd"))
{
string code = GetPostString("code");
string uname = GetPostString("uname");
string pwd = GetPostString("pwd");
string nickname = GetPostString("nickname");
string avatarUrl = GetPostString("avatarUrl");
int gender = GetPostInt("gender");
//向微信服务端 使用登录凭证 code 获取 session_key 和 openid
string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + mini_Appid + "&secret=" + mini_Secret + "&js_code=" + code + "&grant_type=" + grant_type;
string type = "utf-8";
string json = GetUrltoHtml(url, type);//获取微信服务器返回字符串
//微信服务器验证成功
JObject jo = (JObject)JsonConvert.DeserializeObject(json);
try
{
string openid = jo["openid"].ToString();
string session_key = jo["session_key"].ToString();
if (openid == "" || session_key == "")
{
conError("无法登录!");
return;
}
pwd = SecurityHelper.EncryptMD5(pwd);//加密
SUser entity = SUser.Get(uname, pwd);
if (entity == null)
{
conError("用户名或密码错误!");
return;
}
entity.Sex = gender;
entity.NickName = nickname;
entity.UserPic = avatarUrl;
entity.Update();
//封装成对象
string session_id = con.Session.SessionID;
UserObj uObj = new UserObj()
{
session_key = session_key,
openid = openid,
userid = entity.ID
};
//存入内存中
RedisHelper.StringSet(session_id, JsonConvert.SerializeObject(uObj));
//返回数据给小程序
StringBuilder userStr = new StringBuilder();
userStr.Append("{");
userStr.AppendFormat("\"session3\":\"{0}\"", session_id);
userStr.AppendFormat(",\"userpic\":\"{0}\"", entity.UserPic);
userStr.AppendFormat(",\"mobile\":\"{0}\"", entity.Mobile);
userStr.AppendFormat(",\"username\":\"{0}\"", entity.RealName);
userStr.Append("}");
conSuccess("登录成功", userStr.ToString());
return;
}
catch (Exception)
{
//微信服务器验证失败
string msg = jo["errcode"].ToString() + "," + jo["errmsg"].ToString();
conError(msg);
}
return;
}
conError("错误的参数");
}
}
}