password != $request->repassword) { throw new ApiException('两次输入密码不一致'); } $user = User::create([ 'phone' => $verifyData['phone'], 'password' => bcrypt($request->password), 'user_type' => $request->user_type ?? 0, ]); // 清除验证码缓存 Cache::forget($request->verification_key); if (!$user) { throw new ApiException('注册失败,请重试'); } return $this->response->item($user, new UserTransformer()) ->setMeta([ 'access_token' => \Auth::guard('api')->fromUser($user), 'token_type' => 'Bearer', 'expires_in' => \Auth::guard('api')->factory()->getTTL() * 60 ]) ->setStatusCode(201); } /** * 获取用户信息 * @return array * @return \Dingo\Api\Http\Response */ public function me() { $user = $this->auth->user(); // $user_data = UserService::getUserInfo($user->id); $UserCourses = UserCourses::query()->where(['user_id' => $user->id])->orderByDesc('id')->with('course')->get(); $course_data = []; foreach ($UserCourses as $k => $v) { $course_data[] = [ 'subject_name' => $v->course->name, 'bar' => $v->bar ]; } $data = [ 'id' => $user->id, 'nick_name' => $user->nick_name, 'phone' => $user->phone, 'avatar_url' => $user->avatar_url, 'school' => $user->school, 'make_num' => $user->make_num, 'user_type' => $user->user_type, 'carbon_emission' => $user->carbon_emission, 'course_data' => $course_data ]; return apiJson($data); } /** * 验证码路由校验 校验 * @param Request $request * @return array * */ public function isVerification(Request $request) { self::verificationCode($request); return apiJsonMsg('验证码正确!'); } /** * 忘记密码 * @param Request $request * @return mixed */ public function forgetPassword(Request $request) { $verifyData = self::verificationCode($request); $query = User::query()->where(['phone' => $verifyData['phone']])->first(); if (!$query) { throw new ApiException('用户不存在'); } if ($request->password != $request->repassword) { throw new ApiException('两次输入密码不一致'); } $user = $query->update([ 'password' => bcrypt($request->password), ]); if (!$user) { throw new ApiException('修改失败,请重试'); } // 清除验证码缓存 Cache::forget($request->verification_key); return apiJsonMsg('修改成功请前往登录!', 200, $verifyData['phone']); } /** * 校验验证码是否正确 * @param $request * @return mixed */ public static function verificationCode($request) { $verifyData = Cache::get($request->verification_key); if (!$verifyData) { throw new ApiException('验证码已失效'); } if (!hash_equals($verifyData['code'], $request->verification_code)) { throw new ApiException('验证码错误'); } return $verifyData; } }