123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- using LYFZ.Weixin.SDK;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- namespace LYFZ.WXLibrary
- {
-
-
-
- public class OpenPlatformConfig
- {
- public OpenPlatformConfig()
- {
- }
-
-
-
- public static string MicroLetterTestAPPID { get; } = "wx570bc396a51b8ff8";
-
-
-
- public static string MicroLetterTestUsername { get; } = "gh_76705a519b76";
-
-
-
- public static string Redirect_uri { get; set; } = "http://kf.lyfz.net/AuthorizeCallback.aspx";
-
-
-
- public static string OpenAppID { get; set; } = "wxab4873842bdb30b6";
-
-
-
- public static string OpenAppSecret { get; set; } = "dcda055876be4b0e6133058216aaa930";
-
-
-
- public static string OpenToken { get; set; } = "WMHEDRD4NWZACPCXZ8JYXOOC";
-
-
-
- public static string OpenEncodingAESKey { get; set; } = "BZy405POqT7Jg4QgSyyAEq0Ve4Ih4QyNCSJMswSG6kI";
- static string _component_verify_ticket = "";
-
-
-
- public static string Component_verify_ticket
- {
- get
- {
- if (string.IsNullOrEmpty(OpenPlatformConfig._component_verify_ticket))
- {
- OpenPlatformConfig._component_verify_ticket = OpenPlatformConfig.ReadComponent_verify_ticket();
- }
- return OpenPlatformConfig._component_verify_ticket;
- }
- set { OpenPlatformConfig._component_verify_ticket = value; }
- }
- static string Component_verify_ticketFilename = LYFZ.WinAPI.CustomPublicMethod.BasePath + "\\Temp\\Component_verify_ticket.txt";
- public static bool SaveComponent_verify_ticket(string _Component_verify_ticket)
- {
- try
- {
- if (!Directory.Exists(System.IO.Path.GetDirectoryName(Component_verify_ticketFilename)))
- Directory.CreateDirectory(System.IO.Path.GetDirectoryName(Component_verify_ticketFilename));
- File.WriteAllText(Component_verify_ticketFilename, _Component_verify_ticket, Encoding.UTF8);
- }
- catch
- {
- return false;
- }
- return true;
- }
- public static string ReadComponent_verify_ticket()
- {
- try
- {
- if (System.IO.File.Exists(Component_verify_ticketFilename))
- {
- return File.ReadAllText(Component_verify_ticketFilename, Encoding.UTF8);
- }
- else
- {
- return "";
- }
- }
- catch
- {
- return "";
- }
- }
- static Dictionary<string, AuthorizedAPPIDHandle> _AuthorizedAPPIDHandleList = new Dictionary<string, AuthorizedAPPIDHandle>();
-
-
-
- public static Dictionary<string, AuthorizedAPPIDHandle> AuthorizedAPPIDHandleList
- {
- get { return OpenPlatformConfig._AuthorizedAPPIDHandleList; }
- set { OpenPlatformConfig._AuthorizedAPPIDHandleList = value; }
- }
-
-
-
- private static DateTime GetAccessToken_Time;
-
-
-
- private static int Expires_Period = 7200;
- private static string mComponent_Access_Token = "";
-
-
-
- public static string Component_Access_Token
- {
- get
- {
-
- if (string.IsNullOrEmpty(mComponent_Access_Token) || HasExpired())
- {
-
- LYFZ.WXLibrary.CommonHandleClass.WriteLog("开始请求获取新的有效Component_Access_Token令牌", "-2");
- mComponent_Access_Token = GetComponent_AccessToken(OpenAppID, OpenAppSecret, Component_verify_ticket);
- }
- return mComponent_Access_Token;
- }
- }
-
-
-
- static string _pre_auth_code = "";
-
-
-
-
- public static string Pre_auth_code
- {
- get
- {
-
- if (string.IsNullOrEmpty(_pre_auth_code) || HasExpiredPreAuthCode())
- {
-
- LYFZ.WXLibrary.CommonHandleClass.WriteLog("开始请求获取预授权码pre_auth_code", "-2");
- _pre_auth_code = GetComponent_PreAuthCode(OpenAppID);
- }
- return _pre_auth_code;
- }
- }
-
-
-
-
-
-
- private static string GetComponent_PreAuthCode(string appId)
- {
- try
- {
- Dictionary<string, string> dic = new Dictionary<string, string>
- {
- {"component_appid",appId},
- };
- string json = (new System.Web.Script.Serialization.JavaScriptSerializer()).Serialize(dic);
- string retmsg = "";
- var retObj = BasicAPI.GetApi_Component_PreAuthCode(OpenPlatformConfig.Component_Access_Token, json, out retmsg);
- LYFZ.WXLibrary.CommonHandleClass.WriteLog("请求获取预授权码返回信息:" + retmsg, "-2");
- if (retObj != null)
- {
- string retPreAuthCode = retObj.pre_auth_code.Trim();
- if (retPreAuthCode.Trim().Length > 0)
- {
- Get_PreAuthCode_Time = DateTime.Now;
- Expires_Period_PreAuthCode = Convert.ToInt32(retObj.expires_in);
- return retPreAuthCode;
- }
- else
- {
- Get_PreAuthCode_Time = DateTime.MinValue;
- }
- }
- else
- {
- Get_PreAuthCode_Time = DateTime.MinValue;
- LYFZ.WXLibrary.CommonHandleClass.WriteLog("获取预授权码pre_auth_code时出错:" + retmsg, "-2");
- }
- }
- catch (Exception ex)
- {
- Get_PreAuthCode_Time = DateTime.MinValue;
- LYFZ.WXLibrary.CommonHandleClass.WriteLog("请求获取预授权码时出错:" + ex.Message, "-2");
- }
- return "";
- }
-
-
-
- private static int Expires_Period_PreAuthCode = 600;
-
-
-
- private static DateTime Get_PreAuthCode_Time;
-
-
-
-
- private static bool HasExpiredPreAuthCode()
- {
- if (Get_PreAuthCode_Time != null)
- {
-
- if (DateTime.Now > Get_PreAuthCode_Time.AddSeconds(Expires_Period_PreAuthCode).AddSeconds(-520))
- {
- return true;
- }
- }
- return false;
- }
-
-
-
- public static void ClearAccessToken()
- {
- mComponent_Access_Token = "";
- }
-
-
-
-
-
-
- private static string GetComponent_AccessToken(string appId, string appSecret, string component_verify_ticket)
- {
- try
- {
- Dictionary<string, string> dic = new Dictionary<string, string>
- {
- {"component_appid",appId},
- {"component_appsecret",appSecret},
- {"component_verify_ticket",component_verify_ticket}
- };
- string json = (new System.Web.Script.Serialization.JavaScriptSerializer()).Serialize(dic);
-
-
- string retmsg = "";
- var retObj = BasicAPI.GetApi_Component_Token(json, out retmsg);
- LYFZ.WXLibrary.CommonHandleClass.WriteLog("请求获取新的有效Component_Access_Token令牌返回信息:" + retmsg, "-2");
- if (retObj != null)
- {
- string retComponent_access_token = retObj.component_access_token.Trim();
- if (retComponent_access_token.Trim().Length > 0)
- {
- GetAccessToken_Time = DateTime.Now;
- Expires_Period = Convert.ToInt32(retObj.expires_in);
- return retComponent_access_token;
- }
- else
- {
- GetAccessToken_Time = DateTime.MinValue;
- }
- }
- else
- {
- GetAccessToken_Time = DateTime.MinValue;
- LYFZ.WXLibrary.CommonHandleClass.WriteLog("获取开放平台的接口令牌时出错:" + retmsg, "-2");
- }
- }
- catch (Exception ex)
- {
- GetAccessToken_Time = DateTime.MinValue;
- LYFZ.WXLibrary.CommonHandleClass.WriteLog("请求获取新的有效Component_Access_Token令牌时出错:" + ex.Message, "-2");
- }
- return "";
- }
-
-
-
-
- private static bool HasExpired()
- {
- if (GetAccessToken_Time != null)
- {
-
- if (DateTime.Now > GetAccessToken_Time.AddSeconds(Expires_Period).AddSeconds(-300))
- {
- return true;
- }
- }
- return false;
- }
-
-
-
-
-
- public static bool IsTransmitService(string message)
- {
- bool ret = false;
- if (message.ToLower().Contains("kf")
- || message.ToLower().Contains("客服")
- || message.ToLower().Contains("你好")
- || message.ToLower().Contains("您好")
- || message.ToLower().Contains("投诉")
- || message.ToLower().Contains("在吗")
- || message.ToLower().Contains("ni hao")
- || message.ToLower().Contains("ning hao")
- || message.ToLower().Contains("请问"))
- {
- ret = true;
- }
- return ret;
- }
- }
- }
|