Config.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace app\admin\controller\routine;
  3. use app\common\controller\Backend;
  4. use app\admin\model\Config as ConfigModel;
  5. use think\db\exception\PDOException;
  6. use think\exception\ValidateException;
  7. use think\facade\Cache;
  8. use think\facade\Db;
  9. use Exception;
  10. use app\common\library\Email;
  11. use PHPMailer\PHPMailer\PHPMailer;
  12. use PHPMailer\PHPMailer\Exception as PHPMailerException;
  13. class Config extends Backend
  14. {
  15. protected $model = null;
  16. protected $noNeedLogin = ['index'];
  17. public function initialize()
  18. {
  19. parent::initialize();
  20. $this->model = new ConfigModel();
  21. }
  22. public function index()
  23. {
  24. $configGroup = get_sys_config('config_group');
  25. $config = $this->model->order('weigh desc')->select()->toArray();
  26. $list = [];
  27. foreach ($config as $item) {
  28. $item['title'] = __($item['title']);
  29. $list[$item['group']]['list'][] = $item;
  30. }
  31. foreach ($configGroup as $item) {
  32. $list[$item['key']]['name'] = $item['key'];
  33. $list[$item['key']]['title'] = __($item['value']);
  34. $newConfigGroup[$item['key']] = $list[$item['key']]['title'];
  35. }
  36. $this->success('', [
  37. 'list' => $list,
  38. 'remark' => get_route_remark(),
  39. 'configGroup' => $newConfigGroup ?? [],
  40. 'quickEntrance' => get_sys_config('config_quick_entrance'),
  41. ]);
  42. }
  43. /**
  44. * 编辑
  45. * @param null $id
  46. */
  47. public function edit($id = null)
  48. {
  49. $all = $this->model->select();
  50. foreach ($all as $item) {
  51. if ($item['type'] == 'editor') {
  52. $this->request->filter('trim,htmlspecialchars');
  53. break;
  54. }
  55. }
  56. if ($this->request->isPost()) {
  57. $this->modelValidate = false;
  58. $data = $this->request->post();
  59. if (!$data) {
  60. $this->error(__('Parameter %s can not be empty', ['']));
  61. }
  62. $data = $this->excludeFields($data);
  63. $configValue = [];
  64. foreach ($all as $item) {
  65. if (array_key_exists($item->name, $data)) {
  66. $configValue[] = [
  67. 'id' => $item->id,
  68. 'type' => $item->getData('type'),
  69. 'value' => $data[$item->name]
  70. ];
  71. }
  72. }
  73. $result = false;
  74. Db::startTrans();
  75. try {
  76. // 模型验证
  77. if ($this->modelValidate) {
  78. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  79. if (class_exists($validate)) {
  80. $validate = new $validate;
  81. if ($this->modelSceneValidate) $validate->scene('edit');
  82. $validate->check($data);
  83. }
  84. }
  85. $result = $this->model->saveAll($configValue);
  86. Cache::tag(ConfigModel::$cacheTag)->clear();
  87. Db::commit();
  88. } catch (ValidateException|Exception|PDOException $e) {
  89. Db::rollback();
  90. $this->error($e->getMessage());
  91. }
  92. if ($result !== false) {
  93. $this->success(__('The current page configuration item was updated successfully'));
  94. } else {
  95. $this->error(__('No rows updated'));
  96. }
  97. }
  98. }
  99. public function add()
  100. {
  101. if ($this->request->isPost()) {
  102. $data = $this->request->post();
  103. if (!$data) {
  104. $this->error(__('Parameter %s can not be empty', ['']));
  105. }
  106. $data = $this->excludeFields($data);
  107. $result = false;
  108. Db::startTrans();
  109. try {
  110. // 模型验证
  111. if ($this->modelValidate) {
  112. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  113. if (class_exists($validate)) {
  114. $validate = new $validate;
  115. if ($this->modelSceneValidate) $validate->scene('add');
  116. $validate->check($data);
  117. }
  118. }
  119. $result = $this->model->save($data);
  120. Cache::tag(ConfigModel::$cacheTag)->clear();
  121. Db::commit();
  122. } catch (ValidateException|Exception|PDOException $e) {
  123. Db::rollback();
  124. $this->error($e->getMessage());
  125. }
  126. if ($result !== false) {
  127. $this->success(__('Added successfully'));
  128. } else {
  129. $this->error(__('No rows were added'));
  130. }
  131. }
  132. $this->error(__('Parameter error'));
  133. }
  134. public function sendTestMail()
  135. {
  136. $data = $this->request->post();
  137. $mail = new Email();
  138. try {
  139. $mail->Host = $data['smtp_server'];
  140. $mail->SMTPAuth = true;
  141. $mail->Username = $data['smtp_user'];
  142. $mail->Password = $data['smtp_pass'];
  143. $mail->SMTPSecure = $data['smtp_verification'] == 'SSL' ? PHPMailer::ENCRYPTION_SMTPS : PHPMailer::ENCRYPTION_STARTTLS;
  144. $mail->Port = $data['smtp_port'];
  145. $mail->setFrom($data['smtp_sender_mail'], $data['smtp_user']);
  146. $mail->isSMTP();
  147. $mail->addAddress($data['testMail']);
  148. $mail->isHTML();
  149. $mail->setSubject(__('This is a test email') . '-' . get_sys_config('site_name'));
  150. $mail->Body = __('Congratulations, receiving this email means that your email service has been configured correctly');
  151. $mail->send();
  152. } catch (PHPMailerException $e) {
  153. $this->error($mail->ErrorInfo);
  154. }
  155. $this->success(__('Test mail sent successfully~'));
  156. }
  157. }