| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Author: Tuolaji <479923197@qq.com>
- // +----------------------------------------------------------------------
- namespace app\admin\controller;
- use cmf\controller\AdminBaseController;
- use think\facade\Db;
- use think\db\Query;
- class NewspostController extends AdminBaseController{
-
- protected function initialize()
- {
- parent::initialize();
- $adminId = cmf_get_current_admin_id(); //获取后台管理员id,可判断是否登录
- if (!empty($adminId)) {
- $this->assign('admin_id', $adminId);
- }
- }
-
- // 后台新闻列表
- public function index(){
- //新闻分类
- $cate_list=Db::name('news_cate')
- ->order("sort DESC,id DESC")
- ->select();
- $this->assign('cate_list', $cate_list);
-
- //新闻
- $posts=Db::name('news')
- ->where(function (Query $query) {
- $data = $this->request->param();
-
- // $query->where('post_type', 0);
-
- if (isset($data['id']) && $data['id']!='') {
- $query->where('id', $data['id']);
- }
-
- if (isset($data['cate_id']) && $data['cate_id']!='') {
- $query->where('cate_id', $data['cate_id']);
- }
-
- if (isset($data['keyword']) && !empty($data['keyword'])) {
- $keyword = $data['keyword'];
- $query->where('title|keywords', 'like', "%$keyword%");
- }
- })
- ->order("istop DESC,id DESC")
- ->paginate(20);
-
- $posts->each(function($v,$k){
- $userinfo=Db::name("user")
- ->field("user_nickname")
- ->where("id={$v['author']}")
- ->find();
- if(!$userinfo){
- $userinfo=array(
- 'user_nickname'=>'已删除',
- );
- }
- $v['userinfo']= $userinfo;
- //
- $cate_info=Db::name("news_cate")
- ->field("name")
- ->where("id='$v[cate_id]'")
- ->find();
- if(!$cate_info){
- $termsinfo=array(
- 'name'=>'已删除'
- );
- }
- $v['cate_info']= $cate_info;
- if($v['status'] == 1){
- $v['status_name'] = '显示';
- }else{
- $v['status_name'] = '隐藏';
- }
- return $v;
- });
- //分页-->筛选条件参数
- $data = $this->request->param();
- $posts->appends($data);
- // 获取分页显示
- $page = $posts->render();
-
- $this->assign('posts', $posts);
- $this->assign('page', $page);
-
- $configpub=getConfigPub();
- $this->assign("site",$configpub['site']);
-
- return $this->fetch();
- }
-
- //文章添加
- public function add(){
-
- //文章分类
- $cate_list=Db::name('news_cate')
- ->where('status=1')
- ->order("sort DESC,id DESC")
- ->select();
- $this->assign('cate_list', $cate_list);
-
- return $this->fetch();
- }
-
- public function add_post(){
- $data = input('post.post');
- if(empty($data['cate_id'])){
- $this->error("请至少选择一个分类!");
- }else if(empty($data['title'])){
- $this->error('请输入标题!');
- }
- $data['content']=html_entity_decode($data['content']);
- $data['author']=cmf_get_current_admin_id(); //获取后台管理员id
-
- // $path='';
- // if($data['termid']!=0){
- // $info=Db::name('terms')->where('id',$data['termid'])->value('path');
- // $path=$info;
- // }
-
- // $data['path']=$path;
-
- $data['create_time']=time();
-
-
-
- $add=Db::name('news')->insert($data);
- if($add){
- $this->success('添加成功!','newspost/index');
- }else{
- $this->error('添加失败!');
- }
- }
-
- //新闻编辑
- public function edit(){
- $id = input('param.id');
- if($id){
- //新闻分类
- $cate_list=Db::name('news_cate')
- ->order("sort DESC,id DESC")
- ->select();
- $this->assign('cate_list', $cate_list);
-
- $info=Db::name('news')->where('id',$id)->find();
- $info['content']=str_replace('../../','/upload/',$info['content']);
- $this->assign('info',$info);
- }else{
- $this->error('数据传入失败!');
- }
-
- return $this->fetch();
- }
-
- public function edit_post(){
- $data = input('post.post');
- $data['content']=html_entity_decode($data['content']);
- if(empty($data['cate_id'])){
- $this->error("请至少选择一个分类!");
- }else if(empty($data['title'])){
- $this->error('请输入标题!');
- }
-
- $save=Db::name('news')->where('id',$data['id'])->update($data);
-
- if($save!==false){
- $this->success('保存成功!','newspost/index');
- }else{
- $this->error('保存失败!');
- }
- }
-
-
- // 文章分类删除
- public function del(){
- $id = input('param.id');
- if($id){
- $result=Db::name('news')->delete($id);
- if($result){
- $this->success('删除成功');
- }else{
- $this->error('删除失败');
- }
- }else{
- $this->error('数据传入失败!');
- }
- }
-
-
- // 文章批量审核或取消审核
- public function check(){
- $data = input();
- foreach ($data['ids'] as $k => $r) {
- $save=array();
- $save['post_status'] = $data['check'];
- Db::name('posts')->where(['id'=>$r])->update($save);
- }
- $status = true;
- if ($status) {
- $this->success("操作成功!");
- } else {
- $this->error("操作失败!");
- }
- }
-
- // 文章批量置顶或取消置顶
- public function tops(){
- $data = input();
- foreach ($data['ids'] as $k => $r) {
- $save=array();
- $save['istop'] = $data['top'];
- Db::name('posts')->where(['id'=>$r])->update($save);
- }
- $status = true;
- if ($status) {
- $this->success("操作成功!");
- } else {
- $this->error("操作失败!");
- }
- }
-
- // 文章批量删除
- public function deletes(){
- $data = input();
- foreach ($data['ids'] as $k => $r) {
- Db::name('news')->where(['id'=>$r])->delete();
- }
- $status = true;
- if ($status) {
- $this->success("操作成功!");
- } else {
- $this->error("操作失败!");
- }
- }
- // // 文章排序
- // public function listordersset() {
- //
- //
- // $ids=$this->request->param('listordersset');
- // foreach ($ids as $key => $r) {
- // $data['orderno'] = $r;
- // Db::name("posts")
- // ->where(array('id' => $key))
- // ->update($data);
- // }
- //
- // $status = true;
- // if ($status) {
- // $this->success("排序更新成功!");
- // } else {
- // $this->error("排序更新失败!");
- // }
- // }
-
-
-
- }
|