wechat.js
2.9 KB
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
// import request from './request'; //笔者自己封装的网络请求类,也可以直接使用uni.request
var jweixin = require('jweixin-module');
export default {
//初始化跳转小程序配置
initJssdkXcx:function(){
uni.request({
url:'https://car.goorcar.com/api/h5/getJsSdkConfig',
method:'post',
data:{
url:window.location.href.split('#')[0],
api:'checkJsApi,wx-open-launch-weapp'
},
success: (res) => {
console.log(res)
if(res.data){
jweixin.config({
debug: false,
appId: res.data.data.appId,
timestamp: res.data.data.timestamp,
nonceStr: res.data.data.nonceStr,
signature: res.data.data.signature,
jsApiList: [
'wx-open-launch-weapp'
],
openTagList: ['wx-open-launch-weapp']
});
}
}
})
},
//初始化分享sdk配置
initJssdkShare: function(callback, url) {
//服务端进行签名 ,可使用uni.request替换。 签名算法请看文档
uni.request({
url:'http://obituaries.t.brotop.cn/api/Common/getJsSdkConfig',
method:'post',
data:{
url: url,
api:'updateTimelineShareData,updateAppMessageShareData,getLocation'
},
success: (res) => {
if (res.data) {
jweixin.config({
debug: false,
appId: res.data.data.config.appId,
timestamp: res.data.data.config.timestamp,
nonceStr: res.data.data.config.nonceStr,
signature: res.data.data.config.signature,
jsApiList: [
'checkJsApi',
'updateTimelineShareData',
'updateAppMessageShareData',
'getLocation'
]
});
//配置完成后,再执行分享等功能
if (callback) {
callback(res.data.data.config);
}
}
}
})
},
//在需要自定义分享的页面中调用
share: function(callback,data, url) {
let configUrl = url.split('#')[0]
if (!this.isWechat()) {
return;
}
//每次都需要重新初始化配置,才可以进行分享
this.initJssdkShare(function(signData) {
jweixin.ready(function() {
var shareData = {
title: data && data.title ? data.title : signData.site_name,
desc: data && data.desc ? data.desc : signData.site_description,
link: url,
imgUrl: data && data.img ? data.img : signData.site_logo,
//设置成功
success: function(res) {
callback(res)
}
};
//分享给朋友接口
// jweixin.updateAppMessageShareData(shareData);
//分享到朋友圈接口
jweixin.updateTimelineShareData(shareData);
});
}, configUrl);
},
}