首页 > 程序开发 > 综合编程 > 其他综合 >

Asp.Net页面自定义微信分享

2017-04-01

Asp Net页面自定义微信分享:1、先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。

Asp.Net页面自定义微信分享:1、先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。

2、在需要调用JS接口的页面引入如下JS文件,(支持https):https://res.wx.qq.com/open/js/jweixin-1.0.0.js

要分享的页面代码:

usingSystem.Runtime.Serialization.Json;//需添加System.Runtime.Serialization引用

publicpartialclassWeiXinShareScript:System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{

if(!Page.IsPostBack)

{

RegisterWeiXinShareScript();

}

}

privatevoidRegisterWeiXinShareScript()

{

//公众号的应用ID

stringappid="";

//公众号的应用密钥

stringsecret="";

//生成签名的时间戳

TimeSpants=DateTime.Now-DateTime.Parse("1970-01-0100:00:00");

stringtimestamp=ts.TotalSeconds.ToString().Split('.')[0];

//生成签名的随机串

stringnonceStr="test";

//微信access_token,用于获取微信jsapi_ticket

stringtoken=GetAccess_token(appid,secret);

//微信jsapi_ticket

stringticket=GetTicket(token);

//当前网页的URL

stringpageurl=Request.Url.AbsoluteUri;

//对所有待签名参数按照字段名的ASCII码从小到大排序(字典序)后,使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串

stringstr="jsapi_ticket="+ticket+"&noncestr="+nonceStr+"×tamp="+timestamp+"&url="+pageurl;

//签名,使用SHA1生成

stringsignature=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"SHA1").ToLower();

//要注册到页面的JS脚本

StringBuildersbjsApi=newStringBuilder();

sbjsApi.Append("<script>");
  • //通过config接口注入权限验证配置
  • sbjsApi.Append("wx.config({debug:false,");
  • sbjsApi.Append("appId:'"&#43;appid&#43;"',");
  • sbjsApi.Append("timestamp:"&#43;timestamp&#43;",");
  • sbjsApi.Append("nonceStr:'"&#43;nonceStr&#43;"',");
  • sbjsApi.Append("signature:'"&#43;signature&#43;"',");
  • sbjsApi.Append("jsApiList:['onMenuShareTimeline','onMenuShareAppMessage','chooseImage','uploadImage','getNetworkType']});");
  • //通过ready接口处理成功验证
  • sbjsApi.Append("wx.ready(on_weixin_ready);");
  • sbjsApi.Append("functionon_weixin_ready(){weixin_share();}");
  • //获取“分享给朋友”按钮点击状态及自定义分享内容接口
  • sbjsApi.Append("functionweixin_share(){");
  • sbjsApi.Append("wx.onMenuShareAppMessage({");
  • sbjsApi.Append("title:'分享出去的标题',");
  • sbjsApi.Append("desc:'分享出去的内容',");
  • sbjsApi.Append("link:'分享出去的链接地址',");
  • sbjsApi.Append("imgUrl:'分享出去的图标地址',");
  • sbjsApi.Append("type:'link',");
  • sbjsApi.Append("dataUrl:'',");
  • sbjsApi.Append("success:function(){alert('分享成功');},");
  • sbjsApi.Append("cancel:function(){}");
  • sbjsApi.Append("});");
  • //获取“分享到朋友圈”按钮点击状态及自定义分享内容接口
  • sbjsApi.Append("wx.onMenuShareTimeline({");
  • sbjsApi.Append("title:'分享出去的标题',");
  • sbjsApi.Append("link:'分享出去的链接地址',");
  • sbjsApi.Append("imgUrl:'分享出去的图标地址',");
  • sbjsApi.Append("success:function(){alert('分享成功');},");
  • sbjsApi.Append("cancel:function(){}");
  • sbjsApi.Append("});}");
  • sbjsApi.Append("</script>"); Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"weixin",sbjsApi.ToString().Trim()); } /// ///获取微信jsapi_ticket /// ///access_token ///jsapi_ticket publicstringGetTicket(stringtoken) { stringticketUrl="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+token+"&type=jsapi"; stringjsonresult=HttpGet(ticketUrl,"UTF-8"); WX_TicketwxTicket=JsonDeserialize(jsonresult); returnwxTicket.ticket; } /// ///获取微信access_token /// ///公众号的应用ID ///公众号的应用密钥 ///access_token privatestringGetAccess_token(stringappid,stringsecret) { stringtokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret; stringjsonresult=HttpGet(tokenUrl,"UTF-8"); WX_Tokenwx=JsonDeserialize(jsonresult); returnwx.access_token; } /// ///JSON反序列化 /// ///实体类 ///JSON ///实体类 privateTJsonDeserialize(stringjsonString) { DataContractJsonSerializerser=newDataContractJsonSerializer(typeof(T)); MemoryStreamms=newMemoryStream(Encoding.UTF8.GetBytes(jsonString)); Tobj=(T)ser.ReadObject(ms); returnobj; } /// ///HttpGET请求 /// ///请求地址 ///编码方式:GB2312/UTF-8 ///字符串 privatestringHttpGet(stringurl,stringencode) { HttpWebRequestrequest=(HttpWebRequest)WebRequest.Create(url); request.Method="GET"; request.ContentType="text/html;charset="+encode; HttpWebResponseresponse=(HttpWebResponse)request.GetResponse(); StreammyResponseStream=response.GetResponseStream(); StreamReadermyStreamReader=newStreamReader(myResponseStream,Encoding.GetEncoding(encode)); stringretString=myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); returnretString; } } /// ///通过微信API获取access_token得到的JSON反序列化后的实体 /// publicclassWX_Token { publicstringaccess_token{get;set;} publicstringexpires_in{get;set;} } /// ///通过微信API获取jsapi_ticket得到的JSON反序列化后的实体 /// publicclassWX_Ticket { publicstringerrcode{get;set;} publicstringerrmsg{get;set;} publicstringticket{get;set;} publicstringexpires_in{get;set;} }
  • 相关文章
    最新文章
    热点推荐