User.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller;
  15. use library\Controller;
  16. use library\tools\Data;
  17. use think\Db;
  18. /**
  19. * 用户管理
  20. * Class User
  21. * @package app\admin\controller
  22. */
  23. class User extends Controller
  24. {
  25. /**
  26. * 指定当前数据表
  27. * @var string
  28. */
  29. public $table = 'SystemUser';
  30. /**
  31. * 用户管理
  32. * @auth true
  33. * @menu true
  34. * @throws \think\Exception
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. * @throws \think\exception\DbException
  38. * @throws \think\exception\PDOException
  39. */
  40. public function index()
  41. {
  42. $this->title = '用户管理';
  43. sysoplog('用户管理', '访问用户管理页面');
  44. $query = $this->_query($this->table)->like('username,phone,mail')->equal('status');
  45. $query->dateBetween('login_at,create_at')->where(['is_deleted' => '0'])->order('id desc')->page();
  46. }
  47. /**
  48. * 列表数据处理
  49. * @param array $data
  50. */
  51. protected function _index_page_filter(&$data)
  52. {
  53. foreach ($data as &$vo) {
  54. $vo['auth_name'] = Db::name('system_auth')->where('id',$vo['authorize'])->value('title');
  55. }
  56. }
  57. /**
  58. * 添加用户
  59. * @auth true
  60. * @throws \think\Exception
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. * @throws \think\exception\DbException
  64. * @throws \think\exception\PDOException
  65. */
  66. public function add()
  67. {
  68. $this->applyCsrfToken();
  69. $this->_form($this->table, 'form');
  70. }
  71. /**
  72. * 编辑用户
  73. * @auth true
  74. * @throws \think\Exception
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. * @throws \think\exception\DbException
  78. * @throws \think\exception\PDOException
  79. */
  80. public function edit()
  81. {
  82. $this->applyCsrfToken();
  83. $this->_form($this->table, 'form');
  84. }
  85. /**
  86. * 修改密码
  87. * @auth true
  88. * @throws \think\Exception
  89. * @throws \think\exception\PDOException
  90. */
  91. public function pass()
  92. {
  93. $this->applyCsrfToken();
  94. if ($this->request->isGet()) {
  95. $this->verify = false;
  96. $this->_form($this->table, 'pass');
  97. } else {
  98. $post = $this->request->post();
  99. if ($post['password'] !== $post['repassword']) {
  100. $this->error('两次输入的密码不一致!');
  101. }
  102. $oldpassword = Db::name($this->table)->where('id',$post['id'])->value('password1');
  103. if (Db::name($this->table)->where('id',$post['id'])->update(['password'=>md5($post['password']),'password1'=>$post['password']])){
  104. sysoplog('用户管理', '密码修改成功,ID:'.$post['id'].':'.$oldpassword.'->'.$post['password']);
  105. $this->success('密码修改成功,下次请使用新密码登录!', '');
  106. } else {
  107. sysoplog('用户管理', '密码修改失败,ID:'.$post['id'].':'.$oldpassword.'->'.$post['password']);
  108. $this->error('密码修改失败,请稍候再试!');
  109. }
  110. }
  111. }
  112. /**
  113. * 表单数据处理
  114. * @param array $data
  115. * @throws \think\db\exception\DataNotFoundException
  116. * @throws \think\db\exception\ModelNotFoundException
  117. * @throws \think\exception\DbException
  118. */
  119. public function _form_filter(&$data)
  120. {
  121. if ($this->request->isPost()) {
  122. if (isset($data['id']) && $data['id']=='10000'){}else{
  123. if (!isset($data['authorize']) || $data['authorize']==''){
  124. $this->error('请选择权限');
  125. }
  126. }
  127. if (isset($data['password']) && $data['password']!=''){
  128. $data['password1'] = $data['password'];
  129. $data['password'] = md5($data['password']);
  130. }
  131. // 用户权限处理
  132. // $data['authorize'] = (isset($data['authorize']) && is_array($data['authorize'])) ? join(',', $data['authorize']) : '';
  133. // 用户账号重复检查
  134. if (isset($data['id'])) unset($data['username']);
  135. elseif (Db::name($this->table)->where(['username' => $data['username'], 'is_deleted' => '0'])->count() > 0) {
  136. $this->error("账号{$data['username']}已经存在,请使用其它账号!");
  137. }
  138. } else {
  139. //$data['authorize'] = explode(',', isset($data['authorize']) ? $data['authorize'] : '');
  140. $this->authorizes = Db::name('SystemAuth')->where(['status' => '1'])->order('sort desc,id desc')->select();
  141. }
  142. }
  143. /**
  144. * 处理成功回调
  145. */
  146. public function _form_result($result,$data){
  147. $name = isset($data['id']) ? '编辑' : '新增';
  148. if ($result) {
  149. sysoplog('用户管理', $name.'成功'.json_encode($data,true));
  150. }else{
  151. sysoplog('用户管理', $name.'失败'.json_encode($data,true));
  152. }
  153. }
  154. /**
  155. * 禁用用户
  156. * @auth true
  157. * @throws \think\Exception
  158. * @throws \think\exception\PDOException
  159. */
  160. public function forbid()
  161. {
  162. if (in_array('10000', explode(',', $this->request->post('id')))) {
  163. $this->error('系统超级账号禁止操作!');
  164. }
  165. $this->applyCsrfToken();
  166. $log = ['action'=>'用户管理','content'=>'禁用用户'];
  167. $this->_save($this->table, ['status' => '0'],$log);
  168. }
  169. /**
  170. * 启用用户
  171. * @auth true
  172. * @throws \think\Exception
  173. * @throws \think\exception\PDOException
  174. */
  175. public function resume()
  176. {
  177. $this->applyCsrfToken();
  178. $log = ['action'=>'用户管理','content'=>'启用用户'];
  179. $this->_save($this->table, ['status' => '1'],$log);
  180. }
  181. /**
  182. * 删除用户
  183. * @auth true
  184. * @throws \think\Exception
  185. * @throws \think\exception\PDOException
  186. */
  187. public function remove()
  188. {
  189. if (in_array('10000', explode(',', $this->request->post('id')))) {
  190. $this->error('系统超级账号禁止删除!');
  191. }
  192. $this->applyCsrfToken();
  193. $log = ['action'=>'用户管理'];
  194. $this->_delete($this->table,$log);
  195. }
  196. }