123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- import http from './http';
- var share =
- {
- wechat : false,
- url : '',
- project : '',
- token : '',
- param : {
- title : '',
- pic : '',
- content : '',
- link : '',
- },
- wx : {},
-
- //微信提醒
- wxTip : function(type) {
- var wx = this.is_weixin();
- if (wx) {
- var img = 'live_weixin';
- if (type) {
- img = 'weixin-guide';
- }
- var tip = '<div id="weixin-tip" style="position: fixed; left:0; top:0; background: rgba(0,0,0,0.8); filter:alpha(opacity=80); width: 100%; height:100%; z-index: 100;" onclick="document.getElementById(\'weixin-tip\').remove()"><p style="text-align: center; margin-top: 10%; padding:0 5%;"><img src="static/dever/'+img+'.png" alt="微信打开" style="max-width: 100%; height: auto;"/></p></div>';
- this.html(tip);
- return true;
- }
- return false;
- },
-
- //通用的分享功能
- share : function(provider, scene, type, data, success, error) {
- if (this.source == 'h5') {
- var weixin = this.wxTip(1);
- if (!weixin) {
- this.alert('请点击复制按钮');
- }
- } else {
- var config = {
- provider: provider,
- type: type,
- scene: scene,
- href: data.link,
- title: data.title,
- summary: data.content,
- imageUrl: data.pic,
- success: function(res) {
- if (success) {
- success(res);
- } else {
- console.log("success:" + JSON.stringify(res));
- }
- },
- fail: function(err) {
- if (error) {
- success(err);
- } else {
- console.log("fail:" + JSON.stringify(err));
- }
- }
- };
- if (data.media) {
- config.mediaUrl = media;
- }
- if (this.source == 'applet') {
- if (!data.path) {
- console.log('data.path未定义!');
- return;
- }
- if (!data.applet_id) {
- console.log('data.applet_id未定义!');
- return;
- }
- config.miniProgram = {};
- config.miniProgram.id = data.applet_id;
- config.miniProgram.path = data.path;
- config.miniProgram.type = 0;
- config.miniProgram.webUrl = data.link;
- }
- uni.share(config);
- }
- },
-
- init : function(weixin, wx, project, host, param)
- {
- var self = this;
- self.wx = wx;
- self.wechat = weixin;
- self.project = project;
- self.url = host;
- if (!host) {
- self.url = config.share.host;
- }
-
- self.token = http.getToken();
- self.param = param;
- self.param.timelineCallback = function() {};
- self.param.sendMessageCallback = function() {};
- self.param.shareQQCallback = function() {};
- if (!self.param.source_link) {
- self.param.source_link = location.href;
- }
- if (self.wechat) {
- self.wechatInit();
- }
- self.reflux();
- }
- ,wechatInit: function()
- {
- var self = this;
- var url = this.url + 'init?callback=?';
- var param = this.param;
- var project = this.project;
- var options = {};
- options.project = project;
- options.url = location.href.split('#')[0];
- options.noloading = 1;
- options.token = this.token;
- http.request('post', url, options, function(data) {
- self.wx.config({
- //debug:true,
- appId: data.appId,
- timestamp: data.timestamp,
- nonceStr: data.nonceStr,
- signature: data.signature,
- jsApiList: [
- 'onMenuShareTimeline',
- 'onMenuShareAppMessage',
- 'onMenuShareQQ',
- 'updateAppMessageShareData',
- 'updateTimelineShareData'
- ]
- });
-
- self.wx.ready(function() {
- self.wx.updateAppMessageShareData({
- title: param.title,
- desc: param.content,
- link: param.source_link,
- imgUrl: param.pic,
- success: function(res) {
-
- },
- });
- self.wx.updateTimelineShareData({
- title: param.title,
- link: param.source_link,
- imgUrl: param.img,
- success: function(res) {
-
- },
- });
- self.wx.onMenuShareAppMessage({
- title: param.title,
- desc: param.content,
- link: param.source_link,
- imgUrl: param.pic,
- success: function(res) {
- self.shareLog(1, 1);
- },
- cancel: function(res) {
- self.shareLog(1, 2);
- },
- fail: function(res) {
- self.shareLog(1, 3);
- }
- });
- self.wx.onMenuShareTimeline({
- title: param.title,
- link: param.source_link,
- imgUrl: param.img,
- success: function(res) {
- self.shareLog(2, 1);
- },
- cancel: function(res) {
- self.shareLog(2, 2);
- },
- fail: function(res) {
- self.shareLog(2, 3);
- }
- });
- self.wx.onMenuShareQQ({
- title: param.title,
- link: param.source_link,
- imgUrl: param.img,
- success: function(res) {
- self.shareLog(3, 1);
- },
- cancel: function(res) {
- self.shareLog(3, 2);
- },
- fail: function(res) {
- self.shareLog(3, 3);
- }
- });
- });
- });
- }
- ,reflux: function() {
- var refer = document.referrer,
- url = encodeURIComponent(document.location.href),
- param = location.search.substr(1),
- ua = encodeURIComponent(navigator.userAgent),
- project = this.project,
- token = this.token,
- wechat = this.wechat;
- if (param.indexOf("tsina-") > -1 || param.indexOf("timeline") > -1 || param.indexOf("singlemessage") > -1 || param.indexOf("groupmessage") > -1) {
- var url = this.url + 'reflux&callback=?' + '&project='+project+'&url=' + url + '&ua=' + ua + '¶m=' + encodeURIComponent(param) + '&token=' + token + '&type=' + wechat;
- http.request('post', url, {noloading:1});
- }
- }
- ,shareLog: function(actType, actResult) {
- var project = this.project;
- var token = this.token;
- var wechat = this.wechat;
- var ua = encodeURIComponent(navigator.userAgent);
- var url = encodeURIComponent(document.location.href);
- var url = this.url + 'collect&callback=?' + '&project='+project+'&url=' + url + '&actType=' + actType + '&actResult=' + actResult + '&ua=' + ua + '&token=' + token + '&type=' + wechat
- http.request('post', url, {noloading:1});
- }
- }
- export default share;
|