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 = '
';
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;