tijian_tieying/web/Web/App_Code/globle.cs
2025-02-20 12:14:39 +08:00

130 lines
5.3 KiB
C#
Raw 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.IO;
using System.Net;
using System.Web;
/// <summary>
/// globle 的摘要说明
/// </summary>
public class globle
{
public class Global
{
private static string appid = "wxbff2f5d49ec4be77";
private static string appsecret = "f271039e68a914fd1cd92cf00f79e20f";
//application 名称冲突 不能一样的名字
public static Models.WeiXin.AccessToken getAccessToken
{
get
{
//每次都获取新的 疾控与oa 获取同一个公众号的accesstoken相互影响
var at = new Models.WeiXin.AccessToken();
WebClient wc = new WebClient();
at.access_token= wc.DownloadString("http://oa.dcqcdc.com/dccdc/zzj/getaccesstoken");
return at;
HttpContext hc = HttpContext.Current;
if (hc.Application["AccessTokenYM1"] == null)
{
var token = _getAccessToken();
hc.Application["AccessTokenYM1"] = token;
hc.Application["access_token_expires_in_ym1"] = DateTime.Now.AddSeconds(7000);
return token;
}
else
{
DateTime dt = (DateTime)hc.Application["access_token_expires_in_ym1"];
if (dt >= DateTime.Now)
return hc.Application["AccessTokenYM1"] as Models.WeiXin.AccessToken;
else
{
var token = _getAccessToken();
hc.Application["AccessTokenYM1"] = token;
hc.Application["access_token_expires_in_ym1"] = DateTime.Now.AddSeconds(7000);
return token;
}
}
}
}
private static Models.WeiXin.AccessToken _getAccessToken()
{
WebClient wc = new WebClient();
var stream = wc.OpenRead("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + appsecret + "");
var sr = new StreamReader(stream);
string token = sr.ReadToEnd();
sr.Close();
stream.Dispose();
var accesstoken = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.WeiXin.AccessToken>(token);
if (string.IsNullOrEmpty(accesstoken.access_token))
{
Models.WeiXin.error err = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.WeiXin.error>(token);
Exception wx = new Exception(token);
//wx.Message = token;
throw (wx);
}
return accesstoken;
}
public static string jsapi_ticket
{
get
{
HttpContext hc = HttpContext.Current;
if (hc.Application["jsapi_ticket"] == null || hc.Application["AccessToken"] == null)
{
var token = _getjsapi_ticket();
hc.Application["jsapi_ticket"] = token;
hc.Application["jsapi_ticket_expires_in"] = DateTime.Now.AddSeconds(7000);
return token;
}
else
{
DateTime dt = (DateTime)hc.Application["jsapi_ticket_expires_in"];
if (dt >= DateTime.Now)
return hc.Application["jsapi_ticket"].ToString();
else
{
var token = _getjsapi_ticket();
hc.Application["jsapi_ticket"] = token;
hc.Application["jsapi_ticket_expires_in"] = DateTime.Now.AddSeconds(7000);
return token;
}
}
}
}
private static string _getjsapi_ticket()
{
var token = getAccessToken;
WebClient wc = new WebClient();
var stream = wc.OpenRead("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + token.access_token + "&type=jsapi");
var sr = new StreamReader(stream);
string jsapi = sr.ReadToEnd();
sr.Close();
stream.Dispose();
Dictionary<string, object> jsDict = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(jsapi);
return jsDict["ticket"].ToString();
}
#region
public static string GetOAuthOpenId(string code)
{
string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + appsecret + "&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();
return html;
}
return "";
}
#endregion
}
}