GlobalCache.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. using LYFZ.WXLibrary;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. namespace LYFZ.WeixinServers
  8. {
  9. /// <summary>
  10. /// 全局性缓存;
  11. /// </summary>
  12. public static class GlobalCache
  13. {
  14. #region 数据
  15. // 几个重要的缓存数据表;
  16. public static DataTable DT_AuthorizeApp { get; set; } = new DataTable();
  17. public static DataTable DT_IndustryInfo { get; set; } = new DataTable();
  18. public static DataTable DT_EnterpriseInfo { get; set; } = new DataTable();
  19. public static DataTable DT_AppTypeInfo { get; set; } = new DataTable();
  20. public static DataTable DT_ServerInfo { get; set; } = new DataTable();
  21. public static DataTable DT_EnterpriseAppEntry { get; set; } = new DataTable();
  22. public static DataTable DT_KfParticularsInfo { get; set; } = new DataTable();
  23. #region dal
  24. // dal;
  25. public static LoginDal.app_authorization DalAppAuthorize = new LoginDal.app_authorization();
  26. public static LoginDal.industry DalIndustry = new LoginDal.industry();
  27. public static LoginDal.enterprise DalEnterprise = new LoginDal.enterprise();
  28. public static LoginDal.app_type DalAppType = new LoginDal.app_type();
  29. public static LoginDal.server_entry DalServer = new LoginDal.server_entry();
  30. public static LoginDal.enterprise_app_entry DalEnterpriseAppEntry = new LoginDal.enterprise_app_entry();
  31. public static LoginDal.kf_particulars DalKfParticulars = new LoginDal.kf_particulars();
  32. #endregion
  33. #endregion
  34. #region 刷新缓存数据
  35. public static void Refresh_Industry()
  36. {
  37. DataSet ds = DalIndustry.GetAllList("id");
  38. lock(DT_IndustryInfo)
  39. {
  40. try
  41. {
  42. DT_IndustryInfo.Rows.Clear();
  43. DT_IndustryInfo.Dispose();
  44. DT_IndustryInfo = null;
  45. }
  46. catch
  47. {
  48. }
  49. DT_IndustryInfo = ds.Tables[0];
  50. DT_IndustryInfo.PrimaryKey = new DataColumn[] { DT_IndustryInfo.Columns["ind_name"] };
  51. }
  52. }
  53. public static void Refresh_AppType()
  54. {
  55. DataSet ds = DalAppType.GetAllList("id");
  56. lock (DT_AppTypeInfo)
  57. {
  58. try
  59. {
  60. DT_AppTypeInfo.Rows.Clear();
  61. DT_AppTypeInfo.Dispose();
  62. DT_AppTypeInfo = null;
  63. }
  64. catch
  65. {
  66. }
  67. DT_AppTypeInfo = ds.Tables[0];
  68. DT_AppTypeInfo.PrimaryKey = new DataColumn[] { DT_AppTypeInfo.Columns["app_type_name"] };
  69. }
  70. }
  71. public static void Refresh_MsgServer()
  72. {
  73. DataSet ds = DalServer.GetAllList("id");
  74. lock (DT_ServerInfo)
  75. {
  76. try
  77. {
  78. DT_ServerInfo.Rows.Clear();
  79. DT_ServerInfo.Dispose();
  80. DT_ServerInfo = null;
  81. }
  82. catch
  83. {
  84. }
  85. DT_ServerInfo = ds.Tables[0];
  86. DT_ServerInfo.PrimaryKey = new DataColumn[] { DT_ServerInfo.Columns["server_ip"] };
  87. }
  88. }
  89. public static void Refresh_Enterprise()
  90. {
  91. DataSet ds = DalEnterprise.GetAllList("id");
  92. lock (DT_EnterpriseInfo)
  93. {
  94. try
  95. {
  96. DT_EnterpriseInfo.Rows.Clear();
  97. DT_EnterpriseInfo.Dispose();
  98. DT_EnterpriseInfo = null;
  99. }
  100. catch
  101. {
  102. }
  103. DT_EnterpriseInfo = ds.Tables[0];
  104. DT_EnterpriseInfo.PrimaryKey = new DataColumn[] { DT_EnterpriseInfo.Columns["ent_name"] };
  105. }
  106. }
  107. public static void Refresh_EntAppEntry()
  108. {
  109. DataSet ds = DalEnterpriseAppEntry.GetAllList("id");
  110. lock (DT_EnterpriseAppEntry)
  111. {
  112. try
  113. {
  114. DT_EnterpriseAppEntry.Rows.Clear();
  115. DT_EnterpriseAppEntry.Dispose();
  116. DT_EnterpriseAppEntry = null;
  117. }
  118. catch
  119. {
  120. }
  121. DT_EnterpriseAppEntry = ds.Tables[0];
  122. DT_EnterpriseAppEntry.PrimaryKey = new DataColumn[] { DT_EnterpriseAppEntry.Columns["id"] };
  123. }
  124. }
  125. public static void Refresh_kfParticulars()
  126. {
  127. DataSet ds = DalKfParticulars.GetAllList("id");
  128. lock (DT_KfParticularsInfo)
  129. {
  130. try
  131. {
  132. DT_KfParticularsInfo.Rows.Clear();
  133. DT_KfParticularsInfo.Dispose();
  134. DT_KfParticularsInfo = null;
  135. }
  136. catch
  137. {
  138. }
  139. DT_KfParticularsInfo = ds.Tables[0];
  140. DT_KfParticularsInfo.PrimaryKey = new DataColumn[] { DT_KfParticularsInfo.Columns["kf_account"] };
  141. }
  142. }
  143. public static void Refresh_AppAuth()
  144. {
  145. DataSet ds = DalAppAuthorize.GetAllList("id");
  146. lock (DT_AuthorizeApp)
  147. {
  148. try
  149. {
  150. DT_AuthorizeApp.Rows.Clear();
  151. DT_AuthorizeApp.Dispose();
  152. DT_AuthorizeApp = null;
  153. }
  154. catch
  155. {
  156. }
  157. DT_AuthorizeApp = ds.Tables[0];
  158. DT_AuthorizeApp.PrimaryKey = new DataColumn[] { DT_AuthorizeApp.Columns["app_id"] };
  159. // 添加到字典中;
  160. Refresh_Dictionary_AppAuth(DT_AuthorizeApp);
  161. }
  162. }
  163. #region appId增删改查函数
  164. public static void Refresh_Dictionary_AppAuth(DataTable DT_AuthorizeApp)
  165. {
  166. foreach (DataRow row in DT_AuthorizeApp.Rows)
  167. {
  168. LoginModel.app_authorization model = DalAppAuthorize.DataRowToModel(row);
  169. AddAuthorizedAPPIDHand(model);
  170. }
  171. }
  172. public static void AddAuthorizedAPPIDHand(LoginModel.app_authorization model)
  173. {
  174. lock (LYFZ.WXLibrary.OpenPlatformConfig.AuthorizedAPPIDHandleList)
  175. {
  176. if (!LYFZ.WXLibrary.OpenPlatformConfig.AuthorizedAPPIDHandleList.ContainsKey(model.app_id) && model.authorize_status == 1)
  177. {
  178. LYFZ.WXLibrary.AuthorizedAPPIDHandle authAPPIDHandle =
  179. new LYFZ.WXLibrary.AuthorizedAPPIDHandle(model.authorization_info, "CompanyName", model.authorize_status, model.refresh_token_time);
  180. authAPPIDHandle.EventEvent_AuthorizerRefreshToken += authAPPIDHandle_EventEvent_AuthorizerRefreshToken;
  181. LYFZ.WXLibrary.OpenPlatformConfig.AuthorizedAPPIDHandleList.Add(model.app_id, authAPPIDHandle);
  182. }
  183. }
  184. }
  185. static void authAPPIDHandle_EventEvent_AuthorizerRefreshToken(EventAuthorizerRefreshToken e)
  186. {
  187. LoginModel.app_authorization model = (LoginModel.app_authorization)DalAppAuthorize.GetModelObject("authorizer_appid", e.Appid);
  188. if (model != null && model.id > 0)
  189. {
  190. var authorizationObj = LYFZ.Weixin.SDK.BasicAPI.JsonToDynamic(model.authorization_info);
  191. authorizationObj.authorization_info.authorizer_access_token = e.Authorizer_access_token;
  192. authorizationObj.authorization_info.expires_in = e.Expires_in;
  193. authorizationObj.authorization_info.authorizer_refresh_token = e.Authorizer_refresh_token;
  194. model.refresh_token_time = e.RefreshTokenTime;
  195. model.authorizer_access_token = e.Authorizer_access_token;
  196. model.expires_in = e.Expires_in;
  197. model.authorizer_refresh_token = e.Authorizer_refresh_token;
  198. model.authorization_info = authorizationObj.ToString();
  199. DalAppAuthorize.Update(model);
  200. }
  201. }
  202. public static void RemoveAppObject(string authorizer_Appid, string CompanyName)
  203. {
  204. LYFZ.WXLibrary.AuthorizedAPPIDHandle authAPPIDHandle = null;
  205. if (OpenPlatformConfig.AuthorizedAPPIDHandleList.ContainsKey(authorizer_Appid))
  206. {
  207. authAPPIDHandle = OpenPlatformConfig.AuthorizedAPPIDHandleList[authorizer_Appid];
  208. //authAPPIDHandle.JMGDomainName = JMGDomainName;
  209. authAPPIDHandle.CompanyName = CompanyName;
  210. LoginModel.app_authorization model =
  211. (LoginModel.app_authorization)DalAppAuthorize.GetModelObject("authorizer_appid", authorizer_Appid);
  212. if (model != null && model.id > 0)
  213. {
  214. //model.JMGDomainName = authAPPIDHandle.JMGDomainName;
  215. // model.CompanyName = authAPPIDHandle.CompanyName;
  216. //LYFZ.WXLibrary.CommonHandleClass.WriteLog("更新授权处理对象中的加密锁域名:authorizer_Appid=" + authorizer_Appid + " & JMGDomainName=" + Authorization_infoModel.JMGDomainName, "-2", Global.LogsDongleDomainList);
  217. // Authorization_infoDal.Update(Authorization_infoModel);
  218. // UpdateAuthorizationInfoDaTable(authorizer_Appid, Authorization_infoModel);
  219. }
  220. }
  221. }
  222. public static void UpdateAppObject()
  223. {
  224. }
  225. #endregion
  226. #endregion
  227. #region 函数
  228. public static LoginModel.enterprise GetEnterpriseModel(string ent_id)
  229. {
  230. bool find = false;
  231. LoginModel.enterprise model = new LoginModel.enterprise();
  232. foreach (DataRow dr in DT_EnterpriseInfo.Rows)
  233. {
  234. model = DalEnterprise.DataRowToModel(dr);
  235. if (model.ent_id == ent_id)
  236. {
  237. find = true;
  238. break;
  239. }
  240. }
  241. return find ? model : null;
  242. }
  243. #region 公用普通函数
  244. public static string GuidString()
  245. {
  246. return Guid.NewGuid().ToString("N");
  247. }
  248. public static LoginModel.user GetUserInfo(string strUser, string strUserPsw)
  249. {
  250. LoginDal.user dal = new LoginDal.user();
  251. LoginModel.user model = new LoginModel.user();
  252. DataSet ds = dal.GetList("user_account ='" + strUser + "' and user_psw = '" + strUserPsw + "'", "id");
  253. if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
  254. {
  255. return model = dal.DataRowToModel(ds.Tables[0].Rows[0]);
  256. }
  257. else
  258. {// 没有该账号记录;
  259. return null;
  260. }
  261. }
  262. public static long DateTimeToTimeStamp(DateTime dt)
  263. {
  264. DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  265. return (long)(dt - startTime).TotalSeconds;// 相差秒数,则为Unix时间戳;
  266. }
  267. public static long DateTimeToJSTimeStamp(DateTime dt)
  268. {
  269. DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  270. return (long)(dt - startTime).TotalMilliseconds;// 相差秒数,则为JavaScript时间戳;
  271. }
  272. public static DateTime JSTimeStampToDateTime(long jsTimeStamp)
  273. {
  274. //DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1)).AddMilliseconds(jsTimeStamp);
  275. //DateTime dt = startTime.AddMilliseconds(jsTimeStamp);
  276. return TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddMilliseconds(jsTimeStamp);
  277. }
  278. public static DateTime TimeStampToDateTime(long timeStamp)
  279. {
  280. return TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddSeconds(timeStamp);
  281. }
  282. #endregion
  283. #endregion
  284. }
  285. }