UserResetController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\AdminRoleModel;
  4. use App\Models\AdminUserModel;
  5. use App\Models\AdminUserReset;
  6. use App\Models\Department;
  7. use App\Models\Road;
  8. use App\Models\User;
  9. use App\Models\UserReset;
  10. use Illuminate\Http\Request;
  11. class UserResetController extends BaseController
  12. {
  13. protected $model;
  14. protected $department;
  15. protected $model_name = '密码重置';
  16. protected $pre_uri = '/admin/UserReset/';
  17. protected $view_path = 'admin.user-resets.';
  18. protected $redirect_index = '/admin/UserReset/index';
  19. public function __construct()
  20. {
  21. parent::__construct();
  22. $this->model = new UserReset();
  23. }
  24. public function index()
  25. {
  26. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  27. return view($this->view_path . 'index', compact('model', 'model_name','pre_uri'));
  28. }
  29. public function get(Request $request)
  30. {
  31. $items = $this->model->where('id', '>', 0)->orderBy('updated_at', 'desc');
  32. $tmp_items = collect(['name']);
  33. foreach($tmp_items as $tmp_item) {
  34. if($request->has($tmp_item) && !empty($request->input($tmp_item))) {
  35. $items = $items->where($tmp_item, 'like', '%' . $request->input($tmp_item) . '%');
  36. }
  37. }
  38. $select_items = collect([]);
  39. foreach($select_items as $select_item) {
  40. if($request->has($select_item) && !empty($request->input($select_item))) {
  41. $items = $items->where($select_item, '=', $request->input($select_item));
  42. }
  43. }
  44. $items = $items->paginate();
  45. foreach($items as $item) {
  46. $item->status = $item->status == 1 ? '<span class="layui-badge">待重置</span>' : '<span class="layui-badge layui-bg-green">已重置</span>';
  47. $item->real_name = $item->user_auth ? $item->user_auth->name : '';
  48. }
  49. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  50. }
  51. public function create()
  52. {
  53. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  54. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri'));
  55. }
  56. public function store(Request $request)
  57. {
  58. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  59. $validator = $this->model->getValidator($request, 'store');
  60. if($validator->fails()) {
  61. return back()->withErrors($validator)->withInput();
  62. }
  63. $data = $request->input('data');
  64. $res = $this->model->create($data);
  65. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  66. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  67. }
  68. public function edit(Request $request)
  69. {
  70. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  71. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  72. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item'));
  73. }
  74. public function update(Request $request)
  75. {
  76. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  77. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  78. $validator = $this->model->getValidator($request, 'update');
  79. if($validator->fails()) {
  80. return back()->withErrors($validator)->withInput();
  81. }
  82. $data = $request->input('data');
  83. $res = $this->model->where('id', $request->input('id'))->update($data);
  84. if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
  85. return back()->with(['sg_success_info' => '编辑成功']);
  86. }
  87. public function delete(Request $request)
  88. {
  89. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  90. $res = $item->delete();
  91. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  92. return response()->json(['status' => 'success', 'info' => '操作成功']);
  93. }
  94. public function reset(Request $request)
  95. {
  96. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到数据']);
  97. $user = User::find($item->user_id);
  98. if(empty($user)) return response()->json(['status' => 'fail', 'info' => '找不到数据']);
  99. $res = $user->update(['password' => bcrypt('123456')]);
  100. $item->update(['status' => 2]);
  101. if (!$res) return response()->json(['status' => 'fail', 'info' => '操作失败']);
  102. return response()->json(['status' => 'success', 'info' => '操作成功']);
  103. }
  104. }