AuthortyManage.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Titan 名字就是密码
  5. * Date: 2019/4/25
  6. * Time: 18:03
  7. * 权限管理
  8. */
  9. namespace app\admin\controller;
  10. use think\Controller;
  11. use think\Db;
  12. use think\Request;
  13. class AuthortyManage extends Controller
  14. {
  15. /**
  16. * 管理员首页
  17. * @return mixed
  18. */
  19. public function index()
  20. {
  21. $admin = Db::name('admin')->select();
  22. $this->assign('admin',$admin);
  23. return $this->fetch();
  24. }
  25. /**
  26. * 增加管理员
  27. * @param Request $request
  28. * @return bool|mixed
  29. */
  30. public function addAdmin(Request $request)
  31. {
  32. if($request->isPost() && $request->isAjax()){
  33. $data = $request->param();
  34. $data['user_nickname'] = '管理员';
  35. $data['create_time'] = time();
  36. $data['password'] = md5($data['password']);
  37. $authority = '';
  38. //管理组
  39. foreach ($data['authority'] as $key=>$v){
  40. $authority .= $v .",";
  41. }
  42. $data['authority'] = substr($authority,0,-1);
  43. unset($data['repass']);
  44. $name = Db::name('admin')->where('username',$data['username'])->find();
  45. $openid = Db::name('admin')->where('openid',$data['openid'])->find();
  46. if($name){
  47. echo json_encode(['status'=>0,'message'=>'登录名重复'],JSON_UNESCAPED_UNICODE);
  48. exit;
  49. }
  50. if($openid){
  51. echo json_encode(['status'=>0,'message'=>'此微信已绑定,不能重复绑定'],JSON_UNESCAPED_UNICODE);
  52. exit;
  53. }
  54. $insert = Db::name('admin')->insert($data);
  55. if($insert){
  56. echo json_encode(['status'=>1,'message'=>'增加成功'],JSON_UNESCAPED_UNICODE);
  57. exit;
  58. }
  59. echo json_encode(['status'=>0,'message'=>'增加失败'],JSON_UNESCAPED_UNICODE);
  60. }else{
  61. $this->assign('adminGroup',$this->getGroup());
  62. $this->assign('admin',$this->admin());
  63. return $this->fetch();
  64. }
  65. }
  66. public function admin()
  67. {
  68. $admin = Db::name('user')->where(['identity'=>99,'is_register'=>1,'is_check'=>1])->field(['openid','headimgurl','nickname','username'])->select();
  69. return $admin;
  70. }
  71. /**
  72. * 删除管理员
  73. * @param Request $request
  74. */
  75. public function del(Request $request)
  76. {
  77. $id = $request->param('id');
  78. $result = Db::name('admin')->delete($id);
  79. if($result){
  80. header('Location: /admin/authorty_manage/index');
  81. exit;
  82. }
  83. }
  84. /**
  85. * 获取所有组
  86. * @return false|\PDOStatement|string|\think\Collection
  87. */
  88. public function getGroup()
  89. {
  90. $adminGroup = Db::name('group')->select();
  91. return $adminGroup;
  92. }
  93. /**
  94. * 查看管理员
  95. * @param Request $request
  96. * @return mixed
  97. */
  98. public function view(Request $request)
  99. {
  100. $id = $request->param('adminId');
  101. $data = Db::name('admin')->where('id',$id)->find();
  102. $this->assign('data',$data);
  103. $this->assign('adminGroup',$this->getGroup());
  104. return $this->fetch();
  105. }
  106. /**
  107. * 编辑管理员
  108. * @param Request $request
  109. * @return bool|mixed
  110. */
  111. public function editAdmin(Request $request)
  112. {
  113. if($request->isGet()){
  114. $id = $request->param('adminId');
  115. $data = Db::name('admin')->where('id',$id)->find();
  116. $this->assign('data',$data);
  117. $this->assign('adminGroup',$this->getGroup());
  118. return $this->fetch();
  119. }else{
  120. $id = $request->param('id');
  121. $data = $request->param();
  122. $authority = '';
  123. foreach ($data['authority'] as $key=>$v){
  124. $authority .= $v .",";
  125. }
  126. $data['authority'] = substr($authority,0,-1);
  127. $result = Db::name('admin')->where('id',$id)->update($data);
  128. return true;
  129. }
  130. }
  131. }