EnergyServices.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace App\Http\Services;
  3. use App\Http\Services\RsaServices;
  4. use App\Models\Energy\EnergyPlatform;
  5. use App\Models\Energy\EnergyPlatformBot;
  6. class EnergyServices
  7. {
  8. /**
  9. * 调用预支能量
  10. * @param $data json数据
  11. */
  12. public function sendenergy($request)
  13. {
  14. $pfbotdata = EnergyPlatformBot::where('bot_rid',$request['bot_rid'])->where('status',0)->first();
  15. if(empty($pfbotdata)){
  16. return ['code' => '400', 'msg' => '未配置能量平台,联系管理员'];
  17. }
  18. //轮询,自己质押时判断能量是否足够,用平台则判断平台的trx
  19. $model = EnergyPlatform::where('poll_group',$pfbotdata['poll_group'])
  20. ->where('status',0)
  21. ->whereNotNull('platform_apikey')
  22. ->where(function ($query) use($request) {
  23. $query->where(function ($query1) use($request){
  24. $query1->where('platform_name', 3)->where('platform_balance', '>=', $request['energy_amount']);
  25. });
  26. $query->orwhere(function ($query2) {
  27. $query2->orwhereIn('platform_name', [1,2,4,5])->where('platform_balance', '>', 0);
  28. });
  29. })
  30. ->orderBy('seq_sn','desc')
  31. ->get();
  32. if($model->count() > 0){
  33. $errorMessage = '';
  34. foreach ($model as $k => $v){
  35. $rsa_services = new RsaServices();
  36. $signstr = $rsa_services->privateDecrypt($v->platform_apikey);
  37. if(empty($signstr)){
  38. // return ['code' => '400', 'msg' => '未配置能量平台私钥,联系管理员'];
  39. //未配置能量平台私钥,轮询下一个
  40. $errorMessage = $errorMessage."能量平台:".$v->rid." 平台私钥为空。";
  41. continue;
  42. }
  43. //neee.cc平台
  44. if($v->platform_name == 1){
  45. $header = [
  46. "Content-Type:application/json"
  47. ];
  48. $param = [
  49. "uid" => strval($v->platform_uid),
  50. "resource_type" => "0", //0能量
  51. "receive_address" => $request['receive_address'],
  52. "amount" => strval($request['energy_amount']),
  53. "freeze_day" => strval($request['energy_day']), //0:一小时,1:一天,3:三天
  54. "time" => strval(time())
  55. ];
  56. ksort($param);
  57. reset($param);
  58. foreach($param as $k1 => $v1){
  59. if($k1 != "sign" && $k1 != "sign_type" && $v1 != ''){
  60. $signstr .= $k1.$v1;
  61. }
  62. }
  63. $sign = md5($signstr);
  64. $param['sign'] = $sign;
  65. $balance_url = 'https://api.tronqq.com/openapi/v2/order/submit';
  66. $res = Get_Curl($balance_url,json_encode($param),$header);
  67. //RentEnergysBot平台
  68. }elseif($v->platform_name == 2){
  69. //0:一小时,1:一天,3:三天
  70. switch ($request['energy_day']) {
  71. case 1:
  72. $type = 'day';
  73. break;
  74. case 3:
  75. $type = '3day';
  76. break;
  77. default:
  78. $type = 'hour';
  79. break;
  80. }
  81. //该平台最低33000
  82. $energy_amount = $request['energy_amount'] < 33000 ?33000:$request['energy_amount'];
  83. $balance_url = 'https://api.wallet.buzz?api=getEnergy&apikey='.$signstr.'&address='.$request['receive_address'].'&amount='.$energy_amount.'&type='.$type;
  84. $res = Get_Curl($balance_url);
  85. //自己质押
  86. }elseif($v->platform_name == 3){
  87. $params = [
  88. 'pri' => $signstr,
  89. 'fromaddress' => $v->platform_uid,
  90. 'receiveaddress' => $request['receive_address'],
  91. 'resourcename' => 'ENERGY',
  92. 'resourceamount' => $request['energy_amount'],
  93. 'resourcetype' => 1,
  94. 'permissionid' => $v->permission_id
  95. ];
  96. $res = Get_Curl(base64_decode('aHR0cHM6Ly90cm9ud2Vibm9kZWpzLndhbGxldGltLnZpcC9kZWxlZ2VhbmR1bmRlbGV0ZQ=='),$params);
  97. //trongas.io平台
  98. }elseif($v->platform_name == 4){
  99. //0:一小时,1:一天,3:三天
  100. switch ($request['energy_day']) {
  101. case 1:
  102. $rentTime = 24;
  103. break;
  104. case 3:
  105. $rentTime = 72;
  106. break;
  107. default:
  108. $rentTime = 1;
  109. break;
  110. }
  111. $param = [
  112. "username" => $v->platform_uid, // 用户名
  113. "password" => $signstr, // 用户密码
  114. "resType" => "ENERGY", // 资源类型,ENERGY:能量,BANDWIDTH:带宽
  115. "payNums" => $request['energy_amount'], // 租用数量
  116. "rentTime" => $rentTime, // 单位小时,只能1时或1到30天按天租用其中不能租用2天
  117. "resLock" => 0, // 租用锁定,0:不锁定,1:锁定。能量租用数量不小于500万且租用时间不小于3天才能锁定。带宽租用数量不小于30万租用时间不小于3天才能锁定
  118. "receiveAddress" => $request['receive_address'] // 接收资源地址(请勿输入合约地址或没激活地址)
  119. ];
  120. $balance_url = 'https://trongas.io/api/pay';
  121. $res = Get_Curl($balance_url,$param);
  122. //机器人开发者代理
  123. }elseif($v->platform_name == 5){
  124. $balance_url = env('THIRD_URL');
  125. if(empty($balance_url)){
  126. $errorMessage = $errorMessage."使用开发者能量代理时,env中url为空";
  127. continue;
  128. }
  129. //0:一小时,1:一天,3:三天
  130. switch ($request['energy_day']) {
  131. case 1:
  132. $rentTime = 24;
  133. break;
  134. case 3:
  135. $rentTime = 72;
  136. break;
  137. default:
  138. $rentTime = 1;
  139. break;
  140. }
  141. //使用开发者能量代理,仅支持32000 65000能量代理1小时
  142. if($rentTime != 1){
  143. $errorMessage = $errorMessage."使用开发者能量代理时,时长只能为1小时";
  144. continue;
  145. }
  146. $param = [
  147. "tg_uid" => $v->platform_uid, // 用户名,此处是tg的uid,因为是机器人能量代理模式
  148. "payNums" => $request['energy_amount'], // 租用数量
  149. "rentTime" => $rentTime, // 单位小时,只能1时或1到30天按天租用其中不能租用2天
  150. "receiveAddress" => $request['receive_address'] // 接收资源地址(请勿输入合约地址或没激活地址)
  151. ];
  152. $balance_url = $balance_url.'/api/thirdpart/shanzuorder';
  153. $res = Get_Curl($balance_url,$param);
  154. }else{
  155. // return ['code' => '400', 'msg' => '能量平台不存在,联系管理员'];
  156. //能量平台接口不存在,轮询下一个
  157. $errorMessage = $errorMessage."能量平台:".$v->rid." 能量平台接口不存在。";
  158. continue;
  159. }
  160. if(empty($res)){
  161. // return ['code' => '400', 'msg' => '代理能量失败1,接口为空'];
  162. //能量平台接口返回为空,轮询下一个
  163. $errorMessage = $errorMessage."能量平台:".$v->rid." 能量平台接口返回为空。";
  164. continue;
  165. }else{
  166. $res = json_decode($res,true);
  167. 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)){
  168. if($v->platform_name == 1){
  169. $data['orderNo'] = $res['data']['order_no'];
  170. $data['use_trx'] = 0;
  171. }elseif($v->platform_name == 2){
  172. $data['orderNo'] = $res['txid'];
  173. $data['use_trx'] = 0;
  174. }elseif(isset($res['data']['txid']) && $v->platform_name == 3){
  175. $data['orderNo'] = $res['data']['txid'];
  176. $data['use_trx'] = $res['data']['use_trx'];
  177. }elseif(isset($res['data']['orderId']) && $v->platform_name == 4){
  178. $data['orderNo'] = $res['data']['orderId'];
  179. $data['use_trx'] = $res['data']['orderMoney'];
  180. }elseif($v->platform_name == 5){
  181. $data['orderNo'] = $res['data']['orderId'];
  182. $data['use_trx'] = $res['data']['orderMoney'];
  183. }else{
  184. $data['orderNo'] = '未知'.time();
  185. $data['use_trx'] = 0;
  186. }
  187. $data['energy_platform_rid'] = $v->rid;
  188. $data['energy_platform_bot_rid'] = $pfbotdata['rid'];
  189. $data['platform_name'] = $v->platform_name;
  190. $data['platform_uid'] = $v->platform_uid;
  191. return ['code' => '200', 'msg' => '预支能量成功', 'data' => $data];
  192. }else{
  193. if($v->platform_name == 1){
  194. $msg = '失败,返回:'.$res['msg'];
  195. }elseif($v->platform_name == 2){
  196. $msg = '失败,返回:'.json_encode($res);
  197. }elseif($v->platform_name == 3){
  198. $msg = '失败,检查质押是否足够';
  199. }elseif($v->platform_name == 4){
  200. $msg = '失败,返回:'.json_encode($res);
  201. }elseif($v->platform_name == 5){
  202. $msg = '失败,返回:'.json_encode($res);
  203. }
  204. // return ['code' => '400', 'msg' => $msg];
  205. //能量平台下单失败,轮询下一个
  206. $errorMessage = $errorMessage."能量平台:".$v->rid.$msg;
  207. continue;
  208. }
  209. }
  210. }
  211. return ['code' => '400', 'msg' => '机器人所有能量平台失败:'.$errorMessage];
  212. }else{
  213. return ['code' => '400', 'msg' => '机器人无可用能量平台,请质押或者充值平台'];
  214. }
  215. }
  216. }