shareTools.ts
5.13 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import SDKUtils from "../wxsdk/utils/SDKUtils";
import WXSDK from "../wxsdk/WXSDK";
import { Analytics } from "./Analytics";
import SDKShare from "../wxsdk/share/SDKShare";
import { ShareVideoError } from "../wxsdk/base/SDKConst";
/*
* 分享与视频工具类;
*/
export class ShareTools {
/**
* 是否进入分享 onShow有插屏的话用这个判断
*/
public static onShowAd: boolean = false;
public static isTest: boolean = false;
/**
* 验证分享:可处理成功、失败
* @param shareKey
* @param params params.fail 有就不处理,没有自动处理
* @param opts 目前支持4个key 1,title自定义分享标题 2,img_url自定义分享图片 3,share_type(不走后台配置写死走视频or分享。1分享2视频3无视频则分享)4,closeSimulate是否关闭模拟分享
*/
static share(shareKey: string, params?: { success?: Function, fail?: Function, context?: any }, opts?: any) {
if (typeof my === 'undefined' || this.isTest) {
params && params.success && params.success();
return
}
let time = 1500;
let self = this;
if (SDKUtils.compareVersion(my.env.clientVersion, '10.3.70') < 0) {
Analytics.I.dot('videoFail', { "from": 'versionLow' });
my.showToast({
type: 'none',
content: '当前版本不支持视频广告哦,请您升级支付宝版本',
duration: time,
});
return;
}
this.onShowAd = true;
WXSDK.share.share(shareKey.toString(), params, opts).then(async res => {
this.onShowAd = false;
params && params.success && params.success(res);
}).catch(async videoErr => {
console.log(JSON.stringify(videoErr));
let showShare = !(videoErr.code == ShareVideoError.VideoPlaying.code || videoErr.code == ShareVideoError.VideoQuit.code);
if (showShare && SDKUtils.compareVersion(my.env.clientVersion, '10.1.75') > 0) {
SDKShare.I.share(shareKey, params, opts).then(success => {
params && params.success && params.success(success);
Analytics.I.dot('videoFail', { "from": 'share_success' });
}).catch(shareErr => {
console.log(`share fail${JSON.stringify(shareErr)}`)
Analytics.I.dot('videoFail', { "from": 'share_fail' });
self.onShowAd = false;
if (!params || !params.fail) {
if (cc.sys.platform === cc.sys.WECHAT_GAME) {
wx.showToast({
title: shareErr.msg,
icon: 'none',
duration: time
})
} else if (cc.sys.platform === cc.sys.ALIPAY_GAME) {
Analytics.I.dot('videoFail', { "from": 'other3_' + shareErr.code });
my.showToast({
content: shareErr.msg,
type: 'none',
duration: 1500
})
}
} else {
Analytics.I.dot('videoFail', { "from": 'other4_' + shareErr.code });
params && params.fail && params.fail(shareErr);
}
})
} else {
Analytics.I.dot('videoFail', { "from": 'share_versionLow' });
this.onShowAd = false;
if (!params || !params.fail) {
if (cc.sys.platform === cc.sys.WECHAT_GAME) {
wx.showToast({
title: videoErr.msg,
icon: 'none',
duration: time
})
} else if (cc.sys.platform === cc.sys.ALIPAY_GAME) {
Analytics.I.dot('videoFail', { "from": 'other1_' + videoErr.code });
my.showToast({
content: videoErr.msg,
type: 'none',
duration: 1500
})
}
} else {
Analytics.I.dot('videoFail', { "from": 'other2_' + videoErr.code });
params && params.fail && params.fail(videoErr);
}
}
})//this.buildParams(params)
}
/**
* 纯净分享 不处理回调
* @param shareKey
*/
static pureShare(shareKey: string, opts = {}) {
this.share(shareKey, {}, { closeSimulate: true, ...opts });
}
/**
* 设置右上角分享key
* @param key
*/
static setForwardKey(key) {
WXSDK.share.setForwardKey(key)
}
/**
* 获取分享还是视频
* @param key
*/
static getShareType(key) {
try {
return WXSDK.share.getType(key)
} catch (err) {
console.log("1")
return 2
}
}
}
export enum ShareKey {
}