using AIOWeb.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace
AIOWeb
{
public
class
wxapi : IHttpHandler
{
public
void ProcessRequest(HttpContext context)
{
context.Response.ContentType =
"text/plain"
;
string code =
""
;
string iv =
""
;
string encryptedData =
""
;
try
{
code = HttpContext.Current.Request.QueryString[
"code"
].ToString();
iv = HttpContext.Current.Request.QueryString[
"iv"
].ToString();
encryptedData = HttpContext.Current.Request.QueryString[
"encryptedData"
].ToString();
}
catch
(Exception ex)
{
context.Response.Write(ex.ToString());
}
string Appid =
"wxdb2641f85b04f1b3"
;
string Secret =
"8591d8cd7197b9197e17b3275329a1e7"
;
string grant_type =
"authorization_code"
;
string url =
"https://api.weixin.qq.com/sns/jscode2session?appid="
+ Appid +
"&secret="
+ Secret +
"&js_code="
+ code +
"&grant_type="
+ grant_type;
string type =
"utf-8"
;
AIOWeb.Models.GetUsersHelper GetUsersHelper =
new
AIOWeb.Models.GetUsersHelper();
string j = GetUsersHelper.GetUrltoHtml(url, type);
JObject jo = (JObject)JsonConvert.DeserializeObject(j);
result res =
new
result();
try
{
res.openid = jo[
"openid"
].ToString();
res.session_key = jo[
"session_key"
].ToString();
}
catch
(Exception)
{
res.errcode = jo[
"errcode"
].ToString();
res.errmsg = jo[
"errmsg"
].ToString();
}
if
(!string.IsNullOrEmpty(res.openid))
{
GetUsersHelper.AesIV = iv;
GetUsersHelper.AesKey = res.session_key;
string result = GetUsersHelper.AESDecrypt(encryptedData);
JObject _usrInfo = (JObject)JsonConvert.DeserializeObject(result);
userInfo userInfo =
new
userInfo();
userInfo.openId = _usrInfo[
"openId"
].ToString();
try
{
userInfo.unionId = _usrInfo[
"unionId"
].ToString();
}
catch
(Exception)
{
userInfo.unionId =
"unionId"
;
}
userInfo.nickName = _usrInfo[
"nickName"
].ToString();
userInfo.gender = _usrInfo[
"gender"
].ToString();
userInfo.city = _usrInfo[
"city"
].ToString();
userInfo.province = _usrInfo[
"province"
].ToString();
userInfo.country = _usrInfo[
"country"
].ToString();
userInfo.avatarUrl = _usrInfo[
"avatarUrl"
].ToString();
object watermark = _usrInfo[
"watermark"
].ToString();
object appid = _usrInfo[
"watermark"
][
"appid"
].ToString();
object timestamp = _usrInfo[
"watermark"
][
"timestamp"
].ToString();
#region
SqlConnection conn =
new
SqlConnection(
"server=127.0.0.1;database=Test;uid=sa;pwd=1"
);
conn.Open();
string Qrystr =
"SELECT * FROM WeChatUsers WHERE openId='"
+ userInfo.openId +
"'"
;
SqlCommand cmdQry =
new
SqlCommand(Qrystr, conn);
object obj = cmdQry.ExecuteScalar();
if
((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
string str =
"INSERT INTO WeChatUsers ([UnionId] ,[OpenId],[NickName],[Gender],[City],[Province],[Country],[AvatarUrl],[Appid],[Timestamp],[Memo],[counts])VALUES('"
+ userInfo.unionId +
"','"
+ userInfo.openId +
"','"
+ userInfo.nickName +
"','"
+ userInfo.gender +
"','"
+ userInfo.city +
"','"
+ userInfo.province +
"','"
+ userInfo.country +
"','"
+ userInfo.avatarUrl +
"','"
+ appid.ToString() +
"','"
+ timestamp.ToString() +
"','来自微信小程序','1')"
;
SqlCommand cmdUp =
new
SqlCommand(str, conn);
try
{
int row = cmdUp.ExecuteNonQuery();
}
catch
(Exception ex)
{
context.Response.Write(ex.ToString());
}
}
else
{
string str =
"UPDATE dbo.WeChatUsers SET counts = counts+1,UnionId = '"
+ userInfo.unionId +
"' WHERE OpenId='"
+ userInfo.openId +
"'"
;
SqlCommand cmdUp =
new
SqlCommand(str, conn);
int row = cmdUp.ExecuteNonQuery();
}
conn.Close();
#endregion
context.Response.Write(result);
}
else
{
context.Response.Write(j);
}
}
public
bool IsReusable
{
get
{
return
false;
}
}
}
}
暂时没有评论,来抢沙发吧~