ExpireNoticeController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace backend\modules\admin\controllers;
  3. use Yii;
  4. use backend\controllers\AdminController;
  5. use backend\modules\admin\services\BackupsService;
  6. use backend\modules\admin\services\ExpireNoticeService;
  7. use common\models\common\Backups;
  8. use Exception;
  9. /**
  10. * 过期提醒
  11. * @package backend\modules\admin\controllers
  12. */
  13. class ExpireNoticeController extends AdminController
  14. {
  15. /**
  16. * 配置页面
  17. *
  18. * @return mixed
  19. */
  20. public function actionIndex()
  21. {
  22. $service = new ExpireNoticeService();
  23. if (Yii::$app->request->isAjax) {
  24. return $this->success('ok', $service->getConfig());
  25. }
  26. return $this->render($this->action->id, ['isInstallSalary' => (int)$service->isInstallSalary()]);
  27. }
  28. /**
  29. * 提交配置
  30. *
  31. * @return mixed
  32. */
  33. public function actionSubmit()
  34. {
  35. if (Yii::$app->request->isPost) {
  36. $params = Yii::$app->request->post();
  37. $res = Yii::$app->services->validate->validate($params, [
  38. [['certificate', 'server'], 'required'],
  39. // [['ssl_path'], 'safe'],
  40. ]);
  41. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  42. $service = new ExpireNoticeService();
  43. return $service->setConfig($params)
  44. ? $this->success('保存成功')
  45. : $this->error('保存失败');
  46. }
  47. }
  48. /**
  49. * 证书到期提醒
  50. *
  51. * @return mixed
  52. */
  53. public function actionCertificateNotice()
  54. {
  55. $service = new ExpireNoticeService();
  56. return $this->success('ok', $service->certificateNotice());
  57. }
  58. /**
  59. * 服务器到期提醒
  60. *
  61. * @return mixed
  62. */
  63. public function actionServerNotice()
  64. {
  65. $service = new ExpireNoticeService();
  66. return $this->success('ok', $service->serverNotice());
  67. }
  68. //备份管理
  69. public function actionBackups()
  70. {
  71. $request = Yii::$app->request;
  72. try {
  73. if ($request->isAjax && $request->isGet) {
  74. $get = $request->get();
  75. $res = Yii::$app->services->validate->validate($get, [
  76. [['page', 'limit'], 'integer'],
  77. [['datetime'], 'safe'],
  78. ]);
  79. if (!$res) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  80. $list = (new BackupsService())->getBackupsList($get);
  81. return $this->success('获取成功', $list);
  82. }
  83. } catch (\Exception $e) {
  84. $this->error($e->getMessage());
  85. }
  86. return $this->render($this->action->id);
  87. }
  88. //下载
  89. public function actionDown()
  90. {
  91. try{
  92. $download_id = Yii::$app->request->get('download_id');
  93. // ob_start();
  94. $download = Backups::find()->where(['id' => $download_id])->one();
  95. if (empty($download)) {
  96. $url = \Yii::$app->urlManager->createAbsoluteUrl('expire-notice/backups/index');
  97. return $this->redirect($url, 301);
  98. }
  99. // $filename= $download->name;
  100. // $path_info = pathinfo($download->url);
  101. // // $file = fopen($filename, 'r');
  102. // // $filestat = fstat($file);
  103. // $size=filesize($download->url);
  104. // header( "Content-type: application/octet-stream ");
  105. // header( "Accept-Ranges: bytes ");
  106. // header( "Content-Disposition: attachment; filename= {$filename}.".$path_info['extension']);
  107. // header( "Accept-Length: " .$size);
  108. $file_path = $download->url;
  109. $file_name = $download->name;
  110. // 打开文件
  111. $file_handle = fopen($file_path, 'rb');
  112. // 设置响应头信息
  113. header("Content-Type: application/octet-stream");
  114. header("Content-Disposition: attachment; filename={$file_name}");
  115. header('Content-Transfer-Encoding: binary');
  116. header('Content-Length: ' . filesize($file_path));
  117. // 输出文件内容
  118. while (!feof($file_handle)) {
  119. echo fread($file_handle, 1024);
  120. }
  121. // 关闭文件
  122. fclose($file_handle);
  123. exit;
  124. }catch (\Exception $e){
  125. $url = \Yii::$app->urlManager->createAbsoluteUrl('expire-notice/backups');
  126. return $this->redirect($url, 301);
  127. }
  128. }
  129. //操作删除
  130. public function actionDelete()
  131. {
  132. $request = Yii::$app->request;
  133. if ($request->isAjax && $request->isPost) {
  134. try {
  135. $post = $request->post();
  136. $res = Yii::$app->services->validate->validate($post, [
  137. [['id'], 'safe'],
  138. ]);
  139. if (!$res) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  140. $res = (new BackupsService())->deleteBackups($post);
  141. return $this->success('操作成功');
  142. } catch (\Exception $e) {
  143. return $this->error($e->getMessage());
  144. }
  145. }
  146. }
  147. //开始备份
  148. public function actionStartBackups()
  149. {
  150. $request = Yii::$app->request;
  151. if ($request->isAjax && $request->isPost) {
  152. try {
  153. $res = (new BackupsService())->startBackups();
  154. return $this->success('数据备份中,消耗时间较长,请稍后再刷新页面查看');
  155. } catch (\Exception $e) {
  156. return $this->error($e->getMessage());
  157. }
  158. }
  159. }
  160. }