ConfigController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace App\Http\Controllers\Admin\Setting;
  3. use Illuminate\Http\Request;
  4. use App\Services\AipHttpClient;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\System\SysConfig;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Storage;
  10. class ConfigController extends Controller
  11. {
  12. public function index()
  13. {
  14. $data = SysConfig::get();
  15. return view('admin.setting.config.index', compact('data'));
  16. }
  17. public function update(Request $request)
  18. {
  19. $data = $request->all();
  20. foreach ($data as $k => $v) {
  21. if($v == ''){
  22. return $this->responseData(400, '请把内容全部填写');
  23. }
  24. }
  25. DB::beginTransaction();
  26. try{
  27. $admin_id = Auth::guard('admin')->id();
  28. $time = nowDate();
  29. foreach ($data as $k => $v) {
  30. if(is_array($v)){
  31. $v = json_encode($v);
  32. }
  33. SysConfig::where('config_key',$k)->update([
  34. 'config_val'=> $v
  35. ]);
  36. }
  37. DB::commit();
  38. return $this->responseData(200, 'success');
  39. } catch (\Exception $e) {
  40. DB::rollBack();
  41. return $this->responseData(400, $e->getMessage());
  42. }
  43. }
  44. public function uploadfile($file, $disk)
  45. {
  46. if (!$file->isValid()) {
  47. return $this->responseData(400, '文件不合法');
  48. }
  49. //原文件名
  50. $originalName = $file->getClientOriginalName();
  51. //扩展名
  52. $ext = $file->getClientOriginalExtension();
  53. //MimeType
  54. $type = $file->getClientMimeType();
  55. //临时绝对路径
  56. $realPath = $file->getRealPath();
  57. $filename = date('Ymd') . '/' . date('YmdHis') . rand(100, 1000) . '.' . $ext;
  58. $res = Storage::disk($disk)->put($filename, file_get_contents($realPath));
  59. $url = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http";
  60. $url .= "://" . $_SERVER['HTTP_HOST'];
  61. return $res ? $this->responseData(200, 'success', ['url' => $url . '/uploads/' . $disk . '/' . $filename]) : $this->responseData(400, 'error');
  62. }
  63. public function clearjobcache(Request $request)
  64. {
  65. $data = SysConfig::where('config_key','job_url')->first();
  66. if(empty($data)){
  67. return $this->responseData(400, '请先配置任务域名url.');
  68. }
  69. if(empty($data->config_val->url) || $data->config_val->url == '' || $data->config_val->url == null){
  70. return $this->responseData(400, '请先配置任务域名url');
  71. }
  72. $url = $data->config_val->url.'/api/config/clear_timing';
  73. $AipHttpClient = new AipHttpClient();
  74. $res = $AipHttpClient->postnew($url);
  75. if(empty($res)){
  76. return $this->responseData(400, '检查任务是否已启动1');
  77. }else{
  78. $res = json_decode($res,true);
  79. if(empty($res['code'])){
  80. return $this->responseData(400, '检查任务是否已启动2');
  81. }else{
  82. if($res['code'] == 200){
  83. return $this->responseData(200, 'success');
  84. }else{
  85. return $this->responseData(400, '检查任务是否已启动3');
  86. }
  87. }
  88. }
  89. }
  90. public function checkjob(Request $request)
  91. {
  92. $data = SysConfig::where('config_key','job_url')->first();
  93. if(empty($data)){
  94. return $this->responseData(400, '请先配置任务域名url.');
  95. }
  96. if(empty($data->config_val->url) || $data->config_val->url == '' || $data->config_val->url == null){
  97. return $this->responseData(400, '请先配置任务域名url');
  98. }
  99. $url = $data->config_val->url.'/api/config/check_status';
  100. $AipHttpClient = new AipHttpClient();
  101. $res = $AipHttpClient->postnew($url);
  102. if(empty($res)){
  103. return $this->responseData(400, '已停止1');
  104. }else{
  105. $res = json_decode($res,true);
  106. if(empty($res['code'])){
  107. return $this->responseData(400, '已停止2');
  108. }else{
  109. if($res['code'] == 200){
  110. return $this->responseData(200, '运行正常');
  111. }else{
  112. return $this->responseData(400, '已停止3');
  113. }
  114. }
  115. }
  116. }
  117. }