Api.php 8.8 KB

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