127 lines
4.3 KiB
PHP
127 lines
4.3 KiB
PHP
<?php
|
|
namespace app\api\controller\party;
|
|
use app\common\controller\Api;
|
|
use app\admin\model\party\Article as ArticleModel;
|
|
use app\admin\validate\party\Article as ArticleValdate;
|
|
use think\facade\Cache;
|
|
use think\facade\Db;
|
|
|
|
/**
|
|
* 党群文章
|
|
*/
|
|
class Article extends Api{
|
|
protected $noNeedLogin = ['index','details'];
|
|
protected $noNeedRight = ['*'];
|
|
|
|
|
|
public function index($search='',$category_id=1,$page=1) {
|
|
$order = $this->request->request('order', 1);
|
|
//查询升降序
|
|
if($order==1){
|
|
$orders='desc';
|
|
}else{
|
|
$orders='asc';
|
|
}
|
|
$model = new ArticleModel();
|
|
$where=[
|
|
['status','=', 1],
|
|
['category_id','=', $category_id]
|
|
];
|
|
//根据个人村id进行查询
|
|
if ($this->auth->id) {
|
|
$find = Db::name('szxc_information_useraddress')->where('user_id', $this->auth->id)->find();
|
|
if ($find) {
|
|
if ($find['auth_range']==1){
|
|
$where[] = ['village', '=', $find['village_id']];
|
|
}elseif ($find['auth_range']==2){
|
|
$where[] = ['township', '=', $find['street_id']];
|
|
}elseif ($find['auth_range']==3){
|
|
$where[] = ['county', '=', $find['area_id']];
|
|
}
|
|
}
|
|
}
|
|
$count=$model->where($where)->count();
|
|
$month_count=$model->where($where)->whereMonth('view_time')->count();
|
|
if ($search!=''){
|
|
$where[]=['title','like','%'.$search.'%'];
|
|
}
|
|
$select=$model->with('user')->where($where)->page($page)->limit(20)
|
|
->field('id,title,user_id,view,view_time,image')->order('id',$orders)->select();
|
|
return $this->success('ok',['list'=>$select,'count'=>['count'=>$count,'month_count'=>$month_count]]);
|
|
}
|
|
public function details($id)
|
|
{
|
|
$model = new ArticleModel();
|
|
$find=$model->where('id',$id)->find();
|
|
if ($find){
|
|
// 增加阅读数
|
|
// $ip = 'party_article-details-'.$this->request->ip().'-'.$id;
|
|
// $ip_cache = Cache::get($ip);
|
|
// if(empty($ip_cache)){
|
|
// Cache::set($ip,$id,3600*24);
|
|
$map[] =['id','=', $id];
|
|
Db::name('szxc_party_article')->where($map)->inc('view','1')->update();
|
|
// }
|
|
}
|
|
return $this->success('ok',$find);
|
|
}
|
|
public function add(){
|
|
|
|
}
|
|
public function post(){
|
|
$input=$this->request->post();
|
|
$valdate = new ArticleValdate();
|
|
$res=$valdate->check($input);
|
|
if (!$res){
|
|
return $this->error($valdate->getError());
|
|
}
|
|
$model = new ArticleModel();
|
|
$useraddress = Db::name('szxc_information_useraddress')->where('user_id', $this->auth->id)->where('status', 1)->find();
|
|
if ($useraddress) {
|
|
$input['county'] = $useraddress['area_id'];
|
|
$input['township'] = $useraddress['street_id'];
|
|
$input['village'] = $useraddress['village_id'];
|
|
}
|
|
$input['add_time'] = date('Y-m-d H:i:s');
|
|
$input['view_time'] = date('Y-m-d H:i:s');
|
|
$input['user_id'] = $this->auth->id;
|
|
$res=$model->save($input);
|
|
if ($res){
|
|
return $this->success('添加成功');
|
|
}else{
|
|
return $this->error('添加失败');
|
|
}
|
|
}
|
|
public function edit($id){
|
|
$model = new ArticleModel();
|
|
$find=$model->where('id',$id)->find();
|
|
return $this->success('ok',$find);
|
|
|
|
}
|
|
public function put($id){
|
|
$input=$this->request->post();
|
|
$valdate = new ArticleValdate();
|
|
$res=$valdate->check($input);
|
|
if (!$res){
|
|
return $this->error($valdate->getError());
|
|
}
|
|
$model = new ArticleModel();
|
|
$input['user_id'] = $this->auth->id;
|
|
$res=$model->where('id',$id)->update($input);
|
|
if ($res){
|
|
return $this->success('修改成功');
|
|
}else{
|
|
return $this->error('修改失败');
|
|
}
|
|
}
|
|
public function delete($id){
|
|
$model = new ArticleModel();
|
|
$res=$model->where('id',$id)->update(['status'=>0]);
|
|
if ($res){
|
|
return $this->success('删除成功');
|
|
}else{
|
|
return $this->error('删除失败');
|
|
}
|
|
}
|
|
}
|