User.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. * @var string
  28. */
  29. public $table = 'SystemUser';
  30. /**
  31. * 用户列表
  32. */
  33. public function index() {
  34. // 设置页面标题
  35. $this->title = '系统用户管理';
  36. // 获取到所有GET参数
  37. $get = $this->request->get();
  38. // 实例Query对象
  39. $db = Db::name($this->table)->where('is_deleted', '0');
  40. // 应用搜索条件
  41. foreach (['username', 'phone'] as $key) {
  42. if (isset($get[$key]) && $get[$key] !== '') {
  43. $db->where($key, 'like', "%{$get[$key]}%");
  44. }
  45. }
  46. // 实例化并显示
  47. return parent::_list($db);
  48. }
  49. /**
  50. * 授权管理
  51. * @return array|string
  52. */
  53. public function auth() {
  54. return $this->_form($this->table, 'auth');
  55. }
  56. /**
  57. * 用户添加
  58. */
  59. public function add() {
  60. return $this->_form($this->table, 'form');
  61. }
  62. /**
  63. * 用户编辑
  64. */
  65. public function edit() {
  66. return $this->_form($this->table, 'form');
  67. }
  68. /**
  69. * 用户密码修改
  70. */
  71. public function pass() {
  72. if (in_array('10000', explode(',', $this->request->post('id')))) {
  73. $this->error('系统超级账号禁止操作!');
  74. }
  75. if ($this->request->isGet()) {
  76. $this->assign('verify', false);
  77. return $this->_form($this->table, 'pass');
  78. }
  79. $data = $this->request->post();
  80. if ($data['password'] !== $data['repassword']) {
  81. $this->error('两次输入的密码不一致!');
  82. }
  83. if (DataService::save($this->table, ['id' => $data['id'], 'password' => md5($data['password'])], 'id')) {
  84. $this->success('密码修改成功,下次请使用新密码登录!', '');
  85. }
  86. $this->error('密码修改失败,请稍候再试!');
  87. }
  88. /**
  89. * 表单数据默认处理
  90. * @param array $data
  91. */
  92. public function _form_filter(&$data) {
  93. if ($this->request->isPost()) {
  94. if (isset($data['authorize']) && is_array($data['authorize'])) {
  95. $data['authorize'] = join(',', $data['authorize']);
  96. }
  97. if (isset($data['id'])) {
  98. unset($data['username']);
  99. } elseif (Db::name($this->table)->where('username', $data['username'])->find()) {
  100. $this->error('用户账号已经存在,请使用其它账号!');
  101. }
  102. } else {
  103. $data['authorize'] = explode(',', isset($data['authorize']) ? $data['authorize'] : '');
  104. $this->assign('authorizes', Db::name('SystemAuth')->select());
  105. }
  106. }
  107. /**
  108. * 删除用户
  109. */
  110. public function del() {
  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. if (in_array('10000', explode(',', $this->request->post('id')))) {
  124. $this->error('系统超级账号禁止操作!');
  125. }
  126. if (DataService::update($this->table)) {
  127. $this->success("用户禁用成功!", '');
  128. }
  129. $this->error("用户禁用失败,请稍候再试!");
  130. }
  131. /**
  132. * 用户禁用
  133. */
  134. public function resume() {
  135. if (DataService::update($this->table)) {
  136. $this->success("用户启用成功!", '');
  137. }
  138. $this->error("用户启用失败,请稍候再试!");
  139. }
  140. }