Purchaseemail.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. use Exception;
  8. /**
  9. * 外发发件邮箱配置(仅维护编辑,不增删)
  10. *
  11. * @icon fa fa-envelope
  12. */
  13. class Purchaseemail extends Backend
  14. {
  15. /** @var \app\admin\model\Purchaseemail */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\Purchaseemail;
  21. }
  22. public function index()
  23. {
  24. $this->relationSearch = false;
  25. $this->request->filter(['strip_tags', 'trim']);
  26. if ($this->request->isAjax()) {
  27. if ($this->request->request('keyField')) {
  28. return $this->selectpage();
  29. }
  30. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  31. $list = $this->model
  32. ->where($where)
  33. ->order($sort, $order)
  34. ->paginate($limit);
  35. foreach ($list as $row) {
  36. $row->visible(['id', 'email_addr', 'email_pass', 'createtime', 'updatetime']);
  37. }
  38. return json(['total' => $list->total(), 'rows' => $list->items()]);
  39. }
  40. return $this->view->fetch();
  41. }
  42. /** 发件配置固定一条,不提供新增 */
  43. public function add()
  44. {
  45. $this->error('请使用「编辑」修改发件邮箱配置');
  46. }
  47. /** 不提供删除 */
  48. public function del($ids = null)
  49. {
  50. $this->error('发件邮箱配置不可删除,请使用编辑修改');
  51. }
  52. public function edit($ids = null)
  53. {
  54. $row = $this->model->get($ids);
  55. if (!$row) {
  56. $this->error(__('No Results were found'));
  57. }
  58. if (false === $this->request->isPost()) {
  59. $this->view->assign('row', $row);
  60. return $this->view->fetch();
  61. }
  62. $params = $this->request->post('row/a');
  63. if (empty($params)) {
  64. $this->error(__('Parameter %s can not be empty', ''));
  65. }
  66. $params = $this->preExcludeFields($params);
  67. $addr = trim((string)($params['email_addr'] ?? ''));
  68. if ($addr === '' || !filter_var($addr, FILTER_VALIDATE_EMAIL)) {
  69. $this->error('请填写有效的发件邮箱');
  70. }
  71. $pass = trim((string)($params['email_pass'] ?? ''));
  72. if ($pass === '') {
  73. $this->error('请填写邮箱授权码');
  74. }
  75. $save = [
  76. 'email_addr' => $addr,
  77. 'email_pass' => $pass,
  78. ];
  79. try {
  80. $row->save($save);
  81. } catch (ValidateException|PDOException|Exception $e) {
  82. $this->error($e->getMessage());
  83. }
  84. $this->success();
  85. }
  86. }