Authored by 郭成尧

order-computer-sub

... ... @@ -7,7 +7,7 @@ const headerModel = require('../../../doraemon/models/header');
const userModel = require('../../serverAPI/user');
const addressModel = require('../../serverAPI/user/address');
const orderModel = require('../models/order');
const crypt = global.yoho.crypt;
exports.orderEnsure = (req, res, next) => {
let headerData = headerModel.setNav({
... ... @@ -134,6 +134,132 @@ exports.fastPay = (req, res, next) => {
};
/**
* 购物车选择改变字段,重新运算订单数据
*/
exports.orderCompute = (req, res, next) => {
let cartType = req.body.cartType || 'ordinary';
let deliveryId = req.body.deliveryId || 1;
let paymentTypeId = req.body.paymentTypeId || 1;
let couponCode = req.body.couponCode || null;
let yohoCoin = req.body.yohoCoin || null;
let productSku = req.body.productSku || null;
let buyNumber = req.body.buyNumber || null;
let uid = req.user.uid;
let skuList = req.body.skuList;
let type = req.body.type;
if (type !== 'tickets') {
cartModel.orderCompute(uid, cartType, deliveryId, paymentTypeId, couponCode, yohoCoin, skuList).then(result => {
res.json(result);
}).catch(next);
} else {
cartModel.ticketsOrderCompute(uid, productSku, buyNumber, yohoCoin).then(result => {
res.json(result);
}).catch(next);
}
};
/**
* 确认结算订单
*/
exports.orderSub = (req, res) => {
let uid = req.user.uid;
let addressId = crypt.decrypt('', req.body.addressId);
let cartType = req.body.cartType || 'ordinary';
let deliveryTimeId = req.body.deliveryTimeId || 1;
let deliveryId = req.body.deliveryId || 1;
let paymentTypeId = req.body.paymentTypeId || 15;
let paymentType = req.body.paymentType || 1;
let msg = req.body.msg || null;
let couponCode = req.body.couponCode || null;
let yohoCoin = req.body.yohoCoin || 1;
let skuList = req.body.skuList || '';
let orderInfo;
try {
orderInfo = JSON.parse(req.cookies['order-info']);
} catch (e) {
orderInfo = {};
}
let times = req.body.times || 1;
// 电子发票信息数组
let invoices = {};
if (orderInfo) {
invoices = {
invoices_type_id: orderInfo.invoiceType,
invoices_type: orderInfo.invoicesType,
receiverMobile: orderInfo.receiverMobile,
invoices_title: orderInfo.invoiceText ? orderInfo.invoiceText : '个人'
};
}
/* 判断是否是友盟过来的用户 */
let userAgent = null;
let unionKey = '';
let unionInfo = {};
if (req.cookies.mkt_code || req.cookies._QYH_UNION) {
/* tar modified 161108 添加新的联盟数据处理逻辑,兼容原有联盟数据处理,
区别是旧的北京写 cookie 加密过来,新的 node 写 cookie,没有加密 */
if (req.cookies._QYH_UNION) {
let encryData = decodeURI(req.cookies._QYH_UNION);
let testQyhUnion = JSON.parse(encryData);
if (testQyhUnion.client_id) {
unionKey = encryData;
} else {
// unionKey = helpers.unionDecode(req.cookies._QYH_UNION, 'q_union_yohobuy'); // TODO 这个方法没有
}
} 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;
}
return co(function* () {
let result;
/* tar modified 161206 套餐 */
if (req.body.activityType === 'bundle') {
let activityInfo = JSON.parse(req.cookies['activity-info']);
result = yield cartModel.orderSub(uid, addressId, 'bundle', deliveryTimeId,
deliveryId, invoices, paymentTypeId, paymentType, msg, couponCode,
yohoCoin, null, unionKey, userAgent, times, activityInfo);
} else {
result = yield cartModel.orderSub(uid, addressId, cartType, deliveryTimeId,
deliveryId, invoices, paymentTypeId, paymentType, msg, couponCode,
yohoCoin, skuList, unionKey, userAgent);
}
if (unionInfo) {
result.data.unionCookie = unionInfo;
}
// 提交成功清除Cookie
res.cookie('order-info', null, {
domain: 'yohobuy.com'
});
if (result.code === 409) {
return res.json(decodeURI(result));
} else {
return res.json(result);
}
})();
};
/**
* 下单流程 选择优惠券
*/
exports.selectCoupon = (req, res) => {
... ...
... ... @@ -6,6 +6,57 @@ const paymentProcess = require(global.utils + '/payment-process');
const shoppingAPI = require('../../serverAPI/order/shopping');
/**
* 转换价格
*
* @param float|string $price 价格
* @param boolean $isSepcialZero 是否需要特殊的0,默认否
* @return float|string 转换之后的价格
*/
const transPrice = (price, isSepcialZero) => {
if (!isSepcialZero) {
isSepcialZero = false;
}
return price;
};
/**
*有货币使用前端方案显示及是否可单击判断
*/
const _yohoCoinCompute = (orderCompute) => {
let yohoCoinData = {
totalYohoCoinNum: 0,
yohoCoin: 0,
useYohoCoin: 0,
yohoCoinClick: 0,
yohoCoinMsg: '',
yoho_coin_pay_rule: []
};
if (!orderCompute || !orderCompute.yoho_coin_pay_rule) {
return yohoCoinData;
}
_.assign(yohoCoinData, {
totalYohoCoinNum: orderCompute.total_yoho_coin_num ? parseInt(orderCompute.total_yoho_coin_num, 10) : 0,
yohoCoin: orderCompute.yoho_coin ? transPrice(orderCompute.yoho_coin) : 0,
useYohoCoin: orderCompute.use_yoho_coin ? transPrice(orderCompute.use_yoho_coin) : 0,
yoho_coin_pay_rule: orderCompute.yoho_coin_pay_rule
});
if (yohoCoinData.totalYohoCoinNum < 100) {
yohoCoinData.yohoCoinMsg = `共${yohoCoinData.totalYohoCoinNum}有货币,满${orderCompute.yoho_coin_pay_rule.num_limit}可用`;
} else if (yohoCoinData.useYohoCoin > 0 || yohoCoinData.yohoCoin > 0) {
yohoCoinData.yohoCoinMsg = '可抵¥' + (yohoCoinData.useYohoCoin > 0 ? yohoCoinData.useYohoCoin : yohoCoinData.yohoCoin);
yohoCoinData.yohoCoinClick = 1;
} else {
yohoCoinData.yohoCoinMsg = '不满足有货币使用条件';
}
return yohoCoinData;
};
/**
* 调用购物车结算接口返回的数据处理
*
*
... ... @@ -98,6 +149,143 @@ exports.easyPayment = (uid, skuList) => {
};
/**
* 购物车结算--支付方式和配送方式选择以及是否使用有货币接口返回的数据处理
*
* @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 接口返回的数据
*/
exports.orderCompute = (uid, cartType, deliveryWay, paymentType, couponCode, yohoCoin, skuList) => {
return shoppingAPI.orderComputeAPI(uid, cartType, deliveryWay, paymentType, couponCode, yohoCoin, skuList).then(result => {
if (result && result.data) {
result.data.use_yoho_coin = transPrice(result.data.use_yoho_coin);
result.data.yohoCoinCompute = _yohoCoinCompute(result.data);
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);
result.data.yohoCoinCompute = _.yohoCoinCompute(result.data.shopping_cart_data);
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,
couponCode, yohoCoin, skuList, qhyUnio = '',
userAgent, times, activityInfo = null) => {
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;
});
};
/**
* 处理优惠券列表数据
*
* @param int $uid 用户ID
... ...
... ... @@ -31,6 +31,8 @@ router.get('/index/new/pay/alipayresult', authMW, payController.payAli);// 支ä»
router.get('/index/new/orderEnsure', authMW, order.orderEnsure); // 订单结算
router.get('/index/new/fast', authMW, order.fastPay); // 快速结算订单提交
router.post('/index/new/orderCompute', authMW, order.orderCompute); // 结算页参数改变,重新运算
router.post('/index/new/orderSub', authMW, order.orderSub); // 结算页参数改变,重新运算
router.get('/index/new/selectCoupon', authMW, order.selectCoupon); // 选择优惠券 页面
router.get('/index/new/couponList', order.couponList); // [ajax]获取优惠券列表
router.post('/index/new/couponSearch', order.couponSearch); // [ajax]购物车输入优惠券码使用优惠券
... ...
... ... @@ -2,6 +2,14 @@
const _ = require('lodash');
const api = global.yoho.API;
const privateKeyList = {
android: 'fd4ad5fcfa0de589ef238c0e7331b585',
iphone: 'a85bb0674e08986c6b115d5e3a4884fa',
ipad: 'ad9fcda2e679cf9229e37feae2cdcf80',
web: '0ed29744ed318fd28d2c07985d3ba633',
h5: 'fd4ad5fcfa0de589ef238c0e7331b585'
};
/**
* 购物车结算
* doc: http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/订单/shopping.md
... ... @@ -42,7 +50,7 @@ exports.easyPayment = (uid, skuList) => {
product_sku_list: skuList
};
return api.get('', param);
return api.get('', param, {code: 200});
};
... ... @@ -84,7 +92,139 @@ exports.orderComputeAPI = (uid, cartType, deliveWay, paymentType, couponCode, yo
param.product_sku_list = skuList;
}
return api.get('', param);
return api.get('', param, {code: 200});
};
/**
* 校验电子票
* @param int $uid
* @param int $productSku
* @param int $buyNumber
* @param int $useYohoCoin
* @param bool|int $yohoCoinMode 1:使用有货币;0:不使用有货币
* @return array
* @author sefon 2016-7-2 18:12:30
*/
exports.checkTickets = (uid, productSku, buyNumber, useYohoCoin, yohoCoinMode) => {
if (!useYohoCoin) {
useYohoCoin = 0;
}
if (!yohoCoinMode) {
yohoCoinMode = true;
}
let params = {
method: 'app.shopping.ticket',
uid: uid,
product_sku: productSku,
buy_number: buyNumber,
use_yoho_coin: useYohoCoin
};
if (!yohoCoinMode) {
_.assign(params, {
yoho_coin_mode: 0
});
}
return api.get('', params, {code: 200});
};
/**
* 购物车结算--提交结算信息
*
* @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 $qhyUnion 友盟有关信息
* @param string|null $userAgent 联盟过来用户下单时需要的User-Agent信息
* @param $times
* @param null $activityInfo 套餐信息
* @return array 接口返回的数据
*/
exports.orderSub = (uid, addressId, cartType, deliveryTime,
deliveryWay, invoices, paymentId, paymentType, remark, couponCode,
yohoCoin, skuList, qhyUnion, userAgent, times, activityInfo) => {
if (!activityInfo) {
activityInfo = null;
}
let params = {
debug: 'Y',
private_key: privateKeyList.h5,
method: activityInfo ? 'app.Shopping.easySubmit' : 'app.Shopping.submit',
address_id: addressId,
cart_type: cartType,
delivery_time: deliveryTime,
delivery_way: deliveryWay,
payment_id: paymentId,
payment_type: paymentType,
remark: remark,
uid: uid
};
/* tar add 161130 结算优化 */
if (times === 2 || times === '2') {
params.is_continue_buy = 'Y';
} else {
params.is_continue_buy = 'N';
}
if (invoices.invoices_type_id) {
// 发票内容id--图书:1
params.invoice_content = invoices.invoices_type_id;
}
if (invoices.invoices_type) {
// 数字类型 发票类型 纸质 1 ,电子 2
params.invoices_type = invoices.invoices_type;
}
if (invoices.receiverMobile) {
// 发票人手机
params.receiverMobile = invoices.receiverMobile;
}
if (invoices.invoices_title) {
// 发票抬头 OR 填写单位名称,这二个是一个意思,只是叫法不一样
params.invoices_title = invoices.invoices_title;
}
if (couponCode) {
params.coupon_code = couponCode;
}
if (yohoCoin) {
params.use_yoho_coin = yohoCoin;
}
// 购买限购商品时需要传递product_sku_list参数
if (skuList && !activityInfo) {
params.product_sku_list = skuList;
}
// 购买套餐商品需要的数据
if (activityInfo) {
params.activity_id = activityInfo.activity_id;
params.product_sku_list = activityInfo.product_sku_list; // TODO 检查 JSON 是否正常
}
// 友盟有关信息的传递
if (qhyUnion) {
params.qhy_union = qhyUnion;
}
return api.get('', params);
};
... ...
... ... @@ -17,15 +17,15 @@ module.exports = {
assetUrl: '//127.0.0.1:5001',
testCode: 'yoho4946abcdef#$%&!@',
domains: {
// api: 'http://api-test3.yohops.com:9999/',
// service: 'http://service-test3.yohops.com:9999/',
// liveApi: 'http://testapi.live.yohops.com:9999/',
// singleApi: 'http://api-test3.yohops.com:9999/',
api: 'http://api-test3.yohops.com:9999/',
service: 'http://service-test3.yohops.com:9999/',
liveApi: 'http://testapi.live.yohops.com:9999/',
singleApi: 'http://api-test3.yohops.com:9999/',
api: 'http://dev-api.yohops.com:9999/',
service: 'http://dev-service.yohops.com:9999/',
liveApi: 'http://api.live.yoho.cn/',
singleApi: 'http://single.yoho.cn/',
// api: 'http://dev-api.yohops.com:9999/',
// service: 'http://dev-service.yohops.com:9999/',
// liveApi: 'http://api.live.yoho.cn/',
// singleApi: 'http://single.yoho.cn/',
imSocket: 'wss://imsocket.yohobuy.com:443',
imCs: 'https://imhttp.yohobuy.com/api',
... ...
... ... @@ -207,7 +207,7 @@ function orderCompute() {
loading.showLoadingMask();
$.ajax({
method: 'POST',
url: '/cart/index/orderCompute',
url: '/cart/index/new/orderCompute',
data: data
}).then(function(res) {
if ($.type(res) !== 'object') {
... ...