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 3adfb3b commit a4c2255
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
32 changes: 32 additions & 0 deletions app/Admin/Controllers/OrdersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace App\Admin\Controllers;

use App\Exceptions\InvalidRequestException;
use App\Models\Order;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;

class OrdersController extends Controller
{
Expand Down Expand Up @@ -61,4 +63,34 @@ protected function grid()

return $grid;
}

public function ship(Order $order, Request $request)
{
// 判断当前订单是否已支付
if (!$order->paid_at) {
throw new InvalidRequestException('该订单未付款');
}
// 判断当前订单发货状态是否为未发货
if ($order->ship_status !== Order::SHIP_STATUS_PENDING) {
throw new InvalidRequestException('该订单已发货');
}
// Laravel 5.5 之后 validate 方法可以返回校验过的值
$data = $this->validate($request, [
'express_company' => ['required'],
'express_no' => ['required'],
], [], [
'express_company' => '物流公司',
'express_no' => '物流单号',
]);
// 将订单发货状态改为已发货,并存入物流信息
$order->update([
'ship_status' => Order::SHIP_STATUS_DELIVERED,
// 我们在 Order 模型的 $casts 属性里指明了 ship_data 是一个数组
// 因此这里可以直接把数组传过去
'ship_data' => $data,
]);

// 返回上一页
return redirect()->back();
}
}
1 change: 1 addition & 0 deletions app/Admin/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@

$router->get('orders', 'OrdersController@index')->name('admin.orders.index');
$router->get('orders/{order}', 'OrdersController@show')->name('admin.orders.show');
$router->post('orders/{order}/ship', 'OrdersController@ship')->name('admin.orders.ship');
});
44 changes: 43 additions & 1 deletion resources/views/admin/orders/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,50 @@
@endforeach
<tr>
<td>订单金额:</td>
<td colspan="3">¥{{ $order->total_amount }}</td>
<td>¥{{ $order->total_amount }}</td>
<td>发货状态:</td>
<td>{{ \App\Models\Order::$shipStatusMap[$order->ship_status] }}</td>
</tr>
<!-- 订单发货开始 -->
<!-- 如果订单未发货,展示发货表单 -->
@if($order->ship_status === \App\Models\Order::SHIP_STATUS_PENDING)
<tr>
<td colspan="4">
<form action="{{ route('admin.orders.ship', [$order->id]) }}" method="post" class="form-inline">
<!-- 别忘了 csrf token 字段 -->
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group {{ $errors->has('express_company') ? 'has-error' : '' }}">
<label for="express_company" class="control-label">物流公司</label>
<input type="text" id="express_company" name="express_company" value="" class="form-control" placeholder="输入物流公司">
@if($errors->has('express_company'))
@foreach($errors->get('express_company') as $msg)
<span class="help-block">{{ $msg }}</span>
@endforeach
@endif
</div>
<div class="form-group {{ $errors->has('express_no') ? 'has-error' : '' }}">
<label for="express_no" class="control-label">物流单号</label>
<input type="text" id="express_no" name="express_no" value="" class="form-control" placeholder="输入物流单号">
@if($errors->has('express_no'))
@foreach($errors->get('express_no') as $msg)
<span class="help-block">{{ $msg }}</span>
@endforeach
@endif
</div>
<button type="submit" class="btn btn-success" id="ship-btn">发货</button>
</form>
</td>
</tr>
@else
<!-- 否则展示物流公司和物流单号 -->
<tr>
<td>物流公司:</td>
<td>{{ $order->ship_data['express_company'] }}</td>
<td>物流单号:</td>
<td>{{ $order->ship_data['express_no'] }}</td>
</tr>
@endif
<!-- 订单发货结束 -->
</tbody>
</table>
</div>
Expand Down

0 comments on commit a4c2255

Please sign in to comment.