AdminUserController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\AdminUserModel;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Validator;
  6. class AdminUserController extends Controller
  7. {
  8. protected $redirect_index = '/admin/AdminUser/index';
  9. protected $view_path = 'admin.admin-users.';
  10. protected $pre_uri = '/admin/AdminUser/';
  11. protected $model_name = '账号';
  12. protected $model;
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. $this->model = new AdminUserModel();
  17. }
  18. public function index(Request $request)
  19. {
  20. $list = $this->model->where('id', '>', 0)->orderBy('created_at', 'desc');
  21. if(!empty($request->input('keyword')) && !empty(trim($request->input('keyword')))) {
  22. $keyword = '%' . trim($request->input('keyword')) . '%';
  23. $list = $list->where('name', 'like', $keyword);
  24. }
  25. $list = $list->paginate()->withPath($this->getPaginateUrl());
  26. list($pre_uri, $model_name) = array($this->pre_uri, $this->model_name);
  27. return view($this->view_path . 'index', compact('list', 'pre_uri', 'model_name'));
  28. }
  29. public function create(Request $request)
  30. {
  31. list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
  32. return view($this->view_path . 'create', compact('pre_uri', 'model_name', 'model'));
  33. }
  34. public function store(Request $request)
  35. {
  36. if(!$request->isMethod('POST')) {
  37. return $this->showWarning('访问错误');
  38. }
  39. if(empty($request->input('data')) || !is_array($request->input('data'))) {
  40. return $this->showWarning('数据错误');
  41. }
  42. $validator = Validator::make($request->all(), [
  43. 'name' => 'required|unique:admin_users',
  44. 'password' => 'required|min:6'
  45. ], [
  46. 'name.required' => '用户名必填',
  47. 'name.unique' => '用户名已存在',
  48. 'password.unique' => '密码必填',
  49. 'password.min' => '密码不能小于6位',
  50. ]);
  51. if($validator->fails()) {
  52. return back()->withErrors($validator)->withInput();
  53. }
  54. $data = $request->input('data');
  55. $data['password'] = bcrypt($data['password']);
  56. $res = $this->model->create($data);
  57. if(!$res) {
  58. return $this->showWarning('数据库保存失败!');
  59. }
  60. return $this->showMessage('操作成功', $this->redirect_index);
  61. }
  62. public function edit(Request $request)
  63. {
  64. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  65. return $this->showWarning('数据错误!');
  66. }
  67. list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
  68. return view($this->view_path . 'edit', compact('item','pre_uri', 'model_name', 'model'));
  69. }
  70. public function update(Request $request)
  71. {
  72. if(!$request->isMethod('POST')) {
  73. return $this->showWarning('访问错误');
  74. }
  75. if(empty($request->input('id')) || empty($request->input('data')) || !is_array($request->input('data'))) {
  76. return $this->showWarning('数据错误');
  77. }
  78. $validator = Validator::make($request->input('data'), [
  79. 'name' => 'required'
  80. ], [
  81. 'name.required' => '用户名必填'
  82. ]);
  83. if($validator->fails()) {
  84. return back()->withErrors($validator)->withInput();
  85. }
  86. $data = $request->input('data');
  87. $tmp = $this->model->where([
  88. ['name', '=', $data['name']],
  89. ['id', '<>', $request->input('id')],
  90. ])->first();
  91. if(!empty($tmp)) {
  92. $validator->errors()->add('name', '用户名已存在');
  93. return back()->withErrors($validator)->withInput();
  94. }
  95. if($request->has('is_update_password') && $request->input('is_update_password') == 1) {
  96. $data['password'] = bcrypt($request->input('password', 123456));
  97. }
  98. $res = $this->model->where('id', $request->input('id'))->update($data);
  99. if(!$res) {
  100. return $this->showWarning('数据库保存失败!');
  101. }
  102. return $this->showMessage('操作成功', $this->redirect_index);
  103. }
  104. public function delete(Request $request)
  105. {
  106. if(!$request->isMethod('POST')) {
  107. return $this->showWarning('访问错误');
  108. }
  109. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  110. return $this->showWarning('访问错误');
  111. }
  112. $res = $item->delete();
  113. if(!$res) {
  114. return $this->showWarning('数据库删除失败');
  115. }
  116. return $this->showMessage('操作成功');
  117. }
  118. }