User.php 4.1 KB

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