private void RegisterWeiXinShareScript(Model.pb_user muser)
{
string title = "分享链接";
string img = "图片链接";
string text = "分享链接,识别二维码注册";
string url = "http://xxx";
//公众号的应用ID
string appid = "appid";
//公众号的应用密钥
string secret = "appsecret";
//生成签名的时间戳
TimeSpan ts = DateTime.Now - DateTime.Parse("1970-01-01 00:00:00");
string timestamp = ts.TotalSeconds.ToString().Split('.')[0];
//生成签名的随机串
string nonceStr = "654as2df5as2df5aw2";
//微信access_token,用于获取微信jsapi_ticket
string token = GetAccess_token(appid, secret);
//微信jsapi_ticket
string ticket = GetTicket(token);
//当前网页的URL
string pageurl = Request.Url.AbsoluteUri;
//对所有待签名参数按照字段名的ASCII 码从小到大排序(字典序)后,使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串
string str = "jsapi_ticket=" + ticket + "&noncestr=" + nonceStr + "×tamp=" + timestamp + "&url=" + pageurl;
//签名,使用SHA1生成
string signature = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1").ToLower();
//要注册到页面的JS脚本
StringBuilder sbjsApi = new StringBuilder();
sbjsApi.Append("<script>");
//通过config接口注入权限验证配置
sbjsApi.Append("wx.config({debug:false,");
sbjsApi.Append("appId: '" + appid + "',");
sbjsApi.Append("timestamp: " + timestamp + ",");
sbjsApi.Append("nonceStr: '" + nonceStr + "',");
sbjsApi.Append("signature: '" + signature + "',");
sbjsApi.Append("jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'updateAppMessageShareData', 'updateTimelineShareData', 'onMenuShareWeibo']});");
//通过ready接口处理成功验证
sbjsApi.Append("wx.ready(on_weixin_ready);");
sbjsApi.Append("function on_weixin_ready() {weixin_share();}");
sbjsApi.Append("function weixin_share(){");
//获取“分享给朋友”按钮点击状态及自定义分享内容接口
sbjsApi.Append("wx.onMenuShareAppMessage({");
sbjsApi.Append("title:'" + title + "',");
sbjsApi.Append("desc:'" + text + "',");
sbjsApi.Append("link:'" + url + "',");
sbjsApi.Append("imgUrl:'" + img + "',");
sbjsApi.Append("type:'link',");
sbjsApi.Append("dataUrl:'',");
sbjsApi.Append("success: function () { },");
sbjsApi.Append("cancel:function () {}");
sbjsApi.Append("});");
//获取“分享到朋友圈”按钮点击状态及自定义分享内容接口
sbjsApi.Append("wx.onMenuShareTimeline({");
sbjsApi.Append("title:'" + title + "',");
sbjsApi.Append("link:'" + url + "',");
sbjsApi.Append("imgUrl:'" + img + "',");
sbjsApi.Append("success: function () {},");
sbjsApi.Append("cancel:function () {}");
sbjsApi.Append("});");
//自定义“分享给朋友”及“分享到QQ”按钮的分享内容(1.4.0)
sbjsApi.Append("wx.updateAppMessageShareData({");
sbjsApi.Append("title:'" + title + "',");
sbjsApi.Append("desc:'" + text + "',");
sbjsApi.Append("link:'" + url + "',");
sbjsApi.Append("imgUrl:'" + img + "',");
sbjsApi.Append("success: function () { },");
sbjsApi.Append("cancel:function () {}");
sbjsApi.Append("});");
//自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容
sbjsApi.Append("wx.updateTimelineShareData({");
sbjsApi.Append("title:'" + title + "',");
sbjsApi.Append("desc:'" + text + "',");
sbjsApi.Append("link:'" + url + "',");
sbjsApi.Append("imgUrl:'" + img + "',");
sbjsApi.Append("success: function () { },");
sbjsApi.Append("cancel:function () {}");
sbjsApi.Append("});");
//获取“分享到腾讯微博”按钮点击状态及自定义分享内容接口
sbjsApi.Append("wx.onMenuShareWeibo({");
sbjsApi.Append("title:'" + title + "',");
sbjsApi.Append("desc:'" + text + "',");
sbjsApi.Append("link:'" + url + "',");
sbjsApi.Append("imgUrl:'" + img + "',");
sbjsApi.Append("success: function () { },");
sbjsApi.Append("cancel:function () {}");
sbjsApi.Append("});");
sbjsApi.Append("}");
sbjsApi.Append("</script>");
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "weixin", sbjsApi.ToString().Trim());
}
其中需要去获取token,ticket
/// <summary>
/// 获取微信access_token
/// </summary>
/// <param name="appid">公众号的应用ID</param>
/// <param name="secret">公众号的应用密钥</param>
/// <returns>access_token</returns>
private string GetAccess_token(string appid, string secret)
{
string tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
string jsonresult = HttpGet(tokenUrl, "UTF-8");
WX_Token wx = JsonDeserialize<WX_Token>(jsonresult);
return wx.access_token;
}
/// <summary>
/// JSON反序列化
/// </summary>
/// <typeparam name="T">实体类</typeparam>
/// <param name="jsonString">JSON</param>
/// <returns>实体类</returns>
private T JsonDeserialize<T>(string jsonString)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T obj = (T)ser.ReadObject(ms);
return obj;
}
/// <summary>
/// 获取微信jsapi_ticket
/// </summary>
/// <param name="token">access_token</param>
/// <returns>jsapi_ticket</returns>
public string GetTicket(string token)
{
string ticketUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + token + "&type=jsapi";
string jsonresult = HttpGet(ticketUrl, "UTF-8");
WX_Ticket wxTicket = JsonDeserialize<WX_Ticket>(jsonresult);
return wxTicket.ticket;
}
注意在加载页面就需加载代码,微信点击右上角就可分享自定义的内容,其中js需看是否是title,desc等,以免读取不了东西
在加载代码的前提需要在前端添加:<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js" ></script>
不然一切都无果!