User.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\admin\controller\user;
  3. use Exception;
  4. use ba\Random;
  5. use think\facade\Db;
  6. use app\common\controller\Backend;
  7. use app\admin\model\User as UserModel;
  8. use think\db\exception\PDOException;
  9. use think\exception\ValidateException;
  10. class User extends Backend
  11. {
  12. protected $model = null;
  13. protected $withJoinTable = ['group'];
  14. // 排除字段
  15. protected $preExcludeFields = ['lastlogintime', 'loginfailure', 'password', 'salt'];
  16. protected $quickSearchField = ['username', 'nickname', 'id'];
  17. public function initialize()
  18. {
  19. parent::initialize();
  20. $this->model = new UserModel();
  21. }
  22. /**
  23. * 查看
  24. */
  25. public function index()
  26. {
  27. $this->request->filter(['strip_tags', 'trim']);
  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. public function add()
  46. {
  47. if ($this->request->isPost()) {
  48. $data = $this->request->post();
  49. if (!$data) {
  50. $this->error(__('Parameter %s can not be empty', ['']));
  51. }
  52. $salt = Random::build('alnum', 16);
  53. $passwd = encrypt_password($data['password'], $salt);
  54. $data = $this->excludeFields($data);
  55. $result = false;
  56. Db::startTrans();
  57. try {
  58. $data['salt'] = $salt;
  59. $data['password'] = $passwd;
  60. // 模型验证
  61. if ($this->modelValidate) {
  62. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  63. if (class_exists($validate)) {
  64. $validate = new $validate;
  65. if ($this->modelSceneValidate) $validate->scene('add');
  66. $validate->check($data);
  67. }
  68. }
  69. $result = $this->model->save($data);
  70. Db::commit();
  71. } catch (ValidateException|Exception|PDOException $e) {
  72. Db::rollback();
  73. $this->error($e->getMessage());
  74. }
  75. if ($result !== false) {
  76. $this->success(__('Added successfully'));
  77. } else {
  78. $this->error(__('No rows were added'));
  79. }
  80. }
  81. $this->error(__('Parameter error'));
  82. }
  83. public function edit($id = null)
  84. {
  85. $row = $this->model->find($id);
  86. if (!$row) {
  87. $this->error(__('Record not found'));
  88. }
  89. if ($this->request->isPost()) {
  90. $password = $this->request->post('password', '');
  91. if ($password) {
  92. $this->model->resetPassword($id, $password);
  93. }
  94. parent::edit();
  95. }
  96. unset($row->salt);
  97. $row->password = '';
  98. $this->success('', [
  99. 'row' => $row
  100. ]);
  101. }
  102. /**
  103. * 重写select
  104. */
  105. public function select()
  106. {
  107. $this->request->filter(['strip_tags', 'trim']);
  108. list($where, $alias, $limit, $order) = $this->queryBuilder();
  109. $res = $this->model
  110. ->withJoin($this->withJoinTable, $this->withJoinType)
  111. ->alias($alias)
  112. ->where($where)
  113. ->order($order)
  114. ->paginate($limit);
  115. foreach ($res as $re) {
  116. $re->nickname_text = $re->username . '(ID:' . $re->id . ')';
  117. }
  118. $this->success('', [
  119. 'list' => $res->items(),
  120. 'total' => $res->total(),
  121. 'remark' => get_route_remark(),
  122. ]);
  123. }
  124. }