You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/api/controller/Server.php

393 lines
13 KiB

10 months ago
<?php
/**
* 服务控制器
*/
namespace app\api\controller;
use app\api\model\Server\ServerCategory;
use app\api\model\Server\ServerOrder;
10 months ago
use app\api\service\Server\ServerPayment;
8 months ago
use app\api\service\User as UserService;
10 months ago
use app\common\enum\ServerEnum;
10 months ago
use app\common\service\server\Order as ServerServiceOrder;
10 months ago
use cores\exception\BaseException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;
class Server extends Controller
{
/**
* @notes:分类列表
* @return Json
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author: wanghousheng
*/
public function categoryList(): Json
{
$model = new ServerCategory();
$list = $model->list();
if (!empty($list)) {
foreach ($list as $key => $value) {
unset($list[$key]['image']);
}
}
return $this->renderSuccess(compact('list'));
}
/**
* @notes:服务列表页
* @throws DbException
* @author: wanghousheng
*/
public function list(): Json
{
$server_name = $this->request->post('server_name');
$category_id = intval($this->request->post('category_id'));
10 months ago
$order_field = (string)$this->request->post('order_field');
$order_sort = (string)$this->request->post('order_sort', 'desc');
10 months ago
$where = [];
if ($server_name) {
$where[] = ['server.server_name', 'like', "%$server_name%"];
}
if ($category_id) {
$where[] = ['server.category_id', '=', $category_id];
}
10 months ago
$where[] = ['server.status', '=', 1];
$model = new \app\api\model\Server();
10 months ago
$list = $model->getList($where, 15, $order_field, $order_sort);
10 months ago
$data['list'] = $list->items();
$data['total'] = $list->total();
if (!$list->isEmpty()) {
foreach ($data['list'] as $key => $value) {
unset($data['list'][$key]['image']);
unset($data['list'][$key]['category']);
}
}
return $this->renderSuccess($data);
}
/**
* @notes:订单状态
* @return Json
* @author: wanghousheng
*/
public function orderStatus(): Json
{
$list = array_values(ServerEnum::data());
return $this->renderSuccess(compact('list'));
}
/**
* @notes:用户订单列表
* @throws DbException
* @throws BaseException
* @author: wanghousheng
*/
10 months ago
public function orderList(): Json
10 months ago
{
$order_no = $this->request->post('order_no');
$order_status = intval($this->request->post('order_status'));
$where = [];
if (!empty($order_no)) {
10 months ago
$where[] = ['order_no', '=', $order_no];
10 months ago
}
if ($order_status) {
10 months ago
$where[] = ['order_status', '=', $order_status];
}
$pay_method = $this->request->post('pay_method');
if (!empty($pay_method)) {
$where[] = ['pay_method', '=', $pay_method];
}
$start_time = $this->request->post('start_time');
if (!empty($start_time) && strtotime($start_time)) {
$start_time = strtotime($start_time);
$where[] = ['create_time', '>=', strtotime($start_time)];
}
$end_time = $this->request->post('end_time');
if (!empty($end_time) && strtotime($end_time)) {
$end_time = strtotime($end_time) + 86400;
$where[] = ['create_time', '<', $end_time];
10 months ago
}
10 months ago
$model = new ServerOrder();
10 months ago
$list = $model->orderList($where);
10 months ago
$data['list'] = $list->items();
$data['total'] = $list->total();
if (!$list->isEmpty()) {
10 months ago
foreach ($data['list'] as $key => $value) {
$data['list'][$key]['is_cancel'] = ServerServiceOrder::checkCancel($value);
$data['list'][$key]['is_dispatch'] = ServerServiceOrder::checkDispatch($value);
$data['list'][$key]['is_pay'] = ServerServiceOrder::checkPay($value);
10 months ago
$data['list'][$key]['is_success'] = ServerServiceOrder::checkSuccess($value);
10 months ago
unset($data['list'][$key]['image']);
if (!empty($data['list'][$key]['dealer'])) {
10 months ago
unset($data['list'][$key]['dealer']);
}
10 months ago
if (!empty($data['list'][$key]['user'])) {
unset($data['list'][$key]['user']);
}
10 months ago
}
}
return $this->renderSuccess($data);
}
10 months ago
/**
* @notes:订单详情
* @return Json
* @throws BaseException
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author: wanghousheng
*/
public function orderDetails(): Json
{
$orderId = intval($this->request->post('order_id'));
if (!$orderId) {
return $this->renderError('非法请求');
}
$model = new ServerOrder();
$info = $model->info(['order_id' => $orderId]);
if (!empty($info)) {
$info['is_cancel'] = ServerServiceOrder::checkCancel($info);
$info['is_dispatch'] = ServerServiceOrder::checkDispatch($info);
$info['is_pay'] = ServerServiceOrder::checkPay($info);
$info['is_success'] = ServerServiceOrder::checkSuccess($info);
}
return $this->renderSuccess(['info' => $info]);
}
/**
* @notes:确认完成
* @return Json
* @throws BaseException
* @author: wanghousheng
*/
public function confirmSuccess(): Json
{
$orderId = intval($this->request->post('order_id'));
if (!$orderId) {
return $this->renderError('非法请求');
}
$model = new ServerOrder();
if ($model->confirmSuccess(['order_id' => $orderId])) {
return $this->renderSuccess('操作成功');
}
return $this->renderError('操作失败');
}
10 months ago
/**
10 months ago
* @notes:确认订单
10 months ago
* @return Json
* @throws BaseException
* @throws DbException
* @author: wanghousheng
*/
10 months ago
public function checkOrder(): Json
10 months ago
{
10 months ago
$serverId = intval($this->request->post('server_id'));
$couponId = intval($this->request->post('coupon_id'));
if (!$serverId) {
return $this->renderError('非法请求');
10 months ago
}
10 months ago
$orderService = new ServerServiceOrder();
$data = $orderService->onCheck($serverId, $couponId);
return $this->renderSuccess($data);
}
/**
* @notes:生成订单
* @return Json
* @throws BaseException
* @throws DbException
* @author: wanghousheng
*/
public function createOrder(): Json
{
$serverId = intval($this->request->post('server_id'));
$couponId = intval($this->request->post('coupon_id'));
10 months ago
$server_time = $this->request->post('server_time');
10 months ago
$username = $this->request->post('username');
if (!$username) {
return $this->renderError('联系人不能为空');
}
$mobile = $this->request->post('mobile');
if (!$mobile || !preg_match("/1[3457896]\d{9}$/", $mobile)) {
return $this->renderError('手机号不正确');
}
10 months ago
if (!$server_time) {
return $this->renderError('服务时间不能为空');
}
$server_address = $this->request->post('server_address');
if (!$server_address) {
return $this->renderError('服务地址不能为空');
}
10 months ago
$remake = (string)$this->request->post('remake');
10 months ago
if (!$serverId) {
return $this->renderError('非法请求');
10 months ago
}
8 months ago
//分销工程师限制下单
if (UserService::isDealerEngineer()) {
return $this->renderError('当前角色无法下单');
}
10 months ago
$orderService = new ServerServiceOrder();
10 months ago
$data = [
'server_id' => $serverId,
'server_time' => $server_time,
'server_address' => $server_address,
'buyer_remark' => $remake,
10 months ago
'username' => $username,
'mobile' => $mobile,
10 months ago
];
$result = $orderService->createOrder($data, $couponId);
10 months ago
if ($result) {
return $this->renderSuccess($result);
}
return $this->renderError('创建订单失败');
}
/**
* @notes:取消订单
* @return Json
* @throws BaseException
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author: wanghousheng
*/
public function cancelOrder(): Json
{
$orderId = intval($this->request->post('order_id'));
if (!$orderId) {
return $this->renderError('非法请求');
}
if (ServerServiceOrder::userCancelOrder($orderId)) {
return $this->renderSuccess('取消成功');
}
return $this->renderError('操作失败');
}
/**
* @notes:派单人员列表
* @return Json
* @throws BaseException
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author: wanghousheng
*/
public function getEngineer(): Json
{
$list = ServerServiceOrder::getEngineer();
return $this->renderSuccess(compact('list'));
}
/**
* @notes:派单
* @return Json
* @throws BaseException
* @author: wanghousheng
*/
public function dispatchOrders(): Json
{
$orderId = intval($this->request->post('order_id'));
if (!$orderId) {
return $this->renderError('非法请求');
}
$dealerId = intval($this->request->post('dealer_id'));
if (!$dealerId) {
return $this->renderError('分配人员不能为空');
}
if (ServerServiceOrder::userDispatchOrders($orderId, $dealerId)) {
return $this->renderSuccess('操作成功');
}
return $this->renderError('操作失败');
}
/**
* @notes:获取支付订单的信息
* @return Json
* @throws BaseException
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author: wanghousheng
*/
public function orderInfo(): Json
{
$orderId = intval($this->request->post('order_id'));
if (!$orderId) {
return $this->renderError('非法请求');
10 months ago
}
10 months ago
$client = $this->request->post('client');
if (!$client) {
return $this->renderError('客户端不能为空');
}
$paymentService = new ServerPayment();
$data = $paymentService->setOrderId($orderId)->setClient($client)->orderInfo();
10 months ago
return $this->renderSuccess($data);
}
10 months ago
/**
* @notes:确认订单支付事件
* @throws BaseException
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author: wanghousheng
*/
10 months ago
public function orderPay(): Json
10 months ago
{
$orderId = intval($this->request->post('order_id'));
if (!$orderId) {
return $this->renderError('非法请求');
}
$method = $this->request->post('method');
if (!$method) {
return $this->renderError('支付方式不能为空');
}
$client = $this->request->post('client');
if (!$client) {
return $this->renderError('客户端不能为空');
}
$paymentService = new ServerPayment();
$data = $paymentService->setOrderId($orderId)
->setMethod($method)
->setClient($client)
->orderPay();
return $this->renderSuccess($data, $paymentService->getMessage() ?: '下单成功');
}
/**
* @notes:交易查询
* @return Json
* @throws BaseException
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author: wanghousheng
*/
10 months ago
public function tradeQuery(): Json
10 months ago
{
10 months ago
$method = $this->request->post('method');
if (!$method) {
return $this->renderError('支付方式不能为空');
}
$order_no = $this->request->post('order_no');
if (!$order_no) {
return $this->renderError('订单号不能为空');
}
$client = $this->request->post('client');
if (!$client) {
return $this->renderError('客户端不能为空');
}
10 months ago
$paymentService = new ServerPayment;
10 months ago
$result = $paymentService->setMethod($method)->setClient($client)->tradeQuery($order_no);
10 months ago
$message = $result ? '恭喜您,订单已付款成功' : ($paymentService->getError() ?: '很抱歉,订单未支付,请重新发起');
return $this->renderSuccess(['isPay' => $result], $message);
}
10 months ago
}