Merge branch 'master' into dev

This commit is contained in:
luofei 2023-06-15 14:40:24 +08:00
commit 06b5715403
5 changed files with 183 additions and 1 deletions

View File

@ -0,0 +1,178 @@
<?php
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;
use crmeb\services\UploadService;
use think\App;
class Upload extends BaseController
{
/**
* @var CommunityRepository
*/
public $repository;
public function __construct(App $app)
{
parent::__construct($app);
$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('/<h2 class="title">\s*(.*?)\s*</ims', $html, $matches)) {
$title = $matches[1];
}
if (preg_match('/<div class="conTxt"(.*?)>\s*(.*?)\s*<\/div>/ims', $html, $matches)) {
$content = $matches[2];
}
if (preg_match('/<p.*?>\s*(.*?)\s*<\/p>/ims', $content, $matches)) {
$synopsis = $matches[1];
}
if (preg_match_all('/<img.*?src="(.*?)".*?>/ims', $content, $matches)) {
$images = $matches[1];
}
/** @var UploadService $uploader */
$uploader = UploadService::create();
if (!empty($images)) {
foreach ($images as $k => $image) {
if (!preg_match('/^http/', $image)) {
$imageLink = $domain . $image;
} else {
$imageLink = $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');
if ($rand != md5('qwert12345')) {
return app('json')->fail('无效的请求');
}
$file = $this->request->file('file');
if (!$file) {
return app('json')->fail('请上传文件');
}
//上传视频
validate(["file|视频" => [
'fileSize' => config('upload.filesize'),
'fileExt' => 'mp4,mov',
'fileMime' => 'video/mp4,video/quicktime',
]])->check(['file' => $file]);
/** @var UploadService $uploader */
$uploader = UploadService::create();
$videoRes = $uploader->to('media')->validate([])->move('file');
if ($videoRes === false) {
return app('json')->fail($uploader->getError());
}
$videoLink = tidy_url($uploader->getFileInfo()->filePath);
$posterProcess = $videoLink . '?x-oss-process=video/snapshot,t_0,f_jpg,w_0,h_0,m_fast,ar_auto';
//上传封面
/** @var UploadService $posterUploader */
$posterUploader = UploadService::create();
$posterRes = $posterUploader->to('def')->stream(file_get_contents($posterProcess));
if ($posterRes === false) {
return app('json')->fail($posterUploader->getError());
}
$poster = tidy_url($posterUploader->getFileInfo()->filePath);
//组装数据并保存
$users = file_get_contents('user.json');
$users = json_decode($users, true);
$topicIds = [40 => 69, 42 => 70, 43 => 73];
$userIndex = array_rand($users);
$userPhone = $users[$userIndex]['phone'];
$userId = User::getInstance()->where('account', $userPhone)->value('uid');
$topicId = array_rand($topicIds);
$categoryId = $topicIds[$topicId];
$filename = rtrim($file->getOriginalName(), '.mp4');
$community = [
'image' => [$poster],
'content' => $filename,
'topic_id' => $topicId,
'spu_id' => [],
'video_link' => $videoLink,
'category_id' => $categoryId,
'topic' => [],
'is_type' => 2,
'uid' => $userId,
];
$data = $this->checkParams($community);
$id = $this->repository->create($data);
return app('json')->success('上传成功', ['id' => $id, 'user_id' => $userId, 'phone' => $userPhone]);
}
public function checkParams($data)
{
$data['status'] = 1;
$data['is_show'] = 1;
$data['status_time'] = date('Y-m-d H:i:s', time());
$data['content'] = filter_emoji($data['content']);
app()->make(CommunityValidate::class)->check($data);
$arr = explode("\n", $data['content']);
$title = rtrim(ltrim($arr[0]));
if (mb_strlen($title) > 40 ){
$data['title'] = mb_substr($title,0,30,'utf-8');
} else {
$data['title'] = $title;
}
if ($data['image']) $data['image'] = implode(',',$data['image']);
return $data;
}
}

View File

@ -21,7 +21,7 @@ class ArticleValidate extends Validate
protected $rule = [
'cid|选择分类' => 'require|integer',
'title|标题' => 'require|max:32',
'title|标题' => 'require|max:50',
'content|内容' => 'require',
'author|作者' => 'require|max:32',
'image_input|图片' => 'require|max:128',

1
public/user.json Normal file
View File

@ -0,0 +1 @@
[{"phone":"17711206410","nickname":"田间小天使"},{"phone":"17711425535","nickname":"田园梦想家"},{"phone":"17711426269","nickname":"农家小三妹"},{"phone":"17711429212","nickname":"乡村小旺"},{"phone":"17713748413","nickname":"田间小萌芽"},{"phone":"17713754273","nickname":"乡村阳光"},{"phone":"17713762013","nickname":"三农小达人"},{"phone":"17715706977","nickname":"乡村小智慧"},{"phone":"17715709873","nickname":"田间诗人"},{"phone":"17715716261","nickname":"田野行者"},{"phone":"17715743777","nickname":"小苗苗的微笑"},{"phone":"17715746066","nickname":"乡村小晴天"},{"phone":"17716191181","nickname":"农村小天后"},{"phone":"17716191287","nickname":"三农致富"},{"phone":"17721972769","nickname":"乡村小骄阳"},{"phone":"17721993467","nickname":"乡村小幸福"},{"phone":"17729807412","nickname":"乡村田园"},{"phone":"17729808013","nickname":"悠闲乡村"},{"phone":"17729813629","nickname":"田间的晴天"},{"phone":"17729815926","nickname":"农民的快乐"},{"phone":"17729817633","nickname":"田园小幸福"},{"phone":"17738021074","nickname":"农田小小说"},{"phone":"17738028440","nickname":"乡村小浪漫"},{"phone":"17738041962","nickname":"农夫的心声"},{"phone":"17738575011","nickname":"田间小甜心"},{"phone":"17738688054","nickname":"农田小创意"},{"phone":"17743273158","nickname":"乡村小团队"},{"phone":"17745424248","nickname":"农夫的阳光"},{"phone":"17745424761","nickname":"田野小探险家"},{"phone":"17748041121","nickname":"农田小画家"},{"phone":"17748041259","nickname":"乡村小旅行家"},{"phone":"17748101099","nickname":"农民的乐园"},{"phone":"17748116065","nickname":"田间守护神"},{"phone":"17760108103","nickname":"农田世界"},{"phone":"17760623221","nickname":"乡村智囊"},{"phone":"17760625907","nickname":"快乐田园"},{"phone":"17760625009","nickname":"农民本分"},{"phone":"17760658432","nickname":"农田文艺"},{"phone":"17761074600","nickname":"乡村冒险家"},{"phone":"17778319254","nickname":"农夫的微笑脸"},{"phone":"17780873455","nickname":"田间小奇迹"},{"phone":"17780874321","nickname":"农田小创业家"},{"phone":"17781009478","nickname":"乡村小美食家"},{"phone":"17781017385","nickname":"农夫的阳光心情"},{"phone":"17781019608","nickname":"发现田野"},{"phone":"17781039133","nickname":"农田摄影师"},{"phone":"17781042682","nickname":"乡村热点"},{"phone":"17781058796","nickname":"农夫的甜蜜生活"},{"phone":"17781876366","nickname":"田间探索"},{"phone":"17788655750","nickname":"田间的快乐"}]

View File

@ -21,6 +21,8 @@ use think\facade\Route;
Route::group('api/', function () {
Route::any('test', 'api.Auth/test');
Route::resource('upload', 'api.Upload');
Route::post('articleCatch', 'api.Upload/article');
//强制登录
Route::group(function () {

1
user.json Normal file
View File

@ -0,0 +1 @@
[{"phone":"17711206410","nickname":"田间小天使"},{"phone":"17711425535","nickname":"田园梦想家"},{"phone":"17711426269","nickname":"农家小三妹"},{"phone":"17711429212","nickname":"乡村小旺"},{"phone":"17713748413","nickname":"田间小萌芽"},{"phone":"17713754273","nickname":"乡村阳光"},{"phone":"17713762013","nickname":"三农小达人"},{"phone":"17715706977","nickname":"乡村小智慧"},{"phone":"17715709873","nickname":"田间诗人"},{"phone":"17715716261","nickname":"田野行者"},{"phone":"17715743777","nickname":"小苗苗的微笑"},{"phone":"17715746066","nickname":"乡村小晴天"},{"phone":"17716191181","nickname":"农村小天后"},{"phone":"17716191287","nickname":"三农致富"},{"phone":"17721972769","nickname":"乡村小骄阳"},{"phone":"17721993467","nickname":"乡村小幸福"},{"phone":"17729807412","nickname":"乡村田园"},{"phone":"17729808013","nickname":"悠闲乡村"},{"phone":"17729813629","nickname":"田间的晴天"},{"phone":"17729815926","nickname":"农民的快乐"},{"phone":"17729817633","nickname":"田园小幸福"},{"phone":"17738021074","nickname":"农田小小说"},{"phone":"17738028440","nickname":"乡村小浪漫"},{"phone":"17738041962","nickname":"农夫的心声"},{"phone":"17738575011","nickname":"田间小甜心"},{"phone":"17738688054","nickname":"农田小创意"},{"phone":"17743273158","nickname":"乡村小团队"},{"phone":"17745424248","nickname":"农夫的阳光"},{"phone":"17745424761","nickname":"田野小探险家"},{"phone":"17748041121","nickname":"农田小画家"},{"phone":"17748041259","nickname":"乡村小旅行家"},{"phone":"17748101099","nickname":"农民的乐园"},{"phone":"17748116065","nickname":"田间守护神"},{"phone":"17760108103","nickname":"农田世界"},{"phone":"17760623221","nickname":"乡村智囊"},{"phone":"17760625907","nickname":"快乐田园"},{"phone":"17760625009","nickname":"农民本分"},{"phone":"17760658432","nickname":"农田文艺"},{"phone":"17761074600","nickname":"乡村冒险家"},{"phone":"17778319254","nickname":"农夫的微笑脸"},{"phone":"17780873455","nickname":"田间小奇迹"},{"phone":"17780874321","nickname":"农田小创业家"},{"phone":"17781009478","nickname":"乡村小美食家"},{"phone":"17781017385","nickname":"农夫的阳光心情"},{"phone":"17781019608","nickname":"发现田野"},{"phone":"17781039133","nickname":"农田摄影师"},{"phone":"17781042682","nickname":"乡村热点"},{"phone":"17781058796","nickname":"农夫的甜蜜生活"},{"phone":"17781876366","nickname":"田间探索"},{"phone":"17788655750","nickname":"田间的快乐"}]