IndexModel.js 3.09 KB
'use strict';

const Promise = require('bluebird');
const co = Promise.coroutine;
const OrderData = require('./OrderData');
const OrderModel = require('./OrderModel');
const helpers = global.yoho.helpers;
const api = global.yoho.API;
const searchApi = global.yoho.SearchAPI;

const BrandData = require('./BrandData');
const IndexData = require('./IndexData');
const SearchData = require('./SearchData');

const HelperHome = require('./HelperHome');

const _ = require('lodash');

/**
 * 个人中心——消息提示
 * @param type $uid
 * @param type $udid
 * @return array
 */
const getInfoNumData = ($uid, $udid)=>{
    let $result = [
            {href: helpers.urlFormat('/home/orders'), name: '待处理订单', 'count': 0},
            {href: helpers.urlFormat('/home/message'), name: '未读消息', 'count': 0},
            {href: helpers.urlFormat('/home/comment'), name: '待评论商品', 'count': 0}
    ];
    return co(function * () {
        let $getPendingOrderCount = yield IndexData.getPendingOrderCount($uid);// 待处理订单
        let $infoNumData = yield IndexData.infoNum($uid, $udid); // 未读消息
        let $notCommentRecordCount = yield IndexData.notCommentRecordCount($uid);// 待评论商品
        $result[0]['count'] = $getPendingOrderCount.data.count ? $getPendingOrderCount.data.count : 0;
        $result[1]['count'] = $infoNumData.data.inbox_total ? $infoNumData.data.inbox_total : 0;
        $result[2]['count'] = $notCommentRecordCount.data ? $notCommentRecordCount.data : 0;
        return $result;
    })();
};


/**
 * 个人中心——最新订单
 * @param type $uid
 * @return array
 */
const latestOrders = ($uid)=>{
    return co(function *() {
        let $orders = yield OrderModel.getOrders($uid, 1, 2, 1);
        return {
            more: helpers.urlFormat('/home/orders'),
            orders: $orders
        };
    })();
};
const homeData = ()=>{
    return co(function * () {
        let $result = {};
        let $url = {};
        $url.fav_brand = SearchData.getBrandListUrl();
        $url.new = SearchData.getProductUrl({new: 'Y', viewNum: 10});
        let $data = yield Promise.all([searchApi.get($url.fav_brand, {}, {cache: true}), searchApi.get($url.new, {}, {cache: true})]);

        // 格式化数据
        $result['brand'] = $data[0].data && $data[0].data.length > 0 ? HelperHome.formatFavBrand($data[0].data, 6) : [];
        $result['new'] = $data[1].data['product_list'] && $data[1].data['product_list'].length > 0 ? HelperHome.formatNew($data[1].data['product_list']) : {};
        return $result;

        // $result.brand=$data.fav_brand&&!$data.fav_brand?
    })();
};

/**
 * 底部banner
 * @param string $code
 * @return mixed
 */
const getFooterBanner = ($code)=>{

    $code = $code || '20110609-152143';
    return co(function *() {
        let result = '';
        let $banner = yield BrandData.getByNodeContent($code);
        if ($banner.code && $banner.data) {
            result = $banner.data.replace('http://', '//');
        }
        return result;
    })();
};

module.exports = {
    getInfoNumData,
    getFooterBanner,
    latestOrders,
    homeData
};