AdminUser.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\AdminUser as Request;
  3. use App\Models\AdminUser as Model;
  4. use Illuminate\Support\Facades\DB;
  5. /**
  6. * 系统用户
  7. *
  8. * @author 刘相欣
  9. *
  10. */
  11. class AdminUser extends Auth{
  12. protected function _initialize(){
  13. parent::_initialize();
  14. $this->assign('breadcrumb1','用户管理');
  15. $this->assign('breadcrumb2','后台管理员');
  16. }
  17. /**
  18. * 列表页
  19. *
  20. * */
  21. public function index(Model $Model){
  22. // 查询系统用户
  23. $list = $Model->orderByDesc('uid')->paginate(config('page_num',10));
  24. // 循环处理
  25. foreach ($list as $key => $value) {
  26. // 获取分组名
  27. $group = DB::table('auth_group')
  28. ->join('auth_group_access','auth_group_access.group_id','=','auth_group.id')
  29. ->where([['auth_group_access.user_uid','=',$value['uid']]])
  30. ->pluck('auth_group.title')->toArray();
  31. if (in_array($value['uid'],explode(',',config('administrator')))) $group[] = '超管';
  32. // 切成字符串
  33. $value['title'] = implode('、', $group);
  34. // 重组
  35. $list[$key] = $value;
  36. }
  37. // 分配数据
  38. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  39. $this->assign('list',$list);
  40. // 加载模板
  41. return $this->fetch();
  42. }
  43. /**
  44. * 添加
  45. *
  46. * */
  47. public function add(Request $request,Model $Model){
  48. if( request()->isMethod('post') ){
  49. // 验证参数
  50. $request->scene('add')->validate();
  51. // 接收数据
  52. $data['username'] = request('username','');
  53. $data['phone'] = request('phone','');
  54. $data['password'] = request('password','');
  55. $data['password'] = md5($data['password']);
  56. // 所属权限组
  57. $groups = (array) request('groups',[]);
  58. // 开启事务
  59. DB::beginTransaction();
  60. // 写入数据表
  61. $uid = $Model->add($data);
  62. // 如果操作失败
  63. if( !$uid ) {
  64. // 回滚事务
  65. DB::rollBack();
  66. // 告知错误
  67. return json_send(['code'=>'error','msg'=>'新增失败']);
  68. }
  69. // 权限组
  70. $access = [];
  71. // 循环数据
  72. foreach ( $groups as $group_id) {
  73. // 追加数据
  74. $access[] = ['group_id'=>$group_id,'user_uid'=>$uid];
  75. }
  76. // 写入用户权限组
  77. $result = DB::table('auth_group_access')->insert($access);
  78. // 如果操作失败
  79. if( !$result ) {
  80. // 回滚事务
  81. DB::rollBack();
  82. // 告知错误
  83. return json_send(['code'=>'error','msg'=>'权限分配失败']);
  84. }
  85. // 提交事务
  86. DB::commit();
  87. // 记录行为
  88. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,1,[],$data);
  89. // 告知结果
  90. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  91. }
  92. $whereGroup = [];
  93. // 如果不是超管 查询当前用户所属组
  94. $administrator = explode(',', config('administrator'));
  95. if(!in_array(admin('uid'),$administrator)){
  96. //用户所属组
  97. $gsGroup = DB::table('auth_group_access')->where(['user_uid'=>admin('uid')])->pluck('group_id')->toArray();
  98. //用户所属组 上级
  99. $upGroup = DB::table('auth_group')->whereIn('id',$gsGroup)->pluck('group_pid')->toArray();
  100. //用户所属组的下级
  101. $groupLower = DB::table('auth_group')->whereIn('group_pid',$gsGroup)->pluck('id')->toArray();
  102. //var_dump($groupLower);
  103. $whereGroup = array_merge($upGroup,$gsGroup,$groupLower);
  104. }
  105. // 查询用户组
  106. $query = DB::table('auth_group');
  107. if($whereGroup) $query->whereIn('id',$whereGroup);
  108. $group = $query->whereNotIn('id',explode(',',config('CUSTOM_GROUP')))->select(['id','title'])->get()->toArray();
  109. // 错误告知
  110. if( !$group ) $this->error('请先添加用户组');
  111. // 分配数据
  112. $this->assign('group',$group);
  113. $this->assign('crumbs','新增');
  114. // 加载模板
  115. return $this->fetch();
  116. }
  117. /**
  118. * 修改
  119. *
  120. * */
  121. public function edit(Request $request,Model $Model){
  122. // 接收参数
  123. $uid = request('uid',0);
  124. // 查询用户
  125. $oldData = $Model->where(['uid'=>$uid])->first();
  126. if(request()->isMethod('post')){
  127. // 验证参数
  128. $request->scene('edit')->validate();
  129. // 接收数据
  130. $data['username'] = request('username','');
  131. $data['phone'] = request('phone','');
  132. // 密码
  133. $password = request('password','');
  134. // 所属权限组
  135. $groups = (array) request('groups',[]);
  136. // 如果用户不存在
  137. if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
  138. // 不能修改超管的账号
  139. if( $oldData['username'] == config('administrator') ) return json_send(['code'=>'error','msg'=>'这是被禁止的操作']);
  140. // 如果要修密码
  141. if( $password ) $data['password'] = md5($password);
  142. // 开启事务
  143. DB::beginTransaction();
  144. // 写入数据表
  145. $result = $Model->edit($uid,$data);
  146. // 如果操作失败
  147. if( !$result ) {
  148. // 回滚事务
  149. DB::rollBack();
  150. // 告知错误
  151. return json_send(['code'=>'error','msg'=>'新增失败']);
  152. }
  153. // 清空权限组
  154. DB::table('auth_group_access')->where([['user_uid','=',$uid]])->delete();
  155. // 权限组
  156. $access = [];
  157. // 循环数据
  158. foreach ( $groups as $group_id) {
  159. // 追加数据
  160. $access[] = ['group_id'=>$group_id,'user_uid'=>$uid];
  161. }
  162. // 写入用户权限组
  163. $result = DB::table('auth_group_access')->insert($access);
  164. // 如果操作失败
  165. if( !$result ) {
  166. // 回滚事务
  167. DB::rollBack();
  168. // 告知错误
  169. return json_send(['code'=>'error','msg'=>'权限分配失败']);
  170. }
  171. // 提交事务
  172. DB::commit();
  173. // 记录行为
  174. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,2,$oldData,$data);
  175. // 告知结果
  176. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  177. }
  178. $whereGroup = [];
  179. // 如果不是超管 查询当前用户所属组
  180. $administrator = explode(',', config('administrator'));
  181. if(!in_array(admin('uid'),$administrator)){
  182. // 用户所属组
  183. $gsGroup = DB::table('auth_group_access')->where(['user_uid'=>admin('uid')])->pluck('group_id')->toArray();
  184. // 用户所属组 上级
  185. $upGroup = DB::table('auth_group')->whereIn('id',$gsGroup)->pluck('group_pid')->toArray();
  186. // 用户所属组的下级
  187. $groupLower = DB::table('auth_group')->whereIn('group_pid',$gsGroup)->pluck('id')->toArray();
  188. // 下属组
  189. $whereGroup = array_merge($upGroup,$gsGroup,$groupLower);
  190. }
  191. // 查询用户组
  192. $query = DB::table('auth_group');
  193. // 查询组
  194. if($whereGroup) $query->whereIn('id',$whereGroup);
  195. // 获取
  196. $group = $query->whereNotIn('id',explode(',',config('CUSTOM_GROUP')))->select(['id','title'])->get()->toArray();
  197. // 错误告知
  198. if( !$group ) return $this->error('请先添加用户组');
  199. // 错误告知
  200. if( !$oldData ) return $this->error('查无数据');
  201. // 查询用户的用户组
  202. $oldData['group'] = DB::table('auth_group_access')->where([['user_uid','=',$uid]])->pluck('group_id')->toArray();
  203. // 分配数据
  204. $this->assign('oldData',$oldData);
  205. $this->assign('group',$group);
  206. $this->assign('crumbs','修改');
  207. // 加载模板
  208. return $this->fetch();
  209. }
  210. /**
  211. * 操作历史
  212. *
  213. * */
  214. public function history(){
  215. // 查询
  216. $list = DB::table('user_action')->orderByDesc('ua_id')->paginate(config('page_num',10))->appends(request()->all());
  217. // 分配数据
  218. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  219. $this->assign('breadcrumb2','操作记录');
  220. $this->assign('list',$list);
  221. // 加载模板
  222. return $this->fetch();
  223. }
  224. /**
  225. * 修改状态
  226. *
  227. * */
  228. public function set_status(Request $request,Model $Model){
  229. // 验证参数
  230. $request->scene('set_status')->validate();
  231. // 设置状态
  232. $id = request('uid',0);
  233. $status = request('status',0);
  234. // 查询用户
  235. $oldData = $Model->where(['uid'=>$id])->first();
  236. // 如果用户不存在
  237. if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
  238. // 执行修改
  239. $result = $Model->edit($id,['status'=>$status]);
  240. // 提示新增失败
  241. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  242. // 记录行为
  243. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  244. // 告知结果
  245. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  246. }
  247. }