准备条件:认证的服务号
JavaScript:
<script>
wx.config({
debug: false,
appId: "123",
timestamp: "<%=strTimeA %>",
nonceStr: "<%=nonceStr %>",
signature: "<%=strSha1 %>",
jsApiList: [
// 所有要调用的 API 都要加到这个列表中
'onMenuShareAppMessage'
]
});
wx.ready(function () {
wx.onMenuShareAppMessage({
title: '标题', // 分享标题
desc: '描述', // 分享描述
link: '<%=Request.Url.ToString() %>', // 分享链接,该链接域名必须与当前企业的可信域名一致
imgUrl: 'http://<%=HttpContext.Current.Request.Url.Host%>/images/fenxiang.jpg', // 分享图标
type: '', // 分享类型,music、video或link,不填默认为link
dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
success: function () {
// 用户确认分享后执行的回调函数
alert('suc');
},
cancel: function () {
// 用户取消分享后执行的回调函数
alert('cancel');
}
});
});
</script>.Net:
public string nonceStr = ""; public string strSha1 = ""; public string strTimeA = "";
nonceStr = DateTime.Now.ToString("yyyyMMddHHmmss");
string strAccess = Basic.Engine.CookieDo.GetCookie("token", "str_key");
// Response.Write("strAccess:" + strAccess);
if (string.IsNullOrEmpty(strAccess))
{
strAccess = GetWeb("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=123&secret=321").Split('"')[3];
Basic.Engine.CookieDo.WriteCookie("token", "str_key", strAccess, 120);
// Response.Write("<script>alert('access_token');</script>");
}
strAccess = GetWeb("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + strAccess + "&type=jsapi");
// Response.Write("<script>alert('" + strAccess.Split('"').Length + "');</script>");
if (!(strAccess.Split('"').Length > 9))
{
strAccess = GetWeb("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=123&secret=321").Split('"')[3];
Basic.Engine.CookieDo.WriteCookie("token", "str_key", strAccess, 120);
strAccess = GetWeb("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + strAccess + "&type=jsapi");
//Response.Write("<script>alert('access_token__"+strAccess+"');</script>");
}
strTi = strAccess.Split('"')[9];
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
strTimeA = Convert.ToInt64(ts.TotalSeconds).ToString();
string jj = Request.RawUrl;
if (jj == "/")
{
jj = Request.Url.ToString().Replace("default.aspx", "");
}
else
{
jj = Request.Url.ToString();
}
strSha1 = "jsapi_ticket=" + strTi + "&noncestr=" + nonceStr + "×tamp=" + strTimeA + "&url=" + jj;
strSha1 = Sha1Signature(strSha1); public string strTi = "";
private string GetWeb(string url)
{
//string postString = "";//这里即为传递的参数,可以用工具抓包分析,也可以自己分析,主要是form里面每一个name都要加进来
//byte[] postData = Encoding.UTF8.GetBytes(postString);//编码,尤其是汉字,事先要看下抓取网页的编码方式
//WebClient webClient = new WebClient();
//webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可
//byte[] responseData = webClient.UploadData(url, "POST", postData);//得到返回字符流
//string srcString = Encoding.UTF8.GetString(responseData);
//return srcString;
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
string srcString = reader.ReadToEnd();
return srcString;
}
/// <summary>
/// 基于Sha1的自定义加密字符串方法:输入一个字符串,返回一个由40个字符组成的十六进制的哈希散列(字符串)。
/// </summary>
/// <param name="str">要加密的字符串</param>
/// <returns>加密后的十六进制的哈希散列(字符串)</returns>
public static string Sha1Signature(string str)
{
var buffer = Encoding.UTF8.GetBytes(str);
var data = SHA1.Create().ComputeHash(buffer);
StringBuilder sub = new StringBuilder();
foreach (var t in data)
{
sub.Append(t.ToString("X2"));
}
return sub.ToString();
}