GetAccessToken.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using LitJson;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using WxPayAPI;
  8. namespace LeaRun.Application.Web.Common
  9. {
  10. public class GetAccessToken
  11. {
  12. #region 获取Access_Token
  13. public static String GetAcces_token()
  14. {
  15. //变量定义区
  16. long timeOut = 0;
  17. //token
  18. String acccess_Token = "";
  19. //超时时间
  20. long time = 0;
  21. //1查询数据库
  22. String sql = "Select AccessToken,Time,AppId,Secret from BCS_WxAccessToken ";
  23. DataTable dt = SqlHelper.ExecuteDataTable(sql, CommandType.Text, null);
  24. //1.1判断是否有数据
  25. if (dt.Rows.Count > 0)
  26. {
  27. //1.1.2 有数据判断时间是否超时
  28. if (dt.Rows[0]["Time"].ToString() == "" || dt.Rows[0]["AccessToken"].ToString() == "")
  29. {
  30. String token = GetToken(dt.Rows[0]["AppId"].ToString(), dt.Rows[0]["Secret"].ToString());
  31. acccess_Token = token.Split('|')[0];
  32. timeOut = Convert.ToInt64(token.Split('|')[1].ToString());
  33. //将token存入数据库
  34. time = GetLongTime();
  35. UpdateToken(acccess_Token, (time + timeOut).ToString(), dt.Rows[0]["AppId"].ToString());
  36. }
  37. else
  38. {
  39. timeOut = Convert.ToInt64(dt.Rows[0]["Time"].ToString());
  40. //获取时间戳
  41. time = GetLongTime();
  42. if (timeOut > time)
  43. {
  44. //没有超时则取出来使用
  45. acccess_Token = dt.Rows[0]["AccessToken"].ToString();
  46. }
  47. else
  48. {
  49. //时间超时获取最新的Access_token,并更新数据库中的token和超时时间
  50. String token = GetToken(dt.Rows[0]["AppId"].ToString(), dt.Rows[0]["Secret"].ToString());
  51. acccess_Token = token.Split('|')[0];
  52. timeOut = Convert.ToInt64(token.Split('|')[1].ToString());
  53. //更新token
  54. time = GetLongTime();
  55. UpdateToken(acccess_Token, (time + timeOut).ToString(), dt.Rows[0]["AppId"].ToString());
  56. }
  57. }
  58. }
  59. else
  60. {
  61. //1.1.1 没有数据执行Insert
  62. String token = GetToken(dt.Rows[0]["AppId"].ToString(), dt.Rows[0]["Secret"].ToString());
  63. acccess_Token = token.Split('|')[0];
  64. timeOut = Convert.ToInt64(token.Split('|')[1].ToString());
  65. //将token存入数据库
  66. time = GetLongTime();
  67. InsertToken(acccess_Token, (time + timeOut).ToString(), dt.Rows[0]["AppId"].ToString());
  68. }
  69. return acccess_Token;
  70. }
  71. private static long GetLongTime()
  72. {
  73. DateTime a = DateTime.Now;
  74. DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0));
  75. long time = (long)(a - startTime).TotalSeconds;
  76. return time;
  77. }
  78. #endregion
  79. public static int UpdateToken(String token, String timeOut, String APPID)
  80. {
  81. String sql = "update BCS_WxAccessToken Set AccessToken = '" + token + "',time = '" + timeOut + "' where AppId ='" + APPID + "'";//"insert into BCS_WxAccessToken (AccessToken,Time,AppId) values ('" + token + "'" + timeOut + "',"+WxPayConfig.APPID+")";
  82. return SqlHelper.ExecuteNonQuery(sql, CommandType.Text, null);
  83. }
  84. //将Token信息添加到数据库
  85. public static int InsertToken(String token, String timeOut, String APPID)
  86. {
  87. String sql = "insert into BCS_WxAccessToken (AccessToken,Time,AppId) values ('" + token + "'," + timeOut + ",'" + APPID + "')";
  88. return SqlHelper.ExecuteNonQuery(sql, CommandType.Text, null);
  89. }
  90. public static string GetToken(String APPID, String APPSECRET)
  91. {
  92. String access_token;
  93. long expires_in;
  94. //构造获取https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxdc0ee0e75b3c6188&secret=f878802a0a1ca973659deb62010ee4a4
  95. //WxPayData data = new WxPayData();
  96. //data.SetValue("grant_type", "client_credential");
  97. //data.SetValue("appid", APPID);
  98. //data.SetValue("secret", APPSECRET);
  99. String data = "appid="+APPID+"&grant_type=client_credential&secret="+APPSECRET;
  100. string url = "https://api.weixin.qq.com/cgi-bin/token?" + data;
  101. //Log.Debug(this.GetType().ToString(), "陈新杰 : " + url);
  102. //请求url以获取数据
  103. string result = HttpService.Get(url);
  104. //Log.Debug("测试用:", "GetOpenidAndAccessTokenFromCode response : " + result);
  105. //保存access_token,用于收货地址获取
  106. JsonData jd = JsonMapper.ToObject(result);
  107. access_token = (string)jd["access_token"];
  108. expires_in = Convert.ToInt64(jd["expires_in"].ToString());//超时时间
  109. //data.SetValue("nickname", nickname);
  110. return access_token + "|" + expires_in;
  111. }
  112. }
  113. }