Article.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace app\admin\controller;
  3. /**
  4. * 文章管理
  5. */
  6. class Article extends Admin{
  7. protected $Article = null;
  8. protected $ArticleType = null;
  9. protected function _initialize(){
  10. parent::_initialize();
  11. $this->Article = model('Article');
  12. $this->ArticleType = model('article.Type');
  13. }
  14. public function index(){
  15. $types = $this->ArticleType->where(['pid'=>0])->order('sort asc,id asc')->select();
  16. $this->assign('types',$types);
  17. $this->assign('meta_title','文章列表');
  18. return $this->fetch();
  19. }
  20. public function load(){
  21. $page = input('get.page');
  22. $limit = input('get.limit');
  23. $where = [];
  24. $type_id = input('get.type_id');
  25. if (!empty($type_id)) {
  26. $where['type_1'] = $type_id;
  27. }
  28. $title = input('get.title');
  29. if (!empty($title)) {
  30. $where['title'] = ['like',"%".$title."%"];
  31. }
  32. $list = $this->Article->where($where)->order('id desc')->paginate($limit,false,['page'=>$page]);
  33. $data = [];
  34. foreach ($list as $key => $value) {
  35. $data[$key]['id'] = $value['id'];
  36. $data[$key]['type1_name'] = $value['type1']['name'];
  37. $data[$key]['type_cname'] = $value['type_cname'];
  38. $data[$key]['cover'] = $value['cover'];
  39. $data[$key]['title'] = $value['title'];
  40. $data[$key]['state'] = $value['state'];
  41. $data[$key]['recommend'] = $value['recommend'];
  42. $data[$key]['update_time'] = $value['update_time'];
  43. }
  44. return json(['data'=>$data,'count'=>$list->total(), 'code'=>0,'msg'=>'加载成功']);
  45. }
  46. public function add(){
  47. if ($this->request->isPost()) {
  48. $this->Article->type_1 = input('post.type_1');
  49. $this->Article->type_2 = input('post.type_2');
  50. $this->Article->type_3 = input('post.type_3');
  51. $this->Article->cover = input('post.cover');
  52. $this->Article->name = input('post.name');
  53. $this->Article->title = input('post.title');
  54. $this->Article->keywords = input('post.keywords');
  55. $this->Article->image = input('post.image');
  56. $this->Article->description = input('post.description');
  57. $this->Article->content = input('post.content');
  58. $result = $this->Article->save();
  59. if ($result) {
  60. return json(['data'=>null,'code'=>0,'msg'=>'添加文章成功']);
  61. }
  62. return json(['data'=>null,'code'=>1,'msg'=>'添加文章失败']);
  63. }else{
  64. $Role = model('Role');
  65. $roles = $Role->where(['pid'=>0,'id'=>['neq',1]])->select();
  66. $this->assign('roles',$roles);
  67. $this->assign('meta_title','添加文章');
  68. return $this->fetch();
  69. }
  70. }
  71. public function edit(){
  72. if ($this->request->isPost()) {
  73. $id = input('post.id');
  74. $article = $this->Article->where(['id'=>$id])->find();
  75. if (!$article) {
  76. $this->output(1,'参数错误');
  77. }
  78. $article->type_1 = input('post.type_1');
  79. $article->type_2 = input('post.type_2');
  80. $article->type_3 = input('post.type_3');
  81. $article->cover = input('post.cover');
  82. $article->name = input('post.name');
  83. $article->title = input('post.title');
  84. $article->keywords = input('post.keywords');
  85. $article->image = input('post.image');
  86. $article->description = input('post.description');
  87. $article->content = input('post.content');
  88. $result = $article->save();
  89. if ($result) {
  90. $this->output(0,'编辑文章成功');
  91. }
  92. $this->output(1,'编辑文章失败');
  93. }else{
  94. $id = input('get.id');
  95. $article = $this->Article->where(['id'=>$id])->find();
  96. if (!$article) {
  97. $this->error('参数错误');
  98. }
  99. $this->assign('article',$article);
  100. $this->assign('meta_title','添加文章');
  101. return $this->fetch();
  102. }
  103. }
  104. public function delete(){
  105. $id = input('post.id');
  106. $result = $this->Article->where(['id'=>$id])->delete();
  107. if ($result) {
  108. return json(['data'=>null,'code'=>0,'msg'=>'删除成功']);
  109. }
  110. return json(['data'=>null,'code'=>1,'msg'=>'删除失败']);
  111. }
  112. public function state(){
  113. $id = input('post.id');
  114. $state = input('post.state');
  115. $state = $state == 'true'?1:0;
  116. $result = $this->Article->where(['id'=>$id])->update(['state' => $state]);
  117. if ($result) {
  118. return json(['data'=>null,'code'=>0,'msg'=>'改变状态成功']);
  119. }
  120. return json(['data'=>null,'code'=>1,'msg'=>'改变状态失败']);
  121. }
  122. public function recommend(){
  123. $id = input('post.id');
  124. $recommend = input('post.recommend');
  125. $recommend = $recommend == 'true'?1:0;
  126. $result = $this->Article->where(['id'=>$id])->update(['recommend' => $recommend]);
  127. if ($result) {
  128. return json(['data'=>null,'code'=>0,'msg'=>'改变推荐成功']);
  129. }
  130. return json(['data'=>null,'code'=>1,'msg'=>'改变推荐失败']);
  131. }
  132. public function loadclassforselect(){
  133. $pid = input('post.pid');
  134. $list = $this->ArticleClass->where(['pid'=>$pid])->select();
  135. $data = [];
  136. foreach ($list as $key => $value) {
  137. $data[$key]['id'] = $value['id'];
  138. $data[$key]['cname'] = $value['cname'];
  139. }
  140. return json(['data'=>$data,'code'=>0,'msg'=>'加载分类成功']);
  141. }
  142. //文章详情
  143. public function detail(){
  144. $id = input('post.id');
  145. $artcle = $this->Article->where(['id'=>$id])->find();
  146. if ($artcle) {
  147. //判断是否阅读
  148. $this->read($id);
  149. $data['id'] = $artcle['id'];
  150. $data['cname'] = $artcle['cname'];
  151. $data['content'] = $artcle['content'];
  152. return json(['data'=>$data,'code'=>0,'msg'=>'获取成功']);
  153. }else{
  154. return json(['data'=>$id,'code'=>1,'msg'=>'参数错误']);
  155. }
  156. }
  157. private function read($article_id){
  158. $user_id = $this->user['id'];
  159. $record = $this->ArticleRead->where(['article_id'=>$article_id,'user_id'=>$user_id])->find();
  160. if (!$record) {
  161. $this->ArticleRead->article_id = $article_id;
  162. $this->ArticleRead->user_id = $user_id;
  163. $this->ArticleRead->count = 1;
  164. $this->ArticleRead->save();
  165. }else{
  166. $record->count = ['exp','count + 1'];
  167. $record->save();
  168. }
  169. }
  170. }