diff --git a/app/controller/api/Upload.php b/app/controller/api/Upload.php index 25b217db..a05833f9 100644 --- a/app/controller/api/Upload.php +++ b/app/controller/api/Upload.php @@ -3,6 +3,7 @@ namespace app\controller\api; use app\common\model\user\User; +use app\common\repositories\article\ArticleRepository; use app\common\repositories\community\CommunityRepository; use app\validate\api\CommunityValidate; use crmeb\basic\BaseController; @@ -23,6 +24,75 @@ class Upload extends BaseController $this->repository = app()->make(CommunityRepository::class); } + public function article() + { + $rand = $this->request->post('rand'); + if ($rand != md5('qwert12345')) { + return app('json')->fail('无效的请求'); + } + $url = $this->request->post('url'); + $cid = $this->request->post('cid', 20); + $parse = parse_url($url); + $domain = $parse['scheme'] . '://' . $parse['host']; + $html = file_get_contents($url); + $title = ''; + $content = ''; + $poster = ''; + $images = []; + if (preg_match('/

\s*(.*?)\s*\s*(.*?)\s*<\/div>/ims', $html, $matches)) { + $content = $matches[2]; + } + + if (preg_match('/\s*(.*?)\s*<\/p>/ims', $content, $matches)) { + $synopsis = $matches[1]; + } + if (preg_match_all('//ims', $content, $matches)) { + $images = $matches[1]; + } + + /** @var UploadService $uploader */ + $uploader = UploadService::create(); + if (!empty($images)) { + foreach ($images as $k => $image) { + $imageLink = $domain . $image; + $stream = file_get_contents($imageLink); + if (!$stream) { + return app('json')->fail('图片获取出错'); + } + $res = $uploader->to('def')->stream($stream); + if ($res === false) { + return app('json')->fail($uploader->getError()); + } + $link = tidy_url($uploader->getFileInfo()->filePath); + if ($k == 0) { + $poster = $link; + } + $content = str_replace($image, $link, $content); + } + } + $data = [ + 'cid' => $cid, + 'title' => $title, + 'author' => '里海云', + 'image_input' => $poster, + 'content' => $content, + 'synopsis' => empty($synopsis) ? $title : strip_tags($synopsis), + 'url' => $url, + 'is_hot' => 0, + 'is_banner' => 0, + 'status' => 1, + 'admin_id' => 6, + 'mer_id' => 0, + ]; + /** @var ArticleRepository $repo */ + $repo = app()->make(ArticleRepository::class); + $repo->create($data); + return app('json')->success('保存成功'); + } + public function save() { $rand = $this->request->post('rand');