Blame view

apps/guang/models/index.js 8.21 KB
lijing authored
1
/**
陈峰 authored
2 3 4
 * 逛models
 * @author: chenfeng<feng.chen@yoho.cn>
 * @date: 2016/09/07
lijing authored
5 6 7
 */
'use strict';
const serviceAPI = global.yoho.ServiceAPI;
lijing authored
8
const api = global.yoho.API;
9
const logger = global.yoho.logger;
lijing authored
10
const helpers = global.yoho.helpers;
11
const guangProcess = require(`${global.utils}/guang-process`);
zhangxiaoru authored
12
const _ = require('lodash');
13 14 15 16 17 18

/**
 * [获取作者信息]
 * @param  {[int]} id [作者id]
 * @return {[object]}
 */
19
const getAuthor = (id) => {
20 21
    return serviceAPI.get('guang/service/v1/author/getAuthor', {
        author_id: id
陈峰 authored
22 23
    }, {
        cache: true
24 25 26 27 28 29 30 31 32 33
    }).then((result) => {
        if (result && result.code === 200) {
            return result;
        } else {
            logger.error('getAuthor code no 200');
            return {};
        }
    });
};
lijing authored
34 35 36 37 38 39 40
/**
 * 逛分类
 */
const _category = () => {
    return serviceAPI.get('/guang/api/v1/category/get', {

    }, {
陈峰 authored
41
        cache: true,
lijing authored
42 43 44 45 46
        code: 200
    });
};

/**
47 48 49 50 51 52 53 54 55 56 57 58
 * [逛内容列表]
 * @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]}
 */
59
const getArticleList = (gender, sortId, uid, udid, page, tag, authorId, limit, useCache) => {
60 61 62 63 64
    let param = {
        page: page || 1,
        uid: uid || 0,
        udid: udid || '',
        gender: gender || '',
陈峰 authored
65 66 67
        sort_id: sortId,
        tag: tag,
        author_id: authorId,
68
        limit: 4
69 70
    };
陈峰 authored
71 72 73
    return serviceAPI.get('guang/api/v2/article/getList', param, {
        cache: useCache
    }).then((result) => {
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
        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]}
 */
98 99
const getPageData = (gender, sortId, uid, udid, page, tag, authorId, isApp, showAuthor, isTab) => {
    return getArticleList(gender, sortId, uid, udid, page, tag, authorId).then(article => {
100
        let result = {};
陈峰 authored
101 102

        if (!article.data.list.artList) {
103 104 105
            return result;
        }
陈峰 authored
106
        let adList = article.data.list.adlist;
107 108 109

        // 广告列表
        if (isTab && adList) {
110 111
            result.swiper = adList.map(function(ad) {
                return {
陈峰 authored
112 113
                    url: guangProcess.getFilterUrl(ad.url),
                    img: helpers.image(ad.src, 830, 327)
114
                };
115 116 117 118
            });
        }

        /* 构建资讯文章内容 */
陈峰 authored
119 120
        let artList = article.data.list.artList;
121 122
        result.infos = artList.map(function(art) {
            return guangProcess.formatArticle(art, true, isApp, showAuthor, uid);
123
        });
124
125 126
        return result;
    });
陈峰 authored
127 128 129
};

/**
lijing authored
130 131 132 133 134 135 136 137
 * 逛内容列表
 */
const _article = (param) => {
    return serviceAPI.get('/guang/api/v2/article/getList', {
        gender: param.gender,
        page: param.page || 1,
        uid: param.uid,
        udid: param.udid,
138
        sort_id: 0,
139 140
        tag: param.tag ? param.tag : null,
        limit: 4
141 142 143

        // author_id: param.authorId ? param.authorId : null,
        // limit: param.limit ? param.limit : null
lijing authored
144
    }, {
陈峰 authored
145
        cache: true,
lijing authored
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
        code: 200
    }).then(result => {

        return result;

    });
};


/**
 * 逛
 * @param params
 */
const getArticle = (param) => {

    let page = param.page ? param.page : 1;
沈志敏 authored
163 164 165
    Object.assign(param, {
        page: page
    });
lijing authored
166 167 168 169 170 171

    return api.all([
        _category(),
        _article(param)
    ]).then(result => {
lijing authored
172 173
        let type = param.type;
lijing authored
174 175
        let resu = {
            guang: {
zhangxiaoru authored
176
                gender: param.gender,
lijing authored
177 178 179
            }
        };
lijing authored
180
        // 顶部的分类列表
沈志敏 authored
181
        let curIndex = 0; // 当前tab顺序
lijing authored
182 183 184

        let indexTmp = 0;
lijing authored
185
        if (result[0] && result[0].data) {
lijing authored
186
lijing authored
187 188 189 190 191 192 193 194 195 196 197 198
            indexTmp = 0;

            let cateList = result[0].data;

            let build = [];

            let inf = [];

            cateList.forEach(val => {
                build.push({
                    typeId: val.id,
                    type: val.name,
lijing authored
199
                    focus: (val.id === type)
lijing authored
200 201 202
                });

                inf.push({
lijing authored
203
                    show: (val.id === type),
lijing authored
204 205 206 207
                    typeId: type,
                    info: []
                });
lijing authored
208
                if ((val.id === type)) {
lijing authored
209 210 211 212 213 214 215 216 217 218 219 220 221
                    curIndex = indexTmp;
                }

                indexTmp++;

                resu.guang.navs = build;

                resu.guang.infos = inf;

            });

        }
lijing authored
222
        if (result && result[1] && result[1].data && result[1].data.list) {
lijing authored
223 224 225 226

            let swp = [];

            let swiperList = result[1].data.list.adlist;
lijing authored
227 228

lijing authored
229 230 231 232
            swiperList.forEach(val => {
                swp.push({
                    url: guangProcess.getFilterUrl(val.url),
                    img: helpers.image(val.src, 640, 275)
lijing authored
233 234 235
                });
            });
lijing authored
236 237 238 239
            resu.guang.swiper = swp;

        }
240
        if (result && result[1] && result[1].data && result[1].data.list && result[1].data.list.artList) {
lijing authored
241 242 243 244 245 246 247 248 249 250

            let inf = [];

            let infoList = result[1].data.list.artList;

            infoList.forEach(val => {
                inf.push(guangProcess.formatArticle(val, true, false, true));
            });

            resu.guang.infos[curIndex].info = inf;
lijing authored
251 252 253

        }
lijing authored
254 255 256 257 258 259 260 261
        return resu;

    });

};

const getTagEditor = (param) => {
lijing authored
262 263
    let page = param.page ? param.page : 1;
沈志敏 authored
264 265 266
    Object.assign(param, {
        page: page
    });
lijing authored
267
lijing authored
268 269 270
    return api.all([
        _article(param)
    ]).then(result => {
lijing authored
271
lijing authored
272 273
        let resu = {
            guang: {
lijing authored
274 275 276 277
                tag: param.tag,
                gender: param.gender,
                isApp: param.isApp ? 1 : 0,
                guangList: true
lijing authored
278 279
            }
        };
lijing authored
280
lijing authored
281
        if (result && result[0] && result[0].data && result[0].data.list && result[0].data.list.artList) {
lijing authored
282
lijing authored
283 284
            let inf = [];
            let infoList = result[0].data.list.artList;
郝肖肖 authored
285
lijing authored
286
            infoList.forEach(val => {
沈志敏 authored
287 288 289
                val.colparam = {
                    urlpath: param.path,
                    param: ''
290
                };
沈志敏 authored
291
                if (param.tag) {
沈志敏 authored
292
                    val.colparam.param = `?query=${param.tag}`;
沈志敏 authored
293
                }
沈志敏 authored
294
                inf.push(guangProcess.formatArticle(val, true, param.isApp, true));
lijing authored
295 296 297 298 299
            });

            resu.guang.infos = inf;

        }
lijing authored
300
lijing authored
301 302 303 304 305 306
        return resu;

    });

};
zhangxiaoru authored
307 308 309 310 311 312 313
/**
 * 获取制指定文章的动态信息
 * @param ids
 * @param udid
 * @param other [Obejct] 包含uid,query,type等非必传参数
 * @returns {Promise.<T>|*}
 */
314
zhangxiaoru authored
315 316 317 318 319 320 321
const getDynamicDataByIds = (ids, udid, other) => {
    let params = {
        articleIds: ids,
        udid: udid
    };

    if (other.uid) {
zhangxiaoru authored
322 323 324
        _.assign(params, {
            uid: other.uid
        });
zhangxiaoru authored
325 326 327
    }

    if (other.query) {
zhangxiaoru authored
328 329 330
        _.assign(params, {
            query: other.query
        });
zhangxiaoru authored
331 332 333
    }

    if (other.type) {
zhangxiaoru authored
334 335 336
        _.assign(params, {
            type: other.type
        });
zhangxiaoru authored
337 338
    }
郝肖肖 authored
339
    return serviceAPI.get('guang/api/v6/article/getSimpleArticleList', params);
zhangxiaoru authored
340 341
};
zhangxiaoru authored
342 343 344 345 346
/**
 * 获取制指定文章的动态信息
 * @param ids
 * @returns {Promise.<T>|*}
 */
zhangxiaoru authored
347 348

const getDynamicDataById = (id, uid, udid) => {
349
    return serviceAPI.get('guang/api/v6/article/getArticlePraiseAndFavor', {
zhangxiaoru authored
350 351 352 353 354
        id: id,
        uid: uid,
        udid: udid
    });
};
zhangxiaoru authored
355
lijing authored
356
module.exports = {
357 358 359
    getAuthor,
    getArticleList,
    getPageData,
lijing authored
360
    getArticle,
zhangxiaoru authored
361 362 363
    getTagEditor,
    getDynamicDataByIds,
    getDynamicDataById
364
};