Authored by 郝肖肖

有货币使用改造

... ... @@ -77,7 +77,7 @@ const index = (req, res, next) => {
// 订单金额计算
const compute = (req, res, next) => {
orderModel.compute(req.user.uid, req.query.coin).then(result => {
orderModel.compute(req.user.uid, req.body.coin).then(result => {
res.send(result);
}).catch(next);
};
... ...
... ... @@ -23,6 +23,28 @@ const _orderApi = uid => api.get('', {
});
/**
* 计算有货币可用数量
* @param uid [int] user id
*/
const _yohoCoinPayRule = data => {
if (!data.total_yoho_coin_num) {
return data;
}
data.yoho_coin_num = _.round(data.yoho_coin * 100);
data.yoho_coin = data.yoho_coin.toFixed(2);
data.yoho_msg = '';
if (data.total_yoho_coin_num < 100) {
data.yoho_msg = `抱歉,您的有货币不足,有货币满${data.yoho_coin_pay_rule.num_limit}个方可使用`;
} else if (data.yoho_coin_num <= 0) {
data.yoho_msg = '抱歉,您的订单实付款不满足有货币使用条件';
}
return data;
};
/**
* 结算页面
* @param uid [int] user id
*/
... ... @@ -54,7 +76,8 @@ const index = uid => {
}
});
theData.yoho_coin = _.round(theData.yoho_coin * 100); // 有货币稀释(round浮点精度问题)
//计算有货币使用
Object.assign(theData, _yohoCoinPayRule(theData));
shopping.balanceCoin = theData.use_yoho_coin;
... ... @@ -115,7 +138,14 @@ const compute = (uid, yohoCoin) => {
coin = yohoCoin / 100;
}
return _computeApi(uid, deliveryWay, paymentType, coin).then(result => result);
return _computeApi(uid, deliveryWay, paymentType, coin).then(result => {
if (result.code === 200) {
Object.assign(result.data, _yohoCoinPayRule(result.data));
}
return result;
});
};
/**
... ...
... ... @@ -27,7 +27,7 @@ router.get('/cart/data', cartCtrl.getCartData);
// 结算
router.get('/order', auth, order.index);
router.get('/order/compute', auth, order.compute);
router.post('/order/compute', auth, order.compute);
router.post('/order/submit', auth, order.orderSub);
// 支付
... ...
... ... @@ -269,10 +269,6 @@
</div>
</script>
{{!-- 有货币异步请求渲染 --}}
<script id="yoho-coin-tpl" type="text/html">
{{> order/yoho-coin}}
</script>
</div>
{{/ content}}
</div>
... ...
<p id="coin-tip" class="blue"></p>
<p id="coin-tip" class="blue">{{yoho_msg}}</p>
<div>
有货币满<em class="blue">100</em>个即可使用,每次使用有货币为<em class="blue">100</em>的整数倍
有货币满<em class="blue">{{yoho_coin_pay_rule.num_limit}}</em>个即可使用,每次使用有货币为<em class="blue">{{yoho_coin_pay_rule.num_limit}}</em>的整数倍
<span class="yoho-coin-help">?
<span class="coin-help-dialog hide">
<span class="arrow-left"></span>
<div class="coin-dialog-title">有货币使用条件:</div>
<div class="coin-dialog-content">
<p>(1)订单金额大于20元(含20元)</p>
<p>(2)有货币数量大于100个(含100个)</p>
<p>(3)每笔支付不得超过应付金额的50%</p>
<p>备注:使用有货币数量为100的整数倍,<br />100个有货币抵1元。</p>
<p>(2)有货币数量大于{{yoho_coin_pay_rule.num_limit}}个(含{{yoho_coin_pay_rule.num_limit}}个)</p>
<p>(3)每笔支付不得超过应付金额的{{yoho_coin_pay_rule.max_pay_rate_desc}}</p>
<p>备注:使用有货币数量为{{yoho_coin_pay_rule.num_limit}}的整数倍,<br />100个有货币抵1元。</p>
</div>
</span>
</span>
</div>
<div>您当前共有有货币<em class="blue">53650</em>个,可用<em class="blue">1800</em></div>
<div>本次使用有货币<em class="blue">1800</em>个 抵扣¥18.00</div>
<div>您当前共有有货币<em class="blue">{{total_yoho_coin_num}}</em>个,可用<em class="blue">{{yoho_coin_num}}</em></div>
<div>本次使用有货币<em class="blue">{{yoho_coin_num}}</em>个 抵扣<em class="blue">¥{{yoho_coin}}</em></div>
<div>
<span id="coin-sure" class="btn disable">确定</span>
<span id="coin-sure" class="btn {{#unless yoho_coin_num}}disable{{/unless}}" data-yoho_coin_num="{{yoho_coin_num}}">确定</span>
<span class="btn white cancel">取消</span>
</div>
\ No newline at end of file
... ...
... ... @@ -14,26 +14,14 @@ var minusPlus = {
plus: '&#xe639;'
};
var $coin = $('#input-coin'),
$coinTip = $('#coin-tip'),
$coinSure = $('#coin-sure'),
$coinUsed = $('#coin-used'),
$coinDeduction = $('#coin-deduction'),
coinMax = +$coin.data('max'),
coinStatus = {
err: '请输入一个正整数',
max: '您的有货币不足',
success: '抵扣¥',
maxUse: '您最多使用',
muPostfix: '个有货币'
};
var $balanceCost = $('#balance-cost');
var $printPrice = $('#print-price');
var $invoice = $('#invoice-content');
var balanceTpl = require('../../tpl/shopping/balance.hbs');
var $coinRemarkContent = $('.coin-and-remark .content');
var balanceTpl = require('../../tpl/shopping/balance.hbs'),
yohoCoinTpl = require('../../tpl/shopping/yoho-coin.hbs');
var pkgCache = {};
... ... @@ -140,57 +128,9 @@ $('.coin-ctrl, .remark-ctrl').click(function() {
$this.parent('.title').siblings('.content').toggleClass('hide');
});
// 使用有货币输入框联动
$coin.on('propertychange input', function() {
var c = $.trim($coin.val());
var err = true;
if (c === '') {
// 输入框为空,确定按钮不可点
$coinTip.addClass('vhide');
$coinSure.addClass('disable');
return;
} else if (!/^[1-9]\d*$/.test(c)) {
// 验证输入不为正整数
$coinTip.text(coinStatus.err);
} else if (+c > coinMax) {
// 有货币不足
$coinTip.text(coinStatus.max);
} else if (+c > $balanceCost.data('cost') * 100) {
// 输入的有货币大于订单额度
$coinTip.text(coinStatus.maxUse + ($balanceCost.data('cost') * 100) + coinStatus.muPostfix);
} else {
err = false;
$coinTip.text(coinStatus.success + (c / 100).toFixed(2));
// 已使用面板的数据显示更新
$coinUsed.html(c);
$coinDeduction.html((c / 100).toFixed(2));
}
if (err) {
$coinSure.addClass('disable');
$coinTip.removeClass('vhide');
} else {
$coinSure.removeClass('disable');
$coinTip.addClass('vhide');
}
});
/**
* 切换使用有货币面板显示
*/
function toggleCoinPanel() {
$('.using-coin, .used-coin').toggleClass('hide');
}
function compute(coin) {
$.ajax({
type: 'GET',
type: 'POST',
url: '/shopping/order/compute',
data: {
coin: coin
... ... @@ -204,46 +144,18 @@ function compute(coin) {
cost = cost.toFixed(2);
$('#balance-list').html(balanceTpl(data.data));
$coinRemarkContent.html(yohoCoinTpl(data.data));
}
});
}
// 获取已使用的有货币
function getCoinUsed() {
var coin = $coinUsed.text();
var coin = $('.coin-and-remark').find('#coin-sure').data('yoho_coin_num') || 0;
return coin === '' ? 0 : parseInt(coin, 10); // 使用parseInt可以排除异常情况的转化影响
}
$coinSure.click(function() {
if ($coinSure.hasClass('disable')) {
return;
}
suredCoin = getCoinUsed();
// 切换显示
compute(suredCoin);
toggleCoinPanel();
});
$('.used-coin').on('click', '.modify', function() {
toggleCoinPanel();
}).on('click', '.cancel', function() {
$coin.val('').trigger('input');
// reset used coin panel show
$coinUsed.html('0');
$coinDeduction.html('0');
compute(0);
// 隐藏有货币面板并重置子面板显示
$('.coin-ctrl.iconfont').trigger('click');
toggleCoinPanel();
});
// 添加备注-是否打印价格
$printPrice.printPrice = 'N'; // 默认为false
... ... @@ -293,10 +205,32 @@ $('#balance-list').on('click', '#submit-order', function() {
});
});
$('.coin-and-remark').on('mouseover mouseout', '.yoho-coin-help', function(event) {
$coinRemarkContent.on('mouseover mouseout', '.yoho-coin-help', function(event) {
if (event.type === 'mouseover') {
$('.coin-help-dialog').removeClass('hide');
} else if (event.type === 'mouseout') {
$('.coin-help-dialog').addClass('hide');
}
}).on('click', '#coin-sure', function() {//确定有货币按钮
if ($(this).hasClass('disable')) {
return;
}
suredCoin = getCoinUsed();
// 切换显示
compute(suredCoin);
// 隐藏有货币面板并重置子面板显示
$('.coin-ctrl.iconfont').trigger('click');
}).on('click', '.cancel', function() {//取消有货币按钮
suredCoin = 0;
compute(suredCoin);
// 隐藏有货币面板并重置子面板显示
$('.coin-ctrl.iconfont').trigger('click');
});
... ...