using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using Newtonsoft.Json;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Security.Cryptography;
public
class
WXJSSDK
{
private
string appId;
private
string appSecret;
private
DataTable DT;
public
WXJSSDK(string appId, string appSecret)
{
this.appId = appId;
this.appSecret = appSecret;
}
public
System.Collections.Hashtable getSignPackage()
{
string jsapiTicket = getJsApiTicket();
string url = HttpContext.Current.Request.Url.ToString();
string timestamp = Convert.ToString(ConvertDateTimeInt(DateTime.Now));
string nonceStr = createNonceStr();
string rawstring =
"jsapi_ticket="
+ jsapiTicket +
"&noncestr="
+ nonceStr +
"×tamp="
+ timestamp +
"&url="
+ url +
""
;
string signature = SHA1_Hash(rawstring);
System.Collections.Hashtable signPackage =
new
System.Collections.Hashtable();
signPackage.Add(
"appId"
, appId);
signPackage.Add(
"nonceStr"
, nonceStr);
signPackage.Add(
"timestamp"
, timestamp);
signPackage.Add(
"url"
, url);
signPackage.Add(
"signature"
, signature);
signPackage.Add(
"rawString"
, rawstring);
return
signPackage;
}
private
string createNonceStr()
{
int length = 16;
string chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
;
string str =
""
;
Random rad =
new
Random();
for
(int i = 0; i < length; i++)
{
str += chars.Substring(rad.Next(0, chars.Length - 1), 1);
}
return
str;
}
private
string getJsApiTicket()
{
DT = DbSession.Default.FromSql(
"select jsapi_ticket,ticket_expires from table where ID=1"
).ToDataTable();
int expire_time = (int)DT.Rows[0][
"ticket_expires"
];
string ticket = DT.Rows[0][
"jsapi_ticket"
].ToString();
string accessToken =getAccessToken();
if
(expire_time < ConvertDateTimeInt(DateTime.Now))
{
string url =
"https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token="
+ accessToken +
""
;
Jsapi api =JsonConvert.DeserializeObject<Jsapi>(httpGet(url));
ticket = api.ticket;
if
(ticket !=
""
)
{
expire_time = ConvertDateTimeInt(DateTime.Now) + 7000;
}
}
return
ticket;
}
private
string httpGet(string url)
{
try
{
WebClient MyWebClient =
new
WebClient();
MyWebClient.Credentials = CredentialCache.DefaultCredentials;
Byte[] pageData = MyWebClient.DownloadData(url);
string pageHtml = System.Text.Encoding.Default.GetString(pageData);
return
pageHtml;
}
catch
(WebException webEx)
{
Console.WriteLine(webEx.Message.ToString());
return
null;
}
}
public
string SHA1_Hash(string str_sha1_in)
{
SHA1 sha1 =
new
SHA1CryptoServiceProvider();
byte[] bytes_sha1_in = System.Text.UTF8Encoding.Default.GetBytes(str_sha1_in);
byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in);
string str_sha1_out = BitConverter.ToString(bytes_sha1_out);
str_sha1_out = str_sha1_out.Replace(
"-"
,
""
).ToLower();
return
str_sha1_out;
}
private
void StreamWriterMetod(string str, string patch)
{
try
{
FileStream fsFile =
new
FileStream(patch, FileMode.OpenOrCreate);
StreamWriter swWriter =
new
StreamWriter(fsFile);
swWriter.WriteLine(str);
swWriter.Close();
}
catch
(Exception e)
{
throw
e;
}
}
public
int ConvertDateTimeInt(System.DateTime time)
{
int intResult = 0;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(
new
System.DateTime(1970, 1, 1));
intResult = Convert.ToInt32((time - startTime).TotalSeconds);
return
intResult;
}
}
#region
public
class
JSTicket
{
public
string jsapi_ticket { get; set; }
public
double expire_time { get; set; }
}
public
class
AccToken
{
public
string access_token { get; set; }
public
double expires_in { get; set; }
}
public
class
Jsapi
{
public
int errcode { get; set; }
public
string errmsg { get; set; }
public
string ticket { get; set; }
public
string expires_in { get; set; }
}
#endregion