VPS参考测评推荐
专注分享VPS主机优惠信息
衡天云优惠活动
华纳云最新优惠促销活动
jtti最新优惠促销活动

使用js实现微信分享功能的示例代码(使用js实现微信分享功能的示例代码是什么)

locvps
主机参考:VPS测评参考推荐/专注分享VPS服务器优惠信息!若您是商家可以在本站进行投稿,查看详情!此外我们还提供软文收录、PayPal代付、广告赞助等服务,查看详情!
我们发布的部分优惠活动文章可能存在时效性,购买时建议在本站搜索商家名称可查看相关文章充分了解该商家!若非中文页面可使用Edge浏览器同步翻译!PayPal代付/收录合作

本文主要介绍js在微信上分享好友链接的实现,对学习ASP.NET编程技术有一定的帮助。感兴趣的朋友可以参考一下。

通常,当你想通过微信与朋友分享一个页面时,显示的标题和描述并不是你想要的。我自己查了一些资料,原来是js控制的。显示效果如下:

标题、描述和共享图片都由js控制。Js代码如下

& lt脚本& gtvar data for weixin = { appId:& quot;",MsgImg:& quot;Christmas/2010 12189457639 . gif & quot;,//显示图片TLImg:“Christmas/201012189457639 . gif & quot;,//显示图片URL:“Christmas/6 . html?stra=!u738B!u4F1F & quot,//跳转地址标题:“我把我的思念和祝福送给你,祝大家圣诞快乐”,//标题内容desc:“我把我的思念和祝福送给你,祝大家圣诞快乐”,//Description fake id:“& quot;,回调:function(){ } };(function(){ var onBridgeReady = function(){ weixinsbridge . on(& amp;#39;菜单:共享:appmessage & amp#39;,function(argv){ weixinsbridge . invoke(& amp;#39;sendAppMessage & amp#39;,{ & quotappid & quot:dataForWeixin.appIdimg _ url & quot:dataForWeixin。MsgImgimg _ width & quot:& quot120 & quot,& quotimg _ height & quot:& quot120 & quot,& quot链接& quot:dataForWeixin.urldesc & quot;:dataForWeixin.desc,& quot标题& quot:dataForWeixin.title },function(RES){(data forweixin . callback);});});weixinsbrbridge . on(& amp;#39;菜单:分享:时间线和。#39;,function(argv){(dataforweixin . callback)();weixinsbridge . invoke(& amp;#39;分享时间表。#39;,{ & quotimg _ url & quot:dataForWeixin。TLImg,& quotimg _ width & quot:& quot120 & quot,& quotimg _ height & quot:& quot120 & quot,& quot链接& quot:dataForWeixin.urldesc & quot;:dataForWeixin.desc,& quot标题& quot:dataForWeixin.title },function(RES){ });});weixinsbrbridge . on(& amp;#39;菜单:分享:微博& amp#39;,function(argv){ weixinsbridge . invoke(& amp;#39;分享微博。#39;,{ & quot内容& quot:dataForWeixin.titleurl & quot:dataForWeixin.url },function(RES){(data forweixin . callback);});});weixinsbrbridge . on(& amp;#39;菜单:分享:facebook & amp#39;,function(argv){(dataforweixin . callback)();weixinsbridge . invoke(& amp;#39;共享B&amp。#39;,{ & quotimg _ url & quot:dataForWeixin。TLImg,& quotimg _ width & quot:& quot120 & quot,& quotimg _ height & quot:& quot120 & quot,& quot链接& quot:dataForWeixin.urldesc & quot;:dataForWeixin.desc,& quot标题& quot:dataForWeixin.title },function(RES){ });});};if(document . addevent listener){ document . addevent listener(& amp;#39;微信公众号。#39;,onBridgeReady,false);} else if(document . attach event){ document . attach event(& amp;#39;微信公众号。#39;,onBridgeReady);document . attach event(& amp;#39;onWeixinJSBridgeReady & amp#39;,onBridgeReady);} })();& lt/script & gt;

另一个微信分享js代码:

/**!*微信内置浏览器的Javascript API,功能包括* * 1、分享到微信朋友圈* 2、分享到微信好友* 3、分享到腾讯微博* 4、新增分享界面,包括好友、好友和微博的分享(iOS版)* 5、隐藏/显示右上角菜单项* 6、隐藏/显示底部浏览器工具栏* 7、获取当前网络状态* 8、调用微信客户端的图片播放组件* 9、关闭公众平台网页*/var微信API;/* * *要在微信朋友圈分享的信息* @ param { Object } data * @p-config {String}公众平台appId(服务号可用)* @p-config {String} imgUrl图片地址* @ P-config { String }链接地址* @p-config {String} desc描述分享的标题* @ P-config { String } title * * @ param { Object }回调相关回调方法* @ P-config { Boolean } async ready方法是否需要异步执行,默认为false * @ P-config {function}您也可以取消* @ P-配置{功能}取消(对应)为空* @ P-配置{功能}失败(对应)失败* @ P-配置{功能}确认(对应)成功* @ P-配置{功能}。无论成功或失败都将执行的所有(resp)回调*/function weixinsharetimeline(data,callbacks){ callbacks = callbacks | | { };var share timeline = function(the data){ weixinsbridge . invoke(& amp;#39;分享时间表。#39;,{ & quotappid & quot:theData.appId?thedata . appid:& amp;#39;& amp#39;,& quotimg _ url & quot:theData.imgUrl,& quot链接& quot:Data.linkdesc & quot;:theData.title标题& quot:theData.desc,//注意这里要分享的内容是desc“img _ width & quot;:& quot640 & quot,& quotimg _ height & quot:& quot640 & quot},function(resp){ switch(resp . err _ msg){//share _ timeline:取消用户取消案例&;#39;取消& amp#39;:回调。取消和& amp回调。取消(resp);打破;// share_timeline:确认发送成功。#39;确认& amp#39;:案例& amp#39;分享时间线:ok & amp#39;:回调。确认& amp& amp回调。确认(响应);打破;// share_timeline:发送失败案例失败&;#39;失败与时间线。#39;:默认值:callbacks.fail & amp& amp回调。失败(resp);打破;}//回调。all &;& amp回调。all(resp);});};weixinsbrbridge . on(& amp;#39;菜单:分享:时间线和。#39;,function(argv){ if(callbacks . async & amp;& amp回调。就绪){ window【& quot;_ wx _ loadedCb _ & quot】= callbacks.dataLoaded ||新函数();if(窗口【& quot_ wx _ loadedCb _ & quot].toString()。index of(& quot;_ wx _ loadedCb _ & quot)& gt0){ window【& quot;_ wx _ loadedCb _ & quot】=新函数();} callbacks . data loaded = function(new data){ window【& quot;_ wx _ loadedCb _ & quot】(new data);share timeline(new data);};//然后ready回调。ready& amp回调。就绪(argv);} else {//就绪状态回调。ready& amp回调。就绪(argv);shareTimeline(数据);} });}/* * *要分享的信息发送给微信好友* @ param { Object } data * @p-config {String}公众平台appId(可用服务号)* @p-config {String} imgUrl图片地址* @ P-config { String }链接地址* @p-config {String} desc描述分享的标题* @ P-config { String } title * * @ param { Object }回调相关回调方法* @p-config {Boolean}异步就绪方法是否需要异步执行,默认为false * @ P-config { Boolean您也可以取消* @ P-配置{功能}取消(对应)为空* @ P-配置{功能}失败(对应)失败* @ P-配置{功能}确认(对应)成功* @ P-配置{功能}。all(resp)Callback */Function weixisendappmessage(data,callbacks){ callbacks = callbacks | | { };var sendAppMessage = function(the data){ weixinsbridge . invoke(& amp;#39;sendAppMessage & amp#39;,{ & quotappid & quot:theData.appId?thedata . appid:& amp;#39;& amp#39;,& quotimg _ url & quot:theData.imgUrl,& quot链接& quot:Data.linkdesc & quot;:theData.desc标题& quot:theData.titleimg _ width & quot:& quot640 & quot,& quotimg _ height & quot:& quot640 & quot},function(resp){ switch(resp . err _ msg){//send _ app _ msg:取消用户取消案例&;#39;发送应用程序消息:取消& amp#39;:回调。取消和& amp回调。取消(resp);打破;// send_app_msg:确认发送成功。#39;确认& amp#39;:案例& amp#39;发送应用程序消息:好的。#39;:回调。确认& amp& amp回调。确认(响应);打破;// send_app_msg:fail发送失败案例&;#39;失败& amp#39;:默认值:callbacks.fail & amp& amp回调。失败(resp);打破;}//回调。all &;& amp回调。all(resp);});};weixinsbrbridge . on(& amp;#39;菜单:共享:appmessage & amp#39;,function(argv){ if(callbacks . async & amp;& amp回调。就绪){ window【& quot;_ wx _ loadedCb _ & quot】= callbacks.dataLoaded ||新函数();if(窗口【& quot_ wx _ loadedCb _ & quot].toString()。index of(& quot;_ wx _ loadedCb _ & quot)& gt0){ window【& quot;_ wx _ loadedCb _ & quot】=新函数();} callbacks . data loaded = function(new data){ window【& quot;_ wx _ loadedCb _ & quot】(new data);sendAppMessage(new data);};//然后ready回调。ready& amp回调。就绪(argv);} else {//就绪状态回调。ready& amp回调。就绪(argv);sendAppMessage(数据);} });}/* *要分享到腾讯微博的信息* @ param { Object } data * @ P-config { String }链接地址* @p-config {String} desc描述* * @param {Object}回调相关回调方法*是否需要异步执行@p-config {Boolean} async ready方法,默认为false * @ P-config { function } ready(argv)status * @ P-config { function } data loaded(数据)数据,在数据加载后调用。当异步为真时很有用,您还可以取消* @ P-配置{功能}取消(响应)为空* @ P-配置{功能}失败(响应)失败* @ P-配置{功能}确认(响应)成功* @ P-配置{功能}。all(resp)callback */function weixinsharewebo(data,callbacks){ callbacks = callbacks | | { };var share Weibo = function(the data){ weixinsbridge . invoke(& amp;#39;分享微博。#39;,{ & quot内容& quot:theData.descurl & quot:the data.link},function(resp){ switch(resp . err _ msg){//share _ Weibo:取消用户取消案例&;#39;分享微博:取消& amp#39;:回调。取消和& amp回调。取消(resp);打破;// share_weibo:确认发送成功。#39;分享_微博:确认& amp#39;:案例& amp#39;分享_微博:ok & amp#39;:回调。确认& amp& amp回调。确认(响应);打破;// share_weibo:发送失败案例失败&;#39;分享_微博:失败& amp#39;:默认值:callbacks.fail & amp& amp回调。失败(resp);打破;}//回调。all &;& amp回调。all(resp);});};weixinsbrbridge . on(& amp;#39;菜单:分享:微博& amp#39;,function(argv){ if(callbacks . async & amp;& amp回调。就绪){ window【& quot;_ wx _ loadedCb _ & quot】= callbacks.dataLoaded ||新函数();if(窗口【& quot_ wx _ loadedCb _ & quot].toString()。index of(& quot;_ wx _ loadedCb _ & quot)& gt0){ window【& quot;_ wx _ loadedCb _ & quot】=新函数();} callbacks . data loaded = function(new data){ window【& quot;_ wx _ loadedCb _ & quot】(new data);share Weibo(new data);};//然后ready回调。ready& amp回调。就绪(argv);} else {//就绪状态回调。ready& amp回调。就绪(argv);shareWeibo(数据);} });}/* * *新增分享界面* @param {Object}要分享的数据信息* @p-config {String}公共平台的appId(服务号可用)* @p-config {String} imgUrl图片地址* @p-config {String}链接地址* @p-config {String} desc描述分享的标题* @ P-config { String } title * * @ param { Object }回调相关回调方法* @p-config {Boolean}是否需要异步执行async ready方法,默认为false * @ P-config { function } ready(ready您还可以取消* @ P-配置{function}取消(resp,shareto)* @ P-配置{function}失败(resp,shareto)失败* @ P-配置{function}确认(resp,ShareTo)成功* @ P-配置{function}所有(resp,ShareTo)回调,无论成功与否都将执行*/function weixingeneralshare(data,callbacks){ callbacks =回调||{var general share = function(general,the data){//如果分享到朋友圈,则需要交换标题和desc If(general。共享到= = &;#39;时间线& amp#39;){ var title = theData.titlethedata . title = thedata . desc | | title;theData.desc = title}//与general . general Share({“appid & quot;:theData.appId?thedata . appid:& amp;#39;& amp#39;,& quotimg _ url & quot:theData.imgUrl,& quot链接& quot:Data.linkdesc & quot;:theData.desc标题& quot:theData.titleimg _ width & quot:& quot640 & quot,& quotimg _ height & quot:& quot640 & quot},function(resp){ switch(resp . err _ msg){//general _ share:取消用户取消case &;#39;取消& amp#39;:回调。取消和& ampcallbacks . cancel(resp,general . shareto);打破;// general_share:确认发送成功。#39;确认& amp#39;:案例& amp#39;通用共享:ok & amp#39;:回调。确认& amp& ampcallbacks . confirm(resp,general . shareto);打破;// general_share:发送失败案例失败&;#39;失败& amp#39;:默认值:callbacks.fail & amp& ampcallbacks . fail(resp,general . shareto);打破;}//回调。all &;& ampcallbacks . all(resp,general . shareto);});};weixinsbrbridge . on(& amp;#39;菜单:常规:共享& amp#39;,function(general){ if(callbacks . async & amp;& amp回调。就绪){ window【& quot;_ wx _ loadedCb _ & quot】= callbacks.dataLoaded ||新函数();if(窗口【& quot_ wx _ loadedCb _ & quot].toString()。index of(& quot;_ wx _ loadedCb _ & quot)& gt0){ window【& quot;_ wx _ loadedCb _ & quot】=新函数();} callbacks . data loaded = function(new data){ window【& quot;_ wx _ loadedCb _ & quot】(new data);generalShare(通用,new data);};//然后ready回调。ready& ampcallbacks . ready(general,general . shareto);} else {//就绪状态回调。ready& ampcallbacks . ready(general,general . shareto);generalShare(通用,数据);} });}/* * *注意(此功能只是临时添加的,由于权限限制无法使用。如果你的站点部署在*。QQ。com下,也许是可行的* @ param { String } appWeixinId微信微信官方账号ID * @param {Object} callbacks回调方法* @p-config {Function} fail(resp)失败* @ P-config { Function }。confirm(resp)成功*/function添加联系人(appweixinid,callbacks){ callbacks = callbacks | | { };weixinsbridge . invoke(& quot;添加联系人& quot,{ web type:& quot;1 & quot,用户名:appWeixinId },函数(resp){ var success =!resp.err _ msg | | & quot添加联系人:好的= = resp.err _ msg | | & quot添加联系人:已添加& quot= = resp.err _ msgif(success){ callbacks . success & amp;& amp回调。成功(resp);} else { callbacks.fail & amp& amp回调。失败(resp);} })}/* *调出微信原生的图片播放组件。*此处必须强有力地检测到参数。如果参数非法,将直接导致微信客户端当前播放的图像地址崩溃* * @ param { String } curSrc * @ param { Array } src list图像地址列表*/函数图像预览(curSrc,src list){ if(!curSrc ||!src list | | src list . length = = 0){ return;} weixinsbridge . invoke(& amp;#39;图像预览& amp#39;,{ & amp#39;当前和未来。#39;:curSrc & amp;#39;urls & amp#39;:srcList });}/* * *显示button */function showoptionmenu(){ weixinsbridge . call(&;#39;showOptionMenu & amp#39;);}/* * *隐藏网页右上角的按钮*/Function Hide option Menu(){ weixinsbridge . call(&;#39;隐藏选项菜单& amp#39;);}/* * *显示底部工具栏*/Function show toolbar(){ weixinsbridge . call(&;#39;显示工具栏& amp#39;);}/* * *隐藏底部工具栏*/Function Hide toolbar(){ weixinsbridge . call(&;#39;隐藏工具栏。#39;);}/* * *返回以下类型:* * network_type:wifi wifi网络* network_type:edge非wifi,包括3G/2G * network_type:无法断开网络连接* network_type:wwan 2g或3g * *用法:* weixiAPI。getnetworktype(function(网络类型){ * * });* * @ param callback */function getNetworkType(callback){ if(callback & amp;& amp回调类型= = & amp#39;功能与应用。#39;){ weixinsbridge . invoke(& amp;#39;getNetworkType & amp#39;,{},function(e){//Get e . err _ msg此处包含所有网络类型回调(e . err _ msg);});}}/* *关闭当前微信公众平台页面*/functionclosewindow(){ weixinsbridge . call(“closeWindow & quot;);}/* * *当页面被加载时,它将被执行。用法:*胃脘痛。ready(function(API){ *//从这里看,只有API是WeixinApi * });* @ param ready callback */function wxJsBridgeReady(ready callback){ if(ready callback & amp;& amptypeof readyCallback = = & amp#39;功能与应用。#39;){ var Api = thisvar wxReadyFunc = function(){ readyCallback(Api);};如果(窗口的类型。WeixinJSBridge = = & quot未定义& quot){ if(document . addevent listener){ document . addevent listener(& amp;#39;微信公众号。#39;,wxReadyFunc,false);} else if(document . attach event){ document . attach event(& amp;#39;微信公众号。#39;,wxReadyFunc);document . attach event(& amp;#39;onWeixinJSBridgeReady & amp#39;,wxReadyFunc);} } else { wxReadyFunc();} } }返回{ version:& quot;2.0 & quot,ready :wxJsBridgeReady,shareToTimeline:weixinShareTimeline,shareToWeibo:weixinsharewibo,shareToFriend:weixinSendAppMessage,general share:weixinGeneralShare,addContact :addContact,showOptionMenu :showOptionMenu,hideOptionMenu,showToolbar :showToolbar,hideotbar:getNetworkType:getNetworkType,imagePreview :imagePreview,close window:close window };})();

这几篇文章你可能也喜欢:

本文由主机参考刊发,转载请注明:使用js实现微信分享功能的示例代码(使用js实现微信分享功能的示例代码是什么) https://zhujicankao.com/113201.html

【腾讯云】领8888元采购礼包,抢爆款云服务器 每月 9元起,个人开发者加享折上折!
打赏
转载请注明原文链接:主机参考 » 使用js实现微信分享功能的示例代码(使用js实现微信分享功能的示例代码是什么)
主机参考仅做资料收集,不对商家任何信息及交易做信用担保,购买前请注意风险,有交易纠纷请自行解决!请查阅:特别声明

评论 抢沙发

评论前必须登录!