257 lines
7.1 KiB
PHP
257 lines
7.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\adminapi\controller\order;
|
|
|
|
use app\adminapi\controller\BaseAdminController;
|
|
use app\adminapi\lists\order\OrderLists;
|
|
use app\adminapi\logic\order\OrderLogic;
|
|
use app\adminapi\validate\order\OrderValidate;
|
|
|
|
/**
|
|
* 订单管理控制器
|
|
* Class OrderController
|
|
* @package app\adminapi\controller\order
|
|
*/
|
|
class OrderController extends BaseAdminController
|
|
{
|
|
/**
|
|
* @notes 订单列表
|
|
* @return \think\response\Json
|
|
*/
|
|
public function lists()
|
|
{
|
|
return $this->dataLists(new OrderLists());
|
|
}
|
|
|
|
/**
|
|
* @notes 订单详情
|
|
* @return \think\response\Json
|
|
*/
|
|
public function detail()
|
|
{
|
|
$params = (new OrderValidate())->post()->goCheck('detail');
|
|
$detail = OrderLogic::detail((int)$params['id']);
|
|
|
|
if (!$detail) {
|
|
return $this->fail('订单不存在');
|
|
}
|
|
|
|
return $this->data($detail);
|
|
}
|
|
|
|
/**
|
|
* @notes 创建订单
|
|
* @return \think\response\Json
|
|
*/
|
|
public function create()
|
|
{
|
|
$params = (new OrderValidate())->post()->goCheck('create');
|
|
$params['creator_id'] = $this->adminId;
|
|
|
|
$result = OrderLogic::create($params);
|
|
if (!$result) {
|
|
return $this->fail(OrderLogic::getError());
|
|
}
|
|
|
|
return $this->success('创建成功', $result->toArray());
|
|
}
|
|
|
|
/**
|
|
* @notes 编辑订单
|
|
* @return \think\response\Json
|
|
*/
|
|
public function edit()
|
|
{
|
|
$params = (new OrderValidate())->post()->goCheck('edit');
|
|
|
|
$result = OrderLogic::edit((int)$params['id'], $params);
|
|
if (!$result) {
|
|
return $this->fail(OrderLogic::getError());
|
|
}
|
|
|
|
return $this->success('编辑成功');
|
|
}
|
|
|
|
/**
|
|
* @notes 支付订单
|
|
* @return \think\response\Json
|
|
*/
|
|
public function pay()
|
|
{
|
|
$params = (new OrderValidate())->post()->goCheck('pay');
|
|
|
|
// 判断是否是补单支付
|
|
if (!empty($params['is_supplement']) && $params['is_supplement'] == 1) {
|
|
// 补单支付:通过订单号查找订单
|
|
if (empty($params['order_no'])) {
|
|
return $this->fail('补单支付需要提供订单号');
|
|
}
|
|
$order = \app\common\model\Order::where('order_no', $params['order_no'])->find();
|
|
if (!$order) {
|
|
return $this->fail('订单不存在');
|
|
}
|
|
$orderId = $order->id;
|
|
} else {
|
|
// 正常支付:通过订单ID
|
|
if (empty($params['id'])) {
|
|
return $this->fail('订单ID不能为空');
|
|
}
|
|
$orderId = (int)$params['id'];
|
|
}
|
|
|
|
$result = OrderLogic::pay($orderId, $params['payment_method']);
|
|
if (!$result) {
|
|
return $this->fail(OrderLogic::getError());
|
|
}
|
|
|
|
return $this->success('支付成功');
|
|
}
|
|
|
|
/**
|
|
* @notes 取消订单
|
|
* @return \think\response\Json
|
|
*/
|
|
public function cancel()
|
|
{
|
|
$params = (new OrderValidate())->post()->goCheck('cancel');
|
|
|
|
$result = OrderLogic::cancel((int)$params['id']);
|
|
if (!$result) {
|
|
return $this->fail(OrderLogic::getError());
|
|
}
|
|
|
|
return $this->success('取消成功');
|
|
}
|
|
|
|
/**
|
|
* @notes 退款订单
|
|
* @return \think\response\Json
|
|
*/
|
|
public function refund()
|
|
{
|
|
$params = (new OrderValidate())->post()->goCheck('refund');
|
|
|
|
$result = OrderLogic::refund((int)$params['id']);
|
|
if (!$result) {
|
|
return $this->fail(OrderLogic::getError());
|
|
}
|
|
|
|
return $this->success('退款成功');
|
|
}
|
|
|
|
/**
|
|
* @notes 删除订单
|
|
* @return \think\response\Json
|
|
*/
|
|
public function delete()
|
|
{
|
|
$params = (new OrderValidate())->post()->goCheck('delete');
|
|
|
|
$result = OrderLogic::delete((int)$params['id']);
|
|
if (!$result) {
|
|
return $this->fail(OrderLogic::getError());
|
|
}
|
|
|
|
return $this->success('删除成功');
|
|
}
|
|
|
|
/**
|
|
* @notes 导出订单
|
|
* @return \think\response\Json
|
|
*/
|
|
public function export()
|
|
{
|
|
return $this->dataLists(new OrderLists());
|
|
}
|
|
|
|
/**
|
|
* @notes 支付宝支付
|
|
* @return \think\response\Json
|
|
*/
|
|
public function alipay()
|
|
{
|
|
$params = (new OrderValidate())->post()->goCheck('pay');
|
|
|
|
// 补单支付不需要校验订单是否存在
|
|
if (!empty($params['is_supplement']) && $params['is_supplement'] == 1) {
|
|
// 补单支付:直接返回支付URL,不校验订单
|
|
$payUrl = OrderLogic::alipayPay(['order_no' => $params['order_no']]);
|
|
if (!$payUrl) {
|
|
return $this->fail(OrderLogic::getError());
|
|
}
|
|
return $this->data(['pay_url' => $payUrl]);
|
|
}
|
|
|
|
// 正常支付需要校验订单
|
|
$order = $this->getOrderByParams($params);
|
|
if (!$order) {
|
|
return $this->fail('订单不存在');
|
|
}
|
|
|
|
// 调用支付宝支付接口
|
|
$payUrl = OrderLogic::alipayPay($order);
|
|
if (!$payUrl) {
|
|
return $this->fail(OrderLogic::getError());
|
|
}
|
|
|
|
return $this->data(['pay_url' => $payUrl]);
|
|
}
|
|
|
|
/**
|
|
* @notes 微信支付
|
|
* @return \think\response\Json
|
|
*/
|
|
public function wechat()
|
|
{
|
|
$params = (new OrderValidate())->post()->goCheck('pay');
|
|
|
|
// 补单支付不需要校验订单是否存在
|
|
if (!empty($params['is_supplement']) && $params['is_supplement'] == 1) {
|
|
// 补单支付:直接返回支付数据,不校验订单
|
|
$payData = OrderLogic::wechatPay(['order_no' => $params['order_no']]);
|
|
if (!$payData) {
|
|
return $this->fail(OrderLogic::getError());
|
|
}
|
|
return $this->data($payData);
|
|
}
|
|
|
|
// 正常支付需要校验订单
|
|
$order = $this->getOrderByParams($params);
|
|
if (!$order) {
|
|
return $this->fail('订单不存在');
|
|
}
|
|
|
|
// 调用微信支付接口
|
|
$payData = OrderLogic::wechatPay($order);
|
|
if (!$payData) {
|
|
return $this->fail(OrderLogic::getError());
|
|
}
|
|
|
|
return $this->data($payData);
|
|
}
|
|
|
|
/**
|
|
* @notes 根据参数获取订单
|
|
* @param array $params
|
|
* @return mixed
|
|
*/
|
|
private function getOrderByParams(array $params)
|
|
{
|
|
if (!empty($params['is_supplement']) && $params['is_supplement'] == 1) {
|
|
// 补单支付:通过订单号查找
|
|
if (empty($params['order_no'])) {
|
|
return null;
|
|
}
|
|
return \app\common\model\Order::where('order_no', $params['order_no'])->find();
|
|
} else {
|
|
// 正常支付:通过订单ID
|
|
if (empty($params['id'])) {
|
|
return null;
|
|
}
|
|
return \app\common\model\Order::find((int)$params['id']);
|
|
}
|
|
}
|
|
}
|