share.js
1.35 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
/** 分享
* @param {*} shareTitle 分享标题
* @param {*} sharePath 分享页面路径 不传默认当前页
* @param {*} shareImage 分享图片 不传默认当前页面截图
* @param {*} shareQuery 分享页面参数
* @param {*} noPageAuth 分享页面是否需要登录拦截
* @returns
*/
class Share {
constructor() {
console.log('---------------- 分享初始化 ----------------')
this.initPageProxy()
}
/**全局注册事件 */
initPageProxy() {
let thisPage = Page
let _this = this
Page = function (el) {
try {
el || (el = {}), _this.pageLeaveProxy(el), thisPage.apply(this, arguments)
} catch (el) {
thisPage.apply(this, arguments)
}
}
}
/**设置全局分享 */
pageLeaveProxy(page) {
if (page.onShareAppMessage) {
page.onShareAppMessage = function () {
let {
shareTitle,
sharePath,
shareImage,
shareQuery,
noPageAuth=true
} = this,
path = `/${this.route}`
if (sharePath) path = `/${sharePath}`
if (shareQuery) path += `?${shareQuery}`
if (!noPageAuth) path = `/login/loading/loading?url=${encodeURIComponent(path)}`
console.log(path)
return {
path,
title: shareTitle,
imageUrl: shareImage
}
}
}
}
}
new Share()