share-handler.js
3.64 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
138
139
140
141
142
143
144
145
146
/* eslint-disable indent */
import { get, first } from 'lodash';
import emojiRegex from 'emoji-regex';
function _version2num(version) {
if (!version) {
return 0;
}
let [m, j, b] = version.split('.');
return +m * 10000 + +j * 100 + +b;
}
// eslint-disable-next-line no-unused-vars
function versionCompare(left, right) {
let leftNum = _version2num(left);
let rightNum = _version2num(right);
if (leftNum === rightNum) {
return 0;
} else if (leftNum > rightNum) {
return 1;
} else {
return -1;
}
}
const DEFAULT_SHARE_IMAGE =
'http://static.yohobuy.com/m/v1/img/touch/apple-touch-icon-144x144-precomposed-new.png';
function handleProtocol(url) {
if (url.indexOf('//') < 0 || url.indexOf('http') >= 0) {
return url;
}
url = url.split('//');
url[0] = 'https:';
return url.join('//');
}
const filterEmoji = text => {
text = text.replace(emojiRegex(), '');
text = text.replace(/\ud83c|\ud83d|\ud83e/gi, '');
return text;
};
// eslint-disable-next-line no-unused-vars
const getDetailShareData = (article, app_version = '6.9.11') => {
let shareImage = '';
let desc = '';
if (article.sort === 2) {
shareImage = article.coverImage || '';
desc = article.articleTitle || '';
} else if (article.sort === 4) {
shareImage = article.coverUrl || '';
desc = article.content || '';
} else {
let blockList = get(article, 'blockList', []);
shareImage = get(
first(blockList.filter(block => block.templateKey === 'image')),
'contentData',
'',
);
desc = get(
blockList.filter(block => block.templateKey === 'text'),
'[0].contentData',
'',
).substr(0, 50);
}
if (shareImage && shareImage.indexOf('//') >= 0) {
shareImage = `${window ? window.location.protocol : ''}//${
shareImage.split('//')[1]
}`;
}
// eslint-disable-next-line no-unused-vars
const requiredVersion = '6.9.11';
let hideType = ['7', '8', '9'];
if (versionCompare(app_version, requiredVersion) >= 0) {
hideType = ['8', '9'];
}
let authorName = article.authorName || '';
// 过滤掉换行
// desc = ClearBr(desc);
// authorName = ClearBr(article.authorName || '');
// 过滤掉emoji表情
desc = filterEmoji(desc);
authorName = filterEmoji(article.authorName || '');
return {
title: `@${authorName} 在有货社区上发了一篇内容,快点开看看!`,
imgUrl: handleProtocol(
get(shareImage.split('?'), '[0]') || DEFAULT_SHARE_IMAGE,
),
link: handleProtocol(
`${window ? window.location.origin : ''}/grass/article/${
article.articleId
}?share=true&t=${Date.now()}`,
),
desc: `${desc}`,
hideType,
shareType: 'grassDetail',
userName: authorName,
userIcon: article.authorHeadIco,
articleId: article.articleId,
authGroupId: article.authGroupId,
};
};
const getTopicShareData = topic => {
return {
title: topic.topicName,
imgUrl: handleProtocol(topic.topicImageUrl),
link: handleProtocol(
`${location.origin}/grass/topic/${topic.topicId}?share=true`,
),
desc: '我在有货的社区发现一个热门话题。' + topic.topicDesc,
hideType: ['7', '8', '9'],
};
};
const getAuthorShareData = author => {
return {
title: `@${author.nickName} 在YO!社区,一起来玩潮流!`,
imgUrl: handleProtocol(
get(author, 'headIco', '').split('?')[0] || DEFAULT_SHARE_IMAGE,
),
link: handleProtocol(
`${location.origin}/grass/author/${author.authorType}/${author.authorUid}?share=true`,
),
desc: author.signature || '',
hideType: ['7', '8', '9'],
};
};
export { getDetailShareData, getTopicShareData, getAuthorShareData };