setWebsite(__NAMESPACE__); if ($this->isTest()) { $this->appKey = 'XTRLGLW0bxs8L8RcwbcArrbt3NtQFljt'; $this->secretKey = 'sAal4IGQOdbAbNYa06FE01VVU50jfhKZ'; $this->redirect_uri = 'https://audio.xingyousoft.com/baiduPan/notice'; } } public function getAuthUrl() { $this->isLoginJson(); $token = $this->getToken(); $url = 'http://openapi.baidu.com/oauth/2.0/authorize?response_type=code&client_id=' . $this->appKey . '&redirect_uri=' . urlencode($this->redirect_uri) . '&scope=basic,netdisk&display=page&force_login=1&login_type=sms&state=' . base64_encode($token); return responseMessage(1001, '', $url); } public function notice() { $code = Request::input('code'); if (empty($code)) { echo "授权失败,请重试!"; die(); } $url = 'https://openapi.baidu.com/oauth/2.0/token?grant_type=authorization_code&code=' . $code . '&client_id=' . $this->appKey . '&client_secret=' . $this->secretKey . '&redirect_uri=' . urlencode($this->redirect_uri); $requestArr = $this->getHttp($url); if (is_array($requestArr)) { $isSuccess = $this->save($requestArr); if ($isSuccess) { $msg = '恭喜你,授权成功!请回到软件中使用吧!'; } else { $msg = '服务器错误,请稍后再试!'; } } else { $isSuccess = false; $msg = "授权失败,请重试!"; } return view('home/baidu', ['is_success' => $isSuccess, 'msg' => $msg]); } /** * 获取百度网盘用户的授权信息 * @return \Illuminate\Http\JsonResponse */ public function getPanUserList() { $this->isLoginJson(); $list = DB::table('baidu_pan_token') ->select(['mid', 'pan_user_info', 'access_token', 'expires_in']) ->where('user_id', $this->userId)->where('is_delete', 0)->get(); return responseMessage(1001, 'success', $list); } // 获取baidu,token public function getBaiduAuth() { $mid = Request::post('mid'); if (empty($mid)) { return responseMessage(2003, '参数错误,请重试!'); } $this->isLoginJson(); $panInfo = DB::table('baidu_pan_token') ->where('user_id', $this->userId) ->where('mid', $mid) ->where('is_delete', 0)->first(); if ($panInfo) { if (time() - $panInfo->created_at >= $panInfo->expires_in) { $result = $this->refresh($panInfo); if ($result) { return responseMessage(1001, '', $result); } else { return responseMessage(2001, '获取授权失败,请重新授权!'); } } else { return responseMessage(1001, '', $panInfo->access_token); } } else { return responseMessage(2002, '获取授权失败,请重新授权!'); } } /** * 删除网盘用户 */ public function delPanUser() { $mid = Request::post('mid'); if (empty($mid)) { return responseMessage(2003, '参数错误,请重试!'); } $this->isLoginJson(); $panInfo = DB::table('baidu_pan_token') ->where('user_id', $this->userId) ->where('mid', $mid) ->where('is_delete', 0)->first(); if ($panInfo) { $isSuccess = DB::table('baidu_pan_token')->where('id', $panInfo->id)->update(['is_delete' => 1]); if ($isSuccess) { return responseMessage(1001, '操作成功!'); } else { return responseMessage(2002, '删除失败,请稍后再试!'); } } else { return responseMessage(2001, '删除失败,请稍后再试!'); } } /** * 获取网盘信息 * * @return \Illuminate\Http\JsonResponse */ public function getPanInfo() { $this->isLoginJson(); $mid = Request::post('mid'); if (empty($mid)) { return responseMessage(2003, '参数错误,请重试!'); } $panInfo = DB::table('baidu_pan_token') ->where('user_id', $this->userId) ->where('mid', $mid) ->where('is_delete', 0)->first(); if ($panInfo) { $info = $this->getPanUserInfo($panInfo->access_token); if ($info) { return responseMessage(1001, 'success', $info); } else { return responseMessage(2004, '获取授权失败,请重试!'); } } else { return responseMessage(2002, '获取授权失败,请重新授权!'); } } /** * 保存信息 * * @throws \Throwable */ private function save($requestArr) { $access_token = $requestArr['access_token']; $expires_in = $requestArr['expires_in']; $refresh_token = $requestArr['refresh_token']; $scope = $requestArr['scope']; $state = Request::input('state'); // 为token信息 $state = base64_decode($state); $panUserInfo = $this->getPanUserInfo($access_token); if (empty($panUserInfo)) { echo "获取用户信息失败,请重新授权!"; die(); } $uk = $panUserInfo['uk']; // 获取当前的用户的信息 $loginToken = new LoginTokenService('user_access_token'); $tokenInfo = $loginToken->findToken($state); if (empty($tokenInfo)) { echo "token已经过期,请重新授权!" . $state; die(); } $userId = $tokenInfo->user_id; $data = [ 'user_id' => $userId, 'uk' => $uk, 'pan_user_info' => json_encode($panUserInfo), 'access_token' => $access_token, 'expires_in' => $expires_in, 'refresh_token' => $refresh_token, 'scope' => $scope, 'status' => 1 ]; $panInfo = DB::table('baidu_pan_token') ->where('user_id', $userId) ->where('uk', $uk) ->where('is_delete', 0)->first(); if ($panInfo) { // 更新数据 $isSuccess = DB::table('baidu_pan_token')->where('id', $panInfo->id)->update($data); } else { // 保存到数据库 $data['mid'] = Str::random(12); $data['created_at'] = time(); $data['updated_at'] = time(); $isSuccess = DB::table('baidu_pan_token')->insert($data); } return $isSuccess; } private function getPanUserInfo($access_token) { $url = 'https://pan.baidu.com/rest/2.0/xpan/nas?method=uinfo&access_token=' . $access_token; $requestArr = $this->getHttp($url); if (is_array($requestArr)) { Log::info('===========', $requestArr); return $requestArr; } else { return false; } } /** * 刷新权限 * * @param $panInfo * @return mixed */ private function refresh($panInfo): mixed { $url = 'https://openapi.baidu.com/oauth/2.0/token?grant_type=refresh_token&refresh_token=' . $panInfo->refresh_token . '&client_id=' . $this->appKey . '&client_secret=' . $this->secretKey; $requestArr = $this->getHttp($url); if (is_array($requestArr)) { // 更新数据 $data = [ 'user_id' => $this->userId, 'access_token' => $requestArr['access_token'], 'expires_in' => $requestArr['expires_in'], 'refresh_token' => $requestArr['refresh_token'], 'scope' => $requestArr['scope'], ]; DB::table('baidu_pan_token')->where('id', $panInfo->id)->update($data); return $requestArr['access_token']; } else { return false; } } private function getHttp($url) { $client = new Client(); try { $request = $client->get($url)->getBody()->getContents(); $requestArr = json_decode($request, true); return $requestArr; } catch (GuzzleException $e) { return $e->getMessage(); } } }