EnergyPlatformController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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\Energy\EnergyPlatform;
  8. use App\Models\Telegram\TelegramBot;
  9. use App\Http\Services\RsaServices;
  10. class EnergyPlatformController extends Controller
  11. {
  12. public $PlatformName = ['1' => 'Neee.cc','2' => 'RentEnergysBot','3' => '自己质押代理','4' => 'trongas.io','5' => '机器人开发代理','6' => 'mefree.net'];
  13. public $Status = ['开启','关闭'];
  14. public $PollGroup = ['A' => 'A组','B' => 'B组','C' => 'C组','D' => 'D组','E' => 'E组','F' => 'F组','G' => 'G组'];
  15. public function index(Request $request)
  16. {
  17. $PlatformName = $this->PlatformName;
  18. $Status = $this->Status;
  19. $PollGroup = $this->PollGroup;
  20. $botData = TelegramBot::pluck('bot_username','rid');
  21. return view('admin.energy.platform.index',compact("PlatformName","Status","PollGroup","botData"));
  22. }
  23. //列表
  24. public function getData(Request $request)
  25. {
  26. $model = EnergyPlatform::from('energy_platform as a')
  27. ->join('telegram_bot as b','a.tg_notice_bot_rid','b.rid')
  28. ->where(function($query) use ($request){
  29. if ($request->platform_uid != '') {
  30. $query->where('a.platform_uid', 'like' ,"%" . $request->platform_uid ."%");
  31. }
  32. });
  33. $count = $model->count();
  34. $limit = $request->limit ?? 15;
  35. $offset = $request->page ? ($request->page - 1) * $limit : 0;
  36. $data = $model->limit($limit)->offset($offset)->select('a.*','b.bot_token','b.bot_firstname','b.bot_username')->orderBy('a.rid','desc')->get();
  37. $PlatformName = $this->PlatformName;
  38. $PollGroup = $this->PollGroup;
  39. $rsa_services = new RsaServices();
  40. $data = $data->map(function($query) use ($rsa_services,$PlatformName,$PollGroup){
  41. $platform_apikey = $rsa_services->privateDecrypt($query->platform_apikey); //解密
  42. $query->platform_apikey = mb_substr($platform_apikey, 0,4).'****'.mb_substr($platform_apikey, -4,4);
  43. $query->platform_name_val = $PlatformName[$query->platform_name];
  44. $query->poll_group_val = $PollGroup[$query->poll_group];
  45. return $query;
  46. });
  47. return ['code' => '0', 'data' => $data, 'count' => $count];
  48. }
  49. //添加
  50. public function add(Request $request)
  51. {
  52. $res = EnergyPlatform::create([
  53. 'poll_group' => $request->poll_group,
  54. 'platform_name' => $request->platform_name,
  55. 'platform_uid' => $request->platform_uid,
  56. 'alert_platform_balance' => $request->alert_platform_balance ?? 0,
  57. 'tg_notice_obj' => $request->tg_notice_obj ?? '',
  58. 'tg_notice_bot_rid' => $request->tg_notice_bot_rid ?? '',
  59. 'seq_sn' => $request->seq_sn ?? 0,
  60. 'comments' => $request->comments ?? '',
  61. 'create_time' => nowDate()
  62. ]);
  63. return $res ? $this->responseData(200, '添加成功') : $this->responseData(400, '添加失败');
  64. }
  65. //删除
  66. public function delete(Request $request)
  67. {
  68. $res = EnergyPlatform::where('rid', $request->rid)->delete();
  69. return $res ? $this->responseData(200, '删除成功') : $this->responseData(400, '删除失败');
  70. }
  71. //编辑
  72. public function update(Request $request)
  73. {
  74. DB::beginTransaction();
  75. try {
  76. $data = EnergyPlatform::where('rid', $request->rid)->first();
  77. $data->poll_group = $request->poll_group;
  78. $data->platform_name = $request->platform_name;
  79. $data->platform_uid = $request->platform_uid;
  80. $data->alert_platform_balance = $request->alert_platform_balance ?? 0;
  81. $data->tg_notice_obj = $request->tg_notice_obj ?? '';
  82. $data->tg_notice_bot_rid = $request->tg_notice_bot_rid ?? '';
  83. $data->seq_sn = $request->seq_sn ?? 0;
  84. $data->comments = $request->comments ?? '';
  85. $data->update_time = nowDate();
  86. $data->save();
  87. DB::commit();
  88. return $this->responseData(200, '更新成功');
  89. } catch (\Exception $e) {
  90. DB::rollBack();
  91. return $this->responseData(400, '更新失败'.$e->getMessage());
  92. }
  93. }
  94. //编辑apikey
  95. public function updateapikey(Request $request)
  96. {
  97. $rsa_services = new RsaServices();
  98. $platform_apikey = $rsa_services->publicEncrypt($request->platform_apikey);
  99. DB::beginTransaction();
  100. try {
  101. $data = EnergyPlatform::where('rid', $request->rid)->first();
  102. $model = EnergyPlatform::where('rid', $request->rid)
  103. ->secondData($request->platform_apikey,$data->platform_uid);
  104. $data->platform_apikey = $platform_apikey;
  105. $data->permission_id = $request->permission_id ?? 0;
  106. $data->save();
  107. DB::commit();
  108. return $this->responseData(200, '更新成功');
  109. } catch (\Exception $e) {
  110. DB::rollBack();
  111. return $this->responseData(400, '更新失败'.$e->getMessage());
  112. }
  113. }
  114. //编辑状态
  115. public function change_status(Request $request)
  116. {
  117. DB::beginTransaction();
  118. try {
  119. $data = EnergyPlatform::where('rid', $request->rid)->first();
  120. $data->status = $request->status == 1 ? 0 : 1;
  121. $data->save();
  122. DB::commit();
  123. return $this->responseData(200, '更新成功');
  124. } catch (\Exception $e) {
  125. DB::rollBack();
  126. return $this->responseData(400, '更新失败'.$e->getMessage());
  127. }
  128. }
  129. }