文章抓取
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

202 lines
6.4 KiB

<?php
namespace app\controller;
use app\BaseController;
use app\model\{Article, Expert};
use think\facade\Db;
class ArticleController extends BaseController
{
//获取账号集合
public function expertList($isJson = true){
$experts = Expert::field(['id', 'name', 'platform_name'])->select()->map(function($expert){
$expert['showName'] = $expert['name'] . '_' . $expert['platform_name'];
return $expert;
})->toArray();
if($isJson){
return json([
'msg' => 'success',
'data' => $experts,
'count' => count($experts),
'code' => 200
], 200);
}else{
return $experts;
}
}
//获取文章列表
public function articleList()
{
$param = request()->param();
$list = Article::where(function ($q) use ($param) {
if(!empty($param['status'])){
$q->where('status', $param['status']);
}
if(!empty($param['account'])){
$q->where('FIND_IN_SET(:account, account)', ['account' => $param['account']]);
}
if(!empty($param['title'])){
$q->whereLike('account', '%' . $param['title'] . '%');
}
})->order('id','desc')->paginate([
'list_rows'=> $param["limit"],
'page' => $param["page"],
])->toArray();
$list['data'] = $this->getAccountNames($list['data']);
return json([
'msg' => 'success',
'data' => $list['data'],
'count' => $list['total'],
'code' => 200
], 200);
}
//删除文章
public function deleteArticle()
{
$param = request()->param();
$data = Article::where('id', $param['id'])->find();
if($data['status'] != 1){
return json(['msg' => 'fail', 'data' => '只能删除未就绪的文章', 'code' => 201], 201);
}
$data->delete();
return json(['msg' => 'success', 'data' => '删除成功!', 'code' => 200], 200);
}
public function editArticle(){
$param = request()->param();
$data = Article::where('id', $param['id'])->find();
if(empty($data)){
return json(['msg' => 'fail', 'data' => '文章不存在', 'code' => 201], 201);
}
if(!in_array($data['status'], [1,2])){
return json(['msg' => 'fail', 'data' => '只能编辑未就绪的文章', 'code' => 201], 201);
}
$updateData = [
'account' => $param['account'] ?? $data['account'],
'title' => $param['title'] ?? $data['title'],
'cover' => $param['cover'] ?? $data['cover'],
'des' => $param['des'] ?? $data['des'],
'content' => $param['content'] ?? $data['content'],
'status' => $param['status'] ?? $data['status'],
];
if(isset($param['status']) && $param['status'] == 2){
$updateData['check_time'] = date('Y-m-d H:i:s');
}else{
$updateData['check_time'] = null;
}
Article::where('id', $param['id'])->update($updateData);
return json(['msg' => 'success', 'data' => '编辑成功!', 'code' => 200], 200);
}
public function detailArticle(){
$param = request()->param();
$data = Article::where('id',$param['id'])->find();
return json(['msg' => 'success', 'data' => $data, 'code' => 200], 200);
}
//批量设置就绪
public function readyArticle(){
$param = request()->param();
if(empty($param['id']) || !is_array($param['id'])){
return json(['msg' => 'fail', 'data' => 'id参数不合法', 'code' => 201], 201);
}
Article::update([
'status' => 2,
'check_time' => date('Y-m-d H:i:s')
], [
'id' => $param['id'],
'status' => 1
]);
Article::update([
'status' => 1,
'check_time' => null
], [
'id' => $param['id'],
'status' => 2,
'account' => null
]);
return json(['msg' => 'success', 'data' => '设置成功!', 'code' => 200], 200);
}
//---------------------------------| 内置调用通用方法 |---------------------------------//
//组装账号名称集合
private function getAccountNames(array $datas) : array{
//获取账号集合
$expertsTemp = $this->expertList(false);
$experts = [];
foreach($expertsTemp as $val)$experts[$val['id']] = $val['showName'];
//组装账号
foreach($datas as &$item){
if(!empty($item['account'])){
$accountIds = explode(',', $item['account']);
$accountNames = [];
foreach($accountIds as $accountId){
$accountNames[] = $experts[$accountId] ?? '';
}
$item['account_names'] = implode(',', $accountNames);
}else{
$item['account_names'] = '';
}
}
return $datas;
}
public function copyOrInsertArticle()
{
$param = request()->param();
if(isset($param['id'])) {
$data = Article::where('id', $param['id'])->find();
if (empty($data)) {
return json(['msg' => 'fail', 'data' => '文章不存在', 'code' => 201], 201);
}
$copyData = [
'title' => $data['title'],
'cover' => $data['cover'],
'des' => $data['des'],
'content' => $data['content'],
'platform' => $data['platform'],
'platform_publish_time' => $data['platform_publish_time'],
];
}else{
$copyData = [
'title' => $param['title'],
'cover' => $param['cover'],
'des' => $param['des'],
'content' => $param['content'],
'platform' => '手动添加',
'platform_publish_time' => date('Y-m-d H:i:s'),
'account' => $param['account']
];
}
Article::insert($copyData);
return json(['msg' => 'success', 'data' => '复制成功!', 'code' => 200], 200);
}
}