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.

218 lines
6.3 KiB

<?php
namespace app\api\controller;
use app\common\controller\Api;
/**
* 首页接口
*/
class Research extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
protected $topic = null;
protected $author = null;
protected $research = null;
public function _initialize()
{
parent::_initialize();
$this->topic = new \app\admin\model\research\Topic;
$this->author = new \app\admin\model\research\Author;
$this->research = new \app\admin\model\research\Index;
}
/**
* get topic
*
*/
public function getTopic()
{
$exp = new \think\db\Expression("sort!=0 desc,sort");
$list = $this->topic->order($exp)->select();
$this->success('Request succeeded',$list);
}
/**
* get author
*
*/
public function getAuthor()
{
$exp = new \think\db\Expression("sort!=0 desc,sort");
$list = $this->author->order($exp)->select();
$this->success('Request succeeded',$list);
}
/**
* get research
*
*/
public function getResearch()
{
$keyword = (string)$this->request->request('keyword');
$topic_id = $this->request->request('topic_id','');
$author_id = $this->request->request('author_id','');
$page = (int)$this->request->request('page');
$limit = (int)$this->request->request('limit',10);
$where = [];
$topic_where = [];
$author_where = [];
$where['status'] = 'Live';
$where['publishedtime'] = ['lt',time()];
if($topic_id){
$topic_id_arr = explode(',',$topic_id);
$topic_where = '';
foreach ($topic_id_arr as $k=>$v){
$topic_where .= " or FIND_IN_SET('{$v}', topic_ids)";
}
$topic_where = trim($topic_where,' or');
}
if($author_id){
$author_where_arr = explode(',',$author_id);
$author_where = '';
foreach ($author_where_arr as $k=>$v){
$author_where .= " or FIND_IN_SET('{$v}', author)";
}
$author_where = trim($author_where,' or');
}
if($keyword){
$where['title'] = ['like','%'.$keyword.'%'];
}
$exp = new \think\db\Expression("sort!=0 desc,sort,publishedtime desc");
$list = $this->research->field('id,title,author,topic_ids,cover_image,tag,description,status,publishedtime,createtime')->where($topic_where)->where($author_where)->where($where)->order($exp)->paginate($limit);
foreach ($list as $row) {
if(strpos($row->author,',') || is_numeric($row->author)){
$author = \app\admin\model\research\Author::where(['id'=>['in',$row->author]])->column('name');
$row->author = implode(',',$author);
}
$topic = \app\admin\model\research\Topic::where(['id'=>['in',$row->topic_ids]])->column('title');
$row->topic_names = implode(',',$topic);
}
$this->success('Request succeeded',$list);
}
/**
* view research
*
*/
public function viewResearch()
{
$research_id = (int)$this->request->request('research_id',0);
if(!$research_id){
$this->error('Parameter error');
}
$where = [];
$where['id'] = $research_id;
$info = $this->research->where($where)->find();
if(!$info){
$this->error('Research does not exist');
}
$this->research->where($where)->setInc('views',1);
if(strpos($info->author,',') || is_numeric($info->author)){
$author = \app\admin\model\research\Author::where(['id'=>['in',$info->author]])->column('name');
$info->author = implode(',',$author);
}
$this->success('Request succeeded',$info);
}
/**
* recommend research
*
*/
public function recommendResearch()
{
$research_id = (int)$this->request->request('research_id',0);
$page = (int)$this->request->request('page');
$limit = (int)$this->request->request('limit',10);
if(!$research_id){
$this->error('Parameter error');
}
$info = $this->research->where(['id'=>$research_id])->find();
if(!$info){
$this->error('Research does not exist');
}
$topic_ids_arr = explode(',',$info->topic_ids);
$where = [];
foreach ($topic_ids_arr as $k=>$v){
$where[] = "FIND_IN_SET('{$v}', topic_ids)";
}
if ($where) {
$where = "(" . implode(' OR ', $where) . ")";
}
$exp = new \think\db\Expression("sort!=0 desc,sort,publishedtime desc");
$list = $this->research->where($where)->where(['id'=>['neq',$research_id],'status'=>'Live'])->order($exp)->paginate($limit);
$this->success('Request succeeded',$list);
}
/**
* search research title
*
*/
public function searchTitle()
{
$keyword = (string)$this->request->request('keyword');
if(!$keyword){
$this->error('Parameter error');
}
$exp = new \think\db\Expression("sort!=0 desc,sort,publishedtime desc");
$research_list = $this->research->field('id,title,topic_ids,author,cover_image,publishedtime')->where(['title'=>['like','%'.$keyword.'%']])->order($exp)->select();
foreach ($research_list as $row) {
$topic = \app\admin\model\research\Topic::where(['id'=>['in',$row->topic_ids]])->column('title');
$row->topic_names = implode(',',$topic);
}
$exp = new \think\db\Expression("sort!=0 desc,sort");
$author_list = $this->author->field('id,avatar,name')->where(['name'=>['like','%'.$keyword.'%']])->order($exp)->select();
$data = [
'research' => $research_list,
'author' => $author_list,
];
$this->success('Request succeeded',$data);
}
/**
* Filter Content
*
*/
public function filterContent()
{
$list = $this->research->select();
foreach ($list as $k=>$v){
$data = [
'content'=>str_replace('background-color:#FFFFFF',' ',$v['content'])
];
$this->research->where(['id'=>$v['id']])->update($data);
}
$this->success('Request succeeded');
}
}