using LitJson; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Web; using WxPayAPI; namespace LeaRun.Application.Web.Common { public class GetAccessToken { #region 获取Access_Token public static String GetAcces_token() { //变量定义区 long timeOut = 0; //token String acccess_Token = ""; //超时时间 long time = 0; //1查询数据库 String sql = "Select AccessToken,Time,AppId,Secret from BCS_WxAccessToken "; DataTable dt = SqlHelper.ExecuteDataTable(sql, CommandType.Text, null); //1.1判断是否有数据 if (dt.Rows.Count > 0) { //1.1.2 有数据判断时间是否超时 if (dt.Rows[0]["Time"].ToString() == "" || dt.Rows[0]["AccessToken"].ToString() == "") { String token = GetToken(dt.Rows[0]["AppId"].ToString(), dt.Rows[0]["Secret"].ToString()); acccess_Token = token.Split('|')[0]; timeOut = Convert.ToInt64(token.Split('|')[1].ToString()); //将token存入数据库 time = GetLongTime(); UpdateToken(acccess_Token, (time + timeOut).ToString(), dt.Rows[0]["AppId"].ToString()); } else { timeOut = Convert.ToInt64(dt.Rows[0]["Time"].ToString()); //获取时间戳 time = GetLongTime(); if (timeOut > time) { //没有超时则取出来使用 acccess_Token = dt.Rows[0]["AccessToken"].ToString(); } else { //时间超时获取最新的Access_token,并更新数据库中的token和超时时间 String token = GetToken(dt.Rows[0]["AppId"].ToString(), dt.Rows[0]["Secret"].ToString()); acccess_Token = token.Split('|')[0]; timeOut = Convert.ToInt64(token.Split('|')[1].ToString()); //更新token time = GetLongTime(); UpdateToken(acccess_Token, (time + timeOut).ToString(), dt.Rows[0]["AppId"].ToString()); } } } else { //1.1.1 没有数据执行Insert String token = GetToken(dt.Rows[0]["AppId"].ToString(), dt.Rows[0]["Secret"].ToString()); acccess_Token = token.Split('|')[0]; timeOut = Convert.ToInt64(token.Split('|')[1].ToString()); //将token存入数据库 time = GetLongTime(); InsertToken(acccess_Token, (time + timeOut).ToString(), dt.Rows[0]["AppId"].ToString()); } return acccess_Token; } private static long GetLongTime() { DateTime a = DateTime.Now; DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0)); long time = (long)(a - startTime).TotalSeconds; return time; } #endregion public static int UpdateToken(String token, String timeOut, String APPID) { 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+")"; return SqlHelper.ExecuteNonQuery(sql, CommandType.Text, null); } //将Token信息添加到数据库 public static int InsertToken(String token, String timeOut, String APPID) { String sql = "insert into BCS_WxAccessToken (AccessToken,Time,AppId) values ('" + token + "'," + timeOut + ",'" + APPID + "')"; return SqlHelper.ExecuteNonQuery(sql, CommandType.Text, null); } public static string GetToken(String APPID, String APPSECRET) { String access_token; long expires_in; //构造获取https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxdc0ee0e75b3c6188&secret=f878802a0a1ca973659deb62010ee4a4 //WxPayData data = new WxPayData(); //data.SetValue("grant_type", "client_credential"); //data.SetValue("appid", APPID); //data.SetValue("secret", APPSECRET); String data = "appid="+APPID+"&grant_type=client_credential&secret="+APPSECRET; string url = "https://api.weixin.qq.com/cgi-bin/token?" + data; //Log.Debug(this.GetType().ToString(), "陈新杰 : " + url); //请求url以获取数据 string result = HttpService.Get(url); //Log.Debug("测试用:", "GetOpenidAndAccessTokenFromCode response : " + result); //保存access_token,用于收货地址获取 JsonData jd = JsonMapper.ToObject(result); access_token = (string)jd["access_token"]; expires_in = Convert.ToInt64(jd["expires_in"].ToString());//超时时间 //data.SetValue("nickname", nickname); return access_token + "|" + expires_in; } } }