0
0

News.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class News extends Base
  5. {
  6. protected $createTime = 'create_time';
  7. public function updates(){
  8. $data = request()->post();
  9. $data['org_id'] = cur_org_id();
  10. $result = validate('News')->check($data,[],'');
  11. if(true !== $result){
  12. $this->error = validate('News')->getError();
  13. return false;
  14. }
  15. $data['img'] = implode(',',$data['img']);
  16. $id = $data['id'];
  17. unset($data['id']);
  18. if($id > 0){
  19. $data['update_time'] = date('Y-m-d H:i:s');
  20. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  21. }else{
  22. $data['create_time'] = date('Y-m-d H:i:s');
  23. $ret = $this->allowField(true)->save($data);
  24. }
  25. if(!$ret){
  26. $this->error = '操作失败';
  27. return false;
  28. }
  29. return true;
  30. }
  31. //app接口
  32. public function getLists($cateId,$orgId){
  33. $map[] = ['cate_id','=',$cateId];
  34. $map[] = ['org_id','=',$orgId];
  35. $map[] = ['enable','=',1];
  36. $map[] = ['del','=',0];
  37. $ret = $this->field('id,title,img,create_time,views')->where($map)->order(['id'=>'desc','sort'=>'desc'])->select();
  38. $ret = $ret?$ret->toArray():[];
  39. foreach ($ret as $k=>$v){
  40. $ret[$k]['url'] = (string) url('/h5/index/newDetail',['id'=>$v['id']],true,true);
  41. }
  42. return $ret;
  43. }
  44. public function details($id){
  45. $info = $this->where('id',$id)->find()->toArray();
  46. $data = [
  47. 'views'=>$info['views'] + 1,
  48. ];
  49. $this->where('id',$id)->update($data);
  50. return $info;
  51. }
  52. //api文章列表
  53. public function apiNewList($page,$size,$cateId,$userId,$orgId){
  54. $ret= Db::name('news')
  55. ->field('id,title,user_id,content,img as imgs,create_time,views,source')
  56. ->where([
  57. 'enable'=>1,
  58. 'del'=>0,
  59. 'cate_id'=>$cateId,
  60. 'org_id'=>$orgId
  61. ])
  62. ->order('id','DESC')
  63. ->page($page,$size)
  64. ->select();
  65. $token=Db::name('token')->field('token')
  66. ->where('user_id',$userId)
  67. ->find();
  68. foreach ($ret as $k=>$v){
  69. $imgsArr=$v['imgs']?explode(',',$v['imgs']):[];
  70. if($imgsArr){
  71. $arrImg=[];
  72. foreach ($imgsArr as $kk=>$vv){
  73. $arrImg[]=$vv;
  74. }
  75. $ret[$k]['imgs']=$arrImg;
  76. }else{
  77. $ret[$k]['imgs']=[];
  78. }
  79. $ret[$k]['url']='http://'.$_SERVER['SERVER_NAME'].'/h5/News/newDetails?token='.$token['token'].'&newsid='.$v['id'];
  80. $ret[$k]['shareurl']='http://'.$_SERVER['SERVER_NAME'].'/h5/News/newDetails?newsid='.$v['id'];
  81. }
  82. foreach ($ret as $k=>$v){
  83. $comment_num=Db::name('news_reply')->where([
  84. 'news_id'=>$v['id'],
  85. 'news_reply_id'=>0,
  86. 'del'=>0,
  87. 'enable'=>1
  88. ])->count();
  89. $ret[$k]['comment_num']=$comment_num?$comment_num:0;
  90. }
  91. return $ret?$ret:[];
  92. }
  93. public function apiSearchs($page,$size,$title,$userId,$orgId){
  94. if($title!==''){
  95. $ret=Db::name('news')
  96. ->field('id,title,user_id,content,img as imgs,create_time,views,source')
  97. ->where('title','like', '%' . $title . '%')
  98. ->where('org_id',$orgId)
  99. ->where('del',0)
  100. ->where('enable',1)
  101. ->order('id','desc')
  102. ->page($page,$size)
  103. ->select();
  104. $token=Db::name('token')->field('token')
  105. ->where('user_id',$userId)->find();
  106. foreach ($ret as $k=>$v){
  107. $imgsArr=$v['imgs']?explode(',',$v['imgs']):[];
  108. if($imgsArr){
  109. $arrImg=[];
  110. foreach ($imgsArr as $kk=>$vv){
  111. $arrImg[]=$vv;
  112. }
  113. $ret[$k]['imgs']=$arrImg;
  114. }else{
  115. $ret[$k]['imgs']=[];
  116. }
  117. $ret[$k]['url']='http://'.$_SERVER['SERVER_NAME'].'/h5/News/newDetails?token='.$token['token'].'&newsid='.$v['id'];
  118. $ret[$k]['shareurl']='http://'.$_SERVER['SERVER_NAME'].'/h5/News/newDetails?newsid='.$v['id'];
  119. }
  120. foreach ($ret as $k=>$v){
  121. $comment_num=Db::name('news_reply')
  122. ->where([
  123. 'news_id'=>$v['id'],
  124. 'news_reply_id'=>0,
  125. 'del'=>0,
  126. 'enable'=>1,
  127. ])->count();
  128. $ret[$k]['comment_num']=$comment_num?$comment_num:0;
  129. }
  130. }else{
  131. $ret=[];
  132. }
  133. return $ret;
  134. }
  135. }