News.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\api\controller\worker;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 咨询
  7. */
  8. class News extends Api
  9. {
  10. protected $noNeedLogin = [''];
  11. protected $noNeedRight = [''];
  12. public function index()
  13. {
  14. //所有分类
  15. $typelist = Db::name('news_type')
  16. ->where('company_id',$this->auth->company_id)
  17. ->order('id' , 'asc')
  18. ->select();
  19. //第一个分类的内容
  20. $list = [];
  21. if(!empty($typelist)){
  22. $list = Db::name('news')->field('content',true)
  23. ->where('company_id',$this->auth->company_id)
  24. ->where('type_id',$typelist[0]['id'])
  25. ->autopage()
  26. ->select();
  27. $list = list_domain_image($list,['image']);
  28. }
  29. $rs = [
  30. 'typelist' => $typelist,
  31. 'list' => $list,
  32. ];
  33. $this->success(1, $rs);
  34. }
  35. //
  36. public function lists(){
  37. $type_id = input('type_id',0);
  38. $keyword = input('keyword','','trim');
  39. $wheresearch = [];
  40. if(!empty($keyword)){
  41. $wheresearch['title'] = ['LIKE','%'.$keyword.'%'];
  42. }
  43. $list = Db::name('news')->field('content',true)
  44. ->where('company_id',$this->auth->company_id)
  45. ->where('type_id',$type_id)
  46. ->where($wheresearch)
  47. ->autopage()
  48. ->select();
  49. $list = list_domain_image($list,['image']);
  50. $this->success(1, $list);
  51. }
  52. //详情
  53. public function info(){
  54. $id = input('id',0);
  55. $info = Db::name('news')
  56. ->where('id',$id)
  57. ->find();
  58. $info = info_domain_image($info, ['image']);
  59. //收藏
  60. $info['is_collect'] = $this->is_collect($info['id'],$this->auth->id);
  61. $this->success(1, $info);
  62. }
  63. //收藏,取消收藏
  64. public function collect(){
  65. $where = [
  66. 'worker_id' => $this->auth->id,
  67. 'table' => 'news',
  68. 'table_id' => input('id',0),
  69. ];
  70. $check = Db::name('worker_collect')->where($where)->find();
  71. if($check){
  72. Db::name('worker_collect')->where($where)->delete();
  73. $this->success('已取消收藏');
  74. }else{
  75. Db::name('worker_collect')->insertGetId($where);
  76. $this->success('收藏成功');
  77. }
  78. }
  79. //动态是否收藏
  80. private function is_collect($id,$uid){
  81. $where = [
  82. 'worker_id' => $uid,
  83. 'table' => 'news',
  84. 'table_id' => $id,
  85. ];
  86. $check = Db::name('worker_collect')->where($where)->find();
  87. if($check){
  88. return 1;
  89. }else{
  90. return 0;
  91. }
  92. }
  93. }