UserController.php 6.1 KB

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