Authored by 郭成尧

auto-chose

... ... @@ -115,6 +115,7 @@ exports.orderEnsure = (req, res, next) => {
let userProfile = result[1];
let address = result[2];
let isNewUser = result[3];
let autoSelectCouponCodeStr = _.get(order, 'coupon.couponCode', '');
if (!_.isUndefined(isNewUser)) {
if (isNewUser) {
... ... @@ -124,6 +125,11 @@ exports.orderEnsure = (req, res, next) => {
}
}
if (autoSelectCouponCodeStr) {
orderInfo.couponCode = autoSelectCouponCodeStr;
res.cookie('order-info', JSON.stringify(orderInfo), actCkOpthn);
}
if (req.xhr) {
logger.info(`orderEnsure: ajax request, return json:${JSON.stringify(order)}`);
return res.json(order);
... ... @@ -350,7 +356,6 @@ exports.orderSub = (req, res, next) => {
/**
* 下单流程-选择优惠券页面
* New!
*/
exports.selectCouponsPage = (req, res) => {
let headerData = headerModel.setNav({
... ... @@ -374,19 +379,9 @@ exports.selectCouponsPage = (req, res) => {
* 下单流程--获取优惠券列表
*/
exports.couponList = (req, res, next) => {
let orderInfo;
let uid = req.user.uid;
try {
orderInfo = JSON.parse(req.cookies['order-info']);
} catch (e) {
logger.info(`orderEnsure: get orderInfo from cookie error:${JSON.stringify(e)}`);
orderInfo = {};
res.clearCookie('order-info', actCkOpthn);
}
let delivery_way = orderInfo.deliveryId || 1;
let coupon_code = orderInfo.coupon_code || '';
let delivery_way = req.body.delivery_way || 1;
let coupon_code = req.body.coupon_code || '';
co(function* () {
let couponApi = yield req.ctx(shoppingModel).listCoupon({
... ...
... ... @@ -33,17 +33,22 @@ class cartModel extends global.yoho.BaseModel {
result.isLimit = true;
}
// cookie保存的数据
if (params.orderInfo && !_.isEmpty(params.orderInfo)) {
params.orderInfo.paymentType = params.orderInfo.paymentType ? params.orderInfo.paymentType : '';
let orderInfoCookie = _.get(params, 'orderInfo', null);
// 是否需要重新计算
let needReComputer = orderInfoCookie && !_.isEmpty(orderInfoCookie) &&
(orderInfoCookie.yohoCoin || orderInfoCookie.couponCode);
if (needReComputer) {
orderInfoCookie.paymentType = orderInfoCookie.paymentType ? orderInfoCookie.paymentType : '';
orderComputeAPI = this.ctx.req.ctx(shoppingModel).orderComputeAPI({
uid: params.uid,
cart_type: params.cartType,
delivery_way: params.orderInfo.deliveryId,
payment_type: params.orderInfo.paymentType,
coupon_code: params.orderInfo.couponCode,
gift_card_code: params.orderInfo.gift_card_code,
use_yoho_coin: params.orderInfo.yohoCoin,
delivery_way: orderInfoCookie.deliveryId,
payment_type: orderInfoCookie.paymentType,
coupon_code: orderInfoCookie.couponCode,
gift_card_code: orderInfoCookie.gift_card_code,
use_yoho_coin: orderInfoCookie.yohoCoin,
product_sku_list: skuList,
activityInfo: params.activityInfo
});
... ... @@ -54,11 +59,11 @@ class cartModel extends global.yoho.BaseModel {
let cartPayAPIParams = {
uid: params.uid,
cart_type: params.cartType,
delivery_way: params.orderInfo.deliveryId,
payment_type: params.orderInfo.paymentType,
coupon_code: params.orderInfo.couponCode,
gift_card_code: params.orderInfo.gift_card_code,
use_yoho_coin: params.orderInfo.yohoCoin,
delivery_way: orderInfoCookie.deliveryId,
payment_type: orderInfoCookie.paymentType,
coupon_code: orderInfoCookie.couponCode,
gift_card_code: orderInfoCookie.gift_card_code,
use_yoho_coin: orderInfoCookie.yohoCoin,
product_sku_list: skuList,
activityInfo: params.activityInfo
};
... ... @@ -81,8 +86,8 @@ class cartModel extends global.yoho.BaseModel {
let orderCompute = _.get(res[1], 'data', {});
let validCouponCount = _.get(res[2], 'data.count', 0);
let validGiftCardCount = _.get(res[3], 'data.count', 0);
let goodsList = _.get(pay, 'data.goods_list', []);
let paymentApiCouponData = _.get(pay, 'data.coupon_pay', {});
if (_.isEmpty(goodsList)) {
logger.info(`orderEnsure: goodsList is empty, isLimitGoods: ${isLimitGoods}`);
... ... @@ -99,13 +104,14 @@ class cartModel extends global.yoho.BaseModel {
return result;
}
result = Object.assign(
_.assign(
result,
paymentProcess.tranformPayment(pay.data, params.orderInfo, params.cartType, skuList, orderCompute),
{
coupon: paymentProcess.coupon(validCouponCount, params.orderInfo, orderCompute),
},
paymentProcess.tranformPayment(pay.data, orderInfoCookie, params.cartType, skuList, orderCompute),
{
coupon: paymentProcess.handleCoupons({
paymentApiCouponData,
validCouponCount: validCouponCount
}),
giftCards: paymentProcess.handleGiftCards({
validGiftCardCount: validGiftCardCount,
orderCompute: orderCompute
... ...
... ... @@ -7,10 +7,10 @@
</span>
{{^}}
<span class="count">
{{#if selectedAmount}}已选{{selectedAmount}}{{^}}{{count}}张可用{{/if}}
{{showText}}
</span>
<span class="coupon-info pull-right{{#isEqualOr info '无可用'}} no-can-use{{/isEqualOr}}">
{{info}}
{{priceText}}
<i class="iconfont">&#xe614;</i>
</span>
{{/if}}
... ...
... ... @@ -3,8 +3,8 @@
* @author: bikai<kai.bi@yoho.cn>
* @date: 2015/12/14
*/
require('common');
let info = window.cookie('order-info');
const cookie = require('yoho-cookie');
let info = cookie.get('order-info');
// cookie 参数
let actCkOpthn = {
... ... @@ -29,25 +29,21 @@ function init() {
msg: null,
cartType: 'ordinary'
};
window.setCookie('order-info', JSON.stringify(info), actCkOpthn);
cookie.set('order-info', JSON.stringify(info), actCkOpthn);
}
// info 必须是 JSON 字符串
try {
info = JSON.parse(info);
// 2015/12/31 hf: fixes bug to 购物车页面调用该JS, 会导致有有货币,值却没有传给服务端. 因此需要再设置一下
// info['yohoCoin'] = $('.coin').data('yoho-coin') || 0;
} catch (e) {
init();
}
exports.init = init;
exports.orderInfo = function(key, value) {
if (typeof value === 'undefined') {
return info[key];
}
info[key] = value;
window.setCookie('order-info', JSON.stringify(info), actCkOpthn);
cookie.set('order-info', JSON.stringify(info), actCkOpthn);
};
... ...
import $ from 'yoho-jquery';
import Page from 'yoho-page';
import { orderInfo } from 'cart/order-info';
class SelectCouponController extends Page {
constructor() {
super();
this.selectCouponCodes = [];
if (orderInfo('couponCode')) {
this.selectCouponCodes = orderInfo('couponCode').split(',');
}
this.page = $('.select-coupons-page');
this.renderPage();
}
... ... @@ -20,6 +26,7 @@ class SelectCouponController extends Page {
type: 'POST',
url: '/cart/index/new/couponList',
data: {
delivery_way: orderInfo('deliveryId'),
coupon_code: couponCodeStr
}
}).then(result => {
... ...
... ... @@ -477,16 +477,6 @@
text-align: right;
}
/* .fill-text {
padding: 0.05rem 0.3rem;
color: #fff;
font-size: 0.625rem;
line-height: 0.75rem;
border-radius: 0.75rem;
display: inline-block;
background-color: #e01;
} */
.price {
color: #e01;
}
... ... @@ -533,9 +523,11 @@
.count {
background-color: #ff575c;
margin-left: 9px;
margin-left: 12px;
padding: 4px 15px;
color: #fff;
font-size: 22px;
font-weight: 300;
}
.coupon-count {
... ... @@ -754,6 +746,7 @@
.coupon-help {
color: #b0b0b0;
margin-left: 30px;
}
.yoho-coin-help {
... ...
... ... @@ -375,6 +375,39 @@ function coupon(count, orderInfo, orderComputeData) {
}
/**
* 处理优惠券数据
*/
function handleCoupons(params) {
let paymentApiCouponData = _.get(params, 'paymentApiCouponData', {});
let validCouponCount = _.get(params, 'validCouponCount');
let couponCode = paymentApiCouponData.coupon_code;
let showText = '';
let priceText = '';
if (!validCouponCount && !paymentApiCouponData.coupon_count) {
showText = '无可用';
}
if (validCouponCount && paymentApiCouponData.coupon_count) {
showText = `可用${validCouponCount} 已推荐${paymentApiCouponData.coupon_count}张`;
priceText = `-${paymentApiCouponData.coupon_amount_str}`;
}
if (validCouponCount && !paymentApiCouponData.coupon_count) {
showText = `${validCouponCount}张可用,未使用`;
}
if (paymentApiCouponData.uid) {
showText = `已选${paymentApiCouponData.coupon_count}张`;
priceText = `-${paymentApiCouponData.coupon_amount_str}`;
}
return {
showText, priceText, couponCode
};
}
/**
* 选择礼品卡页面礼品卡数据处理
*/
function handleGiftCardsRender(giftCards, selectCards) {
... ... @@ -525,6 +558,7 @@ module.exports = {
tranformPayment,
yohoCoinCompute,
coupon,
handleCoupons,
handleGiftCards,
handleGiftCardsRender,
transformJit,
... ...