UserResetController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. use Illuminate\Support\Facades\Auth;
  12. class UserResetController extends BaseController
  13. {
  14. protected $model;
  15. protected $department;
  16. protected $model_name = '密码重置';
  17. protected $pre_uri = '/admin/UserReset/';
  18. protected $view_path = 'admin.user-resets.';
  19. protected $redirect_index = '/admin/UserReset/index';
  20. public function __construct()
  21. {
  22. parent::__construct();
  23. $this->model = new UserReset();
  24. }
  25. public function index()
  26. {
  27. $admin_id = Auth::guard('admin')->user()->id;//获取超级管理员信息
  28. $items = $this->model->leftjoin('users','users.id','=','user_resets.user_id')
  29. ->where('user_resets.id', '>', 0)->orderBy('user_resets.updated_at', 'desc')
  30. ->select('user_resets.*','users.name as real_name')
  31. ->get();
  32. /*echo "<pre>";var_dump(json_decode(json_encode($items,JSON_UNESCAPED_UNICODE),true));
  33. return "";*/
  34. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  35. return view($this->view_path . 'index', compact('model', 'model_name','pre_uri'));
  36. }
  37. public function get(Request $request)
  38. {
  39. //$items = $this->model->where('id', '>', 0)->orderBy('updated_at', 'desc');
  40. $items = $this->model->leftjoin('users','users.id','=','user_resets.user_id')
  41. ->where('user_resets.id', '>', 0)->orderBy('user_resets.updated_at', 'desc')
  42. ->select('user_resets.*','users.name as real_name');
  43. $tmp_items = collect(['name']);
  44. foreach($tmp_items as $tmp_item) {
  45. if($request->has($tmp_item) && !empty($request->input($tmp_item))) {
  46. $items = $items->where($tmp_item, 'like', '%' . $request->input($tmp_item) . '%');
  47. }
  48. }
  49. $select_items = collect([]);
  50. foreach($select_items as $select_item) {
  51. if($request->has($select_item) && !empty($request->input($select_item))) {
  52. $items = $items->where($select_item, '=', $request->input($select_item));
  53. }
  54. }
  55. $items = $items->paginate();
  56. foreach($items as $item) {
  57. $item->status = $item->status == 1 ? '<span class="layui-badge">待重置</span>' : '<span class="layui-badge layui-bg-green">已重置</span>';
  58. }
  59. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  60. }
  61. public function create()
  62. {
  63. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  64. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri'));
  65. }
  66. public function store(Request $request)
  67. {
  68. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  69. $validator = $this->model->getValidator($request, 'store');
  70. if($validator->fails()) {
  71. return back()->withErrors($validator)->withInput();
  72. }
  73. $data = $request->input('data');
  74. $res = $this->model->create($data);
  75. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  76. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  77. }
  78. public function edit(Request $request)
  79. {
  80. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  81. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  82. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item'));
  83. }
  84. public function update(Request $request)
  85. {
  86. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  87. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  88. $validator = $this->model->getValidator($request, 'update');
  89. if($validator->fails()) {
  90. return back()->withErrors($validator)->withInput();
  91. }
  92. $data = $request->input('data');
  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. $admin_id = Auth::guard('admin')->user()->id;//获取超级管理员信息
  100. if($admin_id!=1){
  101. return back()->withErrors(['sg_error_info' => '您不是超级管理员没有删除权限!']);
  102. }
  103. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  104. $res = $item->delete();
  105. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  106. return response()->json(['status' => 'success', 'info' => '操作成功']);
  107. }
  108. public function reset(Request $request)
  109. {
  110. $name = $request->input('name');
  111. $real_name = $request->input('real_name');
  112. if($name!==$real_name){
  113. return response()->json(['status' => 'fail', 'info' => '提交人和真实姓名不一致']);
  114. }
  115. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到数据']);
  116. $user = User::find($item->user_id);
  117. if(empty($user)) return response()->json(['status' => 'fail', 'info' => '找不到数据']);
  118. $res = $user->update(['password' => bcrypt('123456')]);
  119. $item->update(['status' => 2]);
  120. if (!$res) return response()->json(['status' => 'fail', 'info' => '操作失败']);
  121. return response()->json(['status' => 'success', 'info' => '操作成功']);
  122. }
  123. }