From 8f7496084a23c98f8b6b2b5b2f993935e2982b72 Mon Sep 17 00:00:00 2001 From: 郭成尧 <chengyao.guo@yoho.cn> Date: Wed, 11 Apr 2018 18:40:25 +0800 Subject: [PATCH] buy-now-coupons --- apps/cart/controllers/buy-now-controller.js | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------- apps/cart/controllers/order.js | 43 +------------------------------------------ apps/cart/models/buy-now-model.js | 9 ++++----- apps/cart/models/shopping.js | 2 +- apps/cart/router.js | 5 ++--- public/js/cart/buynow-select-coupon.page.js | 213 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- public/js/cart/buynow-select-coupons/index.js | 10 ++++++++++ public/js/cart/cartbuynow/select-coupon.js | 33 --------------------------------- public/js/cart/cartbuynow/select-coupons.js | 178 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public/js/cart/select-coupon.page.js | 203 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- public/js/cart/select-coupons/controller.js | 168 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ public/js/cart/select-coupons/index.js | 9 +++++++-- utils/payment-process.js | 82 +++++++++++++++++++++++++++++++++++++++++++--------------------------------------- 13 files changed, 308 insertions(+), 815 deletions(-) delete mode 100644 public/js/cart/buynow-select-coupon.page.js create mode 100644 public/js/cart/buynow-select-coupons/index.js delete mode 100644 public/js/cart/cartbuynow/select-coupon.js create mode 100644 public/js/cart/cartbuynow/select-coupons.js delete mode 100644 public/js/cart/select-coupon.page.js delete mode 100644 public/js/cart/select-coupons/controller.js diff --git a/apps/cart/controllers/buy-now-controller.js b/apps/cart/controllers/buy-now-controller.js index 2e99293..4fb3666 100644 --- a/apps/cart/controllers/buy-now-controller.js +++ b/apps/cart/controllers/buy-now-controller.js @@ -89,6 +89,7 @@ class BuyNowController { let mobile = _.get(userProfile, 'data.mobile', ''); let orderAddress = _.get(result, 'address', []); let addressList = _.get(address, 'data', []); + let autoSelectCouponCodeStr = _.get(result, 'coupon.couponCode', ''); let orderEnsure = {}; if (result.code !== 200 && result.message) { @@ -101,6 +102,11 @@ class BuyNowController { } }); + if (autoSelectCouponCodeStr) { + orderInfo.couponCode = autoSelectCouponCodeStr; + res.cookie('order-info', JSON.stringify(orderInfo), actCkOpthn); + } + // 兼容原有的数据格式 orderInfo.deliveryId = orderInfo.delivery_way; orderInfo.deliveryTimeId = orderInfo.delivery_time; @@ -111,11 +117,11 @@ class BuyNowController { orderEnsure = _.assign( paymentProcess.tranformPayment(_.get(result, 'data', {}), orderInfo, null, null, computeData.data), { - coupon: paymentProcess.coupon( - _.get(validCouponCount, 'data.count', 0), - orderInfo, - computeData.data - ), + coupon: paymentProcess.handleCoupons({ + paymentApiCouponData: _.get(result, 'data.coupon_pay', {}), + validCouponCount: _.get(validCouponCount, 'data.count', 0), + orderComputeCouponPay: _.get(computeData, 'coupon_pay') + }), selectAddressUrl: helpers.urlFormat('/cart/index/buynow/selectAddress', { product_sku: product_sku, buy_number: buy_number @@ -366,18 +372,66 @@ class BuyNowController { navBtn: false }); - res.render('select-coupon', { + res.render('select-coupons-page', { module: 'cart', - page: 'buynow-select-coupon', + page: 'buynow-select-coupons', title: '选择优惠券', selectCouponPage: true, pageHeader: headerData, pageFooter: false, - localCss: true + localCss: true, + width750: true }); } /** + * 获取用户可用和不可用优惠券列表 + */ + couponList(req, res, next) { + let uid = req.user.uid; + let delivery_way = req.body.delivery_way || 1; + let coupon_code = req.body.coupon_code || ''; + let product_sku = req.body.product_sku; + let buy_number = req.body.buy_number; + let sku_type = req.body.sku_type; + + co(function* () { + let couponApi = yield req.ctx(BuyNowModel).listCoupon({ + uid, + delivery_way, + coupon_code, + product_sku, + buy_number, + sku_type + }); + let result = paymentProcess.couponProcess(_.get(couponApi, 'data', {})); + + res.render('select-coupons', { + pageFooter: false, + result: result, + layout: false + }); + })().catch(next); + } + + /** + * 输入优惠券码使用优惠券 + */ + useCouponCode(req, res, next) { + co(function* () { + let result = yield req.ctx(BuyNowModel).usePromotionCode({ + uid: req.user.uid, + product_sku: req.body.product_sku, + sku_type: req.body.sku_type, + buy_number: req.body.buy_number, + promotion_code: req.body.promotion_code + }); + + res.json(result); + })().catch(next); + } + + /** * 选择礼品卡页面 */ selectGiftcard(req, res, next) { @@ -426,104 +480,6 @@ class BuyNowController { }); })().catch(next); } - - /** - * 获取用户可用和不可用优惠券列表 - * @param {*} req - * @param {*} res - * @param {*} next - */ - listCoupon(req, res, next) { - let orderInfo; - - try { - orderInfo = JSON.parse(req.cookies.buynow_info); - } catch (e) { - logger.info(`orderEnsure: get buynow-order-info from cookie error:${JSON.stringify(e)}`); - orderInfo = {}; - res.clearCookie('buynow_info', actCkOpthn); - } - - let delivery_way = orderInfo.deliveryId || 1; - - co(function* () { - let result = yield req.ctx(BuyNowModel).listCoupon({ - uid: req.user.uid, - product_sku: req.query.product_sku, - sku_type: req.query.sku_type, - buy_number: req.query.buy_number, - is_group_frees: 'Y', // 运费券是否分组 - delivery_way: delivery_way, - }); - - let finalResult = { - availableCoupons: [], - usableFreesCoupons: [], - notAvailableCoupons: [], - }; - - let usableCoupons = _.get(result, 'data.usable_coupons', []); - let freesCoupons = _.get(result, 'data.usable_frees_coupons', []); - let unusableCoupons = _.get(result, 'data.unusable_coupons', []); - - let orderInfoUsableUsualCode = _.get(orderInfo, 'usable_usual_code', ''); - let orderInfoUsableFreeCode = _.get(orderInfo, 'usable_free_code', ''); - - let procCouponsData = coupon => { - return { - couponCode: coupon.coupon_code, - couponDetailInfomation: coupon.coupon_name, - couponValue: coupon.coupon_value, - couponValidity: coupon.coupon_validity, - checked: coupon.coupon_code === orderInfoUsableUsualCode || - coupon.coupon_code === orderInfoUsableFreeCode - }; - }; - - finalResult.availableCoupons = usableCoupons.map(procCouponsData); // 可用优惠券 - finalResult.usableFreesCoupons = freesCoupons.map(procCouponsData); // 运费券 - finalResult.notAvailableCoupons = unusableCoupons.map(procCouponsData); // 不可用优惠券 - - res.json(finalResult); - })().catch(next); - } - - /** - * 使用优惠券 - * @param {*} req - * @param {*} res - * @param {*} next - */ - useCoupon(req, res, next) { - co(function* () { - let result = yield req.ctx(BuyNowModel).useCoupon({ - uid: req.user.uid, - product_sku: req.body.product_sku, - sku_type: req.body.sku_type, - buy_number: req.body.buy_number, - coupon_code: req.body.coupon_code - }); - - res.json(result); - })().catch(next); - } - - /** - * 输入优惠券码使用优惠券 - */ - usePromotionCode(req, res, next) { - co(function* () { - let result = yield req.ctx(BuyNowModel).usePromotionCode({ - uid: req.user.uid, - product_sku: req.body.product_sku, - sku_type: req.body.sku_type, - buy_number: req.body.buy_number, - promotion_code: req.body.promotion_code - }); - - res.json(result); - })().catch(next); - } } module.exports = new BuyNowController(); diff --git a/apps/cart/controllers/order.js b/apps/cart/controllers/order.js index 96ca669..3bc9728 100644 --- a/apps/cart/controllers/order.js +++ b/apps/cart/controllers/order.js @@ -12,47 +12,6 @@ const shoppingModel = require('../models/shopping'); const paymentProcess = require(global.utils + '/payment-process'); const logger = global.yoho.logger; -/** - * 优惠券数据处理 - */ -const _couponProcess = conponData => { - let usableCouponsList = _.get(conponData, 'usable_coupon_catalogs.list', []); - let unusableCouponsList = _.get(conponData, 'unusable_coupon_catalogs.list', []); - let usableCouponNum = _.get(usableCouponsList, '[0].coupons.length', 0); - let unusableCouponNum = 0; - let couponPay = _.get(usableCouponsList, '[0].coupon_pay'); - let headMsg = _.replace(_.get(conponData, 'usable_coupon_catalogs.head_msg', []), '\n', '<br>'); - let usableCouponNumStr = ''; - let unusableCouponNumStr = ''; - - _.forEach(unusableCouponsList, listItem => { - if (listItem.coupons) { - unusableCouponNum += listItem.coupons.length; - } - }); - - if (usableCouponNum > 99) { - usableCouponNumStr = '99+'; - } else { - usableCouponNumStr = usableCouponNum + ''; - } - - if (unusableCouponNum > 99) { - unusableCouponNumStr = '99+'; - } else { - unusableCouponNumStr = unusableCouponNum + ''; - } - - return { - usableCouponNumStr, - unusableCouponNumStr, - usableCouponsList, - unusableCouponsList, - couponPay, - headMsg - }; -}; - // cookie 参数 const actCkOpthn = { path: '/cart/index' @@ -405,7 +364,7 @@ exports.couponList = (req, res, next) => { delivery_way: delivery_way, coupon_code: coupon_code }); - let result = _couponProcess(_.get(couponApi, 'data', {})); + let result = paymentProcess.couponProcess(_.get(couponApi, 'data', {})); res.render('select-coupons', { pageFooter: false, diff --git a/apps/cart/models/buy-now-model.js b/apps/cart/models/buy-now-model.js index 5e55f77..b70999a 100644 --- a/apps/cart/models/buy-now-model.js +++ b/apps/cart/models/buy-now-model.js @@ -37,13 +37,12 @@ class BuyNowModel extends global.yoho.BaseModel { data: { method: 'app.Buynow.selectCoupon', uid: params.uid, + delivery_way: params.delivery_way, + coupon_code: params.coupon_code, product_sku: params.product_sku, - sku_type: params.sku_type || 'I', buy_number: params.buy_number, - is_group_frees: params.is_group_frees, - delivery_way: params.delivery_way - }, - param: {cache: false} + sku_type: params.sku_type || 'I', + } }); } diff --git a/apps/cart/models/shopping.js b/apps/cart/models/shopping.js index af2f4dc..631a546 100644 --- a/apps/cart/models/shopping.js +++ b/apps/cart/models/shopping.js @@ -254,7 +254,7 @@ class shoppingModel extends global.yoho.BaseModel { method: 'app.Shopping.selectCoupon', uid: params.uid, delivery_way: params.delivery_way, - coupon_code: params.coupon_code // 'ap6duto122i52i,mp6dutu122i52q' + coupon_code: params.coupon_code }; return this.get({data: param}); diff --git a/apps/cart/router.js b/apps/cart/router.js index 66e19f8..948c303 100644 --- a/apps/cart/router.js +++ b/apps/cart/router.js @@ -71,9 +71,8 @@ router.post('/index/buynow/ordersub', authMW, BuyNowController.orderSub); // 立 router.get('/index/buynow/selectAddress', authMW, BuyNowController.selectAddress); // 选择地址 router.get('/index/buynow/selectInvoice', authMW, BuyNowController.selectInvoice); // 发票信息 router.get('/index/buynow/selectCoupon', authMW, BuyNowController.selectCoupon); // 选择优惠券页面 -router.post('/index/buynow/useCoupon', BuyNowController.useCoupon); // [ajax]使用优惠券 -router.post('/index/buynow/usePromotionCode', BuyNowController.usePromotionCode); // [ajax]输入优惠券码使用优惠券 -router.get('/index/buynow/listCoupon', BuyNowController.listCoupon); // [ajax]获取优惠券列表 +router.post('/index/buynow/useCouponCode', BuyNowController.useCouponCode); // [ajax]输入优惠券码使用优惠券 +router.post('/index/buynow/couponList', BuyNowController.couponList); // [ajax]获取优惠券列表 router.get('/index/buynow/selectGiftcard', authMW, BuyNowController.selectGiftcard); // 选择礼品卡页面 diff --git a/public/js/cart/buynow-select-coupon.page.js b/public/js/cart/buynow-select-coupon.page.js deleted file mode 100644 index 6c4c01b..0000000 --- a/public/js/cart/buynow-select-coupon.page.js +++ /dev/null @@ -1,213 +0,0 @@ -/* - * @Author: Targaryen - * @Date: 2017-06-23 11:43:44 - * @Last Modified by: Targaryen - */ -require('cart/select-coupon.page.css'); -const qs = require('yoho-qs'); -let $ = require('yoho-jquery'), - loading = require('plugin/loading'), - tip = require('plugin/tip'), - conponTmpl = require('cart/select-coupon/coupon.hbs'), - conponNotAvaliableTmpl = require('cart/select-coupon/coupon-not-avaliable.hbs'), - orderInfo = require('cart/buynow/order-info').orderInfo; - -let isGetData; - -let $newCoupon = $('#new-coupon'); -let $couponAvailable = $('#couponAvailable'); -let $couponUnavailable = $('#couponUnavailable'); -let $useCoupon = $('#useCoupon'); -let $notUseCoupon = $('#notUseCoupon'); - -let winH = $(window).height(); - -require('common'); -require('cart/cartbuynow/select-coupon'); - -function fixedLayOut() { - let $null = $('.null'), - navH = $('.nav-title').height(), - nullH = $null.height(); - - if ($null.length === 0) { - return false; - } - - $null.css({ - top: winH / 2 - nullH / 2 + navH - }); -} - -function goToBack() { - history.go(-1); -} - -/** - * 输入优惠券码兑换优惠券 - */ -$newCoupon.on('submit', function() { - let $this = $(this); - let couponCode = $this.find('[name="couponCode"]').val(); - - if (!couponCode) { - tip.show('请输入优惠券码'); - return false; - } - $.ajax({ - method: 'POST', - url: '/cart/index/buynow/useCoupon', - data: { - coupon_code: couponCode, - buy_number: qs.buy_number, - product_sku: qs.product_sku - } - }).then(function(res) { - if (res.message) { - tip.show(res.message); - } - if (res.code === 200) { - orderInfo('usable_usual_code', res.data.coupon_code); - tip.show('优惠券可用'); - setTimeout(function() { - location.reload(); - }, 500); - } - }).fail(function() { - tip.show('网络错误'); - }); - return false; -}); - -/** - * 使用优惠券 - */ -$useCoupon.on('click', function() { - let couponCodeArray = []; - - if (orderInfo('usable_usual_code')) { - couponCodeArray.push(orderInfo('usable_usual_code')); - } - - if (orderInfo('usable_free_code')) { - couponCodeArray.push(orderInfo('usable_free_code')); - } - - orderInfo('coupon_code', couponCodeArray.join(',')); - if (couponCodeArray.length > 0) { - goToBack(); - } else { - tip.show('请选择优惠券'); - } -}); - -/** - * 不使用优惠券 - */ -$notUseCoupon.on('click', function() { - orderInfo('coupon_code', null); - orderInfo('usable_usual_code', null); - orderInfo('usable_free_code', null); - goToBack(); -}); - -/** - * 优惠券列表处理 - * @param {*} allCoupons - */ -function getCouponHandle(allCoupons) { - let availableCoupons = allCoupons.availableCoupons; - let usableFreesCoupons = allCoupons.usableFreesCoupons; - let notAvailableCoupons = allCoupons.notAvailableCoupons; - - $.each(availableCoupons, function(i, coupon) { - coupon.couponValue = Math.floor(coupon.couponValue); - }); - - $.each(usableFreesCoupons, function(i, coupon) { - coupon.couponValue = Math.floor(coupon.couponValue); - }); - - $.each(notAvailableCoupons, function(i, coupon) { - coupon.couponValue = Math.floor(coupon.couponValue); - }); - - $couponUnavailable.append(conponNotAvaliableTmpl({ - notAvailableCoupons: notAvailableCoupons - })); - - // 没有优惠券 - if (!(availableCoupons.length || usableFreesCoupons.length)) { - $('#couponAvailable').html($('#tmplNoCoupon').html()); - fixedLayOut(); - return false; - } - - // 渲染可用的优惠券 - $couponAvailable.append(conponTmpl({ - availableCoupons: availableCoupons, - usableFreesCoupons: usableFreesCoupons - })).find('.coupon').on('touchstart', function() { - let $theCoupon = $(this); - let $theCouponCheckBox = $theCoupon.find('.checkbox'); - - let couponCode = $theCoupon.data('code'); - - // 操作前的选中状态 - let beforeIsChecked = $theCouponCheckBox.hasClass('icon-cb-radio'); - - if ($theCoupon.hasClass('usable-usual')) { - if (beforeIsChecked) { - $theCouponCheckBox.removeClass('icon-cb-radio').addClass('icon-radio'); - - orderInfo('usable_usual_code', null); - } else { - $('.usable-usual').find('.checkbox').removeClass('icon-cb-radio').addClass('icon-radio'); - $theCouponCheckBox.removeClass('icon-radio').addClass('icon-cb-radio'); - - orderInfo('usable_usual_code', couponCode); - } - } else if ($theCoupon.hasClass('usable-free')) { - if (beforeIsChecked) { - $theCouponCheckBox.removeClass('icon-cb-radio').addClass('icon-radio'); - - orderInfo('usable_free_code', null); - } else { - $('.usable-free').find('.checkbox').removeClass('icon-cb-radio').addClass('icon-radio'); - $theCouponCheckBox.removeClass('icon-radio').addClass('icon-cb-radio'); - - orderInfo('usable_free_code', couponCode); - } - } - }); -} - -/** - * 获取优惠券列表 - */ -function getCouponData() { - - if (isGetData) { - return; - } - - loading.showLoadingMask(); - isGetData = true; - - $.ajax({ - type: 'GET', - url: '/cart/index/buynow/listCoupon', - data: { - product_sku: qs.product_sku, - buy_number: qs.buy_number - }, - dataType: 'json' - }).then(getCouponHandle).fail(function() { - tip.show('加载优惠券失败'); - }).always(function() { - isGetData = false; - loading.hideLoadingMask(); - }); -} - -getCouponData(); diff --git a/public/js/cart/buynow-select-coupons/index.js b/public/js/cart/buynow-select-coupons/index.js new file mode 100644 index 0000000..c601dc4 --- /dev/null +++ b/public/js/cart/buynow-select-coupons/index.js @@ -0,0 +1,10 @@ +import 'cart/select-coupons.page.css'; +import SelectCouponsController from '../cartbuynow/select-coupons'; +import { orderInfo } from 'cart/buynow/order-info'; + +new SelectCouponsController({ + orderInfo: orderInfo, + couponListUrl: '/cart/index/buynow/couponList', + useCouponCodeUrl: '/cart/index/buynow/useCouponCode', + isBuyNowPage: true +}); diff --git a/public/js/cart/cartbuynow/select-coupon.js b/public/js/cart/cartbuynow/select-coupon.js deleted file mode 100644 index cf1273b..0000000 --- a/public/js/cart/cartbuynow/select-coupon.js +++ /dev/null @@ -1,33 +0,0 @@ -/* - * @Author: Targaryen - * @Date: 2017-06-27 17:18:56 - * @Last Modified by: Targaryen - */ -const $couponCodeInput = $('input[name=couponCode]'); -const $submit = $('.submit'); -const $useCouponBtnGroup = $('.use-coupon-btn-group'); - -let Tab = require('plugin/tab'); - -Tab.prototype.couponavailable = () => { - $useCouponBtnGroup.removeClass('disable'); -}; - -Tab.prototype.couponunavailable = () => { - $useCouponBtnGroup.addClass('disable'); -}; - -$('.select-coupon-page').css('min-height', function() { - return $(window).height() - $('#yoho-header').height() + 20; -}); - -/** - * 优惠券码输入监听 - */ -$couponCodeInput.bind('input', () => { - if ($couponCodeInput.val()) { - $submit.addClass('active'); - } else { - $submit.removeClass('active'); - } -}); diff --git a/public/js/cart/cartbuynow/select-coupons.js b/public/js/cart/cartbuynow/select-coupons.js new file mode 100644 index 0000000..e44050e --- /dev/null +++ b/public/js/cart/cartbuynow/select-coupons.js @@ -0,0 +1,178 @@ +import $ from 'yoho-jquery'; +import qs from 'yoho-qs'; +import Page from 'yoho-page'; +import tip from 'plugin/tip'; + +class SelectCouponController extends Page { + constructor(params) { + super(); + + this.orderInfo = params.orderInfo; + this.couponListUrl = params.couponListUrl; + this.useCouponCodeUrl = params.useCouponCodeUrl; + this.selectCouponCodes = []; + this.isBuyNowPage = params.isBuyNowPage || false; + + if (this.orderInfo('couponCode')) { + this.selectCouponCodes = this.orderInfo('couponCode').split(','); + } + + this.page = $('.select-coupons-page'); + this.renderPage(); + } + + /** + * 渲染页面 + */ + renderPage() { + let couponCodeStr = this.selectCouponCodes.length > 0 ? this.selectCouponCodes.join(',') : ''; + let postData = { + delivery_way: this.orderInfo('deliveryId'), + coupon_code: couponCodeStr + }; + + if (this.isBuyNowPage) { + postData.product_sku = qs.product_sku; + postData.buy_number = qs.buy_number; + } + + this.ajax({ + type: 'POST', + url: this.couponListUrl, + data: postData + }).then(result => { + this.orderInfo('couponCode', couponCodeStr); + + let resultHtml = $(result); + + this.bindEvents(resultHtml); + this.page.html(resultHtml); + }); + } + + /** + * 事件绑定处理 + */ + bindEvents(resultHtml) { + this.view = { + filterBtn: resultHtml.find('.filter-btn'), + coupon: resultHtml.find('.coupon'), + checkbox: resultHtml.find('.checkbox'), + tipBox: resultHtml.find('.tip-box'), + tipClose: resultHtml.find('.tip-close'), + usableCouponList: resultHtml.find('.usable'), + unusableCouponList: resultHtml.find('.unusable'), + useNowBtn: resultHtml.find('#useNowBtn'), + exchangeCouponBtn: resultHtml.find('#exchangeCouponBtn'), + couponCodeInput: resultHtml.find('input[name=couponCodeInput]') + }; + + this.view.filterBtn.on('click', this.tabChange.bind(this)); + this.view.coupon.on('click', '.checkbox', this.check.bind(this)); + this.view.tipClose.on('click', this.closeTip.bind(this)); + this.view.useNowBtn.on('click', this.useCoupon.bind(this)); + this.view.exchangeCouponBtn.on('click', this.exchangeCoupon.bind(this)); + this.view.couponCodeInput.on('input', this.changeExchangeBtnStatus.bind(this)); + } + + /** + * 改变兑换按钮状态 + */ + changeExchangeBtnStatus() { + if (this.view.couponCodeInput.val().length > 0) { + this.view.exchangeCouponBtn.addClass('active'); + } else { + this.view.exchangeCouponBtn.removeClass('active'); + } + } + + /** + * 使用优惠券码 + */ + exchangeCoupon() { + let couponCode = this.view.couponCodeInput.val(); + + if (!couponCode) { + tip.show('请输入优惠券码'); + return false; + } + + this.ajax({ + method: 'POST', + url: this.useCouponCodeUrl, + data: { + couponCode: couponCode + } + }).then(function(res) { + if (res.message) { + tip.show(res.message); + } + if (res.code === 200) { + this.orderInfo('usable_usual_code', res.data.coupon_code); + tip.show('优惠券可用'); + setTimeout(function() { + location.reload(); + }, 500); + } + }); + } + + /** + * 使用优惠券 + */ + useCoupon() { + window.history.go(-1); + } + + /** + * tab 切换 + */ + tabChange(event) { + let itemClicked = $(event.currentTarget); + + if (itemClicked.hasClass('active')) { + itemClicked.removeClass('active'); + } else { + this.view.filterBtn.removeClass('active'); + itemClicked.addClass('active'); + } + + if (itemClicked.hasClass('valid')) { + this.view.unusableCouponList.addClass('hide'); + this.view.usableCouponList.removeClass('hide'); + } else { + this.view.usableCouponList.addClass('hide'); + this.view.unusableCouponList.removeClass('hide'); + } + } + + /** + * 优惠券勾选处理 + */ + check(e) { + let theCouponCheck = $(e.currentTarget); + let theCoupon = $(e.delegateTarget); + let theCouponData = theCoupon.data(); + + console.log(theCouponData.code); + + if (theCouponCheck.hasClass('icon-cb-radio')) { + this.selectCouponCodes.splice($.inArray(theCouponData.code, this.selectCouponCodes), 1); + theCouponCheck.removeClass('icon-cb-radio').addClass('icon-radio'); + } else { + this.selectCouponCodes.push(theCouponData.code); + theCouponCheck.removeClass('icon-radio').addClass('icon-cb-radio'); + } + + this.renderPage(); + } + + /** + * 关闭提示框 + */ + closeTip() { + this.view.tipBox.hide(); + } +} + +export default SelectCouponController; diff --git a/public/js/cart/select-coupon.page.js b/public/js/cart/select-coupon.page.js deleted file mode 100644 index dd3aa3e..0000000 --- a/public/js/cart/select-coupon.page.js +++ /dev/null @@ -1,203 +0,0 @@ -/** - * 优惠券选择 - * @author: bikai<kai.bi@yoho.cn> - * @date: 2015/12/10 - */ -require('cart/select-coupon.page.css'); -let $ = require('yoho-jquery'), - loading = require('plugin/loading'), - tip = require('plugin/tip'), - conponTmpl = require('cart/select-coupon/coupon.hbs'), - conponNotAvaliableTmpl = require('cart/select-coupon/coupon-not-avaliable.hbs'), - orderInfo = require('./order-info').orderInfo; - -let isGetData; - -let $newCoupon = $('#new-coupon'); -let $couponAvailable = $('#couponAvailable'); -let $couponUnavailable = $('#couponUnavailable'); -let $useCoupon = $('#useCoupon'); -let $notUseCoupon = $('#notUseCoupon'); - -let winH = $(window).height(); - -require('common'); -require('cart/cartbuynow/select-coupon'); - -function fixedLayOut() { - let $null = $('.null'), - navH = $('.nav-title').height(), - nullH = $null.height(); - - if ($null.length === 0) { - return false; - } - - $null.css({ - top: winH / 2 - nullH / 2 + navH - }); -} - -function goToBack() { - history.go(-1); -} - -/** - * 输入优惠券码兑换优惠券 - */ -$newCoupon.on('submit', function() { - let $this = $(this); - - if (!$this.find('[name="couponCode"]').val()) { - tip.show('请输入优惠券码'); - return false; - } - $.ajax({ - method: 'POST', - url: '/cart/index/new/useCouponCode', - data: $this.serialize() - }).then(function(res) { - if (res.message) { - tip.show(res.message); - } - if (res.code === 200) { - orderInfo('usable_usual_code', res.data.coupon_code); - tip.show('优惠券可用'); - setTimeout(function() { - location.reload(); - }, 500); - } - }).fail(function() { - tip.show('网络错误'); - }); - return false; -}); - -/** - * 使用优惠券 - */ -$useCoupon.on('click', function() { - let couponCodeArray = []; - - if (orderInfo('usable_usual_code')) { - couponCodeArray.push(orderInfo('usable_usual_code')); - } - - if (orderInfo('usable_free_code')) { - couponCodeArray.push(orderInfo('usable_free_code')); - } - - orderInfo('couponCode', couponCodeArray.join(',')); - if (couponCodeArray.length > 0) { - goToBack(); - } else { - tip.show('请选择优惠券'); - } -}); - -/** - * 不使用优惠券 - */ -$notUseCoupon.on('click', function() { - orderInfo('couponCode', null); - orderInfo('usable_usual_code', null); - orderInfo('usable_free_code', null); - goToBack(); -}); - -/** - * 优惠券列表处理 - * @param {*} allCoupons - */ -function getCouponHandle(allCoupons) { - let availableCoupons = allCoupons.availableCoupons; - let usableFreesCoupons = allCoupons.usableFreesCoupons; - let notAvailableCoupons = allCoupons.notAvailableCoupons; - - $.each(availableCoupons, function(i, coupon) { - coupon.couponValue = Math.floor(coupon.couponValue); - }); - - $.each(usableFreesCoupons, function(i, coupon) { - coupon.couponValue = Math.floor(coupon.couponValue); - }); - - $.each(notAvailableCoupons, function(i, coupon) { - coupon.couponValue = Math.floor(coupon.couponValue); - }); - - $couponUnavailable.append(conponNotAvaliableTmpl({ - notAvailableCoupons: notAvailableCoupons - })); - - // 没有优惠券 - if (!(availableCoupons.length || usableFreesCoupons.length)) { - $('#couponAvailable').html($('#tmplNoCoupon').html()); - fixedLayOut(); - return false; - } - - // 渲染可用的优惠券 - $couponAvailable.append(conponTmpl({ - availableCoupons: availableCoupons, - usableFreesCoupons: usableFreesCoupons - })).find('.coupon').on('touchstart', function() { - let $theCoupon = $(this); - let $theCouponCheckBox = $theCoupon.find('.checkbox'); - - let couponCode = $theCoupon.data('code'); - - // 操作前的选中状态 - let beforeIsChecked = $theCouponCheckBox.hasClass('icon-cb-radio'); - - if ($theCoupon.hasClass('usable-usual')) { - if (beforeIsChecked) { - $theCouponCheckBox.removeClass('icon-cb-radio').addClass('icon-radio'); - - orderInfo('usable_usual_code', null); - } else { - $('.usable-usual').find('.checkbox').removeClass('icon-cb-radio').addClass('icon-radio'); - $theCouponCheckBox.removeClass('icon-radio').addClass('icon-cb-radio'); - - orderInfo('usable_usual_code', couponCode); - } - } else if ($theCoupon.hasClass('usable-free')) { - if (beforeIsChecked) { - $theCouponCheckBox.removeClass('icon-cb-radio').addClass('icon-radio'); - - orderInfo('usable_free_code', null); - } else { - $('.usable-free').find('.checkbox').removeClass('icon-cb-radio').addClass('icon-radio'); - $theCouponCheckBox.removeClass('icon-radio').addClass('icon-cb-radio'); - - orderInfo('usable_free_code', couponCode); - } - } - }); -} - -/** - * 获取优惠券列表 - */ -function getCouponData() { - - if (isGetData) { - return; - } - - loading.showLoadingMask(); - isGetData = true; - - $.ajax({ - type: 'GET', - url: '/cart/index/new/couponList', - dataType: 'json' - }).then(getCouponHandle).fail(function() { - tip.show('加载优惠券失败'); - }).always(function() { - isGetData = false; - loading.hideLoadingMask(); - }); -} - -getCouponData(); diff --git a/public/js/cart/select-coupons/controller.js b/public/js/cart/select-coupons/controller.js deleted file mode 100644 index cb111c8..0000000 --- a/public/js/cart/select-coupons/controller.js +++ /dev/null @@ -1,168 +0,0 @@ -import $ from 'yoho-jquery'; -import Page from 'yoho-page'; -import tip from 'plugin/tip'; -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(); - } - - /** - * 渲染页面 - */ - renderPage() { - let couponCodeStr = this.selectCouponCodes.length > 0 ? this.selectCouponCodes.join(',') : ''; - - this.ajax({ - type: 'POST', - url: '/cart/index/new/couponList', - data: { - delivery_way: orderInfo('deliveryId'), - coupon_code: couponCodeStr - } - }).then(result => { - orderInfo('couponCode', couponCodeStr); - - let resultHtml = $(result); - - this.bindEvents(resultHtml); - this.page.html(resultHtml); - }); - } - - /** - * 事件绑定处理 - */ - bindEvents(resultHtml) { - this.view = { - filterBtn: resultHtml.find('.filter-btn'), - coupon: resultHtml.find('.coupon'), - checkbox: resultHtml.find('.checkbox'), - tipBox: resultHtml.find('.tip-box'), - tipClose: resultHtml.find('.tip-close'), - usableCouponList: resultHtml.find('.usable'), - unusableCouponList: resultHtml.find('.unusable'), - useNowBtn: resultHtml.find('#useNowBtn'), - exchangeCouponBtn: resultHtml.find('#exchangeCouponBtn'), - couponCodeInput: resultHtml.find('input[name=couponCodeInput]') - }; - - this.view.filterBtn.on('click', this.tabChange.bind(this)); - this.view.coupon.on('click', '.checkbox', this.check.bind(this)); - this.view.tipClose.on('click', this.closeTip.bind(this)); - this.view.useNowBtn.on('click', this.useCoupon.bind(this)); - this.view.exchangeCouponBtn.on('click', this.exchangeCoupon.bind(this)); - this.view.couponCodeInput.on('input', this.changeExchangeBtnStatus.bind(this)); - } - - /** - * 改变兑换按钮状态 - */ - changeExchangeBtnStatus() { - if (this.view.couponCodeInput.val().length > 0) { - this.view.exchangeCouponBtn.addClass('active'); - } else { - this.view.exchangeCouponBtn.removeClass('active'); - } - } - - /** - * 使用优惠券码 - */ - exchangeCoupon() { - let couponCode = this.view.couponCodeInput.val(); - - if (!couponCode) { - tip.show('请输入优惠券码'); - return false; - } - - this.ajax({ - method: 'POST', - url: '/cart/index/new/useCouponCode', - data: { - couponCode: couponCode - } - }).then(function(res) { - if (res.message) { - tip.show(res.message); - } - if (res.code === 200) { - orderInfo('usable_usual_code', res.data.coupon_code); - tip.show('优惠券可用'); - setTimeout(function() { - location.reload(); - }, 500); - } - }); - } - - /** - * 使用优惠券 - */ - useCoupon() { - window.history.go(-1); - } - - /** - * tab 切换 - */ - tabChange(event) { - let itemClicked = $(event.currentTarget); - - if (itemClicked.hasClass('active')) { - itemClicked.removeClass('active'); - } else { - this.view.filterBtn.removeClass('active'); - itemClicked.addClass('active'); - } - - if (itemClicked.hasClass('valid')) { - this.view.unusableCouponList.addClass('hide'); - this.view.usableCouponList.removeClass('hide'); - } else { - this.view.usableCouponList.addClass('hide'); - this.view.unusableCouponList.removeClass('hide'); - } - } - - /** - * 优惠券勾选处理 - */ - check(e) { - let theCouponCheck = $(e.currentTarget); - let theCoupon = $(e.delegateTarget); - let theCouponData = theCoupon.data(); - - console.log(theCouponData.code); - - if (theCouponCheck.hasClass('icon-cb-radio')) { - this.selectCouponCodes.splice($.inArray(theCouponData.code, this.selectCouponCodes), 1); - theCouponCheck.removeClass('icon-cb-radio').addClass('icon-radio'); - } else { - this.selectCouponCodes.push(theCouponData.code); - theCouponCheck.removeClass('icon-radio').addClass('icon-cb-radio'); - } - - this.renderPage(); - } - - /** - * 关闭提示框 - */ - closeTip() { - this.view.tipBox.hide(); - } -} - -export default SelectCouponController; diff --git a/public/js/cart/select-coupons/index.js b/public/js/cart/select-coupons/index.js index 430e5d1..f8297a8 100644 --- a/public/js/cart/select-coupons/index.js +++ b/public/js/cart/select-coupons/index.js @@ -1,4 +1,9 @@ import 'cart/select-coupons.page.css'; -import SelectCouponsController from './controller'; +import SelectCouponsController from '../cartbuynow/select-coupons'; +import { orderInfo } from 'cart/order-info'; -new SelectCouponsController(); +new SelectCouponsController({ + orderInfo: orderInfo, + couponListUrl: '/cart/index/new/couponList', + useCouponCodeUrl: '/cart/index/new/useCouponCode' +}); diff --git a/utils/payment-process.js b/utils/payment-process.js index c84883c..d7302ad 100644 --- a/utils/payment-process.js +++ b/utils/payment-process.js @@ -338,44 +338,7 @@ function tranformPayment(data, orderInfo, cartType, skuList, orderComputeData) { } /** - * 优惠券数据 - * @param count int 用户有效优惠券 - * @param orderComputeData obj app.Shopping.compute返回的data数据 - * @param orderInfo cookie中的数据 - * @return obj - */ -function coupon(count, orderInfo, orderComputeData) { - let coupons = { - count: count, - selectedAmount: 0, - info: '' - }; - - let couponAmount = _.get(orderComputeData, 'coupon_amount', 0); - - if (_.get(orderInfo, 'usable_usual_code', null)) { - coupons.selectedAmount++; - } - - if (_.get(orderInfo, 'usable_free_code', null)) { - coupons.selectedAmount++; - } - - if (count) { - if (coupons.selectedAmount) { - coupons.info = `-¥${couponAmount.toFixed(2)}`; - } else { - coupons.info = '未使用'; - } - } else { - coupons.info = '无可用'; - } - - return coupons; -} - -/** - * 处理优惠券数据 + * 下单页面处理优惠券数据 */ function handleCoupons(params) { let orderComputeCouponPay = _.get(params, 'orderComputeCouponPay', {}); @@ -413,6 +376,47 @@ function handleCoupons(params) { } /** + * 选择优惠券页面数据处理 + */ +const couponProcess = conponData => { + let usableCouponsList = _.get(conponData, 'usable_coupon_catalogs.list', []); + let unusableCouponsList = _.get(conponData, 'unusable_coupon_catalogs.list', []); + let usableCouponNum = _.get(usableCouponsList, '[0].coupons.length', 0); + let unusableCouponNum = 0; + let couponPay = _.get(usableCouponsList, '[0].coupon_pay'); + let headMsg = _.replace(_.get(conponData, 'usable_coupon_catalogs.head_msg', []), '\n', '<br>'); + let usableCouponNumStr = ''; + let unusableCouponNumStr = ''; + + _.forEach(unusableCouponsList, listItem => { + if (listItem.coupons) { + unusableCouponNum += listItem.coupons.length; + } + }); + + if (usableCouponNum > 99) { + usableCouponNumStr = '99+'; + } else { + usableCouponNumStr = usableCouponNum + ''; + } + + if (unusableCouponNum > 99) { + unusableCouponNumStr = '99+'; + } else { + unusableCouponNumStr = unusableCouponNum + ''; + } + + return { + usableCouponNumStr, + unusableCouponNumStr, + usableCouponsList, + unusableCouponsList, + couponPay, + headMsg + }; +}; + +/** * 选择礼品卡页面礼品卡数据处理 */ function handleGiftCardsRender(giftCards, selectCards) { @@ -562,8 +566,8 @@ module.exports = { transPrice, tranformPayment, yohoCoinCompute, - coupon, handleCoupons, + couponProcess, handleGiftCards, handleGiftCardsRender, transformJit, -- libgit2 0.24.0