93 lines
3.1 KiB
PHP
93 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace app\api\logic;
|
|
|
|
use app\common\enum\PayEnum;
|
|
use app\common\logic\BaseLogic;
|
|
use app\common\model\points\PointsOrder;
|
|
use app\common\model\points\PointsProduct;
|
|
|
|
class PointsOrderLogic extends BaseLogic
|
|
{
|
|
public static function products(): array
|
|
{
|
|
return PointsProduct::where('status', 1)
|
|
->order('sort', 'desc')
|
|
->order('id', 'asc')
|
|
->select()
|
|
->toArray();
|
|
}
|
|
|
|
private static function makeSign(int $productId, int $points, string $amount): string
|
|
{
|
|
$secret = env('APP_KEY', 'likeadmin');
|
|
return md5("pid={$productId}&pts={$points}&amt={$amount}&key={$secret}");
|
|
}
|
|
|
|
public static function preOrder(array $params)
|
|
{
|
|
try {
|
|
$product = PointsProduct::where(['id' => $params['product_id'], 'status' => 1])->findOrEmpty();
|
|
if ($product->isEmpty()) {
|
|
throw new \Exception('积分套餐不存在或已下架');
|
|
}
|
|
$amount = (string) $product['amount'];
|
|
return [
|
|
'product_id' => (int) $product['id'],
|
|
'name' => $product['name'],
|
|
'points' => (int) $product['points'],
|
|
'amount' => $amount,
|
|
'sign' => self::makeSign((int) $product['id'], (int) $product['points'], $amount),
|
|
];
|
|
} catch (\Exception $e) {
|
|
self::setError($e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function createOrder(array $params)
|
|
{
|
|
try {
|
|
$product = PointsProduct::where(['id' => $params['product_id'], 'status' => 1])->findOrEmpty();
|
|
if ($product->isEmpty()) {
|
|
throw new \Exception('积分套餐不存在或已下架');
|
|
}
|
|
$order = PointsOrder::create([
|
|
'sn' => generate_sn(PointsOrder::class, 'sn'),
|
|
'user_id' => $params['user_id'],
|
|
'product_id' => $product['id'],
|
|
'product_name' => $product['name'],
|
|
'points' => $product['points'],
|
|
'order_amount' => $product['amount'],
|
|
'pay_status' => PayEnum::UNPAID,
|
|
'order_terminal' => $params['terminal'],
|
|
]);
|
|
return [
|
|
'order_id' => (int) $order['id'],
|
|
'from' => 'points_order'
|
|
];
|
|
} catch (\Exception $e) {
|
|
self::setError($e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function detail(array $params)
|
|
{
|
|
try {
|
|
$order = PointsOrder::where(['id' => $params['order_id'], 'user_id' => $params['user_id']])
|
|
->append(['pay_status_text', 'pay_way_text'])
|
|
->findOrEmpty();
|
|
if ($order->isEmpty()) {
|
|
throw new \Exception('订单不存在');
|
|
}
|
|
$data = $order->toArray();
|
|
$data['pay_time'] = empty($data['pay_time']) ? '' : date('Y-m-d H:i:s', $data['pay_time']);
|
|
return $data;
|
|
} catch (\Exception $e) {
|
|
self::setError($e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
}
|