Api.php 8.3 KB

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