代码提交
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace app\chukebao\controller;
|
||||
|
||||
use app\chukebao\model\TokensCompany;
|
||||
use app\chukebao\model\TokensRecord;
|
||||
use library\ResponseHelper;
|
||||
use think\Db;
|
||||
@@ -107,12 +108,7 @@ class TokensRecordController extends BaseController
|
||||
Db::startTrans();
|
||||
try {
|
||||
// 使用悲观锁获取用户当前tokens余额,确保并发安全
|
||||
$userInfo = Db::name('users')
|
||||
->where('id', $userId)
|
||||
->where('companyId', $companyId)
|
||||
->lock(true) // 悲观锁,防止并发问题
|
||||
->find();
|
||||
|
||||
$userInfo = TokensCompany::where('companyId', $companyId)->lock(true)->find();
|
||||
if (!$userInfo) {
|
||||
throw new \Exception('用户不存在');
|
||||
}
|
||||
@@ -123,10 +119,8 @@ class TokensRecordController extends BaseController
|
||||
$newBalance = $type == 1 ? ($currentTokens + $tokens) : ($currentTokens - $tokens);
|
||||
|
||||
// 使用原子更新操作,基于当前值进行更新,防止并发覆盖
|
||||
$updateResult = Db::name('users')
|
||||
->where('id', $userId)
|
||||
$updateResult = TokensCompany::where('companyId', $companyId)
|
||||
->where('companyId', $companyId)
|
||||
->where('tokens', $currentTokens) // 确保基于当前值更新
|
||||
->update([
|
||||
'tokens' => $newBalance,
|
||||
'updateTime' => time()
|
||||
|
||||
16
Server/application/chukebao/model/TokensCompany.php
Normal file
16
Server/application/chukebao/model/TokensCompany.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace app\chukebao\model;
|
||||
|
||||
use think\Model;
|
||||
class TokensCompany extends Model
|
||||
{
|
||||
protected $pk = 'id';
|
||||
protected $name = 'tokens_company';
|
||||
|
||||
// 自动写入时间戳
|
||||
protected $autoWriteTimestamp = true;
|
||||
protected $createTime = 'createTime';
|
||||
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace app\common\controller;
|
||||
|
||||
use app\chukebao\model\TokensCompany;
|
||||
use think\Db;
|
||||
use app\common\util\PaymentUtil;
|
||||
use think\facade\Config;
|
||||
@@ -60,20 +61,19 @@ class PaymentService
|
||||
//创建订单
|
||||
Order::create([
|
||||
'mchId' => $params['mch_id'],
|
||||
'companyId' => $order['companyId'],
|
||||
'userId' => $order['userId'],
|
||||
'orderType' => $order['orderType'] ?? 1,
|
||||
'companyId' => isset($order['companyId']) ? $order['companyId'] : 0,
|
||||
'userId' => isset($order['userId']) ? $order['userId'] : 0,
|
||||
'orderType' => isset($order['orderType']) ? $order['orderType'] : 1,
|
||||
'status' => 0,
|
||||
'goodsId' => $order['goodsId'],
|
||||
'goodsName' => $order['goodsName'],
|
||||
'money' => $order['money'],
|
||||
'orderNo' => $order['orderNo'],
|
||||
'goodsId' => isset($order['goodsId']) ? $order['goodsId'] : 0,
|
||||
'goodsName' => isset($order['goodsName']) ? $order['goodsName'] : '',
|
||||
'money' => isset($order['money']) ? $order['money'] : 0,
|
||||
'goodsSpecs' => isset($order['goodsSpecs']) ? json_encode($order['goodsSpecs'],256) : json_encode([]),
|
||||
'orderNo' => isset($order['orderNo']) ? $order['orderNo'] : '',
|
||||
'ip' => Request::ip(),
|
||||
'nonceStr' => $params['nonce_str'],
|
||||
'nonceStr' => isset($order['nonceStr']) ? $order['nonceStr'] : '',
|
||||
'createTime' => time(),
|
||||
]);
|
||||
|
||||
|
||||
// XML POST 请求
|
||||
$xmlBody = $this->arrayToXml($params);
|
||||
$response = $this->postXml($url, $xmlBody);
|
||||
@@ -227,15 +227,36 @@ class PaymentService
|
||||
|
||||
if ($pay_result != 0) {
|
||||
$order->payInfo = $payload['pay_info'];
|
||||
$order->payType = $payload['trade_type'] == 'pay.wechat.jspay' ? 1 : 2;
|
||||
$order->status = 3;
|
||||
$order->save();
|
||||
Db::commit();
|
||||
return json_encode(['code' => 500, 'msg' => $payload['pay_info']]);
|
||||
}
|
||||
$order->payType = $payload['trade_type'] == 'pay.wechat.jspay' ? 1 : 2;
|
||||
$order->status = 1;
|
||||
$order->payTime = $this->parsePayTime($time_end);
|
||||
$order->save();
|
||||
//订单处理
|
||||
switch ($order['orderType']){
|
||||
case 1:
|
||||
// 处理购买算力
|
||||
$token = TokensCompany::where(['companyId' => $order->companyId])->find();
|
||||
$goodsSpecs = json_decode($token->goodsSpecs,true);
|
||||
if (!empty($token)){
|
||||
$token->tokens = $token->tokens + $goodsSpecs['tokens'];
|
||||
$token->updateTime = time();
|
||||
$token->save();
|
||||
}else{
|
||||
$tokensCompany = new TokensCompany();
|
||||
$tokensCompany->companyId = $order->companyId;
|
||||
$tokensCompany->tokens = $goodsSpecs['tokens'];
|
||||
$tokensCompany->createTime = time();
|
||||
$tokensCompany->updateTime = time();
|
||||
$tokensCompany-> save();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
return json_encode(['code' => 200, 'msg' => '付款成功']);
|
||||
} catch (\Exception $e) {
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace app\cunkebao\controller;
|
||||
use app\common\controller\PaymentService;
|
||||
use app\cunkebao\model\TokensPackage;
|
||||
use library\ResponseHelper;
|
||||
use think\facade\Env;
|
||||
|
||||
class TokensController extends BaseController
|
||||
{
|
||||
@@ -48,26 +49,36 @@ class TokensController extends BaseController
|
||||
|
||||
if ($package['price'] <= 0){
|
||||
return ResponseHelper::error('套餐金额异常');
|
||||
}else{
|
||||
$price = $package['price'];
|
||||
}
|
||||
|
||||
$specs = [
|
||||
'id' => $package['id'],
|
||||
'name' => $package['name'],
|
||||
'price' => $package['price'],
|
||||
'tokens' => $package['tokens'],
|
||||
];
|
||||
|
||||
}else{
|
||||
$package = [
|
||||
//获取配置的tokens比例
|
||||
$tokens_multiple = Env::get('payment.tokens_multiple',28);
|
||||
$specs = [
|
||||
'id' => 0,
|
||||
'name' => '自定义购买算力',
|
||||
'price' => intval($price * 100)
|
||||
'price' => intval($price * 100),
|
||||
'tokens' => intval($price * $tokens_multiple),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
$orderNo = date('YmdHis') . rand(100000, 999999);
|
||||
$order = [
|
||||
'companyId' => $companyId,
|
||||
'userId' => $userId,
|
||||
'orderNo' => $orderNo,
|
||||
'goodsId' => $package['id'],
|
||||
'goodsName' => $package['name'],
|
||||
'goodsId' => $specs['id'],
|
||||
'goodsName' => $specs['name'],
|
||||
'goodsSpecs' => $specs,
|
||||
'orderType' => 1,
|
||||
'money' => $package['price']
|
||||
'money' => $specs['price']
|
||||
];
|
||||
$paymentService = new PaymentService();
|
||||
$res = $paymentService->createOrder($order);
|
||||
|
||||
Reference in New Issue
Block a user