BaiduPanController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace App\Http\Api;
  3. use App\Services\Login\LoginTokenService;
  4. use GuzzleHttp\Client;
  5. use GuzzleHttp\Exception\GuzzleException;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Facades\Request;
  9. use Illuminate\Support\Str;
  10. class BaiduPanController extends HttpBaseController
  11. {
  12. private string $appKey = 'nSE5u2ldxo0W3OjQ7To5R9bIeUD1o372';
  13. protected string $secretKey = 'Bj1nL69nTMYroU9nPmEmxQ8WbDiLHqMB';
  14. private string $signKey = 'N2w2BKit!0s1Tw9BvEG+=t!6By4KLogd';
  15. private string $redirect_uri = 'https://www.qasimblog.com/baiduPan/notice';
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. $this->setWebsite(__NAMESPACE__);
  20. if ($this->isTest()) {
  21. $this->appKey = 'XTRLGLW0bxs8L8RcwbcArrbt3NtQFljt';
  22. $this->secretKey = 'sAal4IGQOdbAbNYa06FE01VVU50jfhKZ';
  23. $this->redirect_uri = 'https://audio.zhuyou360.com/baiduPan/notice';
  24. }
  25. }
  26. public function getAuthUrl()
  27. {
  28. $this->isLoginJson();
  29. $token = $this->getToken();
  30. $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);
  31. return responseMessage(1001, '', $url);
  32. }
  33. public function notice()
  34. {
  35. $code = Request::input('code');
  36. if (empty($code)) {
  37. echo "授权失败,请重试!";
  38. die();
  39. }
  40. $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);
  41. $requestArr = $this->getHttp($url);
  42. if (is_array($requestArr)) {
  43. $isSuccess = $this->save($requestArr);
  44. if ($isSuccess) {
  45. $msg = '恭喜你,授权成功!请回到软件中使用吧!';
  46. } else {
  47. $msg = '服务器错误,请稍后再试!';
  48. }
  49. } else {
  50. $isSuccess = false;
  51. $msg = "授权失败,请重试!";
  52. }
  53. return view('home/baidu', ['is_success' => $isSuccess, 'msg' => $msg]);
  54. }
  55. /**
  56. * 获取百度网盘用户的授权信息
  57. * @return \Illuminate\Http\JsonResponse
  58. */
  59. public function getPanUserList()
  60. {
  61. $this->isLoginJson();
  62. $list = DB::table('baidu_pan_token')
  63. ->select(['mid', 'pan_user_info', 'access_token', 'expires_in'])
  64. ->where('user_id', $this->userId)->where('is_delete', 0)->get();
  65. return responseMessage(1001, 'success', $list);
  66. }
  67. // 获取baidu,token
  68. public function getBaiduAuth()
  69. {
  70. $mid = Request::post('mid');
  71. if (empty($mid)) {
  72. return responseMessage(2003, '参数错误,请重试!');
  73. }
  74. $this->isLoginJson();
  75. $panInfo = DB::table('baidu_pan_token')
  76. ->where('user_id', $this->userId)
  77. ->where('mid', $mid)
  78. ->where('is_delete', 0)->first();
  79. if ($panInfo) {
  80. if (time() - $panInfo->created_at >= $panInfo->expires_in) {
  81. $result = $this->refresh($panInfo);
  82. if ($result) {
  83. return responseMessage(1001, '', $result);
  84. } else {
  85. return responseMessage(2001, '获取授权失败,请重新授权!');
  86. }
  87. } else {
  88. return responseMessage(1001, '', $panInfo->access_token);
  89. }
  90. } else {
  91. return responseMessage(2002, '获取授权失败,请重新授权!');
  92. }
  93. }
  94. /**
  95. * 删除网盘用户
  96. */
  97. public function delPanUser()
  98. {
  99. $mid = Request::post('mid');
  100. if (empty($mid)) {
  101. return responseMessage(2003, '参数错误,请重试!');
  102. }
  103. $this->isLoginJson();
  104. $panInfo = DB::table('baidu_pan_token')
  105. ->where('user_id', $this->userId)
  106. ->where('mid', $mid)
  107. ->where('is_delete', 0)->first();
  108. if ($panInfo) {
  109. $isSuccess = DB::table('baidu_pan_token')->where('id', $panInfo->id)->update(['is_delete' => 1]);
  110. if ($isSuccess) {
  111. return responseMessage(1001, '操作成功!');
  112. } else {
  113. return responseMessage(2002, '删除失败,请稍后再试!');
  114. }
  115. } else {
  116. return responseMessage(2001, '删除失败,请稍后再试!');
  117. }
  118. }
  119. /**
  120. * 获取网盘信息
  121. *
  122. * @return \Illuminate\Http\JsonResponse
  123. */
  124. public function getPanInfo()
  125. {
  126. $this->isLoginJson();
  127. $mid = Request::post('mid');
  128. if (empty($mid)) {
  129. return responseMessage(2003, '参数错误,请重试!');
  130. }
  131. $panInfo = DB::table('baidu_pan_token')
  132. ->where('user_id', $this->userId)
  133. ->where('mid', $mid)
  134. ->where('is_delete', 0)->first();
  135. if ($panInfo) {
  136. $info = $this->getPanUserInfo($panInfo->access_token);
  137. if ($info) {
  138. return responseMessage(1001, 'success', $info);
  139. } else {
  140. return responseMessage(2004, '获取授权失败,请重试!');
  141. }
  142. } else {
  143. return responseMessage(2002, '获取授权失败,请重新授权!');
  144. }
  145. }
  146. /**
  147. * 保存信息
  148. *
  149. * @throws \Throwable
  150. */
  151. private function save($requestArr)
  152. {
  153. $access_token = $requestArr['access_token'];
  154. $expires_in = $requestArr['expires_in'];
  155. $refresh_token = $requestArr['refresh_token'];
  156. $scope = $requestArr['scope'];
  157. $state = Request::input('state'); // 为token信息
  158. $state = base64_decode($state);
  159. $panUserInfo = $this->getPanUserInfo($access_token);
  160. if (empty($panUserInfo)) {
  161. echo "获取用户信息失败,请重新授权!";
  162. die();
  163. }
  164. $uk = $panUserInfo['uk'];
  165. // 获取当前的用户的信息
  166. $loginToken = new LoginTokenService('user_access_token');
  167. $tokenInfo = $loginToken->findToken($state);
  168. if (empty($tokenInfo)) {
  169. echo "token已经过期,请重新授权!" . $state;
  170. die();
  171. }
  172. $userId = $tokenInfo->user_id;
  173. $data = [
  174. 'user_id' => $userId,
  175. 'uk' => $uk,
  176. 'pan_user_info' => json_encode($panUserInfo),
  177. 'access_token' => $access_token,
  178. 'expires_in' => $expires_in,
  179. 'refresh_token' => $refresh_token,
  180. 'scope' => $scope,
  181. 'status' => 1
  182. ];
  183. $panInfo = DB::table('baidu_pan_token')
  184. ->where('user_id', $userId)
  185. ->where('uk', $uk)
  186. ->where('is_delete', 0)->first();
  187. if ($panInfo) {
  188. // 更新数据
  189. $isSuccess = DB::table('baidu_pan_token')->where('id', $panInfo->id)->update($data);
  190. } else {
  191. // 保存到数据库
  192. $data['mid'] = Str::random(12);
  193. $data['created_at'] = time();
  194. $data['updated_at'] = time();
  195. $isSuccess = DB::table('baidu_pan_token')->insert($data);
  196. }
  197. return $isSuccess;
  198. }
  199. private function getPanUserInfo($access_token)
  200. {
  201. $url = 'https://pan.baidu.com/rest/2.0/xpan/nas?method=uinfo&access_token=' . $access_token;
  202. $requestArr = $this->getHttp($url);
  203. if (is_array($requestArr)) {
  204. Log::info('===========', $requestArr);
  205. return $requestArr;
  206. } else {
  207. return false;
  208. }
  209. }
  210. /**
  211. * 刷新权限
  212. *
  213. * @param $panInfo
  214. * @return mixed
  215. */
  216. private function refresh($panInfo): mixed
  217. {
  218. $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;
  219. $requestArr = $this->getHttp($url);
  220. if (is_array($requestArr)) {
  221. // 更新数据
  222. $data = [
  223. 'user_id' => $this->userId,
  224. 'access_token' => $requestArr['access_token'],
  225. 'expires_in' => $requestArr['expires_in'],
  226. 'refresh_token' => $requestArr['refresh_token'],
  227. 'scope' => $requestArr['scope'],
  228. ];
  229. DB::table('baidu_pan_token')->where('id', $panInfo->id)->update($data);
  230. return $requestArr['access_token'];
  231. } else {
  232. return false;
  233. }
  234. }
  235. private function getHttp($url)
  236. {
  237. $client = new Client();
  238. try {
  239. $request = $client->get($url)->getBody()->getContents();
  240. $requestArr = json_decode($request, true);
  241. return $requestArr;
  242. } catch (GuzzleException $e) {
  243. return $e->getMessage();
  244. }
  245. }
  246. }