Api.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use app\model\SettingModel;
  5. use GuzzleHttp\Client;
  6. use GuzzleHttp\Exception\RequestException;
  7. use PHPHtmlParser\Dom;
  8. use think\facade\Cache;
  9. use think\facade\Filesystem;
  10. use think\helper\Str;
  11. class Api extends BaseController
  12. {
  13. public function site(): \think\response\Json
  14. {
  15. return $this->success("ok", [
  16. 'email' => $this->Setting('email', '')
  17. ]);
  18. }
  19. public function background()
  20. {
  21. $bg = $this->Setting('backgroundImage');
  22. if ($bg) {
  23. return redirect($bg, 302);
  24. }
  25. return download("static/background.jpeg",);
  26. }
  27. //获取邮件验证码
  28. function getMailCode(): \think\response\Json
  29. {
  30. $mail = $this->request->post("mail", false);
  31. $code = rand(100000, 999999);
  32. if ($mail) {
  33. if (Cache::get('code' . $mail)) {
  34. return $this->success("请勿频繁获取验证码");
  35. }
  36. $status = \Mail::send($mail, "<h2>您的验证码是: <b style='color:#1d5cdc'>$code</b></h2>");
  37. if ($status) {
  38. Cache::set('code' . $mail, $code, 60);
  39. return $this->success("发送成功");
  40. }
  41. }
  42. return $this->error('发送失败');
  43. }
  44. function getIcon(): \think\response\Json
  45. {
  46. $avatar = $this->request->post('avatar');
  47. if ($avatar) {
  48. $remote_avatar = $this->Setting("remote_avatar", "https://avatar.mtab.cc/6.x/thumbs/png?seed=", true);
  49. $str = $this->downloadFile($remote_avatar . $avatar, md5($avatar) . '.png');
  50. return $this->success(['src' => $str]);
  51. }
  52. $url = $this->request->post('url', false);
  53. $icon = "";
  54. $cdn = $this->Setting('assets_host', '');
  55. if ($url) {
  56. $urlInfo = parse_url($url);
  57. $host = $urlInfo['host'] ?? $urlInfo['path'];
  58. $title = '';
  59. $scheme = "https";
  60. if (isset($urlInfo['scheme'])) {
  61. $scheme = $urlInfo["scheme"];
  62. }
  63. $realUrl = $scheme . "://" . $host;
  64. $client = new Client();
  65. $response = $client->get($realUrl);
  66. $status = $response->getStatusCode();
  67. if ($status == 200) {
  68. $body = $response->getBody()->getContents();
  69. $dom = new Dom();
  70. $dom->loadStr($body);
  71. $title = $dom->find('title');
  72. if (count($title) > 0) {
  73. $title = $title->innerText;
  74. }
  75. try {
  76. $list = $dom->find('[rel="icon"]');
  77. if (count($list) > 0) {
  78. $icon = $list->href;
  79. if (preg_match('/\.(png|jpg|jpeg|ico|svg )$/', $icon, $matches)) {
  80. $fileFormat = $matches[1];
  81. $iconInfo = parse_url($icon);
  82. if (!isset($iconInfo['scheme'])) {
  83. $icon = $realUrl . $icon;
  84. }
  85. $icon = $this->downloadFile($icon, md5($realUrl) . '.' . $fileFormat);
  86. if ($icon) {
  87. $icon = $cdn . $icon;
  88. }
  89. } else {
  90. $icon = '';
  91. }
  92. }
  93. } catch (\ErrorException $e) {
  94. }
  95. }
  96. if (strlen($icon) == 0) {
  97. $client = new Client();
  98. $response = $client->get($realUrl . '/favicon.ico');
  99. $status = $response->getStatusCode();
  100. if ($status == 200) {
  101. $icon = $realUrl . '/favicon.ico';
  102. $icon = $this->downloadFile($icon, md5($realUrl) . ".ico");
  103. if ($icon) {
  104. $icon = $cdn . $icon;
  105. }
  106. }
  107. }
  108. if (strlen($icon) > 0) {
  109. return $this->success(['src' => $icon, 'name' => $title]);
  110. }
  111. }
  112. return $this->error('no');
  113. }
  114. private function downloadFile($url, $name)
  115. {
  116. $client = new Client();
  117. $path = '/images/' . date('Y/m/d/');
  118. $remotePath = public_path() . $path;
  119. $downloadPath = $remotePath . $name;
  120. if (!is_dir($remotePath)) {
  121. mkdir($remotePath, 0755, true);
  122. }
  123. try {
  124. $response = $client->request('GET', $url, [
  125. 'sink' => $downloadPath
  126. ]);
  127. return $path . $name;
  128. } catch (RequestException $e) {
  129. }
  130. return false;
  131. }
  132. function renderIco(): \think\Response
  133. {
  134. $send = $this->request->get('seed');
  135. $client = new Client();
  136. $remote_avatar = $this->Setting('remote_avatar', 'https://avatar.mtab.cc/6.x/thumbs/png?seed=', true);
  137. $response = $client->get($remote_avatar . urlencode($send), [
  138. 'stream' => true,
  139. 'timeout' => 10,
  140. ]);
  141. return response($response->getBody(), 200, ['Content-Type' => 'image/png']);
  142. }
  143. function upload(): \think\response\Json
  144. {
  145. $file = $this->request->file('file');
  146. if (empty($file)) {
  147. return $this->error('not File');
  148. }
  149. if ($file->getSize() > 1024 * 1024 * 5) {
  150. return $this->error('max fileSize is 5M');
  151. }
  152. if (in_array(strtolower($file->getOriginalExtension()), ['png', 'jpg', 'jpeg', 'webp'])) {
  153. // 验证文件并保存
  154. try {
  155. // 构建保存路径
  156. $savePath = '/images/' . date('Y/m/d');
  157. $hash = Str::random(32);
  158. $fileName = $hash . '.' . $file->getOriginalExtension();
  159. $filePath = Filesystem::disk('images')->putFileAs($savePath, $file, $fileName);
  160. $cdn = $this->Setting('assets_host', '/', true);
  161. return $this->success(['url' => $cdn . $filePath]);
  162. } catch (\think\exception\ValidateException $e) {
  163. // 验证失败,给出错误提示
  164. // ...
  165. }
  166. }
  167. return $this->error('上传失败');
  168. }
  169. function AdminUpload(): \think\response\Json
  170. {
  171. $this->getAdmin();
  172. $file = $this->request->file('file');
  173. if (empty($file)) {
  174. return $this->error('not File');
  175. }
  176. if ($file->getSize() > 1024 * 1024 * 5) {
  177. return $this->error('max fileSize is 5M');
  178. }
  179. // 验证文件并保存
  180. try {
  181. // 构建保存路径
  182. $savePath = '/images/' . date('Y/m/d');
  183. $hash = Str::random(32);
  184. $fileName = $hash . '.' . $file->getOriginalExtension();
  185. $filePath = Filesystem::disk('images')->putFileAs($savePath, $file, $fileName);
  186. $cdn = $this->Setting('assets_host', '/', true);
  187. return $this->success(['url' => $cdn . $filePath]);
  188. } catch (\think\exception\ValidateException $e) {
  189. // 验证失败,给出错误提示
  190. // ...
  191. }
  192. return $this->error('上传失败');
  193. }
  194. }