AdminController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Http\Controllers\Admin\System;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Admin\Admin;
  6. use Illuminate\Support\Facades\Hash;
  7. use Spatie\Permission\Models\Role;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Auth;
  10. class AdminController extends Controller
  11. {
  12. public function index(Request $request)
  13. {
  14. $roles = Role::pluck('name', 'id');
  15. return view('admin.system.admin.index', compact('roles'));
  16. }
  17. public function getData(Request $request)
  18. {
  19. $model = Admin::with([
  20. 'roles:id,name'
  21. ])
  22. ->where(function($query) use ($request){
  23. if ( $request->has('name') && !empty($request->name )) {
  24. $query->where('name', $request->name);
  25. }
  26. })->select(['id', 'name', 'status', 'created_at', 'white_ip']);
  27. $total = $model->count(); //总数
  28. $limit = $request->has('limit') ? $request->limit : 20;
  29. $page = $request->has('page') ? ($request->page - 1) * $limit : 0;
  30. $data = $model->offset($page)->limit($limit)->get();
  31. $data = $data->map(function($query){
  32. $query->role = $query->roles->pluck('id');
  33. return $query;
  34. });
  35. return json_encode(['code' => 0, 'data' => $data, 'count' => $total]);
  36. }
  37. public function changeStatus(Request $request)
  38. {
  39. $res = Admin::where('id', $request->id)->update([
  40. 'status' => $request->status == 1?0:1,
  41. 'updated_at' => date('Y-m-d H:i:s')
  42. ]);
  43. return $res?$this->responseData(200, '修改成功') : $this->responseData(400, '修改失败');
  44. }
  45. public function changePassword(Request $request)
  46. {
  47. if(empty($request->oldpassword) || empty($request->xinpassword) || empty($request->qrpassword)){
  48. return $this->responseData(400, '参数错误');
  49. }
  50. if($request->xinpassword != $request->qrpassword){
  51. return $this->responseData(400, '密码与确认密码不一致');
  52. }
  53. $admin = auth('admin')->user();
  54. $oldpassword = md5($request->oldpassword);
  55. $result = Hash::check($oldpassword,$admin->password);
  56. if(empty($result)){
  57. return $this->responseData(400, '原密码错误');
  58. }
  59. $newpassword = md5($request->xinpassword);
  60. $xinpassword = Hash::make($newpassword);
  61. $res = Admin::where('id', $admin->id)->update([
  62. 'password' => $xinpassword,
  63. 'updated_at' => date('Y-m-d H:i:s')
  64. ]);
  65. return $res?$this->responseData(200, '修改成功') : $this->responseData(400, '修改失败');
  66. }
  67. public function add(Request $request)
  68. {
  69. $password = md5($request->password);
  70. $res = Admin::create([
  71. 'name' => $request->name,
  72. 'password' => Hash::make($password),
  73. 'status' => $request->status,
  74. 'white_ip' => $request->white_ip ?? ''
  75. ]);
  76. return $res ? $this->responseData(200, '添加成功') : $this->responseData(400, '添加失败');
  77. }
  78. public function delete(Request $request)
  79. {
  80. $res = Admin::where('id', $request->id)->delete();
  81. return $res ? $this->responseData(200, '删除成功') : $this->responseData(400, '删除失败');
  82. }
  83. public function update(Request $request)
  84. {
  85. DB::beginTransaction();
  86. try {
  87. $user = Admin::where('id', $request->id)->first();
  88. $user->name = $request->name;
  89. if ( $request->password != '') {
  90. $password = md5($request->password);
  91. $user->password = Hash::make($password);
  92. }
  93. $user->white_ip = $request->white_ip ?? '';
  94. $user->save();
  95. $user->syncRoles($request->role_id);
  96. DB::commit();
  97. return $this->responseData(200, '更新成功');
  98. } catch (\Exception $e) {
  99. DB::rollBack();
  100. return $this->responseData(400, '更新失败'.$e->getMessage());
  101. }
  102. }
  103. public function compare($before,$after){
  104. if (!is_array($before) || !is_array($after)) {
  105. return false;
  106. }
  107. $add = [];
  108. $del = [];
  109. foreach ($after as $val) {
  110. if (!in_array($val, $before)) {
  111. array_push($add, $val);
  112. }
  113. }
  114. foreach ($before as $val) {
  115. if (!in_array($val, $after)) {
  116. array_push($del, $val);
  117. }
  118. }
  119. return [ 'add' => $add, 'del' => $del];
  120. }
  121. }