123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Text.RegularExpressions;
- namespace LYFZ.WXLibrary
- {
- /// <summary>
- /// 常用处理类
- /// </summary>
- public class CommonHandleClass
- {
- public CommonHandleClass()
- {
- }
- /// <summary>
- /// 微信接口日志目录路径
- /// </summary>
- public static string WeiXinlogsPath
- {
- get { return LYFZ.WinAPI.CustomPublicMethod.BasePath + "\\Logs\\WeiXinlogs\\"; }
- }
- private static bool isOpenLogs = false;
- /// <summary>
- /// 是否开启日志功能
- /// </summary>
- public static bool IsOpenLogs
- {
- get { return CommonHandleClass.isOpenLogs; }
- set { CommonHandleClass.isOpenLogs = value; }
- }
- /// <summary>
- /// 写日志(用于跟踪)
- /// </summary>
- public static void WriteLog(string strMsg, string ddomaine="-1", List<string> _LogsDongleDomainList = null)
- {
- try
- {
- if ((_LogsDongleDomainList != null && IsOpenLogs && _LogsDongleDomainList.Contains(ddomaine)) || ddomaine == "-1" || (ddomaine == "-2" && IsOpenLogs))
- {
- string filename = LYFZ.WinAPI.CustomPublicMethod.BasePath + "\\Logs\\WeiXinlogs.txt";
- if (!Directory.Exists(System.IO.Path.GetDirectoryName(filename)))
- Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filename));
- StreamWriter sr = null;
- try
- {
- if (!File.Exists(filename))
- {
- sr = File.CreateText(filename);
- }
- else
- {
- sr = File.AppendText(filename);
- }
- sr.WriteLine("【"+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+"】"+strMsg);
- }
- catch
- {
- }
- finally
- {
- if (sr != null)
- sr.Close();
- }
- }
- }
- catch { }
- }
- public static string GetWeiXinlogs()
- { string filename = LYFZ.WinAPI.CustomPublicMethod.BasePath + "\\Logs\\WeiXinlogs.txt";
- if (System.IO.File.Exists(filename))
- {
- return System.IO.File.ReadAllText(filename).Replace("\r\n", "<br />").Replace("\n", "<br />");
- }
- else {
- return "暂无日志";
- }
- }
- public static bool DeleteWeiXinlogs()
- {
- try
- {
- string filename = LYFZ.WinAPI.CustomPublicMethod.BasePath + "\\Logs\\WeiXinlogs.txt";
- if (System.IO.File.Exists(filename))
- {
- System.IO.File.Delete(filename);
- return true;
- }
- else
- {
- return false;
- }
- }
- catch { return false; }
- }
- //URL参数添加/修改
- public static string Fun_AddQueryToURL(string url, string key, string value)
- {
- int fragPos = url.LastIndexOf("#");
- string fragment = string.Empty;
- if (fragPos > -1)
- {
- fragment = url.Substring(fragPos);
- url = url.Substring(0, fragPos);
- }
- int querystart = url.IndexOf("?");
- if (querystart < 0)
- {
- url += "?" + key + "=" + value;
- }
- else
- {
- Regex reg = new Regex(@"(?<=[&\?])" + key + @"=[^\s&#]*", RegexOptions.Compiled);
- if (reg.IsMatch(url))
- url = reg.Replace(url, key + "=" + value);
- else
- url += "&" + key + "=" + value;
- }
- return url + fragment;
- }
- //URL参数删除
- public static string Fun_RemoveQueryFromURL(string url, string key)
- {
- Regex reg = new Regex(@"[&\?]" + key + @"=[^\s&#]*&?", RegexOptions.Compiled);
- return reg.Replace(url, new MatchEvaluator(PutAwayGarbageFromURL));
- }
- private static string PutAwayGarbageFromURL(Match match)
- {
- string value = match.Value;
- if (value.EndsWith("&"))
- {
- return value.Substring(0, 1);
- }
- else
- {
- return string.Empty;
- }
- }
- public static string GetNetUrl(string ServerHostOrIP, int PortNumber)
- {
- if (ServerHostOrIP.Contains(":"))
- {
- return "http://" + ServerHostOrIP;
- }
- else
- {
- return "http://" + ServerHostOrIP + ":" + PortNumber.ToString();
- }
- }
- static List<string> _MicroLetterEventList = new List<string>();
- /// <summary>
- /// 微信事件列表
- /// </summary>
- public static List<string> MicroLetterEventList
- {
- get { return _MicroLetterEventList; }
- }
- /// <summary>
- /// 关键字集合
- /// </summary>
- static List<KeywordObjects> _KeywordObjectsList = new List<KeywordObjects>();
- /// <summary>
- /// 关键字集合
- /// </summary>
- public static List<KeywordObjects> KeywordObjectsList
- {
- get { return _KeywordObjectsList; }
- set { _KeywordObjectsList = value; }
- }
- public static string GetFunctionCode(string keyword)
- {
-
- KeywordObjects key = KeywordObjectsList.Find(k=>k.Keyword.ToLower()==keyword.ToLower());
- if (key != null && key.FunctionCode.Trim().Length > 0)
- {
- return key.FunctionCode;
- }
- else {
- return "null";
- }
- }
- }
- }
|