tijian_tieying/web/dccdc/Common/Global.cs
2025-02-20 12:14:39 +08:00

776 lines
29 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Web;
namespace dccdc.Common
{
public class Global
{
public static HttpApplicationState MyApplication { get; set; }
public static string getParmValue(string name)
{
return new BLL.Common().getParm_Value(name, "", "");
}
//public const string appid = "wxb79cf945835c07e3";
//public const string secret = "e7ff8220aaae27b4dc88862bdf0a0087";
public static Models.WeiXin.AccessToken getAccessToken
{
get
{
HttpContext hc = HttpContext.Current;
if (MyApplication["AccessToken"] == null)
{
var token = _getAccessToken();
MyApplication["AccessToken"] = token;
MyApplication["access_token_expires_in"] = DateTime.Now.AddSeconds(7000);
return token;
}
else
{
DateTime dt = (DateTime)MyApplication["access_token_expires_in"];
if (dt >= DateTime.Now)
return MyApplication["AccessToken"] as Models.WeiXin.AccessToken;
else
{
var token = _getAccessToken();
MyApplication["AccessToken"] = token;
MyApplication["access_token_expires_in"] = DateTime.Now.AddSeconds(7000);
return token;
}
}
}
}
/// <summary>
/// 微信AppID
/// </summary>
public static string AppId
{
get
{
var hc = HttpContext.Current;
if (MyApplication["AppId"] != null) return (string)MyApplication["AppId"];
var appid = new BLL.Common().getParm_Value("appid", "wxfde2d6cb809cb42c", "appid");
//var appid = new BLL.Common().getParm_Value("appid", "wxb79cf945835c07e3", "appid");
MyApplication["AppId"] = appid;
return appid;
}
}
/// <summary>
/// 微信App秘钥
/// </summary>
public static string Appsecret
{
get
{
var hc = HttpContext.Current;
if (MyApplication["Appsecret"] != null) return (string)MyApplication["Appsecret"];
//var appsecret = new BLL.Common().getParm_Value("appsecret", "e7ff8220aaae27b4dc88862bdf0a0087", "appsecret");
var appsecret = new BLL.Common().getParm_Value("appsecret", "6176304e5d22281fb1f49a8d364bcce2", "appsecret");
MyApplication["Appsecret"] = appsecret;
return appsecret;
}
}
/// <summary>
/// 疫苗微信App秘钥
/// </summary>
public static string YMAppsecret
{
get
{
var hc = HttpContext.Current;
if (MyApplication["YMAppsecret"] != null) return (string)MyApplication["YMAppsecret"];
var appsecret = new BLL.Common().getParm_Value("ymappsecret", "f271039e68a914fd1cd92cf00f79e20f", "疫苗appsecret");
MyApplication["YMAppsecret"] = appsecret;
return appsecret;
}
}
/// <summary>
/// 公众号原始ID
/// </summary>
public static string MyId
{
get
{
var hc = HttpContext.Current;
if (MyApplication["MyId"] != null) return (string)MyApplication["MyId"];
var appid = new BLL.Common().getParm_Value("myid", "gh_7a3845c111a0", "myid");
MyApplication["MyId"] = appid;
return appid;
}
}
public static string QYYMYY
{
get
{
return new BLL.Common().getParm_Value("QYYMYY", "false", "启用疫苗预约");
}
}
//gh_fdc67998c8e4
/// <summary>
/// /// <summary>
/// 疫苗公众号原始ID
/// </summary>
/// </summary>
public static string YMMyId
{
get
{
var hc = HttpContext.Current;
if (MyApplication["YMMyId"] != null) return (string)MyApplication["YMMyId"];
var appid = new BLL.Common().getParm_Value("YMMyId", "gh_fdc67998c8e4", "YMMyId");
MyApplication["YMMyId"] = appid;
return appid;
}
}
/// <summary>
/// 商户秘钥
/// </summary>
public static string Secret
{
get
{
var hc = HttpContext.Current;
if (MyApplication["Secret"] != null) return (string)MyApplication["Secret"];
//var secret = new BLL.Common().getParm_Value("secret", "e7ff8220aaae27b4dc88862bdf0a0087", "secret");
var secret = new BLL.Common().getParm_Value("secret", "6176304e5d22281fb1f49a8d364bcce2", "secret");
MyApplication["Secret"] = secret;
return secret;
}
}
public static string jsapi_ticket
{
get
{
HttpContext hc = HttpContext.Current;
if (MyApplication["jsapi_ticket"] == null || MyApplication["AccessToken"] == null)
{
var token = _getjsapi_ticket();
MyApplication["jsapi_ticket"] = token;
MyApplication["jsapi_ticket_expires_in"] = DateTime.Now.AddSeconds(7000);
return token;
}
else
{
DateTime dt = (DateTime)MyApplication["jsapi_ticket_expires_in"];
if (dt >= DateTime.Now)
return MyApplication["jsapi_ticket"].ToString();
else
{
var token = _getjsapi_ticket();
MyApplication["jsapi_ticket"] = token;
MyApplication["jsapi_ticket_expires_in"] = DateTime.Now.AddSeconds(7000);
return token;
}
}
}
}
public static string jsapi_ticketYM
{
get
{
var commdb = new DAL.Common();
var dts = commdb.getParm_Value("jsapi_ticketYM_expires_in", "", "");
DateTime dt;
if(DateTime.TryParse(dts,out dt)) {
if (dt >= DateTime.Now)
return commdb.getParm_Value("jsapi_ticketYM", "", "");// MyApplication["jsapi_ticketYM"].ToString();
else
{
var token = _getjsapi_ticketYM();
//MyApplication["jsapi_ticketYM"] = token;
//MyApplication["jsapi_ticketYM_expires_in"] = DateTime.Now.AddSeconds(7000);
commdb.setParm_Value("jsapi_ticketYM", token);
commdb.setParm_Value("jsapi_ticketYM_expires_in", DateTime.Now.AddSeconds(7000).ToString("yyyy-MM-dd HH:mm:ss"));
return token;
}
}
else
{
var token = _getjsapi_ticketYM();
commdb.setParm_Value("jsapi_ticketYM", token);
commdb.setParm_Value("jsapi_ticketYM_expires_in", DateTime.Now.AddSeconds(7000).ToString("yyyy-MM-dd HH:mm:ss"));
return token;
}
}
}
public static string Token
{
get
{
var hc = HttpContext.Current;
if (MyApplication["Token"] != null) return (string)MyApplication["Token"];
var Token = new BLL.Common().getParm_Value("Token", "32481440f2fac994106475dc96082534", "微信Token");
MyApplication["Token"] = Token;
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();
}
private static string _getjsapi_ticketYM()
{
var token = getAccessTokenYM;
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();
}
private static Models.WeiXin.AccessToken _getAccessToken()
{
WebClient wc = new WebClient();
string address = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + AppId + "&secret=" + Secret + "";
var stream = wc.OpenRead(address);
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 int jgid
{
get
{
int topid = (HttpContext.Current.Session["loginUser"] as Models.ERPUser).top_id;
if (topid == 0)
topid = 49;
return topid;
}
}
#region
/// <summary>
/// 疫苗用
/// </summary>
public static string YMToken
{
get
{
var hc = HttpContext.Current;
if (MyApplication["YMToken"] != null) return (string)MyApplication["YMToken"];
var Token = new BLL.Common().getParm_Value("YMToken", "32481440f2fac994106475dc96082534", "疫苗微信Token");
MyApplication["YMToken"] = Token;
return Token;
}
}
public static string EncodingAESKey
{
get
{
var hc = HttpContext.Current;
if (MyApplication["EncodingAESKey"] != null) return (string)MyApplication["EncodingAESKey"];
var AESKey = new BLL.Common().getParm_Value("EncodingAESKey", "SxEkpCrknFC7E1UeOX0xAgeNWc21lTGBA7u42boTCBT", "微信YMEncodingAESKey");
MyApplication["EncodingAESKey"] = AESKey;
return AESKey;
}
}
public static string YMEncodingAESKey
{
get
{
var hc = HttpContext.Current;
if (MyApplication["YMEncodingAESKey"] != null) return (string)MyApplication["YMEncodingAESKey"];
var AESKey = new BLL.Common().getParm_Value("YMEncodingAESKey", "SxEkpCrknFC7E1UeOX0xAgeNWc21lTGBA7u42boTCBT", "疫苗微信YMEncodingAESKey");
MyApplication["YMEncodingAESKey"] = AESKey;
return AESKey;
}
}
public static string YMAppId
{
get
{
var hc = HttpContext.Current;
if (MyApplication["YMAppId"] != null) return (string)MyApplication["YMAppId"];
var appid = new BLL.Common().getParm_Value("YMAppId", "wxbff2f5d49ec4be77", "疫苗微信YMAppId");
MyApplication["YMAppId"] = appid;
return appid;
}
}
public static Models.WeiXin.AccessToken getAccessTokenYM
{
get
{
//每次都获取新的 疾控与oa 获取同一个公众号的accesstoken相互影响
//return _getAccessTokenYM();
var dbcomm= new DAL.Common();
var expirse_in_ym = dbcomm.getParm_Value("access_token_expires_in_ym", "", "疫苗access_token到期时间");
DateTime dt ;
if(DateTime.TryParse(expirse_in_ym,out dt))
{
if (dt >= DateTime.Now)
{
var at = new Models.WeiXin.AccessToken();
at.access_token = dbcomm.getParm_Value("AccessTokenYM", "", "");
return at;
}
else
{
var token = _getAccessTokenYM();
//MyApplication["AccessTokenYM"] = token;
//MyApplication["access_token_expires_in_ym"] = DateTime.Now.AddSeconds(7000);
dbcomm.setParm_Value("AccessTokenYM", token.access_token);
dbcomm.setParm_Value("access_token_expires_in_ym", DateTime.Now.AddSeconds(7000).ToString("yyyy-MM-dd HH:mm:ss"));
return token;
}
}
else
{
var token = _getAccessTokenYM();
//MyApplication["AccessTokenYM"] = token;
//MyApplication["access_token_expires_in_ym"] = DateTime.Now.AddSeconds(7000);
dbcomm.setParm_Value("AccessTokenYM", token.access_token);
dbcomm.setParm_Value("access_token_expires_in_ym", DateTime.Now.AddSeconds(7000).ToString("yyyy-MM-dd HH:mm:ss"));
return token;
}
}
}
private static Models.WeiXin.AccessToken _getAccessTokenYM()
{
log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType).Info("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + YMAppId + "&secret=" + YMSecret + "");
WebClient wc = new WebClient();
var stream = wc.OpenRead("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + YMAppId + "&secret=" + YMSecret + "");
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;
}
/// <summary>
/// 商户秘钥
/// </summary>
public static string YMSecret
{
get
{
var hc = HttpContext.Current;
if (MyApplication["YMSecret"] != null) return (string)MyApplication["YMSecret"];
var secret = new BLL.Common().getParm_Value("YMSecret", "c4771a672c379f1f83191d266e986bb8", "YMSecret");
//secret = "f271039e68a914fd1cd92cf00f79e20f";
MyApplication["YMSecret"] = secret;
return secret;
}
}
internal static byte[] getqm(int sprid1)
{
//throw new NotImplementedException();
var bll = new ERPUserBll();
string signstr = bll.GetSignation(sprid1);
if (string.IsNullOrEmpty(signstr))
{
string tn = bll.GetTrueNameById(sprid1);
if (string.IsNullOrEmpty(tn))
{
return null;
}
else
{
Bitmap b = new Bitmap(60, 20);
var g = Graphics.FromImage(b);
g.FillRectangle(Brushes.White, 0, 0, 60, 20);
g.DrawString(tn, new Font("宋体", 14), Brushes.Black, new PointF(2, 2));
MemoryStream ms = new MemoryStream();
b.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
}
else
{
string oapath = System.Configuration.ConfigurationManager.AppSettings["OAPath"];
if (Directory.Exists(oapath))
{
oapath += signstr;
if (File.Exists(oapath))
{
System.Drawing.Bitmap b = new System.Drawing.Bitmap(oapath);
MemoryStream ms = new MemoryStream();
b.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
else
{
return null;
}
}
else
{
return null;
}
}
}
#endregion
#region
/// <summary>
/// 艾滋病用
/// </summary>
public static string InfectionToken
{
get
{
var hc = HttpContext.Current;
if (MyApplication["InfectionToken"] != null) return (string)MyApplication["InfectionToken"];
var Token = new BLL.Common().getParm_Value("InfectionToken", "5324ae192cb68f112c07fb30a0aef9a7", "艾滋病防控微信Token");
MyApplication["InfectionToken"] = Token;
return Token;
}
}
public static string InfectionEncodingAESKey
{
get
{
var hc = HttpContext.Current;
if (MyApplication["InfectionEncodingAESKey"] != null) return (string)MyApplication["InfectionEncodingAESKey"];
var AESKey = new BLL.Common().getParm_Value("InfectionEncodingAESKey", "2vHQyZIgkt1dnLAeECtu80nssEXJ3hoX2CoIPdDTkIX", "艾滋病防控微信EncodingAESKey");
MyApplication["InfectionEncodingAESKey"] = AESKey;
return AESKey;
}
}
public static string InfectionAppId
{
get
{
var hc = HttpContext.Current;
if (MyApplication["InfectionAppId"] != null) return (string)MyApplication["InfectionAppId"];
var appid = new BLL.Common().getParm_Value("InfectionAppId", "wxc200f29cab08b4ca", "艾滋病防控微信AppId");
MyApplication["InfectionAppId"] = appid;
return appid;
}
}
/// <summary>
/// 艾滋病微信App秘钥
/// </summary>
public static string InfectionAppsecret
{
get
{
var hc = HttpContext.Current;
if (MyApplication["InfectionAppsecret"] != null) return (string)MyApplication["InfectionAppsecret"];
var appsecret = new BLL.Common().getParm_Value("InfectionAppsecret", "c4771a672c379f1f83191d266e986bb8", "艾滋病防控微信Appsecret");
MyApplication["InfectionAppsecret"] = appsecret;
return appsecret;
}
}
//gh_fdc67998c8e4
/// <summary>
/// /// <summary>
/// 疫苗公众号原始ID
/// </summary>
/// </summary>
public static string InfectionMyId
{
get
{
var hc = HttpContext.Current;
if (MyApplication["InfectionMyId"] != null) return (string)MyApplication["InfectionMyId"];
var appid = new BLL.Common().getParm_Value("InfectionMyId", "gh_f598661759c4", "InfectionMyId");
MyApplication["InfectionMyId"] = appid;
return appid;
}
}
public static string qrcode_ticketInfection(string recommend)
{
var token = getAccessTokenInfection;
var data = new
{
expire_seconds = 7200,
action_name = "QR_SCENE",
action_info = new
{
scene = new
{
scene_id = recommend
}
}
};
WebClient wc = new WebClient();
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
var result = wc.UploadData("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + token.access_token, System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(data)));
string jg = System.Text.Encoding.UTF8.GetString(result);
Dictionary<string, object> jsDict = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(jg);
if (jsDict.ContainsKey("ticket"))
{
return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + jsDict["ticket"].ToString();
//return jsDict["url"].ToString();
}
else
{
return "";
}
}
public static string jsapi_ticketInfection
{
get
{
HttpContext hc = HttpContext.Current;
if (MyApplication["jsapi_ticketInfection"] == null || MyApplication["AccessTokenInfection"] == null)
{
var token = _getjsapi_ticketInfection();
MyApplication["jsapi_ticketInfection"] = token;
MyApplication["jsapi_ticket_expires_inInfection"] = DateTime.Now.AddSeconds(7000);
return token;
}
else
{
DateTime dt = (DateTime)MyApplication["jsapi_ticket_expires_inInfection"];
if (dt >= DateTime.Now)
return MyApplication["jsapi_ticketInfection"].ToString();
else
{
var token = _getjsapi_ticketInfection();
MyApplication["jsapi_ticketInfection"] = token;
MyApplication["jsapi_ticket_expires_inInfection"] = DateTime.Now.AddSeconds(7000);
return token;
}
}
}
}
private static string _getjsapi_ticketInfection()
{
var token = getAccessTokenInfection;
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();
}
public static Models.WeiXin.AccessToken getAccessTokenInfection
{
get
{
HttpContext hc = HttpContext.Current;
if (MyApplication["AccessTokenInfection"] == null)
{
var token = _getAccessTokenInfection();
MyApplication["AccessTokenInfection"] = token;
MyApplication["access_token_expires_in_infection"] = DateTime.Now.AddSeconds(7000);
return token;
}
else
{
DateTime dt = (DateTime)MyApplication["access_token_expires_in_infection"];
if (dt >= DateTime.Now)
return MyApplication["AccessTokenInfection"] as Models.WeiXin.AccessToken;
else
{
var token = _getAccessTokenInfection();
MyApplication["AccessTokenInfection"] = token;
MyApplication["access_token_expires_in_infection"] = DateTime.Now.AddSeconds(7000);
return token;
}
}
}
}
private static Models.WeiXin.AccessToken _getAccessTokenInfection()
{
WebClient wc = new WebClient();
var stream = wc.OpenRead("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + InfectionAppId + "&secret=" + InfectionAppsecret + "");
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;
}
/// <summary>
/// 商户秘钥
/// </summary>
public static string InfectionSecret
{
get
{
var hc = HttpContext.Current;
if (MyApplication["InfectionSecret"] != null) return (string)MyApplication["InfectionSecret"];
var secret = new BLL.Common().getParm_Value("InfectionSecret", "f271039e68a914fd1cd92cf00f79e20f", "InfectionSecret");
MyApplication["InfectionSecret"] = secret;
return secret;
}
}
/// <summary>
/// 商户秘钥
/// </summary>
public static string InfectionMchId
{
get
{
var hc = HttpContext.Current;
if (MyApplication["InfectionMchId"] != null) return (string)MyApplication["InfectionMchId"];
var mchid = new BLL.Common().getParm_Value("InfectionMchId", "1485492392", "InfectionMchId");
MyApplication["InfectionMchId"] = mchid;
return mchid;
}
}
#endregion
#region
/// <summary>
/// 艾滋病小程序App秘钥
/// </summary>
public static string InfectionWXAppId
{
get
{
var hc = HttpContext.Current;
if (MyApplication["InfectionAppId"] != null) return (string)MyApplication["InfectionWXAppId"];
var appid = new BLL.Common().getParm_Value("InfectionWXAppId", "wx40da82259f98861d", "艾滋病防控小程序AppId");
MyApplication["InfectionWXAppId"] = appid;
return appid;
}
}
/// <summary>
/// 艾滋病小程序App秘钥
/// </summary>
public static string InfectionWXAppsecret
{
get
{
var hc = HttpContext.Current;
if (MyApplication["InfectionAppsecret"] != null) return (string)MyApplication["InfectionWXAppsecret"];
var appsecret = new BLL.Common().getParm_Value("InfectionWXAppsecret", "56fc56ebbe59358d91375328f44a6397", "艾滋病防控小程序Appsecret");
MyApplication["InfectionWXAppsecret"] = appsecret;
return appsecret;
}
}
#endregion
#region
public static bool checkpower(string key)
{
if (HttpContext.Current.Session["QuanXian"] != null)
{
string qxs = HttpContext.Current.Session["QuanXian"].ToString();
if (!string.IsNullOrEmpty(qxs))
{
return qxs.Contains(key);
}
else
{
return false;
}
}
else
{
return false;
}
}
#endregion
/// <summary>
/// 微信支付Model
/// </summary>
public class WxZiFu
{
public string appId { get; set; }
public string timeStamp { get; set; }
public string nonceStr { get; set; }
public string package { get; set; }
public string signType { get; set; }
public string paySign { get; set; }
public int State { get; set; }
}
}
}