1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// 微信分享 公共方法
// var jweixin = require('jweixin-module');
export default {
// 是否在微信中
isWechat: function() {
const ua = window.navigator.userAgent.toLowerCase();
if (ua.match(/micromessenger/i) == 'micromessenger') {
return true;
} else {
return false;
}
},
//初始化sdk配置
initJssdkShare: function(callback, url) {
uni.request({
url: '/uni/api/weixin/get_wx_sign',
method: 'POST',
data: {url: url},
dataType: 'json',
success: (res) => {
const { data } = res;
if(data.code === 0){
jWeixin.config({
debug: false,
appId: data.data.appId,
timestamp: data.data.timestamp,
nonceStr: data.data.noncestr,
signature: data.data.signature,
jsApiList: [
'checkJsApi',
//分享到朋友圈
'onMenuShareTimeline',
// 'updateAppMessageShareData',
//分享给朋友
'onMenuShareAppMessage',
// 'updateTimelineShareData',
//分享到QQ空间
'onMenuShareQZone',
//分享到腾讯微博
'onMenuShareWeibo',
'getLocation'
]
});
//配置完成后,再执行分享等功能
if (callback) {
callback();
}
}
}
});
},
// 自定义分享页面调用方法
share: function(data, url) {
url = url ? url : window.location.href;
if (!this.isWechat()) {
return;
}
//每次都需要重新初始化配置,才可以进行分享
this.initJssdkShare(function(){
jWeixin.ready(function() {
var shareData = {
title: data && data.title ? data.title : '谛宝多多商城',
desc: data && data.desc ? data.desc : '宠物行业一站式B2B采购平台',
link: url,
imgUrl: data && data.img ? data.img : 'https://dbc-static.oss-cn-beijing.aliyuncs.com/credit_shop/global/logo.jpg',
success: function(res) {
//用户点击分享后的回调,这里可以进行统计,例如分享送金币之类的
// post('/api/member/share');
},
cancel: function(res) {}
};
//分享给朋友接口
jWeixin.onMenuShareAppMessage(shareData);
//分享到朋友圈接口
jWeixin.onMenuShareTimeline(shareData);
// 分享到QQ
jWeixin.onMenuShareQQ(shareData);
// 分享到微博
jWeixin.onMenuShareWeibo(shareData);
});
}, url);
},
// 向小程序发送分享的数据
postData: function(t){
// imgUrl: image || 'https://dbc-static.oss-cn-beijing.aliyuncs.com/wx/logo.jpg'
const postData = {
link: window.location.href,
title: t
}
jWeixin.miniProgram.postMessage({ data: JSON.stringify(postData) });
},
isMini: function(){
jWeixin.miniProgram.getEnv(function (res) {
//获取当前环境
if(res.miniprogram){
// true 在微信小程序中
return true;
}else{
// false 在微信公众号里
return false;
}
});
}
}