123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- <?php
- namespace App\Http\Services;
- use App\Http\Services\RsaServices;
- use App\Models\Energy\EnergyPlatform;
- use App\Models\Energy\EnergyPlatformBot;
- class EnergyServices
- {
- /**
- * 调用预支能量
- * @param $data json数据
- */
- public function sendenergy($request)
- {
- $pfbotdata = EnergyPlatformBot::where('bot_rid',$request['bot_rid'])->where('status',0)->first();
- if(empty($pfbotdata)){
- return ['code' => '400', 'msg' => '未配置能量平台,联系管理员'];
- }
-
- //轮询,自己质押时判断能量是否足够,用平台则判断平台的trx
- $model = EnergyPlatform::where('poll_group',$pfbotdata['poll_group'])
- ->where('status',0)
- ->whereNotNull('platform_apikey')
- ->where(function ($query) use($request) {
- $query->where(function ($query1) use($request){
- $query1->where('platform_name', 3)->where('platform_balance', '>=', $request['energy_amount']);
- });
- $query->orwhere(function ($query2) {
- $query2->orwhereIn('platform_name', [1,2,4,5])->where('platform_balance', '>', 0);
- });
- })
- ->orderBy('seq_sn','desc')
- ->get();
-
- if($model->count() > 0){
- $errorMessage = '';
- foreach ($model as $k => $v){
- $rsa_services = new RsaServices();
- $signstr = $rsa_services->privateDecrypt($v->platform_apikey);
-
- if(empty($signstr)){
- // return ['code' => '400', 'msg' => '未配置能量平台私钥,联系管理员'];
- //未配置能量平台私钥,轮询下一个
- $errorMessage = $errorMessage."能量平台:".$v->rid." 平台私钥为空。";
- continue;
- }
-
- //neee.cc平台
- if($v->platform_name == 1){
- $header = [
- "Content-Type:application/json"
- ];
-
- $param = [
- "uid" => strval($v->platform_uid),
- "resource_type" => "0", //0能量
- "receive_address" => $request['receive_address'],
- "amount" => strval($request['energy_amount']),
- "freeze_day" => strval($request['energy_day']), //0:一小时,1:一天,3:三天
- "time" => strval(time())
- ];
-
- ksort($param);
- reset($param);
-
- foreach($param as $k1 => $v1){
- if($k1 != "sign" && $k1 != "sign_type" && $v1 != ''){
- $signstr .= $k1.$v1;
- }
- }
-
- $sign = md5($signstr);
- $param['sign'] = $sign;
- $balance_url = 'https://api.tronqq.com/openapi/v2/order/submit';
- $res = Get_Curl($balance_url,json_encode($param),$header);
-
- //RentEnergysBot平台
- }elseif($v->platform_name == 2){
- //0:一小时,1:一天,3:三天
- switch ($request['energy_day']) {
- case 1:
- $type = 'day';
- break;
- case 3:
- $type = '3day';
- break;
- default:
- $type = 'hour';
- break;
- }
-
- //该平台最低33000
- $energy_amount = $request['energy_amount'] < 33000 ?33000:$request['energy_amount'];
-
- $balance_url = 'https://api.wallet.buzz?api=getEnergy&apikey='.$signstr.'&address='.$request['receive_address'].'&amount='.$energy_amount.'&type='.$type;
- $res = Get_Curl($balance_url);
-
- //自己质押
- }elseif($v->platform_name == 3){
- $params = [
- 'pri' => $signstr,
- 'fromaddress' => $v->platform_uid,
- 'receiveaddress' => $request['receive_address'],
- 'resourcename' => 'ENERGY',
- 'resourceamount' => $request['energy_amount'],
- 'resourcetype' => 1,
- 'permissionid' => $v->permission_id
- ];
-
- $res = Get_Curl(base64_decode('aHR0cHM6Ly90cm9ud2Vibm9kZWpzLndhbGxldGltLnZpcC9kZWxlZ2VhbmR1bmRlbGV0ZQ=='),$params);
-
- //trongas.io平台
- }elseif($v->platform_name == 4){
- //0:一小时,1:一天,3:三天
- switch ($request['energy_day']) {
- case 1:
- $rentTime = 24;
- break;
- case 3:
- $rentTime = 72;
- break;
- default:
- $rentTime = 1;
- break;
- }
-
- $param = [
- "username" => $v->platform_uid, // 用户名
- "password" => $signstr, // 用户密码
- "resType" => "ENERGY", // 资源类型,ENERGY:能量,BANDWIDTH:带宽
- "payNums" => $request['energy_amount'], // 租用数量
- "rentTime" => $rentTime, // 单位小时,只能1时或1到30天按天租用其中不能租用2天
- "resLock" => 0, // 租用锁定,0:不锁定,1:锁定。能量租用数量不小于500万且租用时间不小于3天才能锁定。带宽租用数量不小于30万租用时间不小于3天才能锁定
- "receiveAddress" => $request['receive_address'] // 接收资源地址(请勿输入合约地址或没激活地址)
- ];
-
- $balance_url = 'https://trongas.io/api/pay';
- $res = Get_Curl($balance_url,$param);
-
- //机器人开发者代理
- }elseif($v->platform_name == 5){
- $balance_url = env('THIRD_URL');
- if(empty($balance_url)){
- $errorMessage = $errorMessage."使用开发者能量代理时,env中url为空";
- continue;
- }
-
- //0:一小时,1:一天,3:三天
- switch ($request['energy_day']) {
- case 1:
- $rentTime = 24;
- break;
- case 3:
- $rentTime = 72;
- break;
- default:
- $rentTime = 1;
- break;
- }
- //使用开发者能量代理,仅支持32000 65000能量代理1小时
- if($rentTime != 1){
- $errorMessage = $errorMessage."使用开发者能量代理时,时长只能为1小时";
- continue;
- }
-
- $param = [
- "tg_uid" => $v->platform_uid, // 用户名,此处是tg的uid,因为是机器人能量代理模式
- "payNums" => $request['energy_amount'], // 租用数量
- "rentTime" => $rentTime, // 单位小时,只能1时或1到30天按天租用其中不能租用2天
- "receiveAddress" => $request['receive_address'] // 接收资源地址(请勿输入合约地址或没激活地址)
- ];
-
- $balance_url = $balance_url.'/api/thirdpart/shanzuorder';
- $res = Get_Curl($balance_url,$param);
-
- }else{
- // return ['code' => '400', 'msg' => '能量平台不存在,联系管理员'];
- //能量平台接口不存在,轮询下一个
- $errorMessage = $errorMessage."能量平台:".$v->rid." 能量平台接口不存在。";
- continue;
- }
-
- if(empty($res)){
- // return ['code' => '400', 'msg' => '代理能量失败1,接口为空'];
- //能量平台接口返回为空,轮询下一个
- $errorMessage = $errorMessage."能量平台:".$v->rid." 能量平台接口返回为空。";
- continue;
- }else{
- $res = json_decode($res,true);
- if((isset($res['status']) && $res['status'] == 200 && $v->platform_name == 1) || (isset($res['status']) && $res['status'] == 'success' && $v->platform_name == 2) || (isset($res['code']) && $res['code'] == 200 && $v->platform_name == 3) || (isset($res['code']) && $res['code'] == 10000 && $v->platform_name == 4) || (isset($res['code']) && $res['code'] == 200 && $v->platform_name == 5)){
- if($v->platform_name == 1){
- $data['orderNo'] = $res['data']['order_no'];
- $data['use_trx'] = 0;
-
- }elseif($v->platform_name == 2){
- $data['orderNo'] = $res['txid'];
- $data['use_trx'] = 0;
-
- }elseif(isset($res['data']['txid']) && $v->platform_name == 3){
- $data['orderNo'] = $res['data']['txid'];
- $data['use_trx'] = $res['data']['use_trx'];
-
- }elseif(isset($res['data']['orderId']) && $v->platform_name == 4){
- $data['orderNo'] = $res['data']['orderId'];
- $data['use_trx'] = $res['data']['orderMoney'];
-
- }elseif($v->platform_name == 5){
- $data['orderNo'] = $res['data']['orderId'];
- $data['use_trx'] = $res['data']['orderMoney'];
-
- }else{
- $data['orderNo'] = '未知'.time();
- $data['use_trx'] = 0;
- }
- $data['energy_platform_rid'] = $v->rid;
- $data['energy_platform_bot_rid'] = $pfbotdata['rid'];
- $data['platform_name'] = $v->platform_name;
- $data['platform_uid'] = $v->platform_uid;
- return ['code' => '200', 'msg' => '预支能量成功', 'data' => $data];
- }else{
- if($v->platform_name == 1){
- $msg = '失败,返回:'.$res['msg'];
- }elseif($v->platform_name == 2){
- $msg = '失败,返回:'.json_encode($res);
- }elseif($v->platform_name == 3){
- $msg = '失败,检查质押是否足够';
- }elseif($v->platform_name == 4){
- $msg = '失败,返回:'.json_encode($res);
- }elseif($v->platform_name == 5){
- $msg = '失败,返回:'.json_encode($res);
- }
- // return ['code' => '400', 'msg' => $msg];
- //能量平台下单失败,轮询下一个
- $errorMessage = $errorMessage."能量平台:".$v->rid.$msg;
- continue;
- }
- }
- }
- return ['code' => '400', 'msg' => '机器人所有能量平台失败:'.$errorMessage];
- }else{
- return ['code' => '400', 'msg' => '机器人无可用能量平台,请质押或者充值平台'];
- }
- }
- }
|