User.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 管理员
  7. */
  8. namespace app\system\controller\admin;
  9. use app\common\controller\Admin;
  10. use app\common\model\SystemAdmin;
  11. use app\common\event\Admin as AdminUser;
  12. use think\facade\Request;
  13. class User extends Admin{
  14. protected $login;
  15. public function initialize() {
  16. parent::initialize();
  17. $this->assign('pathMaps',[['name'=>'管理员','url'=>url("system/admin.user/index")]]);
  18. $this->login = AdminUser::getLoginSession();
  19. }
  20. /**
  21. * 列表
  22. * @access public
  23. */
  24. public function index(){
  25. $view['list'] = SystemAdmin::order('id desc')->paginate(10,true);
  26. return view()->assign($view);
  27. }
  28. /**
  29. * 添加
  30. * @access public
  31. */
  32. public function add(){
  33. if(request()->isAjax()){
  34. $data = [
  35. 'token' => $this->request->param('__token__/s'),
  36. 'username' => $this->request->param('username/s'),
  37. 'password' => $this->request->param('password/s'),
  38. 'password_confirm' => $this->request->param('repassword/s'),
  39. 'about' => $this->request->param('about/s'),
  40. ];
  41. $validate = $this->validate($data,'Admin.add');
  42. if(true !== $validate){
  43. return json(['code'=>1,'msg'=>$validate]);
  44. }
  45. $result = SystemAdmin::updateUser($data);
  46. if(!$result){
  47. return json(['code'=>1,'msg'=>'操作失败']);
  48. }else{
  49. return json(['code'=>200,'msg'=>'操作成功','url'=>url('system/admin.user/index')]);
  50. }
  51. }else{
  52. return view();
  53. }
  54. }
  55. /**
  56. * 编辑用户
  57. * @access public
  58. */
  59. public function edit(){
  60. if(request()->isAjax()){
  61. $data = [
  62. 'id' => $this->request->param('id/d'),
  63. 'username' => $this->request->param('username/s'),
  64. 'password' => $this->request->param('password/s'),
  65. 'password_confirm' => $this->request->param('repassword/s'),
  66. 'about' => $this->request->param('about/s'),
  67. ];
  68. $validate = $this->validate($data,'Admin.edit');
  69. if(true !== $validate){
  70. return json(['code'=>0,'msg'=>$validate]);
  71. }
  72. $result = SystemAdmin::updateUser($data);
  73. if(!$result){
  74. return json(['code'=>0,'msg'=>'操作失败']);
  75. }else{
  76. return json(['code'=>200,'msg'=>'操作成功','url'=>url('system/admin.user/index')]);
  77. }
  78. }else{
  79. $id = Request::param('id/d');
  80. $view['info'] = SystemAdmin::where(['id' => $id])->find();;
  81. if(empty($view['info'])){
  82. return $this->error("404 NOT FOUND");
  83. }
  84. return view()->assign($view);
  85. }
  86. }
  87. /**
  88. * [删除]
  89. * @access public
  90. * @return bool
  91. */
  92. public function delete(){
  93. $id = $this->request->param('id/d');
  94. if($id == $this->login['admin_id']){
  95. return json(['code' => 0,'msg' => lang('lock_user')]);
  96. }
  97. $result = SystemAdmin::destroy($id);
  98. if(!$result){
  99. return json(['code' => 0,'msg'=>'操作失败']);
  100. }else{
  101. return json(['code' =>200,'msg'=>'操作成功']);
  102. }
  103. }
  104. /**
  105. * 修改密码
  106. * @access public
  107. */
  108. public function password(){
  109. if(request()->isAjax()){
  110. $data = [
  111. 'password' => $this->request->param('password/s'),
  112. 'password_confirm' => $this->request->param('repassword/s'),
  113. 'about' => $this->request->param('about/s'),
  114. 'login' => $this->login
  115. ];
  116. $validate = $this->validate($data,'Admin.password');
  117. if(true !== $validate){
  118. return json(['code'=>0,'msg'=>$validate]);
  119. }
  120. $result = SystemAdmin::upDatePasspowrd($data);
  121. if(!$result){
  122. return json(['code'=>1,'msg'=>'操作失败']);
  123. }else{
  124. return json(['code'=>0,'msg'=>'操作成功','url'=>url('system/admin.index/logout')]);
  125. }
  126. }else{
  127. $view['info'] = SystemAdmin::where(['id' => $this->login['admin_id']])->find();
  128. return view()->assign($view);
  129. }
  130. }
  131. /**
  132. * 用户重复
  133. * @param integer $id
  134. * @return void
  135. */
  136. public function isPass(){
  137. $condition[] = ['username','=',$this->request->param('param/s')];
  138. $condition[] = ['id','<>',$this->request->param('id/d',0)];
  139. $result = SystemAdmin::where($condition)->count();
  140. if($result){
  141. return json(['status'=>'n','info'=>'用户名重复']);
  142. }else{
  143. return json(['status'=>'y','info'=>'可以使用']);
  144. }
  145. }
  146. }