/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if (!$request->ajax()) { return JsonHelper::fail("invalid request", ErrorCode::REQUEST_NOT_AJAX); } return $next($request); }
protected function resourceNotFound() { return JsonHelper::fail('resource not exists', ErrorCode::RESOURCE_NOT_EXISTS); }
/** * 分享写入用户分享历史 * @param Request $request * @return \Symfony\Component\HttpFoundation\Response * @throws \App\Exceptions\DevInvalidParamsException */ public function postShare(Request $request) { if (!$request->ajax()) { return JsonHelper::invalidRequest(); } if ($this->_user->isGuest) { return JsonHelper::mustLogin(); } $params = []; $params['post_id'] = $request->request->get('post_id'); $method = $request->request->get('method'); $params['type'] = LookUp::itemIndex('SOCIALIZE_TYPE', $method); $params['uid'] = $this->_user->uid; $params['object_id'] = (int) $request->request->get('object_id'); $type = $request->request->get('object_type'); $params['object_type'] = camel_case($type); $return = UserShareService::getInstance()->createUserShareHistory($params); if (!$return) { return JsonHelper::InternalDbFail(); } return JsonHelper::success($return); }
/** * Handle a login request to the application. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function postLogin(Request $request) { if (!$request->ajax()) { return JsonHelper::invalidRequest(); } $validator = Validator::make(['email' => $request->request->get('email'), 'password' => $request->request->get('password')], ['email' => 'required|regex:/^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$/', 'password' => 'required|regex:/^[0-9A-Za-z!@#$%*]{6,20}$/']); if ($validator->fails()) { return JsonHelper::json('', $validator->messages(), 50001); } $credentials['email'] = $request->get('email'); $credentials['password'] = $request->get('password'); $credentials['is_active'] = 1; if ($this->auth->attempt($credentials, $request->has('remember'))) { if (LoginUserHistory::getInstance()->createLoginUserHistory($this->auth->user()->id)) { return JsonHelper::json('', 'login success', 10000); } } return JsonHelper::json([], $this->getFailedLoginMesssage(), 50001); }
/** * Reset the given user's password. * @param Request $request * @return \Symfony\Component\HttpFoundation\Response */ public function postReset(Request $request) { if (!$request->ajax()) { return JsonHelper::invalidRequest(); } $validator = Validator::make(['token' => $request->request->get('token'), 'token2' => $request->request->get('token2'), 'password' => $request->request->get('password'), 'password_confirmation' => $request->request->get('password_confirmation')], ['token' => 'required', 'token2' => 'required', 'password' => 'required|regex:/^[0-9A-Za-z!@#$%*]{6,20}$/|confirmed']); if ($validator->fails()) { return JsonHelper::json('', $validator->messages(), 50001); } $user = PasswordReset::where('token', '=', $request->request->get('token'))->first(); if (empty($user)) { return JsonHelper::json('', 'password reset fail', ErrorCode::INVALID_TOKEN); } if ($request->request->get('token2') != md5($request->request->get('token') . $user['email'])) { return JsonHelper::json('', 'password reset fail', ErrorCode::EXPIRED_TOKEN); } $credentials = $request->only('password', 'password_confirmation', 'token'); $credentials['email'] = $user['email']; $response = $this->passwords->reset($credentials, function ($user, $password) { $user->password = bcrypt($password); $user->save(); $this->auth->login($user); }); switch ($response) { case PasswordBroker::PASSWORD_RESET: return JsonHelper::json('', 'password reset success', 10000); default: return JsonHelper::json('', 'password reset fail', 50001); } }