Blame view

apps/cart/models/easypay.js 3.98 KB
yyq authored
1 2 3 4 5 6 7 8 9 10 11
/**
 * 立即购买controller
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/9/23
 */
'use strict';

const _ = require('lodash');

const helper = global.yoho.helpers;
郝肖肖 authored
12 13 14
const EasypayApi = require('./easypay-api');
const AddressApi = require('./address-api');
const EnsureApi = require('./order-ensure-api');
yyq authored
15
郝肖肖 authored
16 17 18 19 20
const EnsureHandle = require('./order-ensure-handle');

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
yyq authored
21 22
    }
郝肖肖 authored
23 24
    _getLimitProductData(params) {
        let resList = [];
yyq authored
25
郝肖肖 authored
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
        if (params.limitcode && params.sku && params.skn) {
            resList.push({
                type: 'limitcode',
                buy_number: 1,
                sku: params.sku,
                skn: params.skn,
                limitproductcode: params.limitcode
            });
        } else if (params.bundle && params.sku) {
            resList = [];

            _.forEach(_.split(params.sku, ',', 10), val => {
                resList.push({
                    type: 'bundle',
                    sku: parseInt(val, 10),
                    buy_number: 1
                });
yyq authored
43 44 45
            });
        }
郝肖肖 authored
46 47
        return resList.length ? JSON.stringify(resList) : false;
    }
yyq authored
48
郝肖肖 authored
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    // 获取结算信息
    getEasypayOrderData(params, uid) {
        let resData = {};
        let strInfo = this._getLimitProductData(params);
        let bundle = params.bundle ? parseInt(params.bundle, 10) : false;

        return Promise.all([
            new EasypayApi(this.ctx).getEasyPaymentAsync(uid, strInfo, bundle),
            new AddressApi(this.ctx).getAddressListAsync(uid),
            new EnsureApi(this.ctx).getUserProfileAsync(uid)
        ]).then(result => {
            let data = _.get(result, '[0].data', false);
            let address = _.get(result, '[1].data', []);
            let receiver = _.get(result, '[2].data.mobile', '');
yyq authored
64 65
            if (+_.get(result, '[0].code') !== 200) {
                resData.pageErrorTip = _.get(result, '[0].message') || '网络拥堵,请刷新后重试';
yyq authored
66 67 68 69

                return resData;
            }
郝肖肖 authored
70 71 72 73 74
            if (data) {
                Object.assign(resData, new EnsureHandle(this.ctx).handlePaymentInfo(data, address), {
                    receiverMobile: receiver,
                    hideReceiverMobile: _.replace(receiver, /(\d{3})\d{4}(\d{4,9})/, '$1****$2')
                });
yyq authored
75 76
            }
郝肖肖 authored
77 78 79
            return resData;
        });
    }
80
郝肖肖 authored
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    // 价格计算
    getOrderComputeData(uid, cartType, params) {
        params.productSkuList = this._getLimitProductData(params);

        return new EasypayApi(this.ctx).getEasypayComputeAsync(uid, cartType, params.paymentType, params.deliveryWay, params).then(result => { // eslint-disable-line
            if (result.code === 200) {
                let ensureHandleModel = new EnsureHandle(this.ctx);

                if (_.has(result, 'data.last_order_amount')) {
                    result.data.last_order_amount = (result.data.last_order_amount).toFixed(2);
                }

                if (_.has(result, 'data.promotion_formula_list')) {
                    ensureHandleModel.handleViewPrice(result.data.promotion_formula_list);
                }

                result.data = Object.assign(result.data, ensureHandleModel.handleUseYohoCoin(result.data));
            }
            return result;
        });
    }
yyq authored
102
郝肖肖 authored
103 104 105
    // 订单提交
    easypayOrderSubmit(uid, cartType, params, remoteIp) {
        params.productSkuList = this._getLimitProductData(params);
yyq authored
106
郝肖肖 authored
107 108 109 110 111
        return new EasypayApi(this.ctx).easypayOrderSubmitAsync(
            uid, cartType, params.addressId, params.deliveryTime,
            params.deliveryWay, params.paymentType, params.paymentId,
            params.printPrice, params, remoteIp
        ).then(result => {
yyq authored
112 113 114
            if (result.code === 200) {
                let d = result.data;
郝肖肖 authored
115
                d.url = helper.urlFormat('/shopping/newpay', {
yyq authored
116 117 118 119
                    ordercode: d.order_code
                });
            }
            return result;
郝肖肖 authored
120 121
        });
    }
yyq authored
122
};