Skip to content

Commit

Permalink
用户界面 - 检查优惠券
Browse files Browse the repository at this point in the history
  • Loading branch information
leo108 committed Dec 23, 2018
1 parent 8f7af32 commit 7dc64dc
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
36 changes: 36 additions & 0 deletions app/Http/Controllers/CouponCodesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Http\Controllers;

use App\Models\CouponCode;
use Carbon\Carbon;

class CouponCodesController extends Controller
{
public function show($code)
{
// 判断优惠券是否存在
if (!$record = CouponCode::where('code', $code)->first()) {
abort(404);
}

// 如果优惠券没有启用,则等同于优惠券不存在
if (!$record->enabled) {
abort(404);
}

if ($record->total - $record->used <= 0) {
return response()->json(['msg' => '该优惠券已被兑完'], 403);
}

if ($record->not_before && $record->not_before->gt(Carbon::now())) {
return response()->json(['msg' => '该优惠券现在还不能使用'], 403);
}

if ($record->not_after && $record->not_after->lt(Carbon::now())) {
return response()->json(['msg' => '该优惠券已过期'], 403);
}

return $record;
}
}
51 changes: 51 additions & 0 deletions resources/views/cart/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@
<textarea name="remark" class="form-control" rows="3"></textarea>
</div>
</div>
<!-- 优惠码开始 -->
<div class="form-group row">
<label class="col-form-label col-sm-3 text-md-right">优惠码</label>
<div class="col-sm-4">
<input type="text" class="form-control" name="coupon_code">
<span class="form-text text-muted" id="coupon_desc"></span>
</div>
<div class="col-sm-3">
<button type="button" class="btn btn-success" id="btn-check-coupon">检查</button>
<button type="button" class="btn btn-danger" style="display: none;" id="btn-cancel-coupon">取消</button>
</div>
</div>
<!-- 优惠码结束 -->
<div class="form-group">
<div class="offset-sm-3 col-sm-3">
<button type="button" class="btn btn-primary btn-create-order">提交订单</button>
Expand Down Expand Up @@ -174,6 +187,44 @@
});
});
// 检查按钮点击事件
$('#btn-check-coupon').click(function () {
// 获取用户输入的优惠码
var code = $('input[name=coupon_code]').val();
// 如果没有输入则弹框提示
if(!code) {
swal('请输入优惠码', '', 'warning');
return;
}
// 调用检查接口
axios.get('/coupon_codes/' + encodeURIComponent(code))
.then(function (response) { // then 方法的第一个参数是回调,请求成功时会被调用
$('#coupon_desc').text(response.data.description); // 输出优惠信息
$('input[name=coupon_code]').prop('readonly', true); // 禁用输入框
$('#btn-cancel-coupon').show(); // 显示 取消 按钮
$('#btn-check-coupon').hide(); // 隐藏 检查 按钮
}, function (error) {
// 如果返回码是 404,说明优惠券不存在
if(error.response.status === 404) {
swal('优惠码不存在', '', 'error');
} else if (error.response.status === 403) {
// 如果返回码是 403,说明有其他条件不满足
swal(error.response.data.msg, '', 'error');
} else {
// 其他错误
swal('系统内部错误', '', 'error');
}
})
});
// 隐藏 按钮点击事件
$('#btn-cancel-coupon').click(function () {
$('#coupon_desc').text(''); // 隐藏优惠信息
$('input[name=coupon_code]').prop('readonly', false); // 启用输入框
$('#btn-cancel-coupon').hide(); // 隐藏 取消 按钮
$('#btn-check-coupon').show(); // 显示 检查 按钮
});
});
</script>
@endsection
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
Route::get('payment/{order}/alipay', 'PaymentController@payByAlipay')->name('payment.alipay');
Route::get('payment/alipay/return', 'PaymentController@alipayReturn')->name('payment.alipay.return');
Route::get('payment/{order}/wechat', 'PaymentController@payByWechat')->name('payment.wechat');

Route::get('coupon_codes/{code}', 'CouponCodesController@show')->name('coupon_codes.show');
});
Route::get('products/{product}', 'ProductsController@show')->name('products.show');
Route::post('payment/alipay/notify', 'PaymentController@alipayNotify')->name('payment.alipay.notify');
Expand Down

0 comments on commit 7dc64dc

Please sign in to comment.