Authored by 陈轩

订单结算 重构 save commit

... ... @@ -7,9 +7,9 @@ const cartModel = require('../models/cart');
exports.orderEnsure = (req, res, next) => {
let uid = req.user.uid;
let returnUrl = helpers.urlFormat('/cart/index/new');
let cartType = req.query.cartType;
let cartType = req.query.cartType || 'ordinary';
let orderInfo;
let orderInfo;
try {
orderInfo = JSON.parse(req.cookies['order-info']);
} catch (e) {
... ... @@ -26,7 +26,11 @@ exports.orderEnsure = (req, res, next) => {
returnUrl = req.get('Referer') || returnUrl;
}
cartModel.cartPay(uid, cartType, orderInfo, limitProductCode)
cartModel.cartPay(uid, cartType, orderInfo, sku, skn, buyNumber, req.xhr)
.then(order => {
})
.catch(next);
res.send('todo: orderEnsure');
};
... ...
'use strict';
const _ = require('lodash');
const api = global.yoho.api;
const helpers = global.yoho.helpers;
const paymentProcess = require(global.utils + '/payment-process');
/**
* 购物车结算
*
* @param int uid 用户ID
* @param string cartType 购物车类型,ordinary表示普通购物车
* @param int isUseYohoCoin 是否使用有货币,0不使用, 1使用
* @param string skuList 购买限购商品时需要传递的参数
* @param int $uid 用户ID
* @param string $cartType 购物车类型,ordinary表示普通购物车
* @param int $isUseYohoCoin 是否使用有货币,0不使用, 1使用
* @param string $skuList 购买限购商品时需要传递的参数
* @return array 接口返回的数据
*/
exports.cartPay = (uid, cartType, isUseYohoCoin, skuList) => {
function cartPayAPI(uid, cartType, isUseYohoCoin, skuList) {
let param = {
method: 'app.Shopping.payment',
enable_red_envelopes: 0, // h5不返回红包
... ... @@ -20,9 +23,62 @@ exports.cartPay = (uid, cartType, isUseYohoCoin, skuList) => {
uid: uid
};
if (skuList) {
// 购买限购商品时需要传递product_sku_list参数
if (!_.isEmpty(skuList)) {
param.product_sku_list = skuList;
}
return api.get('', param);
}
/**
* 调用购物车结算接口返回的数据处理
*
* @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 接口返回的数据
*/
exports.cartPay = (uid, cartType, orderInfo, limitProductCode, sku, skn, buyNumber, isAjax) => {
isAjax = isAjax || false;
let result = {};
let skuList = null;
let isLimitGoods = skn && sku && buyNumber; // 存在sku, skn 和buyNumber时为限购商品
if (isLimitGoods) {
skuList = {
type: 'limitcode',
limitproductcode: limitProductCode,
buy_number: buyNumber,
skn,
sku
};
result.isLimit = true;
}
return cartPayAPI(uid, cartType, 0, skuList)
.then(pay => {
let goodsList = _.get(pay, 'data.goods_list', []);
if (_.isEmpty(goodsList)) {
if (isLimitGoods) {
result.error = true;
result.message = pay.message;
} else {
result.cartUrl = helpers.urlFormat('/cart/index/new');
}
return pay;
}
return paymentProcess.tranformPayment(pay.data, orderInfo);
});
};
... ...
... ... @@ -11,6 +11,7 @@ const cRoot = './controllers';
const authMW = require('../../doraemon/middleware/auth');
const seckill = require(cRoot + '/seckill');
const order = require(cRoot + '/order');
const countController = require(`${cRoot}/count`);
// Your controller here
... ... @@ -22,4 +23,9 @@ router.post('/index/seckill/submit', seckill.submit);
router.get('/index/count', countController.cartCount);
router.get('/index/new/orderEnsure', authMW, order.orderEnsure); // 订单结算
router.get('/index/new/selectAddress', authMW, ()=>{}); // 选择地址
router.get('/index/new/selectCoupon', authMW, ()=>{}); // 选择优惠券
module.exports = router;
... ...
... ... @@ -40,27 +40,27 @@ function tranformPayment(data, orderInfo) {
let defaultKey = 0;
deliveryWay.forEach((way, index) => {
if (way.delivery_way_name === '顺丰速运' && !isSunfengSupport) {
return;
}
if (way.delivery_way_name === '顺丰速运' && !isSunfengSupport) {
return;
}
let obj = {};
let obj = {};
(way.default === 'Y') && (defaultKey = index);
(way.default === 'Y') && (defaultKey = index);
if (cookieWayId && (way.delivery_way_id === cookieWayId)) {
obj.isSelected = true;
isDeliveryId = false;
}
if (cookieWayId && (way.delivery_way_id === cookieWayId)) {
obj.isSelected = true;
isDeliveryId = false;
}
arr.push(
Object.assign({
id: way.delivery_way_id,
name: way.delivery_way_name,
cost: way.delivery_way_cost,
}, obj)
);
});
arr.push(
Object.assign({
id: way.delivery_way_id,
name: way.delivery_way_name,
cost: way.delivery_way_cost,
}, obj)
);
});
if (isDeliveryId) {
arr[defaultKey].isSelected = true;
... ...