input('phone', ''); // if (empty($mobile)) { // return $this->apiResponseError('请输入手机号码'); // } // $password = request()->input('password', ''); // if (empty($password)) { // return $this->apiResponseError('请输入登录密码'); // } $data=request()->input(); $rules = [ 'account'=>'required|max:255', 'password'=>'required', ]; $rulesMsg = [ 'account.required' => '请输入账号', 'account.max' => '账号最大255', 'password.required' => '请输入登录密码', ]; $validator = Validator::make($data, $rules,$rulesMsg); if ($validator->fails()) { return $this->apiResponseError($validator->errors()->first()); } $validatedData = $validator->validated(); $user = User::where('account', $validatedData['account'])->where('is_del',0)->select(['id', 'password', 'encrypt'])->first(); if(empty($user)){ return $this->apiResponseError('用户信息不存在'); } $pass_server = PassServer::creatServer($validatedData['password'], $user->{'encrypt'}); if (!$pass_server->verifyPass($user->{'password'})) { return $this->apiResponseError('登陆密码错误'); } $token = Str::random(60); $api_token = hash('sha256', $token); $ret = $user->forceFill(['api_token' => $api_token])->save(); if (empty($ret)) { return $this->apiResponseError('登陆失效'); } Auth::loginUsingId($user->{'id'}); return $this->apiResponseSuccess('登陆成功', ['u_id' =>$user->{'id'}, 'token' => $api_token]); } /** * 修改登录密码-忘记密码 * @return \Illuminate\Http\JsonResponse */ function retrievePassword() { $mobile = request()->input('phone', ''); $password = request()->input('password', ''); $confirm_password = request()->input('confirm_password',''); $password = str_replace(' ', '', $password); $confirm_password = str_replace(' ', '', $confirm_password); if (empty($password)) { return $this->apiResponseError('请设置登录密码'); } if (strlen($password) < 6 || strlen($password) > 12) { return $this->apiResponseError('密码为6-12位'); } if ($password != $confirm_password){ return $this->apiResponseError('确认密码错误'); } if (empty($mobile)) { return $this->apiResponseError('请输入手机号码'); } if (!CommonServer::creatServer()->verifyPhoneNumber($mobile)) { return $this->apiResponseError('手机号码格式错误'); } $code = request()->input('code', ''); if (empty($code)) { return $this->apiResponseError('请输入验证码'); } $send_server = SmsServer::creatServer(); $code_ret = $send_server->verifyCode($mobile, $code, 'retrieve'); if (empty($code_ret)) { return $this->apiResponseError($send_server->getErrorMsg() ?: '验证码错误'); } $user = User::where('phone', $mobile)->where('is_del',0)->first(); if(empty($user)){ return $this->apiResponseError('账户信息不存在'); } $pass_server = PassServer::creatServer($password, $user->{'encrypt'}); $new_pass = $pass_server->creatPassword(); $ret = $user->update(['password' => $new_pass['password']]); if (empty($ret)) { return $this->apiResponseError('密码修改失败'); } else { return $this->apiResponseSuccess('密码修改成功'); } } }