AdminUserController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\AdminRoleModel;
  4. use App\Models\AdminUserModel;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Facades\Hash;
  8. class AdminUserController extends BaseController
  9. {
  10. protected $model;
  11. protected $adminRole;
  12. protected $road;
  13. protected $zone;
  14. protected $model_name = '账号';
  15. protected $pre_uri = '/admin/AdminUser/';
  16. protected $view_path = 'admin.admin-users.';
  17. protected $redirect_index = '/admin/AdminUser/index';
  18. public function __construct()
  19. {
  20. parent::__construct();
  21. $this->model = new AdminUserModel();
  22. $this->adminRole = new AdminRoleModel();
  23. }
  24. public function index()
  25. {
  26. $admin_role_options = $this->adminRole->getOptions();
  27. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  28. return view($this->view_path . 'index', compact('model', 'model_name','pre_uri', 'admin_role_options'));
  29. }
  30. public function get(Request $request)
  31. {
  32. $items = $this->model->where('id', '>', 0);
  33. $tmp_items = collect(['name']);
  34. foreach($tmp_items as $tmp_item) {
  35. if($request->has($tmp_item) && !empty($request->input($tmp_item))) {
  36. $items = $items->where($tmp_item, 'like', '%' . $request->input($tmp_item) . '%');
  37. }
  38. }
  39. $select_items = collect([]);
  40. foreach($select_items as $select_item) {
  41. if($request->has($select_item) && !empty($request->input($select_item))) {
  42. $items = $items->where($select_item, '=', $request->input($select_item));
  43. }
  44. }
  45. $items = $items->paginate();
  46. // foreach($items as $item) {
  47. // $item->role_name = empty($item->adminRole) ? '' : $item->adminRole->name;
  48. // }
  49. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  50. }
  51. public function create()
  52. {
  53. $admin_role_options = $this->adminRole->getOptions();
  54. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  55. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri', 'admin_role_options'));
  56. }
  57. public function store(Request $request)
  58. {
  59. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  60. $validator = $this->model->getValidator($request, 'store');
  61. if($validator->fails()) {
  62. return back()->withErrors($validator)->withInput();
  63. }
  64. $data = $request->input('data');
  65. unset($data['password_confirmation']);
  66. $data['password'] = bcrypt($data['password']);
  67. $res = $this->model->create($data);
  68. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  69. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  70. }
  71. public function edit(Request $request)
  72. {
  73. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  74. $admin_role_options = $this->adminRole->getOptions();
  75. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  76. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item', 'admin_role_options'));
  77. }
  78. public function update(Request $request)
  79. {
  80. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  81. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  82. $validator = $this->model->getValidator($request, 'update');
  83. if($validator->fails()) {
  84. return back()->withErrors($validator)->withInput();
  85. }
  86. $data = $request->input('data');
  87. if(isset($data['password']) && !empty($data['password'])) {
  88. $data['password'] = bcrypt($data['password']);
  89. } else {
  90. unset($data['password']);
  91. }
  92. unset($data['password_confirmation']);
  93. $res = $this->model->where('id', $request->input('id'))->update($data);
  94. if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
  95. return back()->with(['sg_success_info' => '编辑成功']);
  96. }
  97. public function delete(Request $request)
  98. {
  99. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  100. $res = $item->delete();
  101. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  102. return response()->json(['status' => 'success', 'info' => '操作成功']);
  103. }
  104. public function changePassword()
  105. {
  106. $item = Auth::guard('admin')->user();
  107. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  108. return view($this->view_path . 'change-password', compact('model', 'model_name', 'pre_uri', 'item'));
  109. }
  110. public function updatePassword(Request $request)
  111. {
  112. $validator = $this->model->getValidator($request, 'change-password');
  113. $user = Auth::guard('admin')->user();
  114. $data = $request->input('data');
  115. $validator->after(function ($validator) use($request, $data, $user) {
  116. if(!isset($data['old_password'])) {
  117. $validator->errors()->add('old_password', '请填写原来密码');
  118. } else if(!Hash::check($data['old_password'], $user['password'])) {
  119. $validator->errors()->add('old_password', '原来密码错误');
  120. }
  121. });
  122. if($validator->fails()) {
  123. return back()->withErrors($validator)->withInput();
  124. }
  125. $this->model->where('id', $user['id'])->update(['password' => $data['password']]);
  126. return back()->with(['sg_success_info' => '操作成功']);
  127. }
  128. }