wechat-shares
Version:
这是一个针对微信分享好友以及微信分享朋友圈的封装插件,不需要引入微信js以及jq等库,采用原生ajax动态加载最新的微信js,实现微信分享功能及回调函数处理等,并且处理微信二次分享,能够多次分享朋友圈。1.4.0版本增加对后端解码,前端去掉参数过滤。
1 lines • 3.98 kB
JavaScript
var wxShare=function(e){for(var t in this.options={title_swx:"自定义分享标题",desc_swx:"自定义分享内容描述",shareImgs:"",serverUrl:""},e=e||{})e.hasOwnProperty(t)&&(this.options[t]=e[t]);this._init()};wxShare.prototype={_init:function(){document.title=this.options.title_swx,this.creatWXjs()},creatWXjs:function(){var e=this,t=document.createElement("script");t.src="//res.wx.qq.com/open/js/jweixin-1.4.0.js",t.onload=function(){e.getTicket()},document.body.appendChild(t)},refreshUrl:function(){var e="",t="";if(this.fnGetQueryString("from")){var n=(e=this.funcUrlDel("from")).indexOf("?");t=e.substr(n),this.getlinkSearch("isappinstalled",t)?(e=e.substr(0,e.indexOf("?")),window.location.href=e):window.location.href=e}},fnGetQueryString:function(e){var t=new RegExp("(^|&)"+e+"=([^&]*)(&|$)"),n=window.location.search.substr(1).match(t);return!!n&&decodeURIComponent(n[2])},funcUrlDel:function(e){var t=window.location,n=t.origin+t.pathname+"?",o=t.search.substr(1);if(o.indexOf(e)>-1){for(var r={},s=o.split("&"),a=0;a<s.length;a++)s[a]=s[a].split("="),r[s[a][0]]=s[a][1];return delete r[e],n+JSON.stringify(r).replace(/[\"\{\}]/g,"").replace(/\:/g,"=").replace(/\,/g,"&")}},getlinkSearch:function(e,t){var n=new RegExp("(^|&)"+e+"=([^&]*)(&|$)"),o=t.substr(1).match(n);return!!o&&decodeURIComponent(o[2])},getTicket:function(){var e=this,t=e.options;ajaxurl=t.serverUrl,title_swx=t.title_swx,desc_swx=t.desc_swx,shareImgs=t.shareImgs,links=e.trimStr(location.href.split("#")[0]),encodeurl=encodeURIComponent(e.trimStr(location.href.split("#")[0])),this.wxAjax({url:ajaxurl,data:{urll:encodeurl,post_type:"json"},type:"POST",dataType:"json",success:function(t){e.setShare(t,title_swx,desc_swx,shareImgs,links)},error:function(){console.log("通信失败")}})},setShare:function(e,t,n,o,r){wx.config({debug:!1,appId:e.appId,timestamp:e.timestamp,nonceStr:e.nonceStr,signature:e.signature,jsApiList:["onMenuShareTimeline","onMenuShareAppMessage"]}),wx.ready(function(){wx.onMenuShareAppMessage({title:t,desc:n,link:r,imgUrl:o,type:"",dataUrl:"",success:function(){},cancel:function(){}}),wx.onMenuShareTimeline({title:t,link:r,imgUrl:o,success:function(){},cancel:function(){}}),wx.error(function(e){console.log(e)})})},wxAjax:function(e){(e=e||{}).data=e.data||{};e.jsonp?this.ajaxJsonp(e):this.ajaxJson(e)},ajaxJson:function(e){e.type=(e.type||"GET").toUpperCase(),e.data=this.formatParams(e.data);var t=null;(t=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObjcet("Microsoft.XMLHTTP")).onreadystatechange=function(){if(4==t.readyState){var n=t.status;if(n>=200&&n<300){var o="",r=t.getResponseHeader("Content-type");o=-1!==r.indexOf("xml")&&t.responseXML?t.responseXML:"json"==e.dataType||"application/json"===r?JSON.parse(t.responseText):t.responseText,e.success&&e.success(o)}else e.error&&e.error(n)}},"GET"==e.type?(t.open(e.type,e.url+"?"+e.data,!0),t.send(null)):(t.open(e.type,e.url,!0),t.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"),t.send(e.data))},ajaxJsonp:function(e){var t=e.jsonp+this.random(),n=document.getElementsByTagName("head")[0];e.data.callback=t;var o=this.formatParams(e.data),r=document.createElement("script");n.appendChild(r),window[t]=function(o){n.removeChild(r),clearTimeout(r.timer),window[t]=null,e.success&&e.success(o)},r.src=e.url+"?"+o,e.time&&(r.timer=setTimeout(function(){window[t]=null,n.removeChild(r),e.error&&e.error({message:"超时"})},time))},formatParams:function(e){var t=[];for(var n in e)t.push(encodeURIComponent(n)+"="+encodeURIComponent(e[n]));return t.push("v="+this.random()),t.join("&")},random:function(){return Math.floor(1e4*Math.random()+500)},trimStr:function(e){return e.replace(/(^\s*)|(\s*$)/g,"")}},"object"==typeof module&&"object"==typeof module.exports?module.exports=wxShare:"function"==typeof define&&define.amd&&define("wxShare",[],function(){return wxShare}),"object"==typeof window&&"object"==typeof window.document&&(window.wxShare=wxShare);