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 be4bf08 commit 7f9a7c2
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
25 changes: 25 additions & 0 deletions app/Http/Controllers/OrdersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Events\OrderReviewed;
use App\Exceptions\InvalidRequestException;
use App\Http\Requests\ApplyRefundRequest;
use App\Http\Requests\OrderRequest;
use App\Http\Requests\SendReviewRequest;
use App\Models\UserAddress;
Expand Down Expand Up @@ -100,4 +101,28 @@ public function sendReview(Order $order, SendReviewRequest $request)

return redirect()->back();
}

public function applyRefund(Order $order, ApplyRefundRequest $request)
{
// 校验订单是否属于当前用户
$this->authorize('own', $order);
// 判断订单是否已付款
if (!$order->paid_at) {
throw new InvalidRequestException('该订单未支付,不可退款');
}
// 判断订单退款状态是否正确
if ($order->refund_status !== Order::REFUND_STATUS_PENDING) {
throw new InvalidRequestException('该订单已经申请过退款,请勿重复申请');
}
// 将用户输入的退款理由放到订单的 extra 字段中
$extra = $order->extra ?: [];
$extra['refund_reason'] = $request->input('reason');
// 将订单退款状态改为已申请退款
$order->update([
'refund_status' => Order::REFUND_STATUS_APPLIED,
'extra' => $extra,
]);

return $order;
}
}
20 changes: 20 additions & 0 deletions app/Http/Requests/ApplyRefundRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Http\Requests;

class ApplyRefundRequest extends Request
{
public function rules()
{
return [
'reason' => 'required',
];
}

public function attributes()
{
return [
'reason' => '原因',
];
}
}
2 changes: 1 addition & 1 deletion resources/sass/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ body {
padding-right: 20px;
}
}
.payment-buttons, .receive-button {
.payment-buttons, .receive-button, .refund-button {
margin-top: 10px;
padding-right: 10px;
}
Expand Down
38 changes: 38 additions & 0 deletions resources/views/orders/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@
<div class="line-value">{{ $order->ship_data['express_company'] }} {{ $order->ship_data['express_no'] }}</div>
</div>
@endif
<!-- 订单已支付,且退款状态不是未退款时展示退款信息 -->
@if($order->paid_at && $order->refund_status !== \App\Models\Order::REFUND_STATUS_PENDING)
<div class="line">
<div class="line-label">退款状态:</div>
<div class="line-value">{{ \App\Models\Order::$refundStatusMap[$order->refund_status] }}</div>
</div>
<div class="line">
<div class="line-label">退款理由:</div>
<div class="line-value">{{ $order->extra['refund_reason'] }}</div>
</div>
@endif
</div>
<div class="order-summary text-right">
<div class="total-amount">
Expand Down Expand Up @@ -94,6 +105,12 @@
<button type="button" id="btn-receive" class="btn btn-sm btn-success">确认收货</button>
</div>
@endif
<!-- 订单已支付,且退款状态是未退款时展示申请退款按钮 -->
@if($order->paid_at && $order->refund_status === \App\Models\Order::REFUND_STATUS_PENDING)
<div class="refund-button">
<button class="btn btn-sm btn-danger" id="btn-apply-refund">申请退款</button>
</div>
@endif
</div>
</div>
</div>
Expand Down Expand Up @@ -142,6 +159,27 @@
})
});
});
// 退款按钮点击事件
$('#btn-apply-refund').click(function () {
swal({
text: '请输入退款理由',
content: "input",
}).then(function (input) {
// 当用户点击 swal 弹出框上的按钮时触发这个函数
if(!input) {
swal('退款理由不可空', '', 'error');
return;
}
// 请求退款接口
axios.post('{{ route('orders.apply_refund', [$order->id]) }}', {reason: input})
.then(function () {
swal('申请退款成功', '', 'success').then(function () {
// 用户点击弹框上按钮时重新加载页面
location.reload();
});
});
});
});
});
</script>
Expand Down
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
Route::post('orders/{order}/received', 'OrdersController@received')->name('orders.received');
Route::get('orders/{order}/review', 'OrdersController@review')->name('orders.review.show');
Route::post('orders/{order}/review', 'OrdersController@sendReview')->name('orders.review.store');
Route::post('orders/{order}/apply_refund', 'OrdersController@applyRefund')->name('orders.apply_refund');
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');
Expand Down

0 comments on commit 7f9a7c2

Please sign in to comment.