yohood.js 6.4 KB
/**
 * Created by qiujun on 2018/7/19.
 */

const request = require('request-promise');
const YohoodModel = require('../models/yohood');
const INVALID_PARAMS = '参数错误';
const YOHOOD_DOMAIN = 'http://www.yohood.cn/';
const GET_BRAND_LIST_URL = YOHOOD_DOMAIN + 'brand/getbrand';
const GET_BRAND_BY_ID_URL = YOHOOD_DOMAIN + 'brand/getBrandById';
const GET_NEWS_LIST_URL = YOHOOD_DOMAIN + 'default/H5LoadNews';
const GET_NEWS_DETAIL_URL = YOHOOD_DOMAIN + 'mobileapi/getNewsDetail';
const GET_NEWS_RECOMMEND_URL = YOHOOD_DOMAIN + 'news/getdetailnews';
const _ = require('lodash');

const yohood = {
    /**
     * 获取品牌列表
     * @param req
     * @param res
     * @returns {Promise.<T>|*}
     */
    getBrandList(req, res) {
        return request({
            method: 'GET',
            url: GET_BRAND_LIST_URL,
            qs: {},
            json: true
        }).then(result => {
            return res.json(result);
        }).catch(() => {
            return res.json({
                code: 503,
                message: '获取[品牌列表]接口数据失败'
            });
        });
    },

    /**
     * 通过id获取品牌详情
     * @param req
     * @param res
     * @returns {Promise.<T>|*}
     */
    getBrandById(req, res) {
        let id = req.query.id;

        if (!id) {
            return res.json({
                code: 400,
                message: INVALID_PARAMS
            });
        }

        return request({
            method: 'GET',
            url: GET_BRAND_BY_ID_URL,
            qs: {
                id: id
            },
            json: true
        }).then(result => {
            return res.json(result);
        }).catch(() => {
            return res.json({
                code: 503,
                message: '获取[品牌详情]接口数据失败'
            });
        });
    },

    /**
     * 获取资讯列表
     * @param req
     * @param res
     */
    getNewsList(req, res) {
        let {page = 1, type = 1} = req.query;

        return request({
            method: 'GET',
            url: GET_NEWS_LIST_URL,
            qs: {
                page: page,
                type: type
            },
            json: true
        }).then(result => {
            let results = [];

            _.each(result, item => {
                if (parseInt(item.create_time, 10) > 1530403200) {
                    results.push(item);
                }
            });
            return res.json(results);

        }).catch(() => {
            return res.json({
                code: 503,
                message: '获取[品牌资讯列表]接口数据失败'
            });
        });
    },

    /**
     * 通过id获取资讯详情
     * @param req
     * @param res
     * @returns {*|Promise.<T>}
     */
    getNewsDetail(req, res) {
        let id = req.query.id;

        if (!id) {
            return res.json({
                code: 400,
                message: INVALID_PARAMS
            });
        }

        return request({
            method: 'GET',
            url: GET_NEWS_DETAIL_URL,
            qs: {
                id: id
            },
            json: true
        }).then(result => {
            return res.json(result);
        }).catch(() => {
            return res.json({
                code: 503,
                message: '获取[品牌详情]接口数据失败'
            });
        });
    },

    /**
     * 根据id获取详情页推荐
     * @param req
     * @param res
     * @returns {*|Promise.<T>}
     */
    getRecommend(req, res) {
        let id = req.query.id;

        if (!id) {
            return res.json({
                code: 400,
                message: INVALID_PARAMS
            });
        }

        return request({
            method: 'GET',
            url: GET_NEWS_RECOMMEND_URL,
            qs: {
                id: id
            },
            json: true
        }).then(result => {
            return res.json({code: result.code, data: {recom: result.data.recom || []}});
        }).catch(() => {
            return res.json({
                code: 503,
                message: '获取[品牌详情]接口数据失败'
            });
        });
    },

    /**
     * 获取有货资源位数据
     * @param req
     * @param res
     */
    getYohoBuyResource(req, res) {
        console.log(req.query);
        let {contentCode = 'a10a4c543335901cd4f1065c1d6849db'} = req.query;

        return req.ctx(YohoodModel).getYohoBuyResource(contentCode).then(result => {
            res.json(result);
        });
    },

    /**
     *
     * @param req
     * @param res
     */
    getYohoBuyShopProduct(req, res) {
        let shopId = req.query.id;

        if (!shopId) {
            return res.json({
                code: 400,
                message: INVALID_PARAMS
            });
        }
        return req.ctx(YohoodModel).getShopProductsByShopId(shopId).then(result => {
            return res.json(result);
        });
    },

    async getCoupon(req, res) {
        let {uid, couponId} = req.query;

        // console.log(uid, couponId);
        uid = parseInt(uid, 10);

        if (!uid || !couponId) {
            return res.json({
                code: 400,
                message: INVALID_PARAMS
            });
        }

        let couponInfo = await req.ctx(YohoodModel).getCouponInfo(couponId);

        let couponDetail = await req.ctx(YohoodModel).getCouponDetail(uid, couponId);

        // console.log('couponDetail:', couponDetail, uid, couponId);
        // console.log('couponInfo', couponInfo.data[0].coupon_num);

        if (couponDetail.code === 200 && couponDetail.data.length > 0) {
            return res.json(couponDetail);
        } else if (couponInfo.data[0].coupon_num > 0) {
            let getCoupon = await req.ctx(YohoodModel).getOfflineStoreCoupon(uid, couponId);

            // console.log('getCoupon:', getCoupon);
            if (getCoupon.code === 200) {
                couponDetail = await req.ctx(YohoodModel).getCouponDetail(uid, couponId);

                return res.json(couponDetail);
            } else if (getCoupon.code === 201) {
                return res.json(couponDetail);
            } else {
                return res.json({
                    code: 203,
                    message: '领券失败'
                });
            }

        } else {
            return res.json({
                code: 204,
                message: '券已领完'
            });
        }
    }
};

module.exports = yohood;