where('id', '<>', 1); }; } protected function _indexPost($datum) { foreach ( $datum as &$data ) { $data->role_name = Role::where('id',$data->{'roles_id'})->value('name')?:'--'; } unset($data); return $datum; } protected function _storeGet($request) { // 角色 $roles = Role::where('status',1)->orderBy('id', 'asc')->get(); $option = [ 'checked' => '请选择角色', 'attr' => [ [ 'value' => 0, 'label' => "请选择角色", 'disabled' => false, 'notice' => '*' ] ] ]; foreach ( $roles as $category ) { $option['attr'][] = [ 'value' => $category->id, 'label' => $category->name, 'disabled' => false, 'notice' => "*" ]; } $params['options'] = json_encode($option,JSON_UNESCAPED_SLASHES); return $params; } protected function _storePost($request) { $email = $request->input('email', ""); $password = $request->input('password', ""); $roleID = $request->input('role_id', ""); if ( empty($email) ) { $this->errorMsg = "注册账号必填"; return false; } if ( empty($password) ) { $this->errorMsg = "登录密码必填"; return false; } if ( $roleID <= 0 ) { $this->errorMsg = "请选择角色"; return false; } $count = User::where('email', '=', $email)->count(); if ( $count > 0 ) { $this->errorMsg = "账号已注册"; return false; } $user = $this->repository->create([ 'name' => $request->input('name', ""), 'email' => $email, 'password' => bcrypt($password), 'roles_id' =>$roleID, ]); // // 添加角色 // DB::table('user_has_roles')->insert([ // 'role_id' => $roleID, // 'user_id' => $user->id, // ]); return true; } protected function _editGet($id) { $params['model'] = $this->repository->find($id); $userRole = DB::table('users')->where('id', $id)->first(); if ( !empty($userRole) ) { $userRole = Role::where('id', $userRole->roles_id)->first(); } // 角色 $roles = Role::where('status',1)->orderBy('id', 'asc')->get(); $option = [ 'checked' => empty($userRole) ? "" : $userRole->name, 'attr' => [] ]; foreach ( $roles as $category ) { $option['attr'][] = [ 'value' => $category->id, 'label' => $category->name, 'disabled' => false, 'notice' => "*" ]; } $params['options'] = json_encode($option,JSON_UNESCAPED_SLASHES); return $params; } protected function _updatePost($request, $id) { $model = $this->repository->find($id); $email = $request->input('email', ""); $password = $request->input('password', ""); $roleID = $request->input('role_id', ""); if ( empty($email) ) { $this->errorMsg = "注册账号必填"; return false; } $role = Role::where('id', $roleID)->orWhere('name',$roleID)->first(); if ( empty($role) ) { $this->errorMsg = "请选择角色"; return false; } $roleID = $role['id']; $count = User::where('email', '=', $email)->where('id', '<>', $id)->count(); if ( $count > 0 ) { $this->errorMsg = "账号已注册"; return false; } $update = array(); if ( $model->name != $request->input('name', "") ) { $update['name'] = $request->input('name', ""); } if ( $model->email != $email ) { $update['email'] = $email; } if ( !empty($password) ) { $update['password'] = bcrypt($password); } if(!empty($roleID)){ $update['roles_id'] = $roleID; } if ( !empty($update) ) { $this->repository->update($update, $id); } // $userRole = DB::table('user_has_roles')->where('user_id', $id)->first(); // if ( $userRole->role_id != $role->{'id'} ) { // // 更新角色 // DB::table('user_has_roles')->where('user_id', $id)->where('role_id', $userRole->role_id)->update([ // 'role_id' => $roleID, // ]); // } return true; } /** * @param Request $request * @param ResponseFactory $response * @param $id * @return \Illuminate\Http\JsonResponse * * 删除一行数据 */ protected function destroy(Request $request,ResponseFactory $response,$id = null) { if( $request->isMethod('post') && $request->ajax() ){ if ( $id <= 1 ) { $data = [ 'status' => 1, 'message' => '超级账号不能删除' ]; }else{ $deleted = $this->repository->delete($id); $deleted === false ? $data = [ 'status' => 1, 'message'=> self::ERROR_MSG ]: $data = [ 'status' => 0, 'message' => '' ]; } if( $request->wantsJson() ){ return $response->json($data); } } } }