123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- namespace app\admin\controller;
- /**
- * 文章管理
- */
- class Article extends Admin{
-
- protected $Article = null;
- protected $ArticleType = null;
- protected function _initialize(){
- parent::_initialize();
- $this->Article = model('Article');
- $this->ArticleType = model('article.Type');
- }
- public function index(){
- $types = $this->ArticleType->where(['pid'=>0])->order('sort asc,id asc')->select();
- $this->assign('types',$types);
- $this->assign('meta_title','文章列表');
- return $this->fetch();
- }
- public function load(){
- $page = input('get.page');
- $limit = input('get.limit');
- $where = [];
- $type_id = input('get.type_id');
- if (!empty($type_id)) {
- $where['type_1'] = $type_id;
- }
- $title = input('get.title');
- if (!empty($title)) {
- $where['title'] = ['like',"%".$title."%"];
- }
- $list = $this->Article->where($where)->order('id desc')->paginate($limit,false,['page'=>$page]);
- $data = [];
- foreach ($list as $key => $value) {
- $data[$key]['id'] = $value['id'];
- $data[$key]['type1_name'] = $value['type1']['name'];
- $data[$key]['type_cname'] = $value['type_cname'];
- $data[$key]['cover'] = $value['cover'];
- $data[$key]['title'] = $value['title'];
- $data[$key]['state'] = $value['state'];
- $data[$key]['recommend'] = $value['recommend'];
- $data[$key]['update_time'] = $value['update_time'];
- }
- return json(['data'=>$data,'count'=>$list->total(), 'code'=>0,'msg'=>'加载成功']);
- }
- public function add(){
- if ($this->request->isPost()) {
- $this->Article->type_1 = input('post.type_1');
- $this->Article->type_2 = input('post.type_2');
- $this->Article->type_3 = input('post.type_3');
- $this->Article->cover = input('post.cover');
- $this->Article->name = input('post.name');
- $this->Article->title = input('post.title');
- $this->Article->keywords = input('post.keywords');
- $this->Article->image = input('post.image');
- $this->Article->description = input('post.description');
- $this->Article->content = input('post.content');
- $result = $this->Article->save();
- if ($result) {
- return json(['data'=>null,'code'=>0,'msg'=>'添加文章成功']);
- }
- return json(['data'=>null,'code'=>1,'msg'=>'添加文章失败']);
- }else{
- $Role = model('Role');
- $roles = $Role->where(['pid'=>0,'id'=>['neq',1]])->select();
- $this->assign('roles',$roles);
- $this->assign('meta_title','添加文章');
- return $this->fetch();
- }
- }
- public function edit(){
- if ($this->request->isPost()) {
- $id = input('post.id');
- $article = $this->Article->where(['id'=>$id])->find();
- if (!$article) {
- $this->output(1,'参数错误');
- }
- $article->type_1 = input('post.type_1');
- $article->type_2 = input('post.type_2');
- $article->type_3 = input('post.type_3');
- $article->cover = input('post.cover');
- $article->name = input('post.name');
- $article->title = input('post.title');
- $article->keywords = input('post.keywords');
- $article->image = input('post.image');
- $article->description = input('post.description');
- $article->content = input('post.content');
- $result = $article->save();
- if ($result) {
- $this->output(0,'编辑文章成功');
- }
- $this->output(1,'编辑文章失败');
- }else{
- $id = input('get.id');
- $article = $this->Article->where(['id'=>$id])->find();
- if (!$article) {
- $this->error('参数错误');
- }
- $this->assign('article',$article);
- $this->assign('meta_title','添加文章');
- return $this->fetch();
- }
- }
- public function delete(){
- $id = input('post.id');
- $result = $this->Article->where(['id'=>$id])->delete();
- if ($result) {
- return json(['data'=>null,'code'=>0,'msg'=>'删除成功']);
- }
- return json(['data'=>null,'code'=>1,'msg'=>'删除失败']);
- }
- public function state(){
- $id = input('post.id');
- $state = input('post.state');
- $state = $state == 'true'?1:0;
- $result = $this->Article->where(['id'=>$id])->update(['state' => $state]);
- if ($result) {
- return json(['data'=>null,'code'=>0,'msg'=>'改变状态成功']);
- }
- return json(['data'=>null,'code'=>1,'msg'=>'改变状态失败']);
- }
- public function recommend(){
- $id = input('post.id');
- $recommend = input('post.recommend');
- $recommend = $recommend == 'true'?1:0;
- $result = $this->Article->where(['id'=>$id])->update(['recommend' => $recommend]);
- if ($result) {
- return json(['data'=>null,'code'=>0,'msg'=>'改变推荐成功']);
- }
- return json(['data'=>null,'code'=>1,'msg'=>'改变推荐失败']);
- }
- public function loadclassforselect(){
- $pid = input('post.pid');
- $list = $this->ArticleClass->where(['pid'=>$pid])->select();
- $data = [];
- foreach ($list as $key => $value) {
- $data[$key]['id'] = $value['id'];
- $data[$key]['cname'] = $value['cname'];
- }
- return json(['data'=>$data,'code'=>0,'msg'=>'加载分类成功']);
- }
- //文章详情
- public function detail(){
- $id = input('post.id');
- $artcle = $this->Article->where(['id'=>$id])->find();
- if ($artcle) {
- //判断是否阅读
- $this->read($id);
- $data['id'] = $artcle['id'];
- $data['cname'] = $artcle['cname'];
- $data['content'] = $artcle['content'];
- return json(['data'=>$data,'code'=>0,'msg'=>'获取成功']);
- }else{
- return json(['data'=>$id,'code'=>1,'msg'=>'参数错误']);
- }
- }
- private function read($article_id){
- $user_id = $this->user['id'];
- $record = $this->ArticleRead->where(['article_id'=>$article_id,'user_id'=>$user_id])->find();
- if (!$record) {
- $this->ArticleRead->article_id = $article_id;
- $this->ArticleRead->user_id = $user_id;
- $this->ArticleRead->count = 1;
- $this->ArticleRead->save();
- }else{
- $record->count = ['exp','count + 1'];
- $record->save();
- }
- }
-
- }
|