Authored by 郭成尧

ctx-add

... ... @@ -151,7 +151,7 @@ app.use((req, res, next) => {
next();
});
// 添加请求上下文
// 添加请求上下文
app.use(global.yoho.httpCtx());
// dispatcher
... ...
... ... @@ -7,7 +7,7 @@
const mRoot = '../models';
const typeLib = require('../../../config/type-lib');
const indexModel = require(`${mRoot}/index`);
const IndexModel = require(`${mRoot}/index`);
const headerModel = require('../../../doraemon/models/header'); // 头部model
const footerModel = require('../../../doraemon/models/footer_tab'); // 底部tab
const guangProcess = require(`${global.utils}/guang-process`);
... ... @@ -44,8 +44,8 @@ const editor = (req, res, next) => {
};
}
return Promise.all([
indexModel.getAuthor(id),
indexModel.getArticleList(gender, 0, uid, udid, 1, null, id)]
req.ctx(IndexModel).getAuthor(id),
req.ctx(IndexModel).getArticleList(gender, 0, uid, udid, 1, null, id)]
).then(datas => {
let authorData = datas[0],
articleListData = datas[1];
... ... @@ -157,7 +157,7 @@ const pageData = (req, res, next) => {
if (!authorId && isNaN(authorId)) {
showAuthor = true;
}
return indexModel.getPageData(gender,
return req.ctx(IndexModel).getPageData(gender,
sortId,
uid,
udid,
... ... @@ -204,7 +204,7 @@ const index = (req, res, next) => {
gender: req.query.gender || req.query.channel && typeLib.gender[req.query.channel] || '1,3'
};
indexModel.getArticle(param).then(result => {
req.ctx(IndexModel).getArticle(param).then(result => {
if (result && result.guang && result.guang.infos) {
if (!result.guang.infos.length) {
res.set('Cache-Control', 'no-cache');
... ... @@ -250,7 +250,7 @@ const tag = (req, res, next) => {
responseData.pageHeader.navTitle = param.tag || '标签';
indexModel.getTagEditor(param).then(result => {
req.ctx(IndexModel).getTagEditor(param).then(result => {
res.render('index/list', Object.assign(responseData, result));
}).catch(next);
};
... ... @@ -281,7 +281,7 @@ const listDynamicData = (req, res) => {
other.type = type;
}
indexModel.getDynamicDataByIds(ids, udid, other).then(ret => {
req.ctx(IndexModel).getDynamicDataByIds(ids, udid, other).then(ret => {
res.send(ret);
});
};
... ... @@ -297,7 +297,7 @@ const detailDynamicData = (req, res) => {
uid = req.user.uid || req.query.uid,
udid = req.sessionID;
indexModel.getDynamicDataById(id, uid, udid).then((ret) => {
req.ctx(IndexModel).getDynamicDataById(id, uid, udid).then((ret) => {
res.status(200).send(ret);
}).catch(() => {
res.status(400);
... ...
... ... @@ -11,354 +11,351 @@ const helpers = global.yoho.helpers;
const guangProcess = require(`${global.utils}/guang-process`);
const _ = require('lodash');
/**
* [获取作者信息]
* @param {[int]} id [作者id]
* @return {[object]}
*/
const getAuthor = (id) => {
return serviceAPI.get('guang/service/v1/author/getAuthor', {
author_id: id
}, {
cache: true
}).then((result) => {
if (result && result.code === 200) {
return result;
} else {
logger.error('getAuthor code no 200');
return {};
}
});
};
class IndexModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* 逛分类
*/
const _category = () => {
return serviceAPI.get('/guang/api/v1/category/get', {
/**
* [获取作者信息]
* @param {[int]} id [作者id]
* @return {[object]}
*/
getAuthor(id) {
return serviceAPI.get('guang/service/v1/author/getAuthor', {
author_id: id
}, {
cache: true
}).then((result) => {
if (result && result.code === 200) {
return result;
} else {
logger.error('getAuthor code no 200');
return {};
}
});
}
}, {
cache: true,
code: 200
});
};
/**
* [逛内容列表]
* @param {[string]} gender ["1,3"表示男, "2,3"表示女, "1,2,3"表示所有]
* @param {[int]} sortId [分类ID]
* @param {Number} uid [用户ID]
* @param {String} udid [客户端唯一标识]
* @param {Number} page [分页第几页, 默认第1页]
* @param {[string]} tag [标签]
* @param {[int]} authorId [作者ID]
* @param {[int]} limit [返回的限制数]
* @param {Boolean} useCache [是否使用缓存]
* @return {[array]}
*/
getArticleList(gender, sortId, uid, udid, page, tag, authorId, limit, useCache) {
let param = {
page: page || 1,
uid: uid || 0,
udid: udid || '',
gender: gender || '',
sort_id: sortId,
tag: tag,
author_id: authorId,
limit: 4
};
/**
* [逛内容列表]
* @param {[string]} gender ["1,3"表示男, "2,3"表示女, "1,2,3"表示所有]
* @param {[int]} sortId [分类ID]
* @param {Number} uid [用户ID]
* @param {String} udid [客户端唯一标识]
* @param {Number} page [分页第几页, 默认第1页]
* @param {[string]} tag [标签]
* @param {[int]} authorId [作者ID]
* @param {[int]} limit [返回的限制数]
* @param {Boolean} useCache [是否使用缓存]
* @return {[array]}
*/
const getArticleList = (gender, sortId, uid, udid, page, tag, authorId, limit, useCache) => {
let param = {
page: page || 1,
uid: uid || 0,
udid: udid || '',
gender: gender || '',
sort_id: sortId,
tag: tag,
author_id: authorId,
limit: 4
};
return serviceAPI.get('guang/api/v2/article/getList', param, {
cache: useCache
}).then((result) => {
if (result && result.code === 200) {
return result;
} else {
logger.error('getAuthor code no 200');
return [];
}
});
return serviceAPI.get('guang/api/v2/article/getList', param, {
cache: useCache
}).then((result) => {
if (result && result.code === 200) {
return result;
} else {
logger.error('getAuthor code no 200');
return [];
}
});
};
}
/**
* [获取切换逛类别或者分页时的文章数据]
* @param {[string]} gender ["1,3"表示男, "2,3"表示女]
* @param {[int]} sortId [分类ID]
* @param {[int]} uid [用户ID]
* @param {[string]} udid [客户端唯一标识]
* @param {[int]} page [分页第几页, 默认第1页]
* @param {[string]} tag [标签]
* @param {[string]} authorId [作者ID]
* @param {Boolean} isApp [是否是APP]
* @param {[Boolean]} showAuthor [是否显示作者]
* @param {Boolean} isTab [是否为tab切换操作]
* @return {[array]}
*/
const getPageData = (gender, sortId, uid, udid, page, tag, authorId, isApp, showAuthor, isTab) => {
return getArticleList(gender, sortId, uid, udid, page, tag, authorId).then(article => {
let result = {};
/**
* [获取切换逛类别或者分页时的文章数据]
* @param {[string]} gender ["1,3"表示男, "2,3"表示女]
* @param {[int]} sortId [分类ID]
* @param {[int]} uid [用户ID]
* @param {[string]} udid [客户端唯一标识]
* @param {[int]} page [分页第几页, 默认第1页]
* @param {[string]} tag [标签]
* @param {[string]} authorId [作者ID]
* @param {Boolean} isApp [是否是APP]
* @param {[Boolean]} showAuthor [是否显示作者]
* @param {Boolean} isTab [是否为tab切换操作]
* @return {[array]}
*/
getPageData(gender, sortId, uid, udid, page, tag, authorId, isApp, showAuthor, isTab) {
return this.getArticleList(gender, sortId, uid, udid, page, tag, authorId).then(article => {
let result = {};
if (!_.get(article, 'data.list.artList', false)) {
return result;
}
if (!_.get(article, 'data.list.artList', false)) {
return result;
}
let adList = article.data.list.adlist;
let adList = article.data.list.adlist;
// 广告列表
if (isTab && adList) {
result.swiper = adList.map(function(ad) {
return {
url: guangProcess.getFilterUrl(ad.url),
img: helpers.image(ad.src, 830, 327)
};
});
}
// 广告列表
if (isTab && adList) {
result.swiper = adList.map(function(ad) {
return {
url: guangProcess.getFilterUrl(ad.url),
img: helpers.image(ad.src, 830, 327)
};
});
}
/* 构建资讯文章内容 */
let artList = article.data.list.artList;
/* 构建资讯文章内容 */
let artList = article.data.list.artList;
result.infos = artList.map(function(art) {
return guangProcess.formatArticle(art, true, isApp, showAuthor, uid);
});
result.infos = artList.map(function(art) {
return guangProcess.formatArticle(art, true, isApp, showAuthor, uid);
return result;
});
}
return result;
});
};
/**
* 逛
* @param params
*/
getArticle(param) {
/**
* 逛内容列表
*/
const _article = (param) => {
return serviceAPI.get('/guang/api/v2/article/getList', {
gender: param.gender,
page: param.page || 1,
uid: param.uid,
udid: param.udid,
sort_id: param.type || 0,
tag: param.tag ? param.tag : null,
limit: 4
let page = param.page ? param.page : 1;
// author_id: param.authorId ? param.authorId : null,
// limit: param.limit ? param.limit : null
}, {
cache: true,
code: 200
}).then(result => {
Object.assign(param, {
page: page
});
return result;
return api.all([
this._category(),
this._article(param)
]).then(result => {
});
};
let type = param.type;
let resu = {
guang: {
gender: param.gender,
}
};
/**
* 逛
* @param params
*/
const getArticle = (param) => {
// 顶部的分类列表
let curIndex = 0; // 当前tab顺序
let page = param.page ? param.page : 1;
let indexTmp = 0;
Object.assign(param, {
page: page
});
if (result[0] && result[0].data) {
return api.all([
_category(),
_article(param)
]).then(result => {
indexTmp = 0;
let type = param.type;
let cateList = result[0].data;
let resu = {
guang: {
gender: param.gender,
}
};
let build = [];
// 顶部的分类列表
let curIndex = 0; // 当前tab顺序
let inf = [];
let indexTmp = 0;
cateList.forEach(val => {
build.push({
typeId: val.id,
type: val.name,
focus: (val.id === type)
});
if (result[0] && result[0].data) {
inf.push({
show: (val.id === type),
typeId: type,
info: []
});
indexTmp = 0;
if ((val.id === type)) {
curIndex = indexTmp;
}
let cateList = result[0].data;
indexTmp++;
let build = [];
resu.guang.navs = build;
let inf = [];
resu.guang.infos = inf;
cateList.forEach(val => {
build.push({
typeId: val.id,
type: val.name,
focus: (val.id === type)
});
inf.push({
show: (val.id === type),
typeId: type,
info: []
});
}
if ((val.id === type)) {
curIndex = indexTmp;
}
if (result && result[1] && result[1].data && result[1].data.list) {
indexTmp++;
let swp = [];
resu.guang.navs = build;
let swiperList = _.get(result[1], 'data.list.adlist', []);
resu.guang.infos = inf;
});
swiperList.forEach(val => {
swp.push({
url: guangProcess.getFilterUrl(val.url),
img: helpers.image(val.src, 640, 275)
});
});
}
resu.guang.swiper = swp;
if (result && result[1] && result[1].data && result[1].data.list) {
}
let swp = [];
if (result && result[1] && result[1].data && result[1].data.list && result[1].data.list.artList) {
let swiperList = _.get(result[1], 'data.list.adlist', []);
let inf = [];
let infoList = result[1].data.list.artList;
swiperList.forEach(val => {
swp.push({
url: guangProcess.getFilterUrl(val.url),
img: helpers.image(val.src, 640, 275)
infoList.forEach(val => {
inf.push(guangProcess.formatArticle(val, true, false, true));
});
});
resu.guang.swiper = swp;
resu.guang.infos[curIndex].info = inf;
}
}
if (result && result[1] && result[1].data && result[1].data.list && result[1].data.list.artList) {
return resu;
let inf = [];
});
let infoList = result[1].data.list.artList;
}
infoList.forEach(val => {
inf.push(guangProcess.formatArticle(val, true, false, true));
});
getTagEditor(param) {
resu.guang.infos[curIndex].info = inf;
let page = param.page ? param.page : 1;
}
Object.assign(param, {
page: page
});
return resu;
return api.all([
this._article(param)
]).then(result => {
});
let resu = {
guang: {
tag: param.tag,
gender: param.gender,
isApp: param.isApp ? 1 : 0,
guangList: true
}
};
if (result && result[0] && result[0].data && result[0].data.list && result[0].data.list.artList) {
let inf = [];
let infoList = result[0].data.list.artList;
infoList.forEach(val => {
val.colparam = {
urlpath: param.path,
param: ''
};
if (param.tag) {
val.colparam.param = `?query=${param.tag}`;
}
inf.push(guangProcess.formatArticle(val, true, param.isApp, true));
});
};
resu.guang.infos = inf;
const getTagEditor = (param) => {
}
let page = param.page ? param.page : 1;
return resu;
Object.assign(param, {
page: page
});
});
return api.all([
_article(param)
]).then(result => {
}
let resu = {
guang: {
tag: param.tag,
gender: param.gender,
isApp: param.isApp ? 1 : 0,
guangList: true
}
/**
* 获取制指定文章的动态信息
* @param ids
* @param udid
* @param other [Obejct] 包含uid,query,type等非必传参数
* @returns {Promise.<T>|*}
*/
getDynamicDataByIds(ids, udid, other) {
let params = {
articleIds: ids,
udid: udid
};
if (result && result[0] && result[0].data && result[0].data.list && result[0].data.list.artList) {
let inf = [];
let infoList = result[0].data.list.artList;
infoList.forEach(val => {
val.colparam = {
urlpath: param.path,
param: ''
};
if (param.tag) {
val.colparam.param = `?query=${param.tag}`;
}
inf.push(guangProcess.formatArticle(val, true, param.isApp, true));
if (other.uid) {
_.assign(params, {
uid: other.uid
});
resu.guang.infos = inf;
}
return resu;
});
};
/**
* 获取制指定文章的动态信息
* @param ids
* @param udid
* @param other [Obejct] 包含uid,query,type等非必传参数
* @returns {Promise.<T>|*}
*/
if (other.query) {
_.assign(params, {
query: other.query
});
}
const getDynamicDataByIds = (ids, udid, other) => {
let params = {
articleIds: ids,
udid: udid
};
if (other.type) {
_.assign(params, {
type: other.type
});
}
if (other.uid) {
_.assign(params, {
uid: other.uid
});
return serviceAPI.get('guang/api/v6/article/getSimpleArticleList', params);
}
if (other.query) {
_.assign(params, {
query: other.query
/**
* 获取制指定文章的动态信息
* @param ids
* @returns {Promise.<T>|*}
*/
getDynamicDataById(id, uid, udid) {
return serviceAPI.get('guang/api/v6/article/getArticlePraiseAndFavor', {
id: id,
uid: uid,
udid: udid
});
}
if (other.type) {
_.assign(params, {
type: other.type
/**
* 逛分类
*/
_category() {
return serviceAPI.get('/guang/api/v1/category/get', {
}, {
cache: true,
code: 200
});
}
return serviceAPI.get('guang/api/v6/article/getSimpleArticleList', params);
};
/**
* 逛内容列表
*/
_article(param) {
return serviceAPI.get('/guang/api/v2/article/getList', {
gender: param.gender,
page: param.page || 1,
uid: param.uid,
udid: param.udid,
sort_id: param.type || 0,
tag: param.tag ? param.tag : null,
limit: 4
// author_id: param.authorId ? param.authorId : null,
// limit: param.limit ? param.limit : null
}, {
cache: true,
code: 200
}).then(result => {
/**
* 获取制指定文章的动态信息
* @param ids
* @returns {Promise.<T>|*}
*/
return result;
});
}
}
const getDynamicDataById = (id, uid, udid) => {
return serviceAPI.get('guang/api/v6/article/getArticlePraiseAndFavor', {
id: id,
uid: uid,
udid: udid
});
};
module.exports = {
getAuthor,
getArticleList,
getPageData,
getArticle,
getTagEditor,
getDynamicDataByIds,
getDynamicDataById
};
module.exports = IndexModel;
... ...