Authored by 郭成尧

Merge branch 'feature/coupon' into release/180420

@@ -89,7 +89,8 @@ class BuyNowController { @@ -89,7 +89,8 @@ class BuyNowController {
89 uid: uid, 89 uid: uid,
90 product_sku: req.query.product_sku, 90 product_sku: req.query.product_sku,
91 sku_type: req.query.sku_type, 91 sku_type: req.query.sku_type,
92 - buy_number: buy_number 92 + buy_number: buy_number,
  93 + delivery_way: orderInfo.delivery_way
93 }), 94 }),
94 req.ctx(shoppingModel).countUsableGiftCard(uid) // 可用礼品卡数量 95 req.ctx(shoppingModel).countUsableGiftCard(uid) // 可用礼品卡数量
95 ]); 96 ]);
@@ -131,7 +132,8 @@ class BuyNowController { @@ -131,7 +132,8 @@ class BuyNowController {
131 coupon: paymentProcess.handleCoupons({ 132 coupon: paymentProcess.handleCoupons({
132 paymentApiCouponData: _.get(result, 'data.coupon_pay', {}), 133 paymentApiCouponData: _.get(result, 'data.coupon_pay', {}),
133 validCouponCount: _.get(validCouponCount, 'data.count', 0), 134 validCouponCount: _.get(validCouponCount, 'data.count', 0),
134 - orderComputeCouponPay: _.get(computeData, 'data.coupon_pay') 135 + orderComputeCouponPay: _.get(computeData, 'data.coupon_pay'),
  136 + userCheckCoupon: orderInfo.user_check_coupon
135 }), 137 }),
136 selectAddressUrl: helpers.urlFormat('/cart/index/buynow/selectAddress', { 138 selectAddressUrl: helpers.urlFormat('/cart/index/buynow/selectAddress', {
137 product_sku: product_sku, 139 product_sku: product_sku,
@@ -176,26 +178,51 @@ class BuyNowController { @@ -176,26 +178,51 @@ class BuyNowController {
176 * @param {*} next 178 * @param {*} next
177 */ 179 */
178 orderCompute(req, res, next) { 180 orderCompute(req, res, next) {
179 - co(function * () {  
180 - let result = yield req.ctx(BuyNowModel).compute({  
181 - uid: req.user.uid,  
182 - cart_type: req.body.cart_type,  
183 - delivery_way: req.body.delivery_way,  
184 - payment_type: req.body.payment_type,  
185 - product_sku: req.body.product_sku,  
186 - buy_number: req.body.buy_number,  
187 - coupon_code: req.body.coupon_code,  
188 - gift_card_code: req.body.gift_card_code,  
189 - use_yoho_coin: req.body.use_yoho_coin  
190 - }); 181 + let orderInfo;
  182 +
  183 + try {
  184 + orderInfo = JSON.parse(req.cookies.buynow_info);
  185 + } catch (e) {
  186 + logger.info(`orderEnsure: get buynow-order-info from cookie error:${JSON.stringify(e)}`);
  187 + orderInfo = {};
  188 + res.clearCookie('buynow_info', actCkOpthn);
  189 + }
  190 +
  191 + co(function* () {
  192 + let [result, validCouponCount] = yield Promise.all([
  193 + req.ctx(BuyNowModel).compute({
  194 + uid: req.user.uid,
  195 + cart_type: req.body.cart_type,
  196 + delivery_way: req.body.delivery_way,
  197 + payment_type: req.body.payment_type,
  198 + product_sku: req.body.product_sku,
  199 + buy_number: req.body.buy_number,
  200 + coupon_code: req.body.coupon_code,
  201 + gift_card_code: req.body.gift_card_code,
  202 + use_yoho_coin: req.body.use_yoho_coin
  203 + }),
  204 + req.ctx(BuyNowModel).countUsableCoupon({
  205 + uid: req.user.uid,
  206 + product_sku: req.body.product_sku,
  207 + sku_type: req.body.sku_type,
  208 + buy_number: req.body.buy_number,
  209 + delivery_way: req.body.delivery_way
  210 + })
  211 + ]);
191 212
192 let finalResult = _.get(result, 'data', {}); 213 let finalResult = _.get(result, 'data', {});
193 214
194 if (finalResult) { 215 if (finalResult) {
195 _.set(finalResult, 'use_yoho_coin', paymentProcess.transPrice(_.get(result, 'data.use_yoho_coin'))); 216 _.set(finalResult, 'use_yoho_coin', paymentProcess.transPrice(_.get(result, 'data.use_yoho_coin')));
196 _.set(finalResult, 'yohoCoinCompute', paymentProcess.yohoCoinCompute(result.data)); 217 _.set(finalResult, 'yohoCoinCompute', paymentProcess.yohoCoinCompute(result.data));
  218 + _.set(finalResult, 'coupon', paymentProcess.handleCoupons({
  219 + paymentApiCouponData: {},
  220 + validCouponCount: _.get(validCouponCount, 'data.count', 0),
  221 + orderComputeCouponPay: _.get(finalResult, 'coupon_pay'),
  222 + userCheckCoupon: orderInfo.user_check_coupon
  223 + }));
197 } 224 }
198 - return res.json(result.data); 225 + return res.json(finalResult);
199 })().catch(next); 226 })().catch(next);
200 } 227 }
201 228
@@ -163,6 +163,15 @@ exports.orderCompute = (req, res, next) => { @@ -163,6 +163,15 @@ exports.orderCompute = (req, res, next) => {
163 let uid = req.user.uid; 163 let uid = req.user.uid;
164 let skuList = req.body.skuList; 164 let skuList = req.body.skuList;
165 let type = req.body.type; 165 let type = req.body.type;
  166 + let orderInfo;
  167 +
  168 + try {
  169 + orderInfo = JSON.parse(req.cookies['order-info']);
  170 + } catch (e) {
  171 + logger.info(`orderEnsure: get orderInfo from cookie error:${JSON.stringify(e)}`);
  172 + orderInfo = {};
  173 + res.clearCookie('order-info', actCkOpthn);
  174 + }
166 175
167 if (type !== 'tickets') { 176 if (type !== 'tickets') {
168 let params = { 177 let params = {
@@ -185,7 +194,8 @@ exports.orderCompute = (req, res, next) => { @@ -185,7 +194,8 @@ exports.orderCompute = (req, res, next) => {
185 }).catch(next); 194 }).catch(next);
186 } else { 195 } else {
187 req.ctx(cartModel).orderCompute(_.assign(params, { 196 req.ctx(cartModel).orderCompute(_.assign(params, {
188 - product_sku_list: skuList 197 + product_sku_list: skuList,
  198 + userCheckCoupon: orderInfo.user_check_coupon
189 })).then(result => { 199 })).then(result => {
190 res.json(result); 200 res.json(result);
191 }).catch(next); 201 }).catch(next);
@@ -20,7 +20,8 @@ class BuyNowModel extends global.yoho.BaseModel { @@ -20,7 +20,8 @@ class BuyNowModel extends global.yoho.BaseModel {
20 uid: params.uid, 20 uid: params.uid,
21 product_sku: params.product_sku, 21 product_sku: params.product_sku,
22 sku_type: params.sku_type || 'I', 22 sku_type: params.sku_type || 'I',
23 - buy_number: params.buy_number 23 + buy_number: params.buy_number,
  24 + delivery_way: params.delivery_way
24 }, 25 },
25 param: { 26 param: {
26 cache: false 27 cache: false
@@ -114,7 +114,8 @@ class cartModel extends global.yoho.BaseModel { @@ -114,7 +114,8 @@ class cartModel extends global.yoho.BaseModel {
114 coupon: paymentProcess.handleCoupons({ 114 coupon: paymentProcess.handleCoupons({
115 paymentApiCouponData, 115 paymentApiCouponData,
116 validCouponCount: validCouponCount, 116 validCouponCount: validCouponCount,
117 - orderComputeCouponPay: orderCompute.coupon_pay 117 + orderComputeCouponPay: orderCompute.coupon_pay,
  118 + userCheckCoupon: orderInfoCookie.user_check_coupon
118 }), 119 }),
119 giftCards: paymentProcess.handleGiftCards({ 120 giftCards: paymentProcess.handleGiftCards({
120 validGiftCardCount: validGiftCardCount, 121 validGiftCardCount: validGiftCardCount,
@@ -130,11 +131,26 @@ class cartModel extends global.yoho.BaseModel { @@ -130,11 +131,26 @@ class cartModel extends global.yoho.BaseModel {
130 * 购物车结算--支付方式和配送方式选择以及是否使用有货币接口返回的数据处理 131 * 购物车结算--支付方式和配送方式选择以及是否使用有货币接口返回的数据处理
131 */ 132 */
132 orderCompute(params) { 133 orderCompute(params) {
133 - return this.ctx.req.ctx(shoppingModel).orderComputeAPI(params).then(result => {  
134 - if (result && result.data) {  
135 - result.data.use_yoho_coin = paymentProcess.transPrice(result.data.use_yoho_coin);  
136 - result.data.yohoCoinCompute = paymentProcess.yohoCoinCompute(result.data);  
137 - return result.data; 134 + return Promise.all([
  135 + this.ctx.req.ctx(shoppingModel).orderComputeAPI(params),
  136 + this.ctx.req.ctx(shoppingModel).getValidCouponCount({
  137 + uid: params.uid,
  138 + delivery_way: params.delivery_way
  139 + })
  140 + ]).then(result => {
  141 + let computerResult = result[0];
  142 + let validCouponCount = _.get(result[1], 'data.count', 0);
  143 +
  144 + if (computerResult && computerResult.data) {
  145 + computerResult.data.use_yoho_coin = paymentProcess.transPrice(computerResult.data.use_yoho_coin);
  146 + computerResult.data.yohoCoinCompute = paymentProcess.yohoCoinCompute(computerResult.data);
  147 + computerResult.data.coupon = paymentProcess.handleCoupons({
  148 + paymentApiCouponData: {},
  149 + validCouponCount: validCouponCount,
  150 + orderComputeCouponPay: _.get(computerResult, 'data.coupon_pay'),
  151 + userCheckCoupon: params.user_check_coupon
  152 + });
  153 + return computerResult.data;
138 } else { 154 } else {
139 return {}; 155 return {};
140 } 156 }
1 {{#result}} 1 {{#result}}
2 <section> 2 <section>
3 <div class="filter-box"> 3 <div class="filter-box">
4 - <span class="filter-btn valid active" data-num="{{usableCouponNumStr}}">可用({{usableCouponNumStr}})</span>  
5 - <span class="filter-btn invalid" data-num="{{unusableCouponNumStr}}">不可用({{unusableCouponNumStr}})</span> 4 + <span class="filter-btn valid active" data-num="{{usableCouponNumStr}}">可用{{#notEqualEither usableCouponNumStr '0'}}({{usableCouponNumStr}}){{/notEqualEither}}</span>
  5 + <span class="filter-btn invalid" data-num="{{unusableCouponNumStr}}">不可用{{#notEqualEither unusableCouponNumStr '0'}}({{unusableCouponNumStr}}){{/notEqualEither}}</span>
6 </div> 6 </div>
7 <div class="exchange-box"> 7 <div class="exchange-box">
8 <input type="text" name="couponCodeInput" placeholder="请输入优惠券码"> 8 <input type="text" name="couponCodeInput" placeholder="请输入优惠券码">
@@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
10 {{showText}} 10 {{showText}}
11 </span> 11 </span>
12 <span class="coupon-info pull-right{{#isEqualOr info '无可用'}} no-can-use{{/isEqualOr}}"> 12 <span class="coupon-info pull-right{{#isEqualOr info '无可用'}} no-can-use{{/isEqualOr}}">
13 - {{priceText}} 13 + <span class="coupon-price-info">{{priceText}}</span>
14 <i class="iconfont">&#xe614;</i> 14 <i class="iconfont">&#xe614;</i>
15 </span> 15 </span>
16 {{/if}} 16 {{/if}}
@@ -35,9 +35,9 @@ class CouponNew { @@ -35,9 +35,9 @@ class CouponNew {
35 let pageData = _.get(couponsApi, 'data', {}); 35 let pageData = _.get(couponsApi, 'data', {});
36 let couponNum = _.get(couponNumApi, 'data', null); 36 let couponNum = _.get(couponNumApi, 'data', null);
37 let couponNumStr = { 37 let couponNumStr = {
38 - notuse: 0,  
39 - use: 0,  
40 - overtime: 0 38 + notuse: '0',
  39 + use: '0',
  40 + overtime: '0'
41 }; 41 };
42 let couponList = _.get(pageData, 'couponList', []); 42 let couponList = _.get(pageData, 'couponList', []);
43 43
@@ -2,14 +2,14 @@ @@ -2,14 +2,14 @@
2 {{#couponNumStr}} 2 {{#couponNumStr}}
3 <div class="filter-box"> 3 <div class="filter-box">
4 <span class="filter-btn-box"> 4 <span class="filter-btn-box">
5 - <span class="filter-btn no-used active" data-num="{{notuse}}">未使用({{notuse}})</span> 5 + <span class="filter-btn no-used active" data-num="{{notuse}}">未使用{{#notEqualEither notuse '0'}}({{notuse}}){{/notEqualEither}}</span>
6 <span class="iconfont icon-down show-filter-btn active"></span> 6 <span class="iconfont icon-down show-filter-btn active"></span>
7 </span> 7 </span>
8 <span class="filter-btn-box"> 8 <span class="filter-btn-box">
9 - <span class="filter-btn used" data-num="{{use}}">已使用{{use}}</span> 9 + <span class="filter-btn used" data-num="{{use}}">已使用{{#notEqualEither use '0'}}{{use}}{{/notEqualEither}}</span>
10 </span> 10 </span>
11 <span class="filter-btn-box"> 11 <span class="filter-btn-box">
12 - <span class="filter-btn invalid" data-num="{{overtime}}">已失效{{overtime}}</span> 12 + <span class="filter-btn invalid" data-num="{{overtime}}">已失效{{#notEqualEither overtime '0'}}{{overtime}}{{/notEqualEither}}</span>
13 </span> 13 </span>
14 </div> 14 </div>
15 {{/ couponNumStr}} 15 {{/ couponNumStr}}
@@ -131,6 +131,8 @@ function orderCompute() { @@ -131,6 +131,8 @@ function orderCompute() {
131 $('.price-cost span').html('¥' + res.last_order_amount); 131 $('.price-cost span').html('¥' + res.last_order_amount);
132 $('.bill span').html('¥' + res.last_order_amount); 132 $('.bill span').html('¥' + res.last_order_amount);
133 $('.total').html(total); 133 $('.total').html(total);
  134 + $('.coupon .count').html(res.coupon.showText);
  135 + $('.coupon .coupon-price-info').html(res.coupon.priceText);
134 } 136 }
135 137
136 updateDeliverWay(deliver_way); 138 updateDeliverWay(deliver_way);
@@ -173,6 +173,8 @@ function orderCompute() { @@ -173,6 +173,8 @@ function orderCompute() {
173 $('.price-cost span').html('¥' + res.last_order_amount); 173 $('.price-cost span').html('¥' + res.last_order_amount);
174 $('.bill span').html('¥' + res.last_order_amount); 174 $('.bill span').html('¥' + res.last_order_amount);
175 $('.total').html(total); 175 $('.total').html(total);
  176 + $('.coupon .count').html(res.coupon.showText);
  177 + $('.coupon .coupon-price-info').html(res.coupon.priceText);
176 } 178 }
177 179
178 updateDeliverId(deliverId); 180 updateDeliverId(deliverId);
@@ -180,6 +180,27 @@ module.exports = { @@ -180,6 +180,27 @@ module.exports = {
180 return opt.inverse(this); // eslint-disable-line 180 return opt.inverse(this); // eslint-disable-line
181 } 181 }
182 }, 182 },
  183 +
  184 + notEqualEither: function() {
  185 + let args = Array.prototype.slice.call(arguments);
  186 + let v1 = args[0];
  187 + let opt = args[args.length - 1];
  188 + let isTrue = false;
  189 +
  190 + for (let i = 1; i < args.length - 1; i++) {
  191 + if (v1 === args[i]) {
  192 + isTrue = true;
  193 + break;
  194 + }
  195 + }
  196 +
  197 + if (!isTrue) {
  198 + return opt.fn(this); // eslint-disable-line
  199 + } else {
  200 + return opt.inverse(this); // eslint-disable-line
  201 + }
  202 + },
  203 +
183 ifand: function() { 204 ifand: function() {
184 let args = Array.prototype.slice.call(arguments); 205 let args = Array.prototype.slice.call(arguments);
185 let opt = args[args.length - 1]; 206 let opt = args[args.length - 1];
@@ -366,7 +366,9 @@ function handleCoupons(params) { @@ -366,7 +366,9 @@ function handleCoupons(params) {
366 showText = `${validCouponCount}张可用`; 366 showText = `${validCouponCount}张可用`;
367 } 367 }
368 368
369 - if (!_.isEmpty(orderComputeCouponPay) && couponData.coupon_count) { 369 + if (!_.isEmpty(orderComputeCouponPay) &&
  370 + couponData.coupon_count &&
  371 + params.userCheckCoupon === 'Y') {
370 showText = `已选${couponData.coupon_count}张`; 372 showText = `已选${couponData.coupon_count}张`;
371 priceText = `-${couponData.coupon_amount_str}`; 373 priceText = `-${couponData.coupon_amount_str}`;
372 } 374 }