Blame view

apps/cart/models/cart.js 10.9 KB
陈轩 authored
1 2
'use strict';
3 4 5
const _ = require('lodash');
const helpers = global.yoho.helpers;
const paymentProcess = require(global.utils + '/payment-process');
陈轩 authored
6
const shoppingAPI = require('../../serverAPI/order/shopping');
7 8

/**
郭成尧 authored
9 10 11 12 13 14 15
 * 转换价格
 *
 * @param float|string $price 价格
 * @param boolean $isSepcialZero 是否需要特殊的0,默认否
 * @return float|string 转换之后的价格
 */
const transPrice = (price, isSepcialZero) => {
郭成尧 authored
16 17 18 19
    if (!price) {
        price = 0;
    }
郭成尧 authored
20 21 22 23
    if (!isSepcialZero) {
        isSepcialZero = false;
    }
郭成尧 authored
24 25 26 27
    if ((price || isSepcialZero) && _.isNumber(price)) {
        return price.toFixed(2);
    } else {
        return price;
郭成尧 authored
28
    }
郭成尧 authored
29 30 31
};

/**
32 33
 * 调用购物车结算接口返回的数据处理
 *
34 35
 *
 *
36 37 38 39 40 41 42 43 44 45
 * @param int $uid 用户ID
 * @param string $cartType 购物车类型,ordinary表示普通购物车
 * @param array $orderInfo cookie中记录的一些订单有关数据
 * @param string $limitProductCode 限购商品码,用户限购商品购买
 * @param string $sku 商品sku,用于限购商品购买
 * @param stirng $skn 商品skn,用于限购商品购买
 * @param int $buyNumber 购买商品数目,用户限购商品支付
 * @param bool $isAjax 是否是异步请求
 * @return array 接口返回的数据
 */
郭成尧 authored
46
exports.cartPay = (uid, cartType, orderInfo, limitProductCode, sku, skn, buyNumber, activityInfo) => {
47
    let result = {};
48
    let skuList = [];
49
    let isLimitGoods = skn && sku && buyNumber; // 存在sku, skn 和buyNumber时为限购商品
陈轩 authored
50
    let orderComputeAPI = null;
51 52

    if (isLimitGoods) {
53
        skuList.push({
54 55 56 57 58
            type: 'limitcode',
            limitproductcode: limitProductCode,
            buy_number: buyNumber,
            skn,
            sku
59
        });
60 61 62 63

        result.isLimit = true;
    }
64 65 66
    // cookie保存的数据
    if (!_.isEmpty(orderInfo)) {
        orderInfo.paymentType = orderInfo.paymentType ? orderInfo.paymentType : '';
陈轩 authored
67
        orderComputeAPI = shoppingAPI.orderComputeAPI(
68 69 70 71 72 73 74 75 76
            uid,
            cartType,
            orderInfo.deliveryId,
            orderInfo.paymentType,
            orderInfo.couponCode,
            orderInfo.yohoCoin, skuList
        );
    }
郭成尧 authored
77
    // 区分套餐量贩和普通商品
郭成尧 authored
78
    let cartPayAPI;
郭成尧 authored
79 80 81

    if (activityInfo) {
        cartPayAPI = shoppingAPI.cartPayAPI(uid, cartType, 0, skuList, activityInfo);
郭成尧 authored
82 83
    } else {
        cartPayAPI = shoppingAPI.cartPayAPI(uid, cartType, 0, skuList);
郭成尧 authored
84 85
    }
陈轩 authored
86
    return Promise.all([
郭成尧 authored
87
        cartPayAPI, // 0. 订单数据
陈轩 authored
88 89 90 91 92 93 94 95 96 97 98 99 100 101
        orderComputeAPI,
        shoppingAPI.getValidCouponCount(uid) // 2. 有效优惠券
    ]).then(res => {
        let pay = res[0];
        let orderCompute = _.get(res[1], 'data', {});
        let validCouponCount = _.get(res[2], 'data.count', 0);

        let goodsList = _.get(pay, 'data.goods_list', []);

        if (_.isEmpty(goodsList)) {
            if (isLimitGoods) {
                result.error = true;
                result.message = pay.message;
            } else {
郭成尧 authored
102
                result.cartUrl = helpers.urlFormat('/cart/index/index');
陈轩 authored
103 104
            }
郭成尧 authored
105
            return result;
陈轩 authored
106
        }
107
陈轩 authored
108 109 110
        result = Object.assign(
            result,
            paymentProcess.tranformPayment(pay.data, orderInfo, cartType, skuList, orderCompute),
111
陈轩 authored
112 113
            {
                coupon: paymentProcess.coupon(validCouponCount, orderInfo, orderCompute)
114
            }
陈轩 authored
115 116 117 118 119
        );

        return result;
    });
};
120
陈轩 authored
121
/**
郭成尧 authored
122 123 124 125 126 127 128 129 130 131 132
 * 购物车结算--支付方式和配送方式选择以及是否使用有货币接口返回的数据处理
 *
 * @param int $uid 用户ID
 * @param string $cartType 购物车类型,ordinary表示普通购物车
 * @param int $deliveryWay 配送方式,1表示普通快递,2表示顺丰速运
 * @param int $paymentType 支付方式,1表示在线支付,2表示货到付款
 * @param string $couponCode 优惠券码
 * @param mixed $yohoCoin 使用的有货币数量
 * @param string $skuList 购买限购商品时需要传递的参数
 * @return array 接口返回的数据
 */
郭成尧 authored
133 134
exports.orderCompute = (uid, cartType, deliveryWay, paymentType, couponCode, yohoCoin, skuList, activityInfo) => {
    return shoppingAPI.orderComputeAPI(uid, cartType, deliveryWay, paymentType, couponCode, yohoCoin, skuList, activityInfo).then(result => {
郭成尧 authored
135 136
        if (result && result.data) {
            result.data.use_yoho_coin = transPrice(result.data.use_yoho_coin);
郭成尧 authored
137
            result.data.yohoCoinCompute = paymentProcess.yohoCoinCompute(result.data);
郭成尧 authored
138 139 140 141 142 143 144 145 146 147 148 149
            return result.data;
        } else {
            return {};
        }
    });
};


exports.ticketsOrderCompute = (uid, productSku, buyNumber, yohoCoin) => {
    return shoppingAPI.checkTickets(uid, productSku, buyNumber, yohoCoin).then(result => {
        if (result && result.data) {
            result.data.shopping_cart_data.use_yoho_coin = transPrice(result.data.shopping_cart_data.use_yoho_coin);
郭成尧 authored
150
            result.data.yohoCoinCompute = paymentProcess.yohoCoinCompute(result.data.shopping_cart_data);
郭成尧 authored
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

            return result.data;
        } else {
            return {};
        }
    });
};

/**
 * 购物车结算--提交结算信息
 *
 * @param int $uid 用户ID
 * @param int $addressId 地址ID
 * @param int $cartType 购物车类型ID
 * @param int $deliveryTime 寄送时间ID
 * @param int $deliveryWay 寄送方式ID
 * @param array $invoices 发票参数数组
 * @param int $paymentId 支付方式ID
 * @param int $paymentType 支付类型ID
 * @param string $remark 留言
 * @param string $couponCode 优惠券码
 * @param mixed $yohoCoin 使用的有货币数量或为空
 * @param string $skuList 购买限购商品时需要传递的参数
 * @param string $qhyUnio 友盟有关信息
 * @param string|null $userAgent 联盟过来用户下单时需要的User-Agent信息
 * @param int $times
 * @param null $activityInfo 套餐数据
 * @return array 接口返回的数据
 */
exports.orderSub = (uid, addressId, cartType, deliveryTime,
    deliveryWay, invoices, paymentId, paymentType, remark,
郭成尧 authored
182 183 184 185 186
    couponCode, yohoCoin, skuList, qhyUnio,
    userAgent, times, activityInfo) => {
    if (!qhyUnio) {
        qhyUnio = '';
    }
郭成尧 authored
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261

    if (!userAgent) {
        userAgent = null;
    }

    if (!times) {
        times = 1;
    }

    if (!activityInfo) {
        activityInfo = null;
    }

    if (!addressId) {
        return Promise.resolve({code: 401, message: '配送地址不能为空'});
    }

    if (!deliveryTime) {
        return Promise.resolve({code: 402, message: '请选择配送时间'});
    }

    if (!deliveryWay) {
        return Promise.resolve({code: 403, message: '请选择配送方式'});
    }

    return shoppingAPI.orderSub(uid, addressId, cartType, deliveryTime,
        deliveryWay, invoices, paymentId, paymentType,
        remark, couponCode, yohoCoin, skuList, qhyUnio,
        userAgent, times, activityInfo).then(orderSubRes => {
            let finalResult = {};

            if (orderSubRes && orderSubRes.data && orderSubRes.data.is_hint === 'Y') {
                finalResult.code = 409;

                if (orderSubRes.data.hintInfo) {
                    let productName = _.get('orderSubRes', 'data.hintInfo.productName', '');

                    if (productName.length > 50) { // TODO 需要检查长度是否正确
                        orderSubRes.data.hintInfo.productName =
                           productName.substring(0, 47) + '...';
                    }

                    finalResult.message = [
                        encodeURI(productName + _.get(orderSubRes, 'data.hintInfo.suffix', '')),
                        encodeURI(_.get(orderSubRes, 'data.hintInfo.lastLine', ''))
                    ];
                }

                finalResult.buttons = [
                    {
                        href: helpers.urlFormat('/cart/index/index'),
                        text: '重新选择商品',
                        class: ''
                    },
                    {
                        href: '',
                        text: '继续结算',
                        class: 'order-tip-btnred'
                    }
                ];
            } else if (orderSubRes && orderSubRes.code) {
                finalResult = orderSubRes;
            } else {
                finalResult = {
                    code: 400,
                    message: '出错啦'
                };
            }

            return finalResult;
        });

};

/**
陈轩 authored
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
 * 处理优惠券列表数据
 *
 * @param int $uid 用户ID
 * @return array|mixed 处理之后的优惠券数据
 */
exports.getCouponList = uid => {
    let result = {
        notAvailableCoupons: [],
        coupons: []
    };

    return shoppingAPI.listCoupon(uid)
        .then(coupons => {
            let unusableCoupons = _.get(coupons, 'data.unusable_coupons', []);
            let usableCoupons = _.get(coupons, 'data.usable_coupons', []);

            let procCouponsData = coupon => {
                return {
                    couponCode: coupon.coupon_code,
                    couponDetailInfomation: coupon.coupon_name,
                    couponValue: coupon.coupon_value,
                    couponValidity: coupon.coupon_validity
                };
            };

            result.notAvailableCoupons = unusableCoupons.map(procCouponsData);
            result.coupons = usableCoupons.map(procCouponsData);

            return result;
        },
            () => result
        );
};

exports.searchCoupon = (uid, couponCode) => {
    let result = {code: 400, message: '出错啦~'};

    if (!couponCode) {
        return Promise.resolve({
            code: 401,
            message: '优惠券代码为空'
303
        });
陈轩 authored
304 305 306 307
    }

    return shoppingAPI.useCoupon(uid, couponCode)
        .catch(() => result);
陈轩 authored
308
};
郭成尧 authored
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

 /**
 * jit拆单数据(结算页和订单详情页)
 * @param type $uid 用户uid
 * @param type $cartType 购物车类型
 * @param type $skuList cookie中记录的一些订单有关数据
 * @param type $orderCode 订单号
 * @param type $sessionKey 用户会话
 * @param type $deliveryId 配送方式,1表示普通快递,2表示顺丰速运
 * @param type $paymentType 支付方式,1表示在线支付,2表示货到付款
 * @param type $couponCode 优惠券码
 * @param type $yohoCoin 使用的有货币数量
 * @return type
 */
exports.jitDetailData = (uid, cartType, skuList, orderCode, sessionKey,
    deliveryId, paymentType, couponCode, yohoCoin) => {
    if (deliveryId) {

        // 购物车选择改变字段,重新运算订单数据
        return shoppingAPI.orderComputeAPI(
            uid,
            cartType,
            deliveryId,
            paymentType,
            couponCode,
            yohoCoin,
            skuList
        ).then(result => {
            return paymentProcess.transformJit(_.get(result, 'data.package_list', []));
        });
    } else {
        return shoppingAPI.cartPayAPI(
            uid,
            cartType,
            0,
            skuList
        ).then(result => {
            return paymentProcess.transformJit(_.get(result, 'data.shopping_cart_data.package_list', []));
        });
    }
};