123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- using LYFZ.Weixin.SDK;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- namespace LYFZ.WXLibrary
- {
- /// <summary>
- /// 开放平台 配置信息
- /// </summary>
- public class OpenPlatformConfig
- {
- public OpenPlatformConfig()
- {
- }
- /// <summary>
- /// 微信后台专用测试公众号APPID (wx570bc396a51b8ff8)
- /// </summary>
- public static string MicroLetterTestAPPID { get; } = "wx570bc396a51b8ff8";
- /// <summary>
- /// 微信后台专用测试公众号 (gh_76705a519b76)
- /// </summary>
- public static string MicroLetterTestUsername { get; } = "gh_76705a519b76";
- /// <summary>
- /// 授权回调地址 AuthorizeCallback
- /// </summary>
- public static string Redirect_uri { get; set; } = "http://kf.lyfz.net/AuthorizeCallback.aspx";
- /// <summary>
- /// 第三方开放平台的AppID
- /// </summary>
- public static string OpenAppID { get; set; } = "wxab4873842bdb30b6";
- /// <summary>
- /// 第三方开放平台的AppSecret
- /// </summary>
- public static string OpenAppSecret { get; set; } = "dcda055876be4b0e6133058216aaa930";
- /// <summary>
- /// 公众号消息校验Token
- /// </summary>
- public static string OpenToken { get; set; } = "WMHEDRD4NWZACPCXZ8JYXOOC";
- /// <summary>
- /// 公众号消息加解密Key
- /// </summary>
- public static string OpenEncodingAESKey { get; set; } = "BZy405POqT7Jg4QgSyyAEq0Ve4Ih4QyNCSJMswSG6kI";
- static string _component_verify_ticket = "";
- /// <summary>
- /// Component_verify_ticket
- /// </summary>
- 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>();
- /// <summary>
- /// 已授权公众号的授权信息处理集合
- /// </summary>
- public static Dictionary<string, AuthorizedAPPIDHandle> AuthorizedAPPIDHandleList
- {
- get { return OpenPlatformConfig._AuthorizedAPPIDHandleList; }
- set { OpenPlatformConfig._AuthorizedAPPIDHandleList = value; }
- }
- /// <summary>
- /// 获取令牌时的时间
- /// </summary>
- private static DateTime GetAccessToken_Time;
- /// <summary>
- /// 过期时间为7200秒
- /// </summary>
- private static int Expires_Period = 7200;
- private static string mComponent_Access_Token = "";
- /// <summary>
- /// 获取新的有效Component_Access_Token令牌
- /// </summary>
- 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;
- }
- }
- /// <summary>
- ///
- /// </summary>
- static string _pre_auth_code = "";
- /// <summary>
- /// 获取预授权码pre_auth_code
- /// 该API用于获取预授权码。预授权码用于公众号授权时的第三方平台方安全验证。
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 获取预授权码pre_auth_code
- /// </summary>
- /// <param name="appId"></param>
- /// <param name="appSecret"></param>
- /// <returns></returns>
- 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 "";
- }
- /// <summary>
- /// 预授权码Pre_auth_code过期时间为600秒
- /// </summary>
- private static int Expires_Period_PreAuthCode = 600;
- /// <summary>
- /// 获取预授权码Pre_auth_code时的时间
- /// </summary>
- private static DateTime Get_PreAuthCode_Time;
- /// <summary>
- /// 判断预授权码Pre_auth_code是否过期 (有效期为)
- /// </summary>
- /// <returns>bool</returns>
- 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;
- }
- /// <summary>
- /// 清除mAccessToken
- /// </summary>
- public static void ClearAccessToken()
- {
- mComponent_Access_Token = "";
- }
- /// <summary>
- /// 获取开放平台的接口令牌
- /// </summary>
- /// <param name="appId"></param>
- /// <param name="appSecret"></param>
- /// <returns></returns>
- 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 formatmsg = "{"
- + "\"component_appid\":\"" + sAppID + "\" ,"
- + "\"component_appsecret\": \"" + sAppSecret + "\","
- + "\"component_verify_ticket\": \"" + component_verify_ticket + "\" "
- +"\"}";*/
- //获取第三方平台access_token
- 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 "";
- }
- /// <summary>
- /// 判断Access_token是否过期
- /// </summary>
- /// <returns>bool</returns>
- private static bool HasExpired()
- {
- if (GetAccessToken_Time != null)
- {
- //过期时间,允许有一定的误差,五分钟。获取时间消耗
- if (DateTime.Now > GetAccessToken_Time.AddSeconds(Expires_Period).AddSeconds(-300))
- {
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// 是否转到多客户
- /// </summary>
- /// <param name="message"></param>
- /// <returns></returns>
- 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;
- }
- }
- }
|