Authored by 郭成尧

ctx-add

@@ -151,7 +151,7 @@ app.use((req, res, next) => { @@ -151,7 +151,7 @@ app.use((req, res, next) => {
151 next(); 151 next();
152 }); 152 });
153 153
154 - // 添加请求上下文 154 +// 添加请求上下文
155 app.use(global.yoho.httpCtx()); 155 app.use(global.yoho.httpCtx());
156 156
157 // dispatcher 157 // dispatcher
@@ -7,7 +7,7 @@ @@ -7,7 +7,7 @@
7 7
8 const mRoot = '../models'; 8 const mRoot = '../models';
9 const typeLib = require('../../../config/type-lib'); 9 const typeLib = require('../../../config/type-lib');
10 -const indexModel = require(`${mRoot}/index`); 10 +const IndexModel = require(`${mRoot}/index`);
11 const headerModel = require('../../../doraemon/models/header'); // 头部model 11 const headerModel = require('../../../doraemon/models/header'); // 头部model
12 const footerModel = require('../../../doraemon/models/footer_tab'); // 底部tab 12 const footerModel = require('../../../doraemon/models/footer_tab'); // 底部tab
13 const guangProcess = require(`${global.utils}/guang-process`); 13 const guangProcess = require(`${global.utils}/guang-process`);
@@ -44,8 +44,8 @@ const editor = (req, res, next) => { @@ -44,8 +44,8 @@ const editor = (req, res, next) => {
44 }; 44 };
45 } 45 }
46 return Promise.all([ 46 return Promise.all([
47 - indexModel.getAuthor(id),  
48 - indexModel.getArticleList(gender, 0, uid, udid, 1, null, id)] 47 + req.ctx(IndexModel).getAuthor(id),
  48 + req.ctx(IndexModel).getArticleList(gender, 0, uid, udid, 1, null, id)]
49 ).then(datas => { 49 ).then(datas => {
50 let authorData = datas[0], 50 let authorData = datas[0],
51 articleListData = datas[1]; 51 articleListData = datas[1];
@@ -157,7 +157,7 @@ const pageData = (req, res, next) => { @@ -157,7 +157,7 @@ const pageData = (req, res, next) => {
157 if (!authorId && isNaN(authorId)) { 157 if (!authorId && isNaN(authorId)) {
158 showAuthor = true; 158 showAuthor = true;
159 } 159 }
160 - return indexModel.getPageData(gender, 160 + return req.ctx(IndexModel).getPageData(gender,
161 sortId, 161 sortId,
162 uid, 162 uid,
163 udid, 163 udid,
@@ -204,7 +204,7 @@ const index = (req, res, next) => { @@ -204,7 +204,7 @@ const index = (req, res, next) => {
204 gender: req.query.gender || req.query.channel && typeLib.gender[req.query.channel] || '1,3' 204 gender: req.query.gender || req.query.channel && typeLib.gender[req.query.channel] || '1,3'
205 }; 205 };
206 206
207 - indexModel.getArticle(param).then(result => { 207 + req.ctx(IndexModel).getArticle(param).then(result => {
208 if (result && result.guang && result.guang.infos) { 208 if (result && result.guang && result.guang.infos) {
209 if (!result.guang.infos.length) { 209 if (!result.guang.infos.length) {
210 res.set('Cache-Control', 'no-cache'); 210 res.set('Cache-Control', 'no-cache');
@@ -250,7 +250,7 @@ const tag = (req, res, next) => { @@ -250,7 +250,7 @@ const tag = (req, res, next) => {
250 responseData.pageHeader.navTitle = param.tag || '标签'; 250 responseData.pageHeader.navTitle = param.tag || '标签';
251 251
252 252
253 - indexModel.getTagEditor(param).then(result => { 253 + req.ctx(IndexModel).getTagEditor(param).then(result => {
254 res.render('index/list', Object.assign(responseData, result)); 254 res.render('index/list', Object.assign(responseData, result));
255 }).catch(next); 255 }).catch(next);
256 }; 256 };
@@ -281,7 +281,7 @@ const listDynamicData = (req, res) => { @@ -281,7 +281,7 @@ const listDynamicData = (req, res) => {
281 other.type = type; 281 other.type = type;
282 } 282 }
283 283
284 - indexModel.getDynamicDataByIds(ids, udid, other).then(ret => { 284 + req.ctx(IndexModel).getDynamicDataByIds(ids, udid, other).then(ret => {
285 res.send(ret); 285 res.send(ret);
286 }); 286 });
287 }; 287 };
@@ -297,7 +297,7 @@ const detailDynamicData = (req, res) => { @@ -297,7 +297,7 @@ const detailDynamicData = (req, res) => {
297 uid = req.user.uid || req.query.uid, 297 uid = req.user.uid || req.query.uid,
298 udid = req.sessionID; 298 udid = req.sessionID;
299 299
300 - indexModel.getDynamicDataById(id, uid, udid).then((ret) => { 300 + req.ctx(IndexModel).getDynamicDataById(id, uid, udid).then((ret) => {
301 res.status(200).send(ret); 301 res.status(200).send(ret);
302 }).catch(() => { 302 }).catch(() => {
303 res.status(400); 303 res.status(400);
@@ -11,354 +11,351 @@ const helpers = global.yoho.helpers; @@ -11,354 +11,351 @@ const helpers = global.yoho.helpers;
11 const guangProcess = require(`${global.utils}/guang-process`); 11 const guangProcess = require(`${global.utils}/guang-process`);
12 const _ = require('lodash'); 12 const _ = require('lodash');
13 13
14 -/**  
15 - * [获取作者信息]  
16 - * @param {[int]} id [作者id]  
17 - * @return {[object]}  
18 - */  
19 -const getAuthor = (id) => {  
20 - return serviceAPI.get('guang/service/v1/author/getAuthor', {  
21 - author_id: id  
22 - }, {  
23 - cache: true  
24 - }).then((result) => {  
25 - if (result && result.code === 200) {  
26 - return result;  
27 - } else {  
28 - logger.error('getAuthor code no 200');  
29 - return {};  
30 - }  
31 - });  
32 -}; 14 +class IndexModel extends global.yoho.BaseModel {
  15 + constructor(ctx) {
  16 + super(ctx);
  17 + }
33 18
34 -/**  
35 - * 逛分类  
36 - */  
37 -const _category = () => {  
38 - return serviceAPI.get('/guang/api/v1/category/get', { 19 + /**
  20 + * [获取作者信息]
  21 + * @param {[int]} id [作者id]
  22 + * @return {[object]}
  23 + */
  24 + getAuthor(id) {
  25 + return serviceAPI.get('guang/service/v1/author/getAuthor', {
  26 + author_id: id
  27 + }, {
  28 + cache: true
  29 + }).then((result) => {
  30 + if (result && result.code === 200) {
  31 + return result;
  32 + } else {
  33 + logger.error('getAuthor code no 200');
  34 + return {};
  35 + }
  36 + });
  37 + }
39 38
40 - }, {  
41 - cache: true,  
42 - code: 200  
43 - });  
44 -}; 39 + /**
  40 + * [逛内容列表]
  41 + * @param {[string]} gender ["1,3"表示男, "2,3"表示女, "1,2,3"表示所有]
  42 + * @param {[int]} sortId [分类ID]
  43 + * @param {Number} uid [用户ID]
  44 + * @param {String} udid [客户端唯一标识]
  45 + * @param {Number} page [分页第几页, 默认第1页]
  46 + * @param {[string]} tag [标签]
  47 + * @param {[int]} authorId [作者ID]
  48 + * @param {[int]} limit [返回的限制数]
  49 + * @param {Boolean} useCache [是否使用缓存]
  50 + * @return {[array]}
  51 + */
  52 + getArticleList(gender, sortId, uid, udid, page, tag, authorId, limit, useCache) {
  53 + let param = {
  54 + page: page || 1,
  55 + uid: uid || 0,
  56 + udid: udid || '',
  57 + gender: gender || '',
  58 + sort_id: sortId,
  59 + tag: tag,
  60 + author_id: authorId,
  61 + limit: 4
  62 + };
45 63
46 -/**  
47 - * [逛内容列表]  
48 - * @param {[string]} gender ["1,3"表示男, "2,3"表示女, "1,2,3"表示所有]  
49 - * @param {[int]} sortId [分类ID]  
50 - * @param {Number} uid [用户ID]  
51 - * @param {String} udid [客户端唯一标识]  
52 - * @param {Number} page [分页第几页, 默认第1页]  
53 - * @param {[string]} tag [标签]  
54 - * @param {[int]} authorId [作者ID]  
55 - * @param {[int]} limit [返回的限制数]  
56 - * @param {Boolean} useCache [是否使用缓存]  
57 - * @return {[array]}  
58 - */  
59 -const getArticleList = (gender, sortId, uid, udid, page, tag, authorId, limit, useCache) => {  
60 - let param = {  
61 - page: page || 1,  
62 - uid: uid || 0,  
63 - udid: udid || '',  
64 - gender: gender || '',  
65 - sort_id: sortId,  
66 - tag: tag,  
67 - author_id: authorId,  
68 - limit: 4  
69 - };  
70 -  
71 - return serviceAPI.get('guang/api/v2/article/getList', param, {  
72 - cache: useCache  
73 - }).then((result) => {  
74 - if (result && result.code === 200) {  
75 - return result;  
76 - } else {  
77 - logger.error('getAuthor code no 200');  
78 - return [];  
79 - }  
80 - }); 64 + return serviceAPI.get('guang/api/v2/article/getList', param, {
  65 + cache: useCache
  66 + }).then((result) => {
  67 + if (result && result.code === 200) {
  68 + return result;
  69 + } else {
  70 + logger.error('getAuthor code no 200');
  71 + return [];
  72 + }
  73 + });
81 74
82 -}; 75 + }
83 76
84 -/**  
85 - * [获取切换逛类别或者分页时的文章数据]  
86 - * @param {[string]} gender ["1,3"表示男, "2,3"表示女]  
87 - * @param {[int]} sortId [分类ID]  
88 - * @param {[int]} uid [用户ID]  
89 - * @param {[string]} udid [客户端唯一标识]  
90 - * @param {[int]} page [分页第几页, 默认第1页]  
91 - * @param {[string]} tag [标签]  
92 - * @param {[string]} authorId [作者ID]  
93 - * @param {Boolean} isApp [是否是APP]  
94 - * @param {[Boolean]} showAuthor [是否显示作者]  
95 - * @param {Boolean} isTab [是否为tab切换操作]  
96 - * @return {[array]}  
97 - */  
98 -const getPageData = (gender, sortId, uid, udid, page, tag, authorId, isApp, showAuthor, isTab) => {  
99 - return getArticleList(gender, sortId, uid, udid, page, tag, authorId).then(article => {  
100 - let result = {}; 77 + /**
  78 + * [获取切换逛类别或者分页时的文章数据]
  79 + * @param {[string]} gender ["1,3"表示男, "2,3"表示女]
  80 + * @param {[int]} sortId [分类ID]
  81 + * @param {[int]} uid [用户ID]
  82 + * @param {[string]} udid [客户端唯一标识]
  83 + * @param {[int]} page [分页第几页, 默认第1页]
  84 + * @param {[string]} tag [标签]
  85 + * @param {[string]} authorId [作者ID]
  86 + * @param {Boolean} isApp [是否是APP]
  87 + * @param {[Boolean]} showAuthor [是否显示作者]
  88 + * @param {Boolean} isTab [是否为tab切换操作]
  89 + * @return {[array]}
  90 + */
  91 + getPageData(gender, sortId, uid, udid, page, tag, authorId, isApp, showAuthor, isTab) {
  92 + return this.getArticleList(gender, sortId, uid, udid, page, tag, authorId).then(article => {
  93 + let result = {};
  94 +
  95 + if (!_.get(article, 'data.list.artList', false)) {
  96 + return result;
  97 + }
101 98
102 - if (!_.get(article, 'data.list.artList', false)) {  
103 - return result;  
104 - } 99 + let adList = article.data.list.adlist;
105 100
106 - let adList = article.data.list.adlist; 101 + // 广告列表
  102 + if (isTab && adList) {
  103 + result.swiper = adList.map(function(ad) {
  104 + return {
  105 + url: guangProcess.getFilterUrl(ad.url),
  106 + img: helpers.image(ad.src, 830, 327)
  107 + };
  108 + });
  109 + }
107 110
108 - // 广告列表  
109 - if (isTab && adList) {  
110 - result.swiper = adList.map(function(ad) {  
111 - return {  
112 - url: guangProcess.getFilterUrl(ad.url),  
113 - img: helpers.image(ad.src, 830, 327)  
114 - };  
115 - });  
116 - } 111 + /* 构建资讯文章内容 */
  112 + let artList = article.data.list.artList;
117 113
118 - /* 构建资讯文章内容 */  
119 - let artList = article.data.list.artList; 114 + result.infos = artList.map(function(art) {
  115 + return guangProcess.formatArticle(art, true, isApp, showAuthor, uid);
  116 + });
120 117
121 - result.infos = artList.map(function(art) {  
122 - return guangProcess.formatArticle(art, true, isApp, showAuthor, uid); 118 + return result;
123 }); 119 });
  120 + }
124 121
125 - return result;  
126 - });  
127 -}; 122 + /**
  123 + * 逛
  124 + * @param params
  125 + */
  126 + getArticle(param) {
128 127
129 -/**  
130 - * 逛内容列表  
131 - */  
132 -const _article = (param) => {  
133 - return serviceAPI.get('/guang/api/v2/article/getList', {  
134 - gender: param.gender,  
135 - page: param.page || 1,  
136 - uid: param.uid,  
137 - udid: param.udid,  
138 - sort_id: param.type || 0,  
139 - tag: param.tag ? param.tag : null,  
140 - limit: 4 128 + let page = param.page ? param.page : 1;
141 129
142 - // author_id: param.authorId ? param.authorId : null,  
143 - // limit: param.limit ? param.limit : null  
144 - }, {  
145 - cache: true,  
146 - code: 200  
147 - }).then(result => { 130 + Object.assign(param, {
  131 + page: page
  132 + });
148 133
149 - return result; 134 + return api.all([
  135 + this._category(),
  136 + this._article(param)
  137 + ]).then(result => {
150 138
151 - });  
152 -}; 139 + let type = param.type;
153 140
  141 + let resu = {
  142 + guang: {
  143 + gender: param.gender,
  144 + }
  145 + };
154 146
155 -/**  
156 - * 逛  
157 - * @param params  
158 - */  
159 -const getArticle = (param) => { 147 + // 顶部的分类列表
  148 + let curIndex = 0; // 当前tab顺序
160 149
161 - let page = param.page ? param.page : 1; 150 + let indexTmp = 0;
162 151
163 - Object.assign(param, {  
164 - page: page  
165 - }); 152 + if (result[0] && result[0].data) {
166 153
167 - return api.all([  
168 - _category(),  
169 - _article(param)  
170 - ]).then(result => { 154 + indexTmp = 0;
171 155
172 - let type = param.type; 156 + let cateList = result[0].data;
173 157
174 - let resu = {  
175 - guang: {  
176 - gender: param.gender,  
177 - }  
178 - }; 158 + let build = [];
179 159
180 - // 顶部的分类列表  
181 - let curIndex = 0; // 当前tab顺序 160 + let inf = [];
182 161
183 - let indexTmp = 0; 162 + cateList.forEach(val => {
  163 + build.push({
  164 + typeId: val.id,
  165 + type: val.name,
  166 + focus: (val.id === type)
  167 + });
184 168
185 - if (result[0] && result[0].data) { 169 + inf.push({
  170 + show: (val.id === type),
  171 + typeId: type,
  172 + info: []
  173 + });
186 174
187 - indexTmp = 0; 175 + if ((val.id === type)) {
  176 + curIndex = indexTmp;
  177 + }
188 178
189 - let cateList = result[0].data; 179 + indexTmp++;
190 180
191 - let build = []; 181 + resu.guang.navs = build;
192 182
193 - let inf = []; 183 + resu.guang.infos = inf;
194 184
195 - cateList.forEach(val => {  
196 - build.push({  
197 - typeId: val.id,  
198 - type: val.name,  
199 - focus: (val.id === type)  
200 }); 185 });
201 186
202 - inf.push({  
203 - show: (val.id === type),  
204 - typeId: type,  
205 - info: []  
206 - }); 187 + }
207 188
208 - if ((val.id === type)) {  
209 - curIndex = indexTmp;  
210 - } 189 + if (result && result[1] && result[1].data && result[1].data.list) {
211 190
212 - indexTmp++; 191 + let swp = [];
213 192
214 - resu.guang.navs = build; 193 + let swiperList = _.get(result[1], 'data.list.adlist', []);
215 194
216 - resu.guang.infos = inf;  
217 195
218 - }); 196 + swiperList.forEach(val => {
  197 + swp.push({
  198 + url: guangProcess.getFilterUrl(val.url),
  199 + img: helpers.image(val.src, 640, 275)
  200 + });
  201 + });
219 202
220 - } 203 + resu.guang.swiper = swp;
221 204
222 - if (result && result[1] && result[1].data && result[1].data.list) { 205 + }
223 206
224 - let swp = []; 207 + if (result && result[1] && result[1].data && result[1].data.list && result[1].data.list.artList) {
225 208
226 - let swiperList = _.get(result[1], 'data.list.adlist', []); 209 + let inf = [];
227 210
  211 + let infoList = result[1].data.list.artList;
228 212
229 - swiperList.forEach(val => {  
230 - swp.push({  
231 - url: guangProcess.getFilterUrl(val.url),  
232 - img: helpers.image(val.src, 640, 275) 213 + infoList.forEach(val => {
  214 + inf.push(guangProcess.formatArticle(val, true, false, true));
233 }); 215 });
234 - });  
235 216
236 - resu.guang.swiper = swp; 217 + resu.guang.infos[curIndex].info = inf;
237 218
238 - } 219 + }
239 220
240 - if (result && result[1] && result[1].data && result[1].data.list && result[1].data.list.artList) { 221 + return resu;
241 222
242 - let inf = []; 223 + });
243 224
244 - let infoList = result[1].data.list.artList; 225 + }
245 226
246 - infoList.forEach(val => {  
247 - inf.push(guangProcess.formatArticle(val, true, false, true));  
248 - }); 227 + getTagEditor(param) {
249 228
250 - resu.guang.infos[curIndex].info = inf; 229 + let page = param.page ? param.page : 1;
251 230
252 - } 231 + Object.assign(param, {
  232 + page: page
  233 + });
253 234
254 - return resu; 235 + return api.all([
  236 + this._article(param)
  237 + ]).then(result => {
255 238
256 - }); 239 + let resu = {
  240 + guang: {
  241 + tag: param.tag,
  242 + gender: param.gender,
  243 + isApp: param.isApp ? 1 : 0,
  244 + guangList: true
  245 + }
  246 + };
  247 +
  248 + if (result && result[0] && result[0].data && result[0].data.list && result[0].data.list.artList) {
  249 +
  250 + let inf = [];
  251 + let infoList = result[0].data.list.artList;
  252 +
  253 + infoList.forEach(val => {
  254 + val.colparam = {
  255 + urlpath: param.path,
  256 + param: ''
  257 + };
  258 + if (param.tag) {
  259 + val.colparam.param = `?query=${param.tag}`;
  260 + }
  261 + inf.push(guangProcess.formatArticle(val, true, param.isApp, true));
  262 + });
257 263
258 -}; 264 + resu.guang.infos = inf;
259 265
260 -const getTagEditor = (param) => { 266 + }
261 267
262 - let page = param.page ? param.page : 1; 268 + return resu;
263 269
264 - Object.assign(param, {  
265 - page: page  
266 - }); 270 + });
267 271
268 - return api.all([  
269 - _article(param)  
270 - ]).then(result => { 272 + }
271 273
272 - let resu = {  
273 - guang: {  
274 - tag: param.tag,  
275 - gender: param.gender,  
276 - isApp: param.isApp ? 1 : 0,  
277 - guangList: true  
278 - } 274 + /**
  275 + * 获取制指定文章的动态信息
  276 + * @param ids
  277 + * @param udid
  278 + * @param other [Obejct] 包含uid,query,type等非必传参数
  279 + * @returns {Promise.<T>|*}
  280 + */
  281 +
  282 + getDynamicDataByIds(ids, udid, other) {
  283 + let params = {
  284 + articleIds: ids,
  285 + udid: udid
279 }; 286 };
280 287
281 - if (result && result[0] && result[0].data && result[0].data.list && result[0].data.list.artList) {  
282 -  
283 - let inf = [];  
284 - let infoList = result[0].data.list.artList;  
285 -  
286 - infoList.forEach(val => {  
287 - val.colparam = {  
288 - urlpath: param.path,  
289 - param: ''  
290 - };  
291 - if (param.tag) {  
292 - val.colparam.param = `?query=${param.tag}`;  
293 - }  
294 - inf.push(guangProcess.formatArticle(val, true, param.isApp, true)); 288 + if (other.uid) {
  289 + _.assign(params, {
  290 + uid: other.uid
295 }); 291 });
296 -  
297 - resu.guang.infos = inf;  
298 -  
299 } 292 }
300 293
301 - return resu;  
302 -  
303 - });  
304 -  
305 -};  
306 -  
307 -/**  
308 - * 获取制指定文章的动态信息  
309 - * @param ids  
310 - * @param udid  
311 - * @param other [Obejct] 包含uid,query,type等非必传参数  
312 - * @returns {Promise.<T>|*}  
313 - */ 294 + if (other.query) {
  295 + _.assign(params, {
  296 + query: other.query
  297 + });
  298 + }
314 299
315 -const getDynamicDataByIds = (ids, udid, other) => {  
316 - let params = {  
317 - articleIds: ids,  
318 - udid: udid  
319 - }; 300 + if (other.type) {
  301 + _.assign(params, {
  302 + type: other.type
  303 + });
  304 + }
320 305
321 - if (other.uid) {  
322 - _.assign(params, {  
323 - uid: other.uid  
324 - }); 306 + return serviceAPI.get('guang/api/v6/article/getSimpleArticleList', params);
325 } 307 }
326 308
327 - if (other.query) {  
328 - _.assign(params, {  
329 - query: other.query 309 + /**
  310 + * 获取制指定文章的动态信息
  311 + * @param ids
  312 + * @returns {Promise.<T>|*}
  313 + */
  314 +
  315 + getDynamicDataById(id, uid, udid) {
  316 + return serviceAPI.get('guang/api/v6/article/getArticlePraiseAndFavor', {
  317 + id: id,
  318 + uid: uid,
  319 + udid: udid
330 }); 320 });
331 } 321 }
332 322
333 - if (other.type) {  
334 - _.assign(params, {  
335 - type: other.type 323 + /**
  324 + * 逛分类
  325 + */
  326 + _category() {
  327 + return serviceAPI.get('/guang/api/v1/category/get', {
  328 +
  329 + }, {
  330 + cache: true,
  331 + code: 200
336 }); 332 });
337 } 333 }
338 334
339 - return serviceAPI.get('guang/api/v6/article/getSimpleArticleList', params);  
340 -}; 335 + /**
  336 + * 逛内容列表
  337 + */
  338 + _article(param) {
  339 + return serviceAPI.get('/guang/api/v2/article/getList', {
  340 + gender: param.gender,
  341 + page: param.page || 1,
  342 + uid: param.uid,
  343 + udid: param.udid,
  344 + sort_id: param.type || 0,
  345 + tag: param.tag ? param.tag : null,
  346 + limit: 4
  347 +
  348 + // author_id: param.authorId ? param.authorId : null,
  349 + // limit: param.limit ? param.limit : null
  350 + }, {
  351 + cache: true,
  352 + code: 200
  353 + }).then(result => {
341 354
342 -/**  
343 - * 获取制指定文章的动态信息  
344 - * @param ids  
345 - * @returns {Promise.<T>|*}  
346 - */ 355 + return result;
  356 +
  357 + });
  358 + }
  359 +}
347 360
348 -const getDynamicDataById = (id, uid, udid) => {  
349 - return serviceAPI.get('guang/api/v6/article/getArticlePraiseAndFavor', {  
350 - id: id,  
351 - uid: uid,  
352 - udid: udid  
353 - });  
354 -};  
355 -  
356 -module.exports = {  
357 - getAuthor,  
358 - getArticleList,  
359 - getPageData,  
360 - getArticle,  
361 - getTagEditor,  
362 - getDynamicDataByIds,  
363 - getDynamicDataById  
364 -}; 361 +module.exports = IndexModel;