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,12 +11,17 @@ const helpers = global.yoho.helpers;
const guangProcess = require(`${global.utils}/guang-process`);
const _ = require('lodash');
/**
class IndexModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* [获取作者信息]
* @param {[int]} id [作者id]
* @return {[object]}
*/
const getAuthor = (id) => {
getAuthor(id) {
return serviceAPI.get('guang/service/v1/author/getAuthor', {
author_id: id
}, {
... ... @@ -29,21 +34,9 @@ const getAuthor = (id) => {
return {};
}
});
};
/**
* 逛分类
*/
const _category = () => {
return serviceAPI.get('/guang/api/v1/category/get', {
}, {
cache: true,
code: 200
});
};
}
/**
/**
* [逛内容列表]
* @param {[string]} gender ["1,3"表示男, "2,3"表示女, "1,2,3"表示所有]
* @param {[int]} sortId [分类ID]
... ... @@ -56,7 +49,7 @@ const _category = () => {
* @param {Boolean} useCache [是否使用缓存]
* @return {[array]}
*/
const getArticleList = (gender, sortId, uid, udid, page, tag, authorId, limit, useCache) => {
getArticleList(gender, sortId, uid, udid, page, tag, authorId, limit, useCache) {
let param = {
page: page || 1,
uid: uid || 0,
... ... @@ -79,9 +72,9 @@ const getArticleList = (gender, sortId, uid, udid, page, tag, authorId, limit, u
}
});
};
}
/**
/**
* [获取切换逛类别或者分页时的文章数据]
* @param {[string]} gender ["1,3"表示男, "2,3"表示女]
* @param {[int]} sortId [分类ID]
... ... @@ -95,8 +88,8 @@ const getArticleList = (gender, sortId, uid, udid, page, tag, authorId, limit, u
* @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 => {
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)) {
... ... @@ -124,39 +117,13 @@ const getPageData = (gender, sortId, uid, udid, page, tag, authorId, isApp, show
return result;
});
};
/**
* 逛内容列表
*/
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
// author_id: param.authorId ? param.authorId : null,
// limit: param.limit ? param.limit : null
}, {
cache: true,
code: 200
}).then(result => {
return result;
});
};
}
/**
/**
* 逛
* @param params
*/
const getArticle = (param) => {
getArticle(param) {
let page = param.page ? param.page : 1;
... ... @@ -165,8 +132,8 @@ const getArticle = (param) => {
});
return api.all([
_category(),
_article(param)
this._category(),
this._article(param)
]).then(result => {
let type = param.type;
... ... @@ -255,9 +222,9 @@ const getArticle = (param) => {
});
};
}
const getTagEditor = (param) => {
getTagEditor(param) {
let page = param.page ? param.page : 1;
... ... @@ -266,7 +233,7 @@ const getTagEditor = (param) => {
});
return api.all([
_article(param)
this._article(param)
]).then(result => {
let resu = {
... ... @@ -302,9 +269,9 @@ const getTagEditor = (param) => {
});
};
}
/**
/**
* 获取制指定文章的动态信息
* @param ids
* @param udid
... ... @@ -312,7 +279,7 @@ const getTagEditor = (param) => {
* @returns {Promise.<T>|*}
*/
const getDynamicDataByIds = (ids, udid, other) => {
getDynamicDataByIds(ids, udid, other) {
let params = {
articleIds: ids,
udid: udid
... ... @@ -337,28 +304,58 @@ const getDynamicDataByIds = (ids, udid, other) => {
}
return serviceAPI.get('guang/api/v6/article/getSimpleArticleList', params);
};
}
/**
/**
* 获取制指定文章的动态信息
* @param ids
* @returns {Promise.<T>|*}
*/
const getDynamicDataById = (id, uid, udid) => {
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
};
}
/**
* 逛分类
*/
_category() {
return serviceAPI.get('/guang/api/v1/category/get', {
}, {
cache: true,
code: 200
});
}
/**
* 逛内容列表
*/
_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 => {
return result;
});
}
}
module.exports = IndexModel;
... ...