order-ensure.js 4.11 KB
/**
 * 订单结算controller
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/12/21
 */

'use strict';

const crypto = global.yoho.crypto;
const authcode = require(`${global.utils}/authcode`);
const headerModel = require('../../../doraemon/models/simple-header');
const oeModel = require('../models/order-ensure');

const stepper = [
    {name: '查看购物车', focus: true},
    {name: '填写订单', focus: true},
    {name: '付款,完成购买'}
];

const index = (req, res, next) => {
    let cartType = req.query.type === '2' ? 'advance' : 'ordinary';

    oeModel.index(req.user.uid, cartType).then(result => {
        let header = headerModel.setSimpleHeaderData() || {};

        Object.assign(result, {
            stepper: stepper
        });

        res.render('order-ensure2016', {
            title: '填写订单 | ' + (res.locals.title || ''),
            page: 'ensure',
            content: result,
            simpleHeader: header
        });
    }).catch(next);
};

// 获取优惠券列表
const getCoupons = (req, res, next) => {
    oeModel.getCoupons(req.user.uid).then(data => {
        res.send(data);
    }).catch(next);
};

// 订单金额计算
const compute = (req, res, next) => {
    let params = req.body;
    let cartType = params.cartType === '2' ? 'advance' : 'ordinary';

    oeModel.compute(req.user.uid, cartType, params).then(data => {
        res.send(data);
    }).catch(next);
};

// 提交订单
const submit = (req, res, next) => {
    let cartType = req.body.cartType === '2' ? 'advance' : 'ordinary';
    let uid = req.user.uid;

    if (!req.body.addressId) {
        res.send({
            code: 500,
            message: '配送地址不能为空'
        });
        return;
    }

    /* 判断是否是友盟过来的用户 */
    let userAgent = null;
    let unionKey = '';
    let unionInfo = {};

    if (req.cookies.mkt_code || req.cookies._QYH_UNION) {
        /* eslint-disable */
        /*
         *1、http://union.yohobuy.com/go?client_id=3415&aid=0118&channel=3415&cid=3601&wi=NDgwMDB8dGVzdA==&target=http://m.yohobuy.com/
         *2、http://union.yoho.cn/union/jump?channel_id=51fanli&u_id=6&tracking_code=fanli123&target_url=http%3a%2f%2fm.yohobuy.com%3funion_type%3d3063%26utm_source%3dmfanli%26utm_medium%3dcps%26utm_campaign%3dmpfanli
         *3、union.yohobuy.com/go/proxy?utm_medium=none&utm_campaign=none&client_id=991002&ads_code=&go_url=https%253A%252F%252Fm.yohobuy.com%252F%253Futm_source%253Dhyyx%2526utm_medium%253Dnone%2526utm_campaign%253Dnone%2526union_type%253D991002&channel_code=hyyx&append=&mbr_name=&u_id=&aid=&channel=cps&cid=&wi=
         **/
        /* tar modified 161108 添加新的联盟数据处理逻辑,兼容原有联盟数据处理,
            区别是旧的北京写 cookie 加密过来,新的 node 写 cookie,没有加密 */
        /* eslint-enable */
        if (req.cookies._QYH_UNION) {
            unionKey = authcode(req.cookies._QYH_UNION, 'q_union_yohobuy');

            if (!unionKey) {
                let encryData, testQyhUnion;

                encryData = crypto.decrypt('', decodeURIComponent(req.cookies._QYH_UNION));
                encryData = encryData.substr(0, encryData.lastIndexOf('}') + 1);
                testQyhUnion = JSON.parse(encryData);
                unionKey = testQyhUnion.client_id ? encryData : '';
            }
        } else {
            unionKey = '{"client_id":' + req.cookies.mkt_code + '}';
        }

        /* 检查联盟参数是否有效 */
        unionInfo = unionKey ? JSON.parse(unionKey) : {};

        /* 模拟APP的User-Agent */
        userAgent = unionInfo.client_id ? 'YOHO!Buy/3.8.2.259(Model/PC;Channel/' +
            unionInfo.client_id + ';uid/' + uid + ')' : null;
    }

    Object.assign(req.body, {
        qhyUnion: unionKey,
        userAgent: userAgent
    });

    oeModel.submit(uid, cartType, req.body).then(data => {
        if (data && data.code === 200 && unionKey) {
            data.data.unionKey = {
                client_id: unionInfo.client_id
            };
        }

        res.send(data);
    }).catch(next);
};

module.exports = {
    index,
    getCoupons,
    compute,
    submit
};