123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- namespace App\Http\Controllers\Admin;
- use App\Models\AdminRoleModel;
- use App\Models\AdminUserModel;
- use App\Models\AdminUserReset;
- use App\Models\Department;
- use App\Models\Road;
- use App\Models\User;
- use App\Models\UserReset;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- class UserResetController extends BaseController
- {
- protected $model;
- protected $department;
- protected $model_name = '密码重置';
- protected $pre_uri = '/admin/UserReset/';
- protected $view_path = 'admin.user-resets.';
- protected $redirect_index = '/admin/UserReset/index';
- public function __construct()
- {
- parent::__construct();
- $this->model = new UserReset();
- }
- public function index()
- {
- $admin_id = Auth::guard('admin')->user()->id;//获取超级管理员信息
- $items = $this->model->leftjoin('users','users.id','=','user_resets.user_id')
- ->where('user_resets.id', '>', 0)->orderBy('user_resets.updated_at', 'desc')
- ->select('user_resets.*','users.name as real_name')
- ->get();
- /*echo "<pre>";var_dump(json_decode(json_encode($items,JSON_UNESCAPED_UNICODE),true));
- return "";*/
- list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
- return view($this->view_path . 'index', compact('model', 'model_name','pre_uri'));
- }
- public function get(Request $request)
- {
- //$items = $this->model->where('id', '>', 0)->orderBy('updated_at', 'desc');
- $items = $this->model->leftjoin('users','users.id','=','user_resets.user_id')
- ->where('user_resets.id', '>', 0)->orderBy('user_resets.updated_at', 'desc')
- ->select('user_resets.*','users.name as real_name');
- $tmp_items = collect(['name']);
- foreach($tmp_items as $tmp_item) {
- if($request->has($tmp_item) && !empty($request->input($tmp_item))) {
- $items = $items->where($tmp_item, 'like', '%' . $request->input($tmp_item) . '%');
- }
- }
- $select_items = collect([]);
- foreach($select_items as $select_item) {
- if($request->has($select_item) && !empty($request->input($select_item))) {
- $items = $items->where($select_item, '=', $request->input($select_item));
- }
- }
- $items = $items->paginate();
- foreach($items as $item) {
- $item->status = $item->status == 1 ? '<span class="layui-badge">待重置</span>' : '<span class="layui-badge layui-bg-green">已重置</span>';
- }
- return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
- }
- public function create()
- {
- list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
- return view($this->view_path . 'create', compact('model', 'model_name','pre_uri'));
- }
- public function store(Request $request)
- {
- if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
- $validator = $this->model->getValidator($request, 'store');
- if($validator->fails()) {
- return back()->withErrors($validator)->withInput();
- }
- $data = $request->input('data');
- $res = $this->model->create($data);
- if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
- return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
- }
- public function edit(Request $request)
- {
- if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
- list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
- return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item'));
- }
- public function update(Request $request)
- {
- if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
- if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
- $validator = $this->model->getValidator($request, 'update');
- if($validator->fails()) {
- return back()->withErrors($validator)->withInput();
- }
- $data = $request->input('data');
- $res = $this->model->where('id', $request->input('id'))->update($data);
- if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
- return back()->with(['sg_success_info' => '编辑成功']);
- }
- public function delete(Request $request)
- {
- $admin_id = Auth::guard('admin')->user()->id;//获取超级管理员信息
- if($admin_id!=1){
- return back()->withErrors(['sg_error_info' => '您不是超级管理员没有删除权限!']);
- }
- if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
- $res = $item->delete();
- if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
- return response()->json(['status' => 'success', 'info' => '操作成功']);
- }
- public function reset(Request $request)
- {
- $name = $request->input('name');
- $real_name = $request->input('real_name');
- if($name!==$real_name){
- return response()->json(['status' => 'fail', 'info' => '提交人和真实姓名不一致']);
- }
- if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到数据']);
- $user = User::find($item->user_id);
- if(empty($user)) return response()->json(['status' => 'fail', 'info' => '找不到数据']);
- $res = $user->update(['password' => bcrypt('123456')]);
- $item->update(['status' => 2]);
- if (!$res) return response()->json(['status' => 'fail', 'info' => '操作失败']);
- return response()->json(['status' => 'success', 'info' => '操作成功']);
- }
- }
|