User.php 4.6 KB

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