123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- namespace app\common\model;
- use think\Db;
- class News extends Base
- {
- protected $createTime = 'create_time';
- public function updates(){
- $data = request()->post();
- $data['org_id'] = cur_org_id();
- $result = validate('News')->check($data,[],'');
- if(true !== $result){
- $this->error = validate('News')->getError();
- return false;
- }
- $data['img'] = implode(',',$data['img']);
- $id = $data['id'];
- unset($data['id']);
- if($id > 0){
- $data['update_time'] = date('Y-m-d H:i:s');
- $ret = $this->allowField(true)->save($data,['id'=>$id]);
- }else{
- $data['create_time'] = date('Y-m-d H:i:s');
- $ret = $this->allowField(true)->save($data);
- }
- if(!$ret){
- $this->error = '操作失败';
- return false;
- }
- return true;
- }
- //app接口
- public function getLists($cateId,$orgId){
- $map[] = ['cate_id','=',$cateId];
- $map[] = ['org_id','=',$orgId];
- $map[] = ['enable','=',1];
- $map[] = ['del','=',0];
- $ret = $this->field('id,title,img,create_time,views')->where($map)->order(['id'=>'desc','sort'=>'desc'])->select();
- $ret = $ret?$ret->toArray():[];
- foreach ($ret as $k=>$v){
- $ret[$k]['url'] = (string) url('/h5/index/newDetail',['id'=>$v['id']],true,true);
- }
- return $ret;
- }
- public function details($id){
- $info = $this->where('id',$id)->find()->toArray();
- $data = [
- 'views'=>$info['views'] + 1,
- ];
- $this->where('id',$id)->update($data);
- return $info;
- }
- //api文章列表
- public function apiNewList($page,$size,$cateId,$userId,$orgId){
- $ret= Db::name('news')
- ->field('id,title,user_id,content,img as imgs,create_time,views,source')
- ->where([
- 'enable'=>1,
- 'del'=>0,
- 'cate_id'=>$cateId,
- 'org_id'=>$orgId
- ])
- ->order('id','DESC')
- ->page($page,$size)
- ->select();
- $token=Db::name('token')->field('token')
- ->where('user_id',$userId)
- ->find();
- foreach ($ret as $k=>$v){
- $imgsArr=$v['imgs']?explode(',',$v['imgs']):[];
- if($imgsArr){
- $arrImg=[];
- foreach ($imgsArr as $kk=>$vv){
- $arrImg[]=$vv;
- }
- $ret[$k]['imgs']=$arrImg;
- }else{
- $ret[$k]['imgs']=[];
- }
- $ret[$k]['url']='http://'.$_SERVER['SERVER_NAME'].'/h5/News/newDetails?token='.$token['token'].'&newsid='.$v['id'];
- $ret[$k]['shareurl']='http://'.$_SERVER['SERVER_NAME'].'/h5/News/newDetails?newsid='.$v['id'];
- }
- foreach ($ret as $k=>$v){
- $comment_num=Db::name('news_reply')->where([
- 'news_id'=>$v['id'],
- 'news_reply_id'=>0,
- 'del'=>0,
- 'enable'=>1
- ])->count();
- $ret[$k]['comment_num']=$comment_num?$comment_num:0;
- }
- return $ret?$ret:[];
- }
- public function apiSearchs($page,$size,$title,$userId,$orgId){
- if($title!==''){
- $ret=Db::name('news')
- ->field('id,title,user_id,content,img as imgs,create_time,views,source')
- ->where('title','like', '%' . $title . '%')
- ->where('org_id',$orgId)
- ->where('del',0)
- ->where('enable',1)
- ->order('id','desc')
- ->page($page,$size)
- ->select();
- $token=Db::name('token')->field('token')
- ->where('user_id',$userId)->find();
- foreach ($ret as $k=>$v){
- $imgsArr=$v['imgs']?explode(',',$v['imgs']):[];
- if($imgsArr){
- $arrImg=[];
- foreach ($imgsArr as $kk=>$vv){
- $arrImg[]=$vv;
- }
- $ret[$k]['imgs']=$arrImg;
- }else{
- $ret[$k]['imgs']=[];
- }
- $ret[$k]['url']='http://'.$_SERVER['SERVER_NAME'].'/h5/News/newDetails?token='.$token['token'].'&newsid='.$v['id'];
- $ret[$k]['shareurl']='http://'.$_SERVER['SERVER_NAME'].'/h5/News/newDetails?newsid='.$v['id'];
- }
- foreach ($ret as $k=>$v){
- $comment_num=Db::name('news_reply')
- ->where([
- 'news_id'=>$v['id'],
- 'news_reply_id'=>0,
- 'del'=>0,
- 'enable'=>1,
- ])->count();
- $ret[$k]['comment_num']=$comment_num?$comment_num:0;
- }
- }else{
- $ret=[];
- }
- return $ret;
- }
- }
|