Files
zzp-server/app/common/command/TronOrder.php
2026-01-19 14:19:22 +08:00

134 lines
6.4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\common\command;
use app\common\service\{ConfigService,UtilsService};
use app\common\model\user\{User,UserTron};
use app\common\model\finance\RechargeRecord;
use app\common\model\setting\RechargeMethod;
use think\console\{Command,Output,Input};
use think\facade\{Db,Log};
class TronOrder extends Command
{
protected function configure()
{
$this->setName('tron_order')
->setDescription('同步波场钱包余额');
}
protected function execute(Input $input, Output $output)
{
Db::startTrans();
try {
//为了性能只获取最后操作时间5分钟内的钱包
$now_5 = time() - 5 * 60;//5分钟前
$userTrons = UserTron::where("last_time > $now_5")
->order(['create_time' => 'desc'])
->select()
->toArray();
//查询余额
$data = [
'action' => 'infos',
];
$addrs = array();
foreach ($userTrons as $key=>$userTron) {
$addrs[$key] = $userTron['address'];
}
if(count($addrs) > 0){
$data['addrs'] = json_encode($addrs);
$response = UtilsService::usdt_request($data, 'POST');
$response = json_decode($response, true);
if($response['code'] == 200){
foreach ($response['data']['data'] as $res) {
UserTron::where(['address' => $res['addr']])
->update([
'money_trx' => $res['trx'],
'money_usdt' => $res['usdt'],
]);
//充值逻辑
foreach ($userTrons as $userTron) {
if($userTron['address'] == $res['addr']){
//金额相差超过0.01才生效
if(floatval($res['usdt']) - floatval($userTron['money_usdt']) >= 0.01){
$method = RechargeMethod::where(['id' => $userTron['method_id']])->findOrEmpty();
if(!$method->isEmpty()){
$money = $res['usdt'] - $userTron['money_usdt'];
$order_amount_act = round($money * $method['rate'] , 2);
//判断充值金额
$config = ConfigService::get('website', 'trade');
if ($order_amount_act >= $config['recharge_min']) {
$data = [
'sn' => generate_sn(RechargeRecord::class, 'sn'),
'user_id' => $userTron['user_id'],
'method_id' => $userTron['method_id'],
'amount' => $order_amount_act,
'amount_act' => round($money , $method['precision']),
'rate' => $method['rate'],
'symbol' => $method['symbol'],
'status' => 1,
];
$record = RechargeRecord::create($data);
//记录日志
UtilsService::user_finance_add(
$userTron['user_id'],
1,
1,
$data['amount'],
'',
'',
1//冻结
);
//用户资金修改
UtilsService::user_money_change($userTron['user_id'], 1, $data['amount'],'user_money');
//充值金额增加
UtilsService::user_money_change($userTron['user_id'], 1, $data['amount'],'total_recharge');
//充值活动奖励
UtilsService::activity_reward_add($userTron['user_id'],$data['amount']);
//更新充值记录用户余额
$user = User::where(['id' => $record['user_id']])->findOrEmpty();
if (!$user->isEmpty()) {
RechargeRecord::update([
'id' => $record['id'],
'user_money' => $user['user_money']
]);
//充值次数+1
User::update([
'id' => $user['id'],
'recharge_num' => $user['recharge_num'] + 1
]);
}
}
}
}
}
}
}
}
}
Db::commit();
} catch (\Exception $e) {
Db::rollback();
Log::write('失败原因:' . $e->getMessage());
return false;
}
}
}