Files

250 lines
6.5 KiB
PHP

<?php
namespace app\api\controller\order;
use app\common\controller\Api;
use think\Db;
use app\common\model\order\Order as OrderModal;
/**
* 订单
*/
class Order extends Api
{
protected $noNeedRight = '*';
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = new OrderModal();
}
/**
* 创建订单
* @return void
*/
public function createOrder(){
$params = [
'goods_list'=>$this->request->post('goodsList/a'),
'order_type'=>$this->request->post('order_type','goods'),
'coupon_id'=>$this->request->post('coupon_id',''),
'buyer_remark'=>$this->request->post('buyer_remark',''),
'live_id'=>$this->request->post('live_id',''),
'live_goods_action_id'=>$this->request->post('live_goods_action_id',''),
'address_id'=>$this->request->post('address_id',''),
];
try{
$order = new \app\common\library\order\OrderCreate($params);
$result = $order->calc('create');
$order = $order->createOrder($result);
if($order['real_price'] == 0){
//直接支付成功;
\app\common\model\order\Order::paySuccess($order,[
'order_no'=>$order['order_no'],
'transaction_id'=>$order['order_no'],
'payment_json'=>$order['order_no'],
'real_pay_price'=>$order['real_price']
]);
$order['status'] = \app\common\constant\order\Status::STATUS_SUCCESS;
}
}catch(\think\Exception $e){
$this->error($e->getMessage());
}
$this->success('获取成功', $order);
}
/**
* 核算订单
* @return void
*/
public function calculate(){
$goodsList = $this->request->post('goodsList/a');
$orderType = $this->request->post('order_type','goods');
$couponId = $this->request->post('coupon_id','');
$liveId = $this->request->post('live_id','');
$liveGoodsActionId = $this->request->post('live_goods_action_id','');
$addressId = $this->request->post('address_id','');
if(!$goodsList){
return $this->error("商品参数无效");
}
$params = [
'goods_list'=>$goodsList,
'order_type'=>$orderType,
'coupon_id'=>$couponId,
'live_id'=>$liveId,
'live_goods_action_id'=>$liveGoodsActionId,
'address_id'=>$addressId
];
try{
$order = new \app\common\library\order\OrderCreate($params);
}catch(\think\Exception $e){
$this->error($e->getMessage());
}
$data = $order->calc();
$this->success('获取成功', $data);
}
/**
* 获取订单详情
* @return void
*/
public function getDetail(){
$orderNo = $this->request->post('order_no','');
if(!$this->model->checkOrderBindUser($orderNo,$this->auth->id)){
$this->error('获取订单信息失败');
}
$detail = $this->model->getOrderDetail($orderNo);
if(!$detail){
$this->error('未获取到相关订单信息');
}
$this->success('获取成功', $detail);
}
/**
* 获取订单列表
* @return void
*/
public function getOrderList(){
$limit = input('limit',10);
$page = input('page',1);
$status = input('status','');
$search = input('search','');
$searchField = input('search_field','order_no');
$list = (new \app\api\model\order\Order())->getList($this->auth->id,[
'limit'=>$limit,
'page'=>$page,
'status'=>$status,
'search'=>$search,
'search_field'=>$searchField
]);
$this->success('获取成功', $list);
}
/**
* 取消订单
* @return void
*/
public function cancelOrder(){
$orderNo = $this->request->post('order_no','');
if(!$this->model->checkOrderBindUser($orderNo,$this->auth->id)){
$this->error('获取订单信息失败');
}
$detail = $this->model->getOrderDetail($orderNo);
if(!$detail ){
$this->error('获取订单信息失败');
}
if($detail['status'] != \app\common\constant\order\Status::STATUS_UNPAID){
$this->error('当前状态不可取消');
}
$this->model->cancelOrder($orderNo);
$this->success('操作成功');
}
/**
* 删除订单
* @return void
*/
public function deleteOrder(){
$orderNo = $this->request->post('order_no','');
if(!$this->model->checkOrderBindUser($orderNo,$this->auth->id)){
$this->error('获取订单信息失败');
}
$detail = $this->model->getOrderDetail($orderNo);
if(!$detail){
$this->error('获取订单信息失败');
}
if($detail['status'] == \app\common\constant\order\Status::STATUS_SERVICE){
$this->error('暂不支持删除售后中的订单');
}
$this->model->where([
'order_no'=>$detail['order_no']
])->update([
'deleted'=>1
]);
\app\common\model\order\Log::set($orderNo,"订单已被删除");
$this->success('操作成功');
}
/**
* 确认收货
* @return void
*/
public function confirmReceive()
{
$orderNo = $this->request->post('order_no', '');
if (!$this->model->checkOrderBindUser($orderNo, $this->auth->id)) {
$this->error('获取订单信息失败');
}
$detail = $this->model->getOrderDetail($orderNo);
if (!$detail) {
$this->error('获取订单信息失败');
}
$allowedStatus = [
\app\common\constant\order\Status::STATUS_UNRECEIVE,
\app\common\constant\order\Status::STATUS_UNSEND,
\app\common\constant\order\Status::STATUS_PAID
];
if (!in_array($detail['status'], $allowedStatus)) {
$this->error('当前状态不可确认收货');
}
$this->model->where([
'order_no' => $detail['order_no']
])->update([
'status' => \app\common\constant\order\Status::STATUS_SUCCESS
]);
\app\common\model\order\Log::set($orderNo, "用户确认收货");
$this->success('操作成功');
}
}