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

'use strict';

const _ = require('lodash');
const crypto = global.yoho.crypto;
const logger = global.yoho.logger;

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

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

const index = (req, res, next) => {
    // 6.7.5 web端下架预售购物车功能
    // let cartType = req.query.type === '2' ? 'advance' : 'ordinary';
    let cartType = 'ordinary';

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

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

        // 预售购物车不支持使用优惠券
        if (req.query.type === '2') {
            result.notUseCoupon = true;
        }

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

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

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

// 获取礼品卡列表
const getGiftCards = (req, res, next) => {
    req.ctx(oeModel).getGiftCards(req.user.uid).then(data => {
        res.send(data);
    }).catch(next);
};

// 订单金额计算
const compute = (req, res, next) => {
    let params = req.body;
    // 6.7.5 web端下架预售购物车功能
    // let cartType = req.query.type === '2' ? 'advance' : 'ordinary';
    let cartType = 'ordinary';

    if (params.sku) { // 快捷结算
        req.ctx(easypayModel).getOrderComputeData(req.user.uid, 'ordinary', params).then(result => {
            res.json(result);
        }).catch(next);
    } else {
        req.ctx(oeModel).compute(req.user.uid, cartType, params).then(data => {
            res.send(data);
        }).catch(next);
    }
};

// 发送虚拟资产使用校验短信(目前仅有礼品卡 --- giftCard)
const sendCheckSms = (req, res, next) => {
    let params = req.body;

    if (params.giftCard) {
        return req.ctx(oeModel).sendGiftCardCkeckSms(req.user.uid, req.yoho.udid).then(result => {
            res.json(result);
        }).catch(next);
    }

    return next();
};

// 订单提交前置校验
const submitCheck = (req, res, next) => {
    let checkCode = req.body.checkCode;

    // 带礼品卡号和短信验证码,则校验短信验证码,否则删除使用礼品卡
    if (req.body.giftCard && checkCode) {
        return req.ctx(oeModel).checkGiftCardSmsCode(req.user.uid, checkCode, req.yoho.udid).then(result => {
            if (result.code === 200) {
                return next();
            }

            res.json(result);
        }).catch(next);
    } else {
        _.unset(req.body, 'giftCard');
    }

    return next();
};

// 提交订单
const submit = (req, res, next) => {
    let params = req.body;
    // 6.7.5 web端下架预售购物车功能
    // let cartType = req.query.type === '2' ? 'advance' : 'ordinary';
    let cartType = 'ordinary';
    let uid = req.user.uid;
    let remoteIp = req.ip;

    if (!params.addressId || !params.paymentType || !params.deliveryWay) {
        res.send({
            code: 500,
            message: '订单参数不完整'
        });
        return;
    }

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

    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 : '';
            }

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

                clientId = unionInfo.client_id;
            } catch (e) {
                logger.error(`unionKey: ${unionKey} format error`);
            }
        } else {
            let unionObj = {
                client_id: req.cookies.mkt_code
            };

            if (req.cookies.union_data) {
                unionObj.union_data = req.cookies.union_data;
            }

            unionKey = JSON.stringify(unionObj);
            clientId = req.cookies.mkt_code;
        }


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

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

    params.udid = req.yoho.udid;

    // 5.8.1 发票优化需求
    // 只接受电子发票(1 纸质 2 电子),发票内容为明细(id 12:明细)
    if (params.invoicesType) {
        Object.assign(params, {
            invoicesType: 2,
            invoicesContent: 12
        });
    }

    if (params.sku) { // 快捷结算
        req.ctx(easypayModel).easypayOrderSubmit(uid, 'ordinary', params, remoteIp).then(result => {
            res.json(result);
        }).catch(next);
    } else {
        req.ctx(oeModel).submit(uid, cartType, params, remoteIp).then(data => {
            if (data && data.code === 200 && unionKey) {
                data.data.unionKey = {
                    client_id: clientId
                };
            }

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

module.exports = {
    index,
    getCoupons,
    convertCoupons,
    getGiftCards,
    compute,
    sendCheckSms,
    submitCheck,
    submit
};