- nativeShare.js封装
-
/* * H5页面,客户端、微信分享组件 */ +function ($) { "use strict"; window.NativeShare = function(shareOptions){ this.oShareOption = $.extend(NativeShare.oShareOption, shareOptions); !this.oShareOption.link && (this.oShareOption.link = window.location.href); this.init(); } NativeShare.oShareOption = { title: '' , desc: '' , link: '' , imgUrl: '' // 初始化图片 , success: function(flag){} , cancel: function(){} , trigger: function(){} , backApp: function(){} }; NativeShare.prototype = { init: function(){ var self = this , supportCheck = self.fSupportCheck(); if( supportCheck.isWeixin ){ self.fWeixinShareInit(); } else if( supportCheck.isApp ){ self.fNativeShareInit(); } } // 检测客户端类型 , fSupportCheck: function(){ var ua = navigator.userAgent , isWeixin = (ua.toLowerCase().match(/MicroMessenger/i)=="micromessenger") ? true : false , isApp = ( ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) || ua.indexOf('Android') > -1 || ua.indexOf('Linux') > -1 ) ? true : false; return { isWeixin: isWeixin, isApp: isApp }; } // 原生分享配置 , fNativeShareInit: function(){ var self = this; /* 原生app */ // 分享文案 window.setShareOptions = function(type) { /*type:1 android type:2 ios*/ var options = { title: self.oShareOption.title , link: self.oShareOption.link , imgUrl: self.oShareOption.imgUrl , desc: self.oShareOption.desc }; if(type && type == 1) { // android方法 AndroidOBJ.getShareInfo(JSON.stringify(options)); return; } else{ return JSON.stringify(options); } }; // 分享回调(method,分享结果,channel:分享渠道) window.commonMethod = function(method, channel) { switch (method) { // 点击icon(就代表用户分享) case 'success': self.oShareOption.success(); break; // 取消分享 case 'cancel': self.oShareOption.cancel(); break; // 分享成功返回 case 'backApp': self.oShareOption.backApp(); break; default: break; } }; /* 原生app end */ /* 针对apicloud app */ apiready = function() { // 分享回调 window.shareCallback = function() { self.oShareOption.success(true); }; // 分享文案 window.setAppShareOptions = function() { var obj = { url: self.oShareOption.link , title: self.oShareOption.title , imgUrl: self.oShareOption.imgUrl , content: self.oShareOption.desc }; api.execScript({ name: 'webUrl' , script: 'setShareInfo(' + JSON.stringify(obj) + ');' }); }; }; /* 针对apicloud app end */ } // 微信分享配置 , fWeixinShareInit: function(){ var self = this , head = document.getElementsByTagName('head')[0] , script = document.createElement('script'); script.src = 'https://res.wx.qq.com/open/js/jweixin-1.0.0.js'; script.type = 'text/javascript'; script.onload = function() { // 获取微信配置参数 $.ajax({ url: '***' , type: 'get' , dataType: 'json' , data: {url: window.location.href} , success: function(data) { if( 0 == data.code ){ wx.config({ // debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId: data.data.appId, // 必填,公众号的唯一标识 timestamp: data.data.timestamp, // 必填,生成签名的时间戳 nonceStr: data.data.nonceStr, // 必填,生成签名的随机串 signature: data.data.signature,// 必填,签名,见附录1 jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 }); wx.ready(function () { wx.onMenuShareTimeline(self.oShareOption); wx.onMenuShareAppMessage(self.oShareOption); }); } } }); }; head.appendChild(script); } }; }(window.Zepto);
-
用法
var self=this;
new NativeShare({
title: ''
, desc: ''
, link: ''
, imgUrl: ''
, success: function(flag){
if(flag.errMsg == "sendAppMessage:ok"){
}else if(flag.errMsg == "shareTimeline:ok"){
}
}
});