tijian_tieying/web/Web/SystemManage/openid.aspx.cs
2025-02-20 12:14:39 +08:00

119 lines
4.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThoughtWorks.QRCode.Codec;
public partial class SystemManage_openid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string code = Request.QueryString["code"];
string state = Request.QueryString["state"];
if (!string.IsNullOrEmpty(code) && !string.IsNullOrEmpty(state))
{
string openid = GetOAuthOpenId(code);
if (openid == "")
{
Response.Write("<h1>获取openid失败</h1>");
Response.End();
}
ZWL.BLL.ERPUser bll = new ZWL.BLL.ERPUser();
bool result = bll.Setopenid(state, openid);
if (result)
{
Response.Write("<h1>绑定成功</h1>");
Response.End();
}
else
{
Response.Write("<h1>绑定失败</h1>");
Response.End();
}
}
else
{
string id = Request.QueryString["ID"];
if (string.IsNullOrEmpty(id))
id = ZWL.Common.PublicMethod.GetSessionValue("UserID");
if(id== "NoLogin")
{
Response.Write("请先登录");
Response.End();
}
ZWL.BLL.ERPUser bll = new ZWL.BLL.ERPUser();
string openid = bll.Getopenid(id);
if (!string.IsNullOrEmpty(openid))
lbl_pic.Text = "目前绑定:" + openid;
//生成二维码
//https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb79cf945835c07e3&redirect_uri=http://dc.51csharp.com/SystemManage/openid.aspx&response_type=code&scope=snsapi_base&state=1&connect_redirect=1#wechat_redirect
string url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb79cf945835c07e3&redirect_uri=http://dc.51csharp.com/SystemManage/openid.aspx&response_type=code&scope=snsapi_base&state="+id+"&connect_redirect=1#wechat_redirect";
getImg(url);
}
}
}
private string GetOAuthOpenId(string code)
{
string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxb79cf945835c07e3&secret=e7ff8220aaae27b4dc88862bdf0a0087&code=" + code + "&grant_type=authorization_code";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream(); //获取响应的字符串流
if (stream != null)
{
StreamReader sr = new StreamReader(stream); //创建一个stream读取流
string html = sr.ReadToEnd(); //从头读到尾放到字符串html李米
sr.Close();
stream.Close();
Dictionary<string, object> jsDict = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(html);
if (jsDict.ContainsKey("openid"))
return jsDict["openid"].ToString();
else
return "";
}
return "";
}
private void getImg(string url)
{
QRCodeEncoder endocder = new QRCodeEncoder();
//二维码背景颜色
//endocder.QRCodeBackgroundColor = System.Drawing.Color.ForestGreen;
//二维码编码方式
endocder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
//每个小方格的宽度
endocder.QRCodeScale = 5;
//二维码版本号
//endocder.QRCodeVersion = 5;
//二维码版本号 改成0能解决长字符串“索引超出了数组界限”问题
endocder.QRCodeVersion = 0;
//纠错等级
endocder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
//将json串做成二维码
try
{
using (Bitmap bitmap = endocder.Encode(url, System.Text.Encoding.UTF8))
{
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
string base64 = Convert.ToBase64String(ms.ToArray());
Image1.ImageUrl = "data:image/png;base64," + base64;
}
}
}
catch (Exception ex)
{
Response.Write("生成二维码失败");
Response.End();
}
}
}