123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <?php
- namespace App\Http\Api;
- use App\Services\Login\LoginTokenService;
- use GuzzleHttp\Client;
- use GuzzleHttp\Exception\GuzzleException;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Request;
- use Illuminate\Support\Str;
- class BaiduPanController extends HttpBaseController
- {
- private string $appKey = 'nSE5u2ldxo0W3OjQ7To5R9bIeUD1o372';
- protected string $secretKey = 'Bj1nL69nTMYroU9nPmEmxQ8WbDiLHqMB';
- private string $signKey = 'N2w2BKit!0s1Tw9BvEG+=t!6By4KLogd';
- private string $redirect_uri = 'https://www.qasimblog.com/baiduPan/notice';
- public function __construct()
- {
- parent::__construct();
- $this->setWebsite(__NAMESPACE__);
- if ($this->isTest()) {
- $this->appKey = 'XTRLGLW0bxs8L8RcwbcArrbt3NtQFljt';
- $this->secretKey = 'sAal4IGQOdbAbNYa06FE01VVU50jfhKZ';
- $this->redirect_uri = 'https://audio.zhuyou360.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();
- }
- }
- }
|