User.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace app\admin\controller\user;
  3. use Throwable;
  4. use app\common\controller\Backend;
  5. use app\admin\model\User as UserModel;
  6. class User extends Backend
  7. {
  8. /**
  9. * @var object
  10. * @phpstan-var UserModel
  11. */
  12. protected object $model;
  13. protected array $withJoinTable = ['userGroup'];
  14. // 排除字段
  15. protected string|array $preExcludeFields = ['last_login_time', 'login_failure', 'password', 'salt'];
  16. protected string|array $quickSearchField = ['username', 'nickname', 'id'];
  17. public function initialize(): void
  18. {
  19. parent::initialize();
  20. $this->model = new UserModel();
  21. }
  22. /**
  23. * 查看
  24. * @throws Throwable
  25. */
  26. public function index(): void
  27. {
  28. if ($this->request->param('select')) {
  29. $this->select();
  30. }
  31. list($where, $alias, $limit, $order) = $this->queryBuilder();
  32. $res = $this->model
  33. ->withoutField('password,salt')
  34. ->withJoin($this->withJoinTable, $this->withJoinType)
  35. ->alias($alias)
  36. ->where($where)
  37. ->order($order)
  38. ->paginate($limit);
  39. $this->success('', [
  40. 'list' => $res->items(),
  41. 'total' => $res->total(),
  42. 'remark' => get_route_remark(),
  43. ]);
  44. }
  45. /**
  46. * 添加
  47. * @throws Throwable
  48. */
  49. public function add(): void
  50. {
  51. if ($this->request->isPost()) {
  52. $data = $this->request->post();
  53. if (!$data) {
  54. $this->error(__('Parameter %s can not be empty', ['']));
  55. }
  56. $result = false;
  57. $passwd = $data['password']; // 密码将被排除不直接入库
  58. $data = $this->excludeFields($data);
  59. $this->model->startTrans();
  60. try {
  61. // 模型验证
  62. if ($this->modelValidate) {
  63. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  64. if (class_exists($validate)) {
  65. $validate = new $validate();
  66. if ($this->modelSceneValidate) $validate->scene('add');
  67. $validate->check($data);
  68. }
  69. }
  70. $result = $this->model->save($data);
  71. $this->model->commit();
  72. if (!empty($passwd)) {
  73. $this->model->resetPassword($this->model->id, $passwd);
  74. }
  75. } catch (Throwable $e) {
  76. $this->model->rollback();
  77. $this->error($e->getMessage());
  78. }
  79. if ($result !== false) {
  80. $this->success(__('Added successfully'));
  81. } else {
  82. $this->error(__('No rows were added'));
  83. }
  84. }
  85. $this->error(__('Parameter error'));
  86. }
  87. /**
  88. * 编辑
  89. * @throws Throwable
  90. */
  91. public function edit(): void
  92. {
  93. $pk = $this->model->getPk();
  94. $id = $this->request->param($pk);
  95. $row = $this->model->find($id);
  96. if (!$row) {
  97. $this->error(__('Record not found'));
  98. }
  99. if ($this->request->isPost()) {
  100. $password = $this->request->post('password', '');
  101. if ($password) {
  102. $this->model->resetPassword($id, $password);
  103. }
  104. parent::edit();
  105. }
  106. unset($row->salt);
  107. $row->password = '';
  108. $this->success('', [
  109. 'row' => $row
  110. ]);
  111. }
  112. /**
  113. * 重写select
  114. * @throws Throwable
  115. */
  116. public function select(): void
  117. {
  118. list($where, $alias, $limit, $order) = $this->queryBuilder();
  119. $res = $this->model
  120. ->withoutField('password,salt')
  121. ->withJoin($this->withJoinTable, $this->withJoinType)
  122. ->alias($alias)
  123. ->where($where)
  124. ->order($order)
  125. ->paginate($limit);
  126. foreach ($res as $re) {
  127. $re->nickname_text = $re->username . '(ID:' . $re->id . ')';
  128. }
  129. $this->success('', [
  130. 'list' => $res->items(),
  131. 'total' => $res->total(),
  132. 'remark' => get_route_remark(),
  133. ]);
  134. }
  135. }