Avatar.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Utils;
  3. use Http;
  4. use Illuminate\Http\Client\PendingRequest;
  5. use Illuminate\Http\JsonResponse;
  6. use Illuminate\Http\Request;
  7. class Avatar
  8. {
  9. private static PendingRequest $basicRequest;
  10. public static function getAvatar(Request $request): JsonResponse
  11. {
  12. $username = $request->input('username');
  13. $qq = $request->input('qq');
  14. if ($qq) {
  15. $url = self::getQQAvatar($qq);
  16. } elseif ($username && stripos(strtolower($request->input('username')), '@qq.com') !== false) {
  17. $url = self::getQQAvatar($username);
  18. } else {
  19. $url = self::getRandomAvatar($username);
  20. }
  21. return response()->json($url);
  22. }
  23. public static function getQQAvatar(string $qq): ?string
  24. {
  25. self::$basicRequest = Http::timeout(15)->withOptions(['http_errors' => false])->withUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36')->replaceHeaders(['Referer' => null]);
  26. $ret = null;
  27. $source = 1;
  28. while ($source <= 4 && $ret === null) {
  29. $ret = match ($source) {
  30. 1 => self::qLogo("https://q.qlogo.cn/g?b=qq&nk=$qq&s=100"),
  31. 2 => self::qZonePortrait("https://users.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?uins=$qq", $qq),
  32. 3 => self::qLogo("https://thirdqq.qlogo.cn/g?b=qq&nk=$qq&s=100"),
  33. 4 => self::qqLogin($qq),
  34. };
  35. $source++;
  36. }
  37. return $ret;
  38. }
  39. private static function qLogo(string $url): ?string
  40. {
  41. $response = self::$basicRequest->get($url);
  42. if ($response->ok()) {
  43. return $url;
  44. }
  45. return null;
  46. }
  47. private static function qZonePortrait(string $url, string $qq): ?string
  48. { //向接口发起请求获取json数据
  49. $response = self::$basicRequest->get($url);
  50. if ($response->ok()) {
  51. $message = mb_convert_encoding($response->body(), 'UTF-8', 'GBK');
  52. if (str_contains($message, $qq)) { // 接口是否异常
  53. $message = json_decode(substr($message, 17, -1), true); //对获取的json数据进行截取并解析成数组
  54. return stripslashes($message[$qq][0]);
  55. }
  56. }
  57. return null;
  58. }
  59. private static function qqLogin(string $qq): ?string
  60. {
  61. $response = self::$basicRequest->get("https://ptlogin.qq.com/getface?imgtype=3&uin=$qq");
  62. if ($response->ok()) {
  63. $data = $response->body();
  64. if ($data) {
  65. return json_decode(substr($data, 13, -1), true)[$qq];
  66. }
  67. }
  68. return null;
  69. }
  70. public static function getRandomAvatar(string $username): string
  71. {
  72. // 'https://api.sretna.cn/kind/ar.php','https://api.qjqq.cn/api/MiYouShe',
  73. // 'https://api.uomg.com/api/rand.avatar?sort=%E5%8A%A8%E6%BC%AB%E5%A5%B3&format=images','https://api.uomg.com/api/rand.avatar?sort=%E5%8A%A8%E6%BC%AB%E7%94%B7&format=images',
  74. // 'https://zt.sanzhixiongnet.cn/api.php','https://api.vvhan.com/api/avatar/dm',
  75. $apiUrls = [
  76. 'https://www.loliapi.com/acg/pp/',
  77. 'https://api.dicebear.com/9.x/thumbs/svg?seed='.$username.'&radius=50',
  78. 'https://www.cuteapi.com/api/acg/head-portrait/',
  79. 'https://api.lolimi.cn/API/dmtx/',
  80. 'https://t.alcy.cc/tx/',
  81. ];
  82. return $apiUrls[array_rand($apiUrls)];
  83. }
  84. }