2025-09-29 17:37:21 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\cunkebao\controller;
|
|
|
|
|
|
|
|
|
|
use app\common\controller\PaymentService;
|
|
|
|
|
use app\cunkebao\model\TokensPackage;
|
|
|
|
|
use library\ResponseHelper;
|
2025-09-30 10:36:51 +08:00
|
|
|
use think\facade\Env;
|
2025-09-29 17:37:21 +08:00
|
|
|
|
|
|
|
|
class TokensController extends BaseController
|
|
|
|
|
{
|
|
|
|
|
public function getList()
|
|
|
|
|
{
|
|
|
|
|
$page = $this->request->param('page', 1);
|
|
|
|
|
$limit = $this->request->param('limit', 10);
|
|
|
|
|
$where = [
|
|
|
|
|
['isDel', '=', 0],
|
|
|
|
|
['status', '=', 1],
|
|
|
|
|
];
|
|
|
|
|
$query = TokensPackage::where($where);
|
|
|
|
|
$total = $query->count();
|
|
|
|
|
$list = $query->where($where)->page($page, $limit)->order('id desc')->select();
|
|
|
|
|
foreach ($list as &$item) {
|
|
|
|
|
$item['description'] = json_decode($item['description'], true);
|
|
|
|
|
$item['price'] = number_format($item['price'] / 100, 2);
|
|
|
|
|
$item['originalPrice'] = number_format($item['originalPrice'] / 100, 2);
|
|
|
|
|
}
|
|
|
|
|
unset($item);
|
|
|
|
|
return ResponseHelper::success(['list' => $list, 'total' => $total]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function pay()
|
|
|
|
|
{
|
|
|
|
|
$id = $this->request->param('id', '');
|
|
|
|
|
$price = $this->request->param('price', '');
|
|
|
|
|
$userId = $this->getUserInfo('id');
|
|
|
|
|
$companyId = $this->getUserInfo('companyId');
|
|
|
|
|
|
|
|
|
|
if (empty($id) && empty($price)) {
|
|
|
|
|
return ResponseHelper::error('套餐和自定义购买金额必须选一个');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!empty($id)){
|
|
|
|
|
$package = TokensPackage::where(['id' => $id, 'status' => 1, 'isDel' => 0])->find();
|
|
|
|
|
if (empty($package)) {
|
|
|
|
|
return ResponseHelper::error('套餐不存在或者已禁用');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($package['price'] <= 0){
|
|
|
|
|
return ResponseHelper::error('套餐金额异常');
|
|
|
|
|
}
|
2025-09-30 10:36:51 +08:00
|
|
|
|
|
|
|
|
$specs = [
|
|
|
|
|
'id' => $package['id'],
|
|
|
|
|
'name' => $package['name'],
|
|
|
|
|
'price' => $package['price'],
|
|
|
|
|
'tokens' => $package['tokens'],
|
|
|
|
|
];
|
|
|
|
|
|
2025-09-29 17:37:21 +08:00
|
|
|
}else{
|
2025-09-30 10:36:51 +08:00
|
|
|
//获取配置的tokens比例
|
|
|
|
|
$tokens_multiple = Env::get('payment.tokens_multiple',28);
|
|
|
|
|
$specs = [
|
2025-09-29 17:37:21 +08:00
|
|
|
'id' => 0,
|
|
|
|
|
'name' => '自定义购买算力',
|
2025-09-30 10:36:51 +08:00
|
|
|
'price' => intval($price * 100),
|
|
|
|
|
'tokens' => intval($price * $tokens_multiple),
|
2025-09-29 17:37:21 +08:00
|
|
|
];
|
|
|
|
|
}
|
2025-09-30 10:36:51 +08:00
|
|
|
|
2025-09-29 17:37:21 +08:00
|
|
|
$orderNo = date('YmdHis') . rand(100000, 999999);
|
|
|
|
|
$order = [
|
|
|
|
|
'companyId' => $companyId,
|
|
|
|
|
'userId' => $userId,
|
|
|
|
|
'orderNo' => $orderNo,
|
2025-09-30 10:36:51 +08:00
|
|
|
'goodsId' => $specs['id'],
|
|
|
|
|
'goodsName' => $specs['name'],
|
|
|
|
|
'goodsSpecs' => $specs,
|
2025-09-29 17:37:21 +08:00
|
|
|
'orderType' => 1,
|
2025-09-30 10:36:51 +08:00
|
|
|
'money' => $specs['price']
|
2025-09-29 17:37:21 +08:00
|
|
|
];
|
|
|
|
|
$paymentService = new PaymentService();
|
|
|
|
|
$res = $paymentService->createOrder($order);
|
|
|
|
|
$res = json_decode($res, true);
|
|
|
|
|
if ($res['code'] == 200){
|
|
|
|
|
return ResponseHelper::success(['orderNo' => $orderNo,'code_url' => $res['data']],'订单创建成功');
|
|
|
|
|
}else{
|
|
|
|
|
return ResponseHelper::error($res['msg']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|