SystemConfigGroupController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\app\controller\system;
  8. use plugin\saiadmin\basic\BaseController;
  9. use plugin\saiadmin\app\logic\system\SystemConfigGroupLogic;
  10. use plugin\saiadmin\app\validate\system\SystemConfigGroupValidate;
  11. use plugin\saiadmin\utils\Arr;
  12. use support\Cache;
  13. use support\Request;
  14. use support\Response;
  15. use plugin\saiadmin\service\EmailService;
  16. use plugin\saiadmin\app\model\system\SystemMail;
  17. /**
  18. * 配置控制器
  19. */
  20. class SystemConfigGroupController extends BaseController
  21. {
  22. /**
  23. * 构造
  24. */
  25. public function __construct()
  26. {
  27. $this->logic = new SystemConfigGroupLogic();
  28. $this->validate = new SystemConfigGroupValidate;
  29. parent::__construct();
  30. }
  31. /**
  32. * 数据列表
  33. * @param Request $request
  34. * @return Response
  35. */
  36. public function index(Request $request) : Response
  37. {
  38. $where = $request->more([
  39. ['name', ''],
  40. ['code', ''],
  41. ]);
  42. $query = $this->logic->search($where);
  43. $data = $this->logic->getAll($query);
  44. return $this->success($data);
  45. }
  46. /**
  47. * 邮件测试
  48. * @param Request $request
  49. * @return Response
  50. */
  51. public function email(Request $request) : Response
  52. {
  53. $email = $request->input('email', '');
  54. if (empty($email)) {
  55. return $this->fail('请输入邮箱');
  56. }
  57. $subject = "测试邮件";
  58. $code = "9527";
  59. $content = "<h1>验证码:{code}</h1><p>这是一封测试邮件,请忽略</p>";
  60. $template = [
  61. 'code' => $code
  62. ];
  63. $config = EmailService::getConfig();
  64. $model = SystemMail::create([
  65. 'gateway' => Arr::getConfigValue($config,'Host'),
  66. 'from' => Arr::getConfigValue($config,'From'),
  67. 'email' => $email,
  68. 'code' => $code,
  69. ]);
  70. try {
  71. $result = EmailService::sendByTemplate($email, $subject, $content, $template);
  72. if (!empty($result)) {
  73. $model->status = 'failure';
  74. $model->response = $result;
  75. $model->save();
  76. return $this->fail('发送失败,请查看日志');
  77. } else {
  78. $model->status = 'success';
  79. $model->save();
  80. return $this->success([], '发送成功');
  81. }
  82. } catch (\Exception $e) {
  83. $model->status = 'failure';
  84. $model->response = $e->getMessage();
  85. $model->save();
  86. return $this->fail($e->getMessage());
  87. }
  88. }
  89. /**
  90. * 数据改变后执行
  91. * @param $type
  92. * @param $args
  93. * @return void
  94. */
  95. protected function afterChange($type, $args): void
  96. {
  97. if (in_array($type, ['save', 'update'])) {
  98. Cache::delete('cfg_' . request()->input('code'));
  99. }
  100. }
  101. }