share-handler.js
1.63 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
import {get, first} from 'lodash';
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 getDetailShareData = (article) => {
let shareImage = '';
let desc = '';
if (article.sort === 2) {
shareImage = article.coverImage;
desc = article.articleTitle;
} 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', '');
}
if (shareImage && shareImage.indexOf('//') >= 0) {
shareImage = `${window ? window.location.protocol : ''}//${shareImage.split('//')[1]}`;
}
return {
title: `@${article.authorName} 在有货社区上发了一篇内容,快点开看看!`,
imgUrl: handleProtocol(get(shareImage.split('?'), '[0]') || DEFAULT_SHARE_IMAGE),
link: handleProtocol(`${window ? window.location.origin : ''}/grass/article/share/${article.articleId}`),
desc,
hideType: ['7', '8', '9']
};
}
const getTopicShareData = (topic) => {
return {
title: topic.topicName,
imgUrl: handleProtocol(topic.topicImageUrl),
link: handleProtocol(`${location.origin}/grass/topic/share/${topic.topicId}`),
desc: '我在有货的社区发现一个热门话题。' + topic.topicDesc,
hideType: ['7', '8', '9']
};
}
export {
getDetailShareData,
getTopicShareData
};