微信JS-SDK

$.getScript("https://res.wx.qq.com/open/js/jweixin-1.0.0.js", function () {
wx.config({
debug: false,
appId: '@Model.appId',
timestamp: '@Model.timeStamp',
nonceStr: '@Model.nonceStr',
signature: '@Model.signature',
jsApiList: ["chooseImage", "previewImage", "uploadImage", "downloadImage", "onMenuShareTimeline", "onMenuShareAppMessage", "onMenuShareQQ", "onMenuShareWeibo", "onMenuShareQZone", "openLocation"]

});
wx.ready(function () {
wx.checkJsApi({
jsApiList: ["signature", "previewImage", "uploadImage", "downloadImage", "onMenuShareTimeline", "onMenuShareAppMessage", "onMenuShareQQ", "onMenuShareWeibo", "onMenuShareQZone", "openLocation"],
success: function (res) {
console.log("checkJsApiError:" + res)
}
});
wx.error(function (res) {
console.log("error:" + res)
});
wx.onMenuShareTimeline({
title: title, // 分享标题
link: link, // 分享链接 如果该链接是一个调用接口的,配有参数的复杂链接 最好就用Request.Url 不然也会没有分享的效果
imgUrl: imgUrl, // 分享图标
success: function () {
if (typeof (eval(SuccessBackFunc)) == "function") {
SuccessBackFunc();// 用户确认分享后执行的回调函数
}
},
cancel: function () {
if (typeof (eval(CancelBackFunc)) == "function") {

CancelBackFunc();// 用户取消分享后执行的回调函数
}
}
});

 

});
});

 

//后台方法

//获取token

string token = string.Empty;
using (WebClient wc = new WebClient())
{
string tokenString = wc.DownloadString("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}");
JObject obj = (JObject)JavaScriptConvert.DeserializeObject(tokenString);
token = obj["access_token"].ToString().Trim('"');
CacheHelper.AddCache("JsSdkAssessToken", token, 100);

}

//获取ticket
using (WebClient wc = new WebClient())
{
var url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+token+"&type=jsapi";
string tokenString = wc.DownloadString(url);
JObject obj = (JObject)JavaScriptConvert.DeserializeObject(tokenString);
string jstoken = obj["ticket"].ToString().Trim('"'); ;
CacheHelper.AddCache("JsSdkticket", jstoken, 100);
return jstoken;
}

 

获取签名,Url 为Request.Url

public static string getsign(string ticket,string timestamp,string url)
{
string arrString =
string.Format("jsapi_ticket={0}&noncestr=111111&timestamp={1}&url={2}", ticket,
timestamp,url);
string sign = FormsAuthentication.HashPasswordForStoringInConfigFile(arrString, "SHA1").ToLower();

return sign;
}

posted @ 2017-01-16 15:52  Amity1006  阅读(185)  评论(0)    收藏  举报