User.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | framework
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2018 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://framework.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/framework
  12. // +----------------------------------------------------------------------
  13. namespace app\admin\controller;
  14. use library\Controller;
  15. use library\tools\Data;
  16. use think\Db;
  17. /**
  18. * 系统用户管理
  19. * Class User
  20. * @package app\admin\controller
  21. * @author Anyon <zoujingli@qq.com>
  22. * @date 2017/02/15 18:12
  23. */
  24. class User extends Controller
  25. {
  26. /**
  27. * 指定当前数据表
  28. * @var string
  29. */
  30. public $table = 'SystemUser';
  31. /**
  32. * 系统用户管理
  33. * @throws \think\Exception
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. * @throws \think\exception\DbException
  37. * @throws \think\exception\PDOException
  38. */
  39. public function index()
  40. {
  41. $this->title = '系统用户管理';
  42. $where = ['is_deleted' => '0'];
  43. $this->_query($this->table)->like('username,phone,mail')->dateBetween('login_at')->equal('status')->where($where)->page();
  44. }
  45. /**
  46. * 用户授权管理
  47. * @return mixed
  48. */
  49. public function auth()
  50. {
  51. $this->applyCsrfToken();
  52. $this->_form($this->table, 'auth');
  53. }
  54. /**
  55. * 添加系统用户
  56. * @return mixed
  57. */
  58. public function add()
  59. {
  60. $this->applyCsrfToken();
  61. $this->_form($this->table, 'form');
  62. }
  63. /**
  64. * 编辑系统用户
  65. * @return mixed
  66. */
  67. public function edit()
  68. {
  69. $this->applyCsrfToken();
  70. $this->_form($this->table, 'form');
  71. }
  72. /**
  73. * 修改用户密码
  74. * @return mixed
  75. * @throws \think\Exception
  76. * @throws \think\exception\PDOException
  77. */
  78. public function pass()
  79. {
  80. $this->applyCsrfToken();
  81. if ($this->request->isGet()) {
  82. $this->verify = false;
  83. $this->_form($this->table, 'pass');
  84. } else {
  85. $post = $this->request->post();
  86. if ($post['password'] !== $post['repassword']) {
  87. $this->error('两次输入的密码不一致!');
  88. }
  89. $result = \app\admin\service\Auth::checkPassword($post['password']);
  90. if (empty($result['code'])) $this->error($result['msg']);
  91. $data = ['id' => $post['id'], 'password' => md5($post['password'])];
  92. if (Data::save($this->table, $data, 'id')) {
  93. $this->success('密码修改成功,下次请使用新密码登录!', '');
  94. } else {
  95. $this->error('密码修改失败,请稍候再试!');
  96. }
  97. }
  98. }
  99. /**
  100. * 表单数据处理
  101. * @param array $data
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\ModelNotFoundException
  104. * @throws \think\exception\DbException
  105. */
  106. public function _form_filter(&$data)
  107. {
  108. if ($this->request->isPost()) {
  109. $data['authorize'] = (isset($data['authorize']) && is_array($data['authorize'])) ? join(',', $data['authorize']) : '';
  110. if (isset($data['id'])) unset($data['username']);
  111. elseif (Db::name($this->table)->where(['username' => $data['username']])->count() > 0) {
  112. $this->error('用户账号已经存在,请使用其它账号!');
  113. }
  114. } else {
  115. $data['authorize'] = explode(',', isset($data['authorize']) ? $data['authorize'] : '');
  116. $this->assign('authorizes', Db::name('SystemAuth')->where(['status' => '1'])->select());
  117. }
  118. }
  119. /**
  120. * 删除系统用户
  121. */
  122. public function del()
  123. {
  124. if (in_array('10000', explode(',', $this->request->post('id')))) {
  125. $this->error('系统超级账号禁止删除!');
  126. }
  127. $this->applyCsrfToken();
  128. $this->_delete($this->table);
  129. }
  130. /**
  131. * 禁用系统用户
  132. */
  133. public function forbid()
  134. {
  135. if (in_array('10000', explode(',', $this->request->post('id')))) {
  136. $this->error('系统超级账号禁止操作!');
  137. }
  138. $this->applyCsrfToken();
  139. $this->_save($this->table, ['status' => '0']);
  140. }
  141. /**
  142. * 启用系统用户
  143. */
  144. public function resume()
  145. {
  146. $this->applyCsrfToken();
  147. $this->_save($this->table, ['status' => '1']);
  148. }
  149. }