Api.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 = "https";
  61. if (isset($urlInfo['scheme'])) {
  62. $scheme = $urlInfo["scheme"];
  63. }
  64. $realUrl = $scheme . "://" . $host;
  65. $client = new Client();
  66. $response = $client->get($realUrl);
  67. $status = $response->getStatusCode();
  68. if ($status == 200) {
  69. $body = $response->getBody()->getContents();
  70. $dom = new Dom();
  71. $dom->loadStr($body);
  72. $title = $dom->find('title');
  73. if (count($title) > 0) {
  74. $title = $title->innerText;
  75. }
  76. try {
  77. $list = $dom->find('[rel="icon"]');
  78. if (count($list) > 0) {
  79. $icon = $list->href;
  80. if (preg_match('/\.(png|jpg|jpeg|ico|svg )$/', $icon, $matches)) {
  81. $fileFormat = $matches[1];
  82. $iconInfo = parse_url($icon);
  83. if (!isset($iconInfo['scheme'])) {
  84. $icon = $realUrl . $icon;
  85. }
  86. $icon = $this->downloadFile($icon, md5($realUrl) . '.' . $fileFormat);
  87. if ($icon) {
  88. $icon = $cdn . $icon;
  89. }
  90. } else {
  91. $icon = '';
  92. }
  93. }
  94. } catch (\ErrorException $e) {
  95. }
  96. }
  97. if (strlen($icon) == 0) {
  98. $client = new Client();
  99. $response = $client->get($realUrl . '/favicon.ico');
  100. $status = $response->getStatusCode();
  101. if ($status == 200) {
  102. $icon = $realUrl . '/favicon.ico';
  103. $icon = $this->downloadFile($icon, md5($realUrl) . ".ico");
  104. if ($icon) {
  105. $icon = $cdn . $icon;
  106. }
  107. }
  108. }
  109. if (strlen($icon) > 0) {
  110. return $this->success(['src' => $icon, 'name' => $title]);
  111. }
  112. }
  113. return $this->error('no');
  114. }
  115. private function downloadFile($url, $name)
  116. {
  117. $client = new Client();
  118. $path = '/images/' . date('Y/m/d/');
  119. $remotePath = public_path() . $path;
  120. $downloadPath = $remotePath . $name;
  121. if (!is_dir($remotePath)) {
  122. mkdir($remotePath, 0755, true);
  123. }
  124. try {
  125. $response = $client->request('GET', $url, [
  126. 'sink' => $downloadPath
  127. ]);
  128. return $path . $name;
  129. } catch (RequestException $e) {
  130. }
  131. return false;
  132. }
  133. function renderIco(): \think\Response
  134. {
  135. $send = $this->request->get('seed');
  136. $client = new Client();
  137. $remote_avatar = $this->Setting('remote_avatar', 'https://avatar.mtab.cc/6.x/thumbs/png?seed=', true);
  138. $response = $client->get($remote_avatar . urlencode($send), [
  139. 'stream' => true,
  140. 'timeout' => 10,
  141. ]);
  142. return response($response->getBody(), 200, ['Content-Type' => 'image/png']);
  143. }
  144. function upload(): \think\response\Json
  145. {
  146. $file = $this->request->file('file');
  147. if (empty($file)) {
  148. return $this->error('not File');
  149. }
  150. if ($file->getSize() > 1024 * 1024 * 5) {
  151. return $this->error('max fileSize is 5M');
  152. }
  153. if (in_array(strtolower($file->getOriginalExtension()), ['png', 'jpg', 'jpeg', 'webp'])) {
  154. // 验证文件并保存
  155. try {
  156. // 构建保存路径
  157. $savePath = '/images/' . date('Y/m/d');
  158. $hash = Str::random(32);
  159. $fileName = $hash . '.' . $file->getOriginalExtension();
  160. $filePath = Filesystem::disk('images')->putFileAs($savePath, $file, $fileName);
  161. $cdn = $this->Setting('assets_host', '/', true);
  162. return $this->success(['url' => $cdn . $filePath]);
  163. } catch (\think\exception\ValidateException $e) {
  164. // 验证失败,给出错误提示
  165. // ...
  166. }
  167. }
  168. return $this->error('上传失败');
  169. }
  170. function AdminUpload(): \think\response\Json
  171. {
  172. $this->getAdmin();
  173. $file = $this->request->file('file');
  174. if (empty($file)) {
  175. return $this->error('not File');
  176. }
  177. if ($file->getSize() > 1024 * 1024 * 5) {
  178. return $this->error('max fileSize is 5M');
  179. }
  180. // 验证文件并保存
  181. try {
  182. // 构建保存路径
  183. $savePath = '/images/' . date('Y/m/d');
  184. $hash = Str::random(32);
  185. $fileName = $hash . '.' . $file->getOriginalExtension();
  186. $filePath = Filesystem::disk('images')->putFileAs($savePath, $file, $fileName);
  187. $cdn = $this->Setting('assets_host', '/', true);
  188. return $this->success(['url' => $cdn . $filePath]);
  189. } catch (\think\exception\ValidateException $e) {
  190. // 验证失败,给出错误提示
  191. // ...
  192. }
  193. return $this->error('上传失败');
  194. }
  195. }