注:并非所有代码原创
using Senparc.Weixin.MP.Containers;
using Senparc.Weixin.MP.Helpers;
using Utility;
namespace Mobile.App_Codes
{
///
/// 微信分享接口封装
///
public class WeiXin
{
///
/// 获取微信分享配置信息
///
/// 当前请求url
///
public static WxConfig GetConfig(string url)
{
var key = "Mobile_Wxconfig" + url;
var wxConfig = Cache.GetCache(key.Md5());
if (wxConfig != null && wxConfig.Signature.Length > 0)
{
return wxConfig;
}
else
{
wxConfig = new WxConfig();
wxConfig.Timestamp = JSSDKHelper.GetTimestamp();
wxConfig.NonceStr = JSSDKHelper.GetNoncestr();
wxConfig.AppId = "wxe62testestetewtstest";
wxConfig.AppSecret = "8b213499dtestesetrtestesteserewtest";
wxConfig.Ticket = JsApiTicketContainer.TryGetJsApiTicket(wxConfig.AppId, wxConfig.AppSecret);
wxConfig.Signature = JSSDKHelper.GetSignature(wxConfig.Ticket, wxConfig.NonceStr, wxConfig.Timestamp,
url);
Cache.SetCache(key.Md5(), wxConfig, 60*60);
return wxConfig;
}
}
}
}
下面列出是sha1计算签名的方法
///
/// 获取JS-SDK权限验证的签名Signature
///
///
///
///
///
///
public static string GetSignature(string ticket, string noncestr, string timestamp, string url)
{
var parameters = new Hashtable();
parameters.Add("jsapi_ticket", ticket);
parameters.Add("noncestr", noncestr);
parameters.Add("timestamp", timestamp);
parameters.Add("url", url);
return CreateSha1(parameters);
}
///
/// sha1加密
///
///
private static string CreateSha1(Hashtable parameters)
{
var sb = new StringBuilder();
var akeys = new ArrayList(parameters.Keys);
akeys.Sort();
foreach (var k in akeys)
{
if (parameters[k] != null)
{
var v = (string)parameters[k];
if (sb.Length == 0)
{
sb.Append(k + "=" + v);
}
else
{
sb.Append("&" + k + "=" + v);
}
}
}
return SHA1UtilHelper.GetSha1(sb.ToString()).ToLower();
}
///
/// 签名算法
///
///
///
public static string GetSha1(string str)
{
//建立SHA1对象
SHA1 sha = new SHA1CryptoServiceProvider();
//将mystr转换成byte[]
ASCIIEncoding enc = new ASCIIEncoding();
byte[] dataToHash = enc.GetBytes(str);
//Hash运算
byte[] dataHashed = sha.ComputeHash(dataToHash);
//将运算结果转换成string
string hash = BitConverter.ToString(dataHashed).Replace("-", "");
return hash;
}
签名,appid,AppSecret,这些放在服务端实现,不要放在js里,不安全,
var shareData = {};
shareData.imgUrl = window.ImgPrex + result.PicUrls;
shareData.link = window.location.href;
shareData.content = delHtmlTag($("#Contents").html());
shareData.title = document.title;
Share(shareData);
function Share(shareData) {
var data = {};
var wxdata = {};
data.method = "GetConfig";
data.url = window.location.href.split('#')[0];
//这里是ajax向后台请求签名,appid等的方法
ajaxProcess("/h5/share.aspx?", data, function callSuccess(oRet) {
//var data = oRet;
var result = oRet;
if (result != null) {
wx.config({
debug: false,
appId: result.AppId,
timestamp: result.Timestamp,
nonceStr: result.NonceStr,
signature: result.Signature,
jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone']
});
}
}, function callError(e) {
alert(e);
});
wxdata.imgUrl = shareData.imgUrl;
wxdata.link = shareData.link;
var content = shareData.content;
content = delHtmlTag(content);
if (content.length > 100) {
wxdata.desc = content.substring(0, 100);
} else {
wxdata.desc = content;
}
wxdata.title = shareData.title;
var friendcallback = function(res) {
//alert("分享成功");
};
wx.ready(function() {
wx.onMenuShareTimeline({
title: wxdata.title,
desc: wxdata.desc,
link: wxdata.link,
imgUrl: wxdata.imgUrl,
img_width: 200,
img_height: 200,
trigger: function(res) {
},
success: function(res) {
friendcallback(res);
},
cancel: function(res) {
},
fail: function(res) {
alert(JSON.stringify(res));
}
});
});
wx.ready(function() {
wx.onMenuShareAppMessage({
title: wxdata.title,
desc: wxdata.desc,
link: wxdata.link,
imgUrl: wxdata.imgUrl,
img_width: 200,
img_height: 200,
trigger: function(res) {
},
success: function(res) {
friendcallback(res);
},
cancel: function(res) {
},
fail: function(res) {
alert(JSON.stringify(res));
}
});
});
wx.ready(function() {
wx.onMenuShareQQ({
title: wxdata.title,
desc: wxdata.desc,
link: wxdata.link,
imgUrl: wxdata.imgUrl,
img_width: 200,
img_height: 200,
trigger: function(res) {
},
success: function(res) {
friendcallback(res);
},
cancel: function(res) {
},
fail: function(res) {
alert(JSON.stringify(res));
}
});
});
wx.ready(function() {
wx.onMenuShareWeibo({
title: wxdata.title,
desc: wxdata.desc,
link: wxdata.link,
imgUrl: wxdata.imgUrl,
img_width: 200,
img_height: 200,
trigger: function(res) {
},
success: function(res) {
friendcallback(res);
},
cancel: function(res) {
},
fail: function(res) {
alert(JSON.stringify(res));
}
});
});
wx.ready(function() {
wx.onMenuShareQZone({
title: wxdata.title,
desc: wxdata.desc,
link: wxdata.link,
imgUrl: wxdata.imgUrl,
img_width: 200,
img_height: 200,
trigger: function(res) {
},
success: function(res) {
friendcallback(res);
},
cancel: function(res) {
},
fail: function(res) {
alert(JSON.stringify(res));
}
});
});
}
--- end ---