AdminUser.php 10 KB

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