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/model/Server/ServerOrder.php

139 lines
4.4 KiB

10 months ago
<?php
namespace app\api\model\Server;
10 months ago
use app\api\model\Setting as SettingModel;
10 months ago
use app\api\service\User as UserService;
10 months ago
use app\common\enum\order\PayStatus;
use app\common\enum\ServerEnum;
10 months ago
use app\common\model\server\Order;
use cores\exception\BaseException;
10 months ago
use think\db\exception\DataNotFoundException;
10 months ago
use think\db\exception\DbException;
10 months ago
use think\db\exception\ModelNotFoundException;
10 months ago
use think\Paginator;
class ServerOrder extends Order
{
/**
* 隐藏字段
* @var array
*/
protected $hidden = [
'store_id',
'create_time',
'update_time',
'trade_id',
'server_image_id',
'is_delete',
];
/**
* @notes:用户服务订单列表
* @param $where
* @param int $listRows
* @return Paginator
* @throws BaseException
* @throws DbException
* @author: wanghousheng
*/
10 months ago
public function orderList($where, int $listRows = 15): Paginator
10 months ago
{
// 当前用户ID
$userId = UserService::getCurrentLoginUserId();
10 months ago
//判断当前用户身份
if (UserService::isDealerEngineer()) {
//分销商工程师
return $this->with(['image', 'user'])
->where($where)
->where('dealer_id', '=', $userId)
->where('is_delete', '=', 0)
->order(['create_time' => 'desc'])
->paginate($listRows);
} elseif (UserService::isStore()) {
// 店主
return $this->with(['image', 'user', 'dealer'])
->where($where)
->where('is_delete', '=', 0)
->order(['create_time' => 'desc'])
->paginate($listRows);
} else {
return $this->with(['image', 'dealer'])
->where($where)
->where('user_id', '=', $userId)
->where('is_delete', '=', 0)
->order(['create_time' => 'desc'])
->paginate($listRows);
}
10 months ago
}
/**
10 months ago
* 获取用户订单详情(仅订单记录)
* @param int $orderId
* @param array $with
* @param bool $onlyCurrentUser 只查询当前登录用户的记录
* @return array|Order|null
10 months ago
* @throws BaseException
10 months ago
*/
public static function getDetail(int $orderId, array $with = [], bool $onlyCurrentUser = true)
{
// 查询条件
$where = ['order_id' => $orderId];
$onlyCurrentUser && $where['user_id'] = UserService::getCurrentLoginUserId();
// 查询订单记录
$order = static::detail($where, $with);
empty($order) && throwError('订单不存在');
return $order;
}
/**
* 待支付订单详情
* @param string $orderNo 订单号
* @return null|static
*/
public static function getPayDetail(string $orderNo): ?Order
{
$where = ['order_no' => $orderNo, 'is_delete' => 0];
return self::detail($where, ['user']);
}
/**
* @notes:获取未支付的订单详情(用于订单支付)
* @param int $orderId 订单ID
* @return array
* @throws BaseException
* @throws DataNotFoundException
10 months ago
* @throws DbException
10 months ago
* @throws ModelNotFoundException
10 months ago
* @author: wanghousheng
*/
10 months ago
public static function getUnpaidOrderDetail(int $orderId): array
10 months ago
{
10 months ago
// 获取订单详情
$orderInfo = static::getDetail($orderId);
// 验证订单状态
10 months ago
if ($orderInfo['order_status'] != ServerEnum::APPLYPAY || $orderInfo['pay_status'] == PayStatus::SUCCESS) {
10 months ago
throwError('当前订单状态不允许支付');
}
// 未支付订单的过期时间
$orderCloseTime = SettingModel::getOrderCloseTime() * 60 * 60;
// 订单超时截止时间
10 months ago
$expirationTime = strtotime($orderInfo['create_time']) + $orderCloseTime;
10 months ago
if ($orderCloseTime > 0 && $expirationTime <= time()) {
throwError('当前订单支付已超时,请重新下单');
}
// 仅返回需要的数据
return [
'orderId' => $orderInfo['order_id'],
'order_no' => $orderInfo['order_no'],
'pay_price' => $orderInfo['pay_price'],
'pay_status' => $orderInfo['pay_status'],
'order_status' => $orderInfo['order_status'],
'create_time' => $orderInfo['create_time'],
'showExpiration' => $orderCloseTime > 0,
'expirationTime' => format_time($expirationTime),
];
10 months ago
}
10 months ago
10 months ago
}