///
/// 获得accesstoken
///
///
public static string AccessToken()
{
return SendRequest("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret, Encoding.UTF8);
}
/// <summary>
/// 根据accesstoken获得ticket
/// </summary>
/// <returns></returns>
public static string GetTicket()
{
string access_token = AccessToken();
string url1 = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token.Substring(access_token.IndexOf(':') + 2, access_token.IndexOf(',') - 3 - access_token.IndexOf(':')) + "&type=jsapi";
string requstStr = SendRequest(url1, Encoding.UTF8);
string ticket = requstStr.Substring(requstStr.IndexOf("ticket") + 9, requstStr.LastIndexOf(',') - 1 - requstStr.IndexOf("ticket") - 9);// 获得json参数没搞懂的自己优化
return ticket;
}
/// <summary>
/// 获取jssdk所需签名
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string GetSignature(string link)
{
DateTime dti = DateTime.Now;
dtime = dti.ToString("yyyy-MM-dd HH:mm:ss");
string noncestr = dti.ToString("yyyyMMddHHmmss");
int timestamp = 1510124527;
string ticket = GetTicket();
time = "1510124527";
randstr = noncestr;
string string1 = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr + "×tamp=" + timestamp + "&url=" + link;
url = string1;
string signature = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(string1, "SHA1");
return signature.ToLower(); // 生成后一定转换为小写
}
/// <summary>
/// Get方式获取url地址输出内容
/// </summary> /// <param name="url">url</param>
/// <param name="encoding">返回内容编码方式,例如:Encoding.UTF8</param>
public static string SendRequest(string url, Encoding encoding)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "GET";
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
StreamReader sr = new StreamReader(webResponse.GetResponseStream(), encoding);
string str = sr.ReadToEnd();
return str;
}
/// <summary>
/// 将值记录到数据库中防止出现调用限制
/// </summary>
/// <param name="url"></param>
public static void SignatureAdd(string link)
{
time = "";
randstr = "";
signstr = "";
signstr = GetSignature(link);
string sqlI = "insert into wxinfo(timestr,nonceStr,signature,timeS,url,link) values('" + time + "','" + randstr + "','" + signstr + "','" + dtime + "','" + url + "','" + link + "') ";
int count = DbHelper.ExecuteCommand(sqlI);
}
/// <summary>
/// 获得微信权限信息,格式:时间戳,随机数,签名
/// </summary>
/// <param name="link"></param>
/// <returns></returns>
public static string GetWXInfo(string link)
{
bool result = false;
// 获得最后一条新增数据
string sql = " select * from wxinfo where link='" + link + "' order by wxinfo_id desc";
DataTable dt = DbHelper.GetDataSet(sql);
if (dt != null)
{
if (dt.Rows.Count > 0)
{
// 当前时间小于获得获得tincket时间时调用数据库中
if (DateTime.Now < Convert.ToDateTime(dt.Rows[0]["timeS"].ToString()).AddSeconds(7200))
{
time = dt.Rows[0]["timestr"].ToString();
randstr = dt.Rows[0]["nonceStr"].ToString();
signstr = dt.Rows[0]["signature"].ToString();
url = dt.Rows[0]["url"].ToString();
result = true;
}
}
}
if (result == false)
{
SignatureAdd(link);
}
return time + "," + randstr + "," + signstr;
}
}