get('code'); $config = [ 'app_id' => '', 'secret' => '', // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名 'response_type' => 'array', ]; $this->app = Factory::miniProgram($config); $session = $this->app->auth->session($code); \Log::info(json_encode($session)); $openid = 'olAS94uwfTdsL3nDnvG67p_v5Vks'; if (!$openid) { $data = [ 'code' => 1001, 'msg' => '获取openid失败!' ]; return $this->api($data); } $userinfo = UserInfoModel::where('openid', $openid)->first(['id', 'nickname', 'openid']); if (!$userinfo) { $data['openid'] = $openid; $data['nickname'] = $request->get('nickName'); $data['avatar'] = $request->get('avatar'); $userinfo = UserInfoModel::create($data); } if (Auth::loginUsingId($userinfo->id)) { $user = Auth::user(); $token = $user->createToken($user->id . '-' . $user->openid)->accessToken; return $this->api(compact('token', 'user')); } else { return $this->error(ErrorCode::INCORRECT_USER_OR_PASS); } } /** * @api {get} /api/auth/logout 退出(logout) * @apiDescription 退出(logout) * @apiGroup Auth * @apiPermission Passport * @apiVersion 0.1.0 * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * "state": true, * "code": 0, * "message": "", * "data": { * "result": true/false * } * } * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request * { * "state": false, * "code": 1104, * "message": "退出失败", * "data": null * } * 可能出现的错误代码: * 1104 LOGOUT_FAILED 退出失败 */ public function logout() { if (Auth::user()->token()->delete()) { return $this->api(['result' => true]); } return $this->error(ErrorCode::LOGOUT_FAILED); } }