Purchaseemail.php 2.8 KB

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