487 lines
19 KiB
C#
487 lines
19 KiB
C#
using System;
|
||
using System.Data;
|
||
using System.Data.SqlClient;
|
||
using System.Configuration;
|
||
using System.Web;
|
||
using System.Net;
|
||
using System.IO;
|
||
using System.Security.Cryptography;
|
||
using System.Xml;
|
||
using System.Collections.Specialized;
|
||
using System.Text.RegularExpressions;
|
||
using System.Collections.Generic;
|
||
using System.Text;
|
||
using System.Globalization;
|
||
using System.Web.Security;
|
||
using System.Web.UI;
|
||
using System.Web.UI.WebControls;
|
||
using System.Web.UI.WebControls.WebParts;
|
||
using System.Web.UI.HtmlControls;
|
||
using System.Web.Script.Serialization;
|
||
namespace Mtxfw.shop
|
||
{
|
||
public partial class tuiguang : System.Web.UI.Page
|
||
{
|
||
/// <summary>
|
||
/// 填写你申请的登录资料
|
||
/// </summary>
|
||
|
||
public Mtxfw.Utility.Config config = new Mtxfw.Utility.Config();
|
||
public Mtxfw.DAL.user_info daoUser = new Mtxfw.DAL.user_info();
|
||
private string appKey = "";
|
||
private string appSecret = "";
|
||
protected string strPic = "", strNCName = "";
|
||
protected void Page_Load(object sender, EventArgs e)
|
||
{
|
||
|
||
if (!IsPostBack)
|
||
{
|
||
Page.Title = "请将图片发送给朋友";
|
||
try
|
||
{
|
||
if (!String.IsNullOrEmpty(Request.QueryString["invieuser"]))
|
||
{
|
||
int ContactID = Convert.ToInt32(Request.QueryString["invieuser"].ToString());
|
||
Mtxfw.Model.user_info ModelUser = daoUser.GetModel(ContactID);
|
||
if (ModelUser != null)
|
||
{
|
||
if (ModelUser.UserPic != "")
|
||
{
|
||
strPic = "<img src='" + ModelUser.UserPic + "' />";
|
||
}
|
||
if (ModelUser.NCName != "")
|
||
{
|
||
strNCName = ModelUser.NCName;
|
||
}
|
||
else
|
||
{
|
||
if (ModelUser.RealName != "")
|
||
{
|
||
strNCName = ModelUser.RealName;
|
||
}
|
||
else
|
||
{
|
||
strNCName = ModelUser.UserName;
|
||
}
|
||
}
|
||
}
|
||
if (Session["VContactID"] == null)
|
||
{
|
||
Session["VContactID"] = ContactID;
|
||
}
|
||
}
|
||
appKey = config.webappKey;
|
||
appSecret = config.webappSecret;
|
||
if (Request.QueryString["code"] != null)
|
||
{
|
||
string code = Request.QueryString["code"].ToString();
|
||
string error = string.Empty;
|
||
|
||
getgzptaccess_token(code, appKey, appSecret);
|
||
}
|
||
else
|
||
{
|
||
if (Session["MemberId"] == null)
|
||
{
|
||
Getcode("http://shop.q-b.cc" + Request.RawUrl.ToString());
|
||
}
|
||
else
|
||
{
|
||
int MemberId = Convert.ToInt32(Session["MemberId"].ToString());
|
||
Mtxfw.Model.user_info uModel = daoUser.GetModel(MemberId);
|
||
if (uModel != null)
|
||
{
|
||
if (uModel.opentype2 == 4)
|
||
{
|
||
bool b = true;
|
||
if (uModel.refresh_token != "" && uModel.refresh_token_time != "")
|
||
{
|
||
DateTime dt = Convert.ToDateTime(uModel.refresh_token_time);
|
||
DateTime dt1 = DateTime.Now;
|
||
DateTime dt2 = dt.AddDays(30);
|
||
if (DateTime.Compare(dt2, dt1) < 0)
|
||
{
|
||
b = false;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
b = false;
|
||
}
|
||
if (!b)
|
||
{
|
||
Getcode("http://shop.q-b.cc" + Request.RawUrl.ToString());
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Getcode("http://shop.q-b.cc" + Request.RawUrl.ToString());
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch(Exception err)
|
||
{
|
||
Response.Redirect("/login.aspx?Error=" + err);
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 获取未授权的临时令牌
|
||
/// </summary>
|
||
private void Getcode(string strUrl)
|
||
{
|
||
string url = "https://open.weixin.qq.com/connect/oauth2/authorize";
|
||
string[] parameters ={
|
||
"appid="+appKey,
|
||
"redirect_uri=" + UrlEncode(strUrl),
|
||
"response_type=code",
|
||
"scope=snsapi_userinfo",
|
||
"#wechat_redirect"
|
||
|
||
};
|
||
string tempParameters = string.Empty;
|
||
for (int i = 0; i < parameters.Length; i++)
|
||
{
|
||
tempParameters += parameters[i] + "&";
|
||
}
|
||
url = url + "?" + tempParameters + "";
|
||
Response.Redirect(url);
|
||
}
|
||
/// <summary>
|
||
/// url编码
|
||
/// </summary>
|
||
/// <param name="value">The value to Url encode</param>
|
||
/// <returns>Returns a Url encoded string</returns>
|
||
private string UrlEncode(string value)
|
||
{
|
||
string unreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
|
||
StringBuilder result = new StringBuilder();
|
||
foreach (char symbol in value)
|
||
{
|
||
if (unreservedChars.IndexOf(symbol) != -1)
|
||
{
|
||
result.Append(symbol);
|
||
}
|
||
else
|
||
{
|
||
result.Append('%' + String.Format("{0:X2}", (int)symbol));
|
||
}
|
||
}
|
||
return result.ToString();
|
||
}
|
||
protected string saveUserPic(string UserPic)
|
||
{
|
||
string savePath = "/Files/", filename = UserPic; //保存文件地址
|
||
string[] filetype = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" }; //文件允许格式
|
||
int fileSize = 3000; //文件大小限制,单位kb
|
||
WebClient wc = new WebClient();
|
||
HttpWebResponse res;
|
||
if (UserPic.Length > 7)
|
||
{
|
||
//格式验证
|
||
int temp = UserPic.LastIndexOf('.');
|
||
string currentType = UserPic.Substring(temp).ToLower();
|
||
if (UserPic.Substring(0, 7) == "http://")
|
||
{
|
||
bool b = true;
|
||
res = (HttpWebResponse)WebRequest.Create(UserPic).GetResponse();
|
||
//http检测
|
||
if (res.ResponseUri.Scheme.ToLower().Trim() != "http")
|
||
{
|
||
b = false;
|
||
}
|
||
//大小验证
|
||
if (res.ContentLength > fileSize * 1024)
|
||
{
|
||
b = false;
|
||
}
|
||
//死链验证
|
||
if (res.StatusCode != HttpStatusCode.OK)
|
||
{
|
||
b = false;
|
||
}
|
||
//检查mime类型
|
||
if (res.ContentType.IndexOf("image") == -1)
|
||
{
|
||
b = false;
|
||
}
|
||
res.Close();
|
||
if (b)
|
||
{
|
||
var filepath = savePath + DateTime.Now.ToString("yyyy-MM-dd") + "/";
|
||
|
||
//创建保存位置
|
||
if (!Directory.Exists(Server.MapPath(filepath)))
|
||
{
|
||
Directory.CreateDirectory(Server.MapPath(filepath));
|
||
}
|
||
|
||
//写入文件
|
||
filename = filepath + System.Guid.NewGuid() + (currentType.Length > 4 ? ".jpg" : currentType);
|
||
wc.DownloadFile(UserPic, Server.MapPath(filename));
|
||
}
|
||
}
|
||
}
|
||
return filename;
|
||
}
|
||
public object FromJson(string json)
|
||
{
|
||
JavaScriptSerializer js = new JavaScriptSerializer();
|
||
return js.Deserialize<WXUserInfo>(json);
|
||
}
|
||
public object FromJson2(string json)
|
||
{
|
||
JavaScriptSerializer js = new JavaScriptSerializer();
|
||
return js.Deserialize<WXaccess_token>(json);
|
||
}
|
||
public void getgzptaccess_token(string code, string appKey, string appSecret)
|
||
{
|
||
string straccess_token = "";
|
||
if (Session["gzptaccess_token"] == null)
|
||
{
|
||
string str = Mtxfw.Utility.Common.getPage2("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appKey + "&secret=" + appSecret + "&code=" + code + "&grant_type=authorization_code", "", "utf-8");
|
||
if (str.IndexOf("openid") != -1)
|
||
{
|
||
WXaccess_token ac = null;
|
||
ac = (WXaccess_token)FromJson2(str);
|
||
Session["gzptaccess_token"] = ac.access_token + "|" + ac.openid + "|" + DateTime.Now;
|
||
login(ac);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
string[] atoken = Session["gzptaccess_token"].ToString().Split('|');
|
||
if (atoken.Length > 2)
|
||
{
|
||
bool b = true;
|
||
if (atoken[2] != "")
|
||
{
|
||
DateTime dt = Convert.ToDateTime(atoken[2]);
|
||
DateTime dt1 = DateTime.Now;
|
||
DateTime dt2 = dt.AddSeconds(7000);
|
||
if (DateTime.Compare(dt2, dt1) < 0)
|
||
{
|
||
b = false;
|
||
|
||
}
|
||
}
|
||
if (b)
|
||
{
|
||
straccess_token = atoken[0];
|
||
WXaccess_token ac = new WXaccess_token();
|
||
ac.access_token = atoken[0];
|
||
ac.openid = atoken[1];
|
||
login(ac);
|
||
}
|
||
else
|
||
{
|
||
string str = Mtxfw.Utility.Common.getPage2("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appKey + "&secret=" + appSecret + "&code=" + code + "&grant_type=authorization_code", "", "utf-8");
|
||
if (str.IndexOf("openid") != -1)
|
||
{
|
||
WXaccess_token ac = null;
|
||
ac = (WXaccess_token)FromJson2(str);
|
||
Session["gzptaccess_token"] = ac.access_token + "|" + ac.openid + "|" + DateTime.Now;
|
||
login(ac);
|
||
}
|
||
else
|
||
{
|
||
Response.Redirect("/login.aspx?Error=openid" + str);
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Response.Redirect("/login.aspx?Error=atoken" + atoken.Length);
|
||
}
|
||
}
|
||
}
|
||
public void login(WXaccess_token ac)
|
||
{
|
||
if (!String.IsNullOrEmpty(ac.openid))
|
||
{
|
||
|
||
if (daoUser.GetCount("opentype=4 And openid='" + ac.openid + "'") > 0)
|
||
{
|
||
Mtxfw.Model.user_info uModel = daoUser.GetModel(ac.openid, 4);
|
||
if (uModel != null)
|
||
{
|
||
if (uModel.opentype2 != 4)
|
||
{
|
||
daoUser.UpdateuLevel("opentype2", 4, uModel.Id);
|
||
}
|
||
Session["MemberId"] = uModel.Id;
|
||
Session["MemberName"] = uModel.UserName;
|
||
Session["MemberNCName"] = uModel.NCName;
|
||
Session["MemberIFStores"] = uModel.IFStores;
|
||
|
||
}
|
||
else
|
||
{
|
||
Response.Redirect("/login.aspx?Error=ac.openid" + ac.openid);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
WXUserInfo Wu = GetUserInfo(ac.access_token, ac.openid);
|
||
if (Wu != null)
|
||
{
|
||
int ContactID = 0;
|
||
string ContactIDS = "";
|
||
if (Session["VContactID"] != null)
|
||
{
|
||
if (!String.IsNullOrEmpty(Session["VContactID"].ToString()))
|
||
{
|
||
ContactID = Convert.ToInt32(Session["VContactID"].ToString());
|
||
|
||
SqlDataReader dr = daoUser.GetUserName(ContactID);
|
||
if (dr.HasRows)
|
||
{
|
||
if (dr.Read())
|
||
{
|
||
ContactIDS = dr["ContactIDS"].ToString();
|
||
}
|
||
}
|
||
dr.Close();
|
||
}
|
||
Session.Remove("VContactID");
|
||
}
|
||
|
||
string strMemberCard = (daoUser.GetMaxMemberCard() + 1).ToString();
|
||
Mtxfw.Model.user_info model = new Mtxfw.Model.user_info();
|
||
model.MemberCard = strMemberCard;
|
||
string strUserName = Mtxfw.Utility.Common.RandNum(6).ToString().ToLower();
|
||
if (daoUser.IsExists(strUserName))
|
||
{
|
||
strUserName = "zh" + strMemberCard;
|
||
}
|
||
model.UserName = strUserName;
|
||
model.Password = Mtxfw.Utility.Security.EncryptString("123456");
|
||
model.Password2 = Mtxfw.Utility.Security.EncryptString("123456");
|
||
model.RealName = "";
|
||
model.NCName = Wu.nickname;
|
||
if (String.IsNullOrEmpty(Wu.headimgurl))
|
||
{
|
||
model.UserPic = saveUserPic(Wu.headimgurl);
|
||
}
|
||
else
|
||
{
|
||
model.UserPic = "";
|
||
}
|
||
model.Sex = (Wu.sex == "1" ? "男" : "女");
|
||
model.SFZ = "";
|
||
model.Mobile = "";
|
||
model.Phone = "";
|
||
model.Email = "";
|
||
model.Province = "";
|
||
model.City = "";
|
||
model.County = "";
|
||
model.MailingAddress = "";
|
||
model.PostalCode = "";
|
||
model.QQ = "";
|
||
model.CompanyName = "";
|
||
model.CompanyNumber = "";
|
||
model.CompanyContact = "";
|
||
model.ProductName = "";
|
||
model.UserState = "正常";
|
||
model.RegTime = DateTime.Now;
|
||
model.RegTime2 = DateTime.Now;
|
||
model.RememberID = 0;
|
||
model.RememberIDS = "";
|
||
|
||
model.SuperiorsID = 0;
|
||
model.SuperiorsIDS = "";
|
||
|
||
model.ContactID = ContactID;
|
||
if (ContactIDS == "")
|
||
{
|
||
model.ContactIDS = "," + ContactID.ToString() + ",";
|
||
}
|
||
else
|
||
{
|
||
model.ContactIDS = ContactIDS + ContactID.ToString() + ",";
|
||
}
|
||
|
||
|
||
model.DLRememberID = 0;
|
||
model.DLRememberIDS = "";
|
||
|
||
model.SJRememberID = 0;
|
||
model.SJRememberIDS = "";
|
||
|
||
model.uLevel0 = 0;
|
||
model.uLevel3 = 0;
|
||
int uLevel5 = 0;
|
||
|
||
model.uLevel5 = uLevel5;
|
||
model.utype = "0";
|
||
model.uutype = "1";
|
||
model.openid = ac.openid;
|
||
model.opentype = 4;
|
||
model.opentype2 = 4;
|
||
model.refresh_token = ac.openid;
|
||
model.refresh_token_time = DateTime.Now.ToString();
|
||
model.IFUpUserName = 1;
|
||
int id = daoUser.Add(model);
|
||
//daoUser.UpdateuLevel("IFStores", 1, id);
|
||
Mtxfw.Model.user_info uModel = daoUser.GetModel(id);
|
||
if (uModel != null)
|
||
{
|
||
Session["MemberId"] = uModel.Id;
|
||
Session["MemberName"] = uModel.UserName;
|
||
Session["MemberNCName"] = uModel.NCName;
|
||
Session["MemberIFStores"] = uModel.IFStores;
|
||
|
||
}
|
||
else
|
||
{
|
||
Response.Redirect("/login.aspx?Error=id" + id);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Response.Redirect("/login.aspx?Error=" + ac.access_token + "|" + ac.openid);
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Response.Redirect("/login.aspx?Error=ac.openid-" + ac.openid);
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 换取访问令牌
|
||
/// </summary>
|
||
private WXUserInfo GetUserInfo(string access_token, string openid)
|
||
{
|
||
string url = "https://api.weixin.qq.com/sns/userinfo";
|
||
string[] parameters ={
|
||
"access_token=" + access_token,
|
||
"openid=" + openid,
|
||
"lang=zh_CN"
|
||
};
|
||
string tempParameters = string.Empty;
|
||
for (int i = 0; i < parameters.Length; i++)
|
||
{
|
||
tempParameters += parameters[i] + "&";
|
||
}
|
||
url = url + "?" + tempParameters;
|
||
string response = Mtxfw.Utility.Common.getPage2(url, "", "utf-8");
|
||
|
||
WXUserInfo wu = null;
|
||
if (response.Length > 0)
|
||
{
|
||
if (response.IndexOf("nickname") != -1)
|
||
{
|
||
wu = (WXUserInfo)FromJson(response);
|
||
|
||
}
|
||
|
||
}
|
||
return wu;
|
||
}
|
||
}
|
||
|
||
|
||
} |