|
|
/**
|
|
|
* 逛处理类
|
|
|
*/
|
|
|
|
|
|
'use strict';
|
|
|
const helpers = global.yoho.helpers;
|
|
|
|
|
|
/**
|
|
|
* [格式化资讯文章]
|
|
|
* @param {[array]} articleData [需要格式化的资讯数据]]
|
|
|
* @param {[Boolean]} showTag [是否显示左上角标签]]
|
|
|
* @param {Boolean} isApp [是否显示分享,在APP客户端里嵌入需要传url链接]
|
|
|
* @param {[Boolean]} showAuthor [控制是否显示作者信息]
|
|
|
* @param {[int]} uid [当前登录的用户ID]
|
|
|
* @param {[string]} reqQueryString [查询字符串]
|
|
|
* @return {[array | false]}
|
|
|
*/
|
|
|
const _formatArticle = (articleData, showTag, isApp, showAuthor, uid, reqQueryString) => {
|
|
|
// 资讯ID不存在,则不显示
|
|
|
if (!articleData['id']) {
|
|
|
return false;
|
|
|
}
|
|
|
var result = {
|
|
|
id: articleData['id'],
|
|
|
showTags: showTag,
|
|
|
img: articleData['src'] ? helpers.image(articleData['src'], 640, 640) : '',
|
|
|
url: isApp ? `${helpers.https(articleData['url'])}&openby:yohobuy={"action":"go.h5","params":{"param":{"id":"${articleData['id']}"},"shareparam":{"id":"${articleData['id']}"},"share":"/guang/api/v1/share/guang","id":${articleData['id']},"type":1,"url":"http:${helpers.urlFormat('/info/index', null, 'guang')}","islogin":"N"}}` : articleData['url'],
|
|
|
title: articleData['title'],
|
|
|
text: articleData['intro'],
|
|
|
publishTime: articleData['publish_time'],
|
|
|
pageView: articleData['views_num']
|
|
|
};
|
|
|
if (result['url'].includes('feature.yoho.cn') ||
|
|
|
result['url'].includes('cdn.yoho.cn')) {
|
|
|
result['url'] = _transHttpsUrl(result['url']);
|
|
|
}
|
|
|
|
|
|
// 收藏
|
|
|
if (isApp) {
|
|
|
result['collect'] = [];
|
|
|
result['collect']['isCollected'] = articleData['isFavor'] === true;
|
|
|
let originUrl = helpers.urlFormat('/author/index', null, 'guang') + reqQueryString; // 跳转回的链接
|
|
|
let collectUrl = 'javascript:;'; // 根据用户是否登录做处理的链接
|
|
|
if (!uid) {
|
|
|
let playUrlEncode = originUrl.replace(/\//g, '\\/');
|
|
|
collectUrl = `${originUrl}?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"${playUrlEncode}","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"N"}}`;
|
|
|
}
|
|
|
result['collect']['url'] = collectUrl;
|
|
|
} // 点赞
|
|
|
else {
|
|
|
result['like'] = {};
|
|
|
result['like']['count'] = articleData['praise_num'];
|
|
|
result['like']['isLiked'] = articleData['isPraise'] === 'Y';
|
|
|
}
|
|
|
|
|
|
if (isApp && articleData['share']['url']) {
|
|
|
|
|
|
// 分享链接
|
|
|
result['share'] = `${articleData['share']['url']}?openby:yohobuy={"action":"go.share","params":{"title":"${articleData['title']}","content":"${articleData['intro']}","url":"${articleData['share']['url']}","pic":"https:${result['img']}"}}`;
|
|
|
}
|
|
|
|
|
|
// 判断是否显示作者信息
|
|
|
if (showAuthor && articleData['author']) {
|
|
|
if (!isApp) {
|
|
|
articleData['author']['url'] = _getFilterUrl(articleData['author']['url']);
|
|
|
}
|
|
|
|
|
|
// 编辑人员 app跳转url处理 20160601
|
|
|
let isLogin = uid ? true : false;
|
|
|
articleData['author']['url'] = `${helpers.https(articleData['author']['url'])}&openby:yohobuy={"action":"go.h5","params":{"param":{"id":"${articleData['author']['author_id']}"},"share":"","id":${articleData['author']['author_id']},"type":0,"islogin":"${isLogin} ","url":"https:${helpers.urlFormat('/author/index', {uid: uid}, 'guang')} "}}&uid=${uid}`;
|
|
|
result['author'] = articleData['author'];
|
|
|
if (result['author']['avatar']) {
|
|
|
result['author']['avatar'] = result['author']['avatar'].replace('http://', '//');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 模板中需要的标签标识
|
|
|
if (showTag && articleData['category_id']) {
|
|
|
switch (articleData['category_id']) {
|
|
|
case '1': // 话题
|
|
|
result['isTopic'] = true;
|
|
|
break;
|
|
|
case '2': // 搭配
|
|
|
result['isCollocation'] = true;
|
|
|
break;
|
|
|
case '3': // 潮人
|
|
|
result['isFashionMan'] = true;
|
|
|
break;
|
|
|
case '4': // 潮品
|
|
|
result['isFashionGood'] = true;
|
|
|
break;
|
|
|
case '5': // 小贴士
|
|
|
result['isTip'] = true;
|
|
|
break;
|
|
|
case '19': // 专题
|
|
|
result['isSpecialTopic'] = true;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
return result;
|
|
|
};
|
|
|
|
|
|
|
|
|
/**
|
|
|
* [将首字符为//的url转换为http://]
|
|
|
* @param {[string]} url [需要转换的url]
|
|
|
* @return {[string]} [description]
|
|
|
*/
|
|
|
const _transHttpsUrl = (url) => {
|
|
|
return url.replace(/^\/\//g, 'http://');
|
|
|
};
|
|
|
const _getFilterUrl = (url) => {
|
|
|
url = url.replace('.m.yohobuy.com', global.yoho.config.subDomains.host).replace('www.yohobuy.com', global.yoho.config.siteUrl);
|
|
|
|
|
|
const whiteDomains = ['m.yohobuy.com', 'cdn.yoho.cn/myohobuy'];
|
|
|
const blackDomains = ['sale.m.yohobuy.com', 'cuxiao.m.yohobuy.com', 'activity.m.yohobuy.com', 'huodong.m.yohobuy.com', '/home/orders/pay'];
|
|
|
|
|
|
if (whiteDomains.every(_ => url.includes(_)) &&
|
|
|
blackDomains.every(_ => !url.includes(_))) {
|
|
|
url = url.replace('http://', '//');
|
|
|
}
|
|
|
|
|
|
if (url.includes('feature.yoho.cn')) {
|
|
|
url = _transHttpsUrl(url);
|
|
|
}
|
|
|
if (url.includes('openby:yohobuy=')) {
|
|
|
let filters = ['openby:yohobuy=', '&', '?'];
|
|
|
filters.forEach(_ => {
|
|
|
url = url.subString(0, url.lastIndexOf(_));
|
|
|
});
|
|
|
return url.replace(/(^\s*)|(\s*$)/g, '');
|
|
|
} else {
|
|
|
return url;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
module.exports = {
|
|
|
formatArticle: _formatArticle,
|
|
|
transHttpsUrl: _transHttpsUrl,
|
|
|
getFilterUrl: _getFilterUrl
|
|
|
}; |
...
|
...
|
|