Api.php 8.6 KB

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