|
<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use app\admin\model\Category as CategoryModel;
use app\admin\model\Article as ArticleModel;
/**
* 文章管理
* @author huwhis@163.com
* @version 0.0.1
*/
class Article extends Base
{
protected $model;
public function __construct(Request $request = null)
{
parent::__construct($request);
$this->model = new ArticleModel();
}
public function index($cid = 0)
{
$category_model = new CategoryModel();
$category_tree = obj_tree($category_model->field('id, pid, name, tablename, url')->where('is_del', 0)->select());
$categories_url = $category_model->where('is_del', 0)->column('id,url');
$categories_name = $category_model->where('is_del', 0)->column('id,name');
$where = ['is_del'=>0];
if ($cid) {
$where['cid'] = $cid;
}
$data = $this->model->where($where)->field('id, cid, title, username, clicks, create_time')->order('id', 'desc')->paginate();
$this->assign('category_tree', $category_tree);
$this->assign('categories_url', $categories_url);
$this->assign('categories_name', $categories_name);
$this->assign('data', $data);
$this->assign('cid', $cid);
return $this->fetch();
}
public function save(Request $request, $cid = 0, $id = 0)
{
if ($request->isPost()) {
$param = $request->param();
// var_dump($param);
// exit;
if (!$param['cid']) {
$this->error('请选择栏目');
}
if ($param['title'] == '') {
$this->error("标题不能为空");
}
$param['content'] = $param['editorValue'];
unset($param['editorValue']);
try {
if ($param['id'] != 0) {
$this->model->update($param);
} else {
$param['userid'] = $this->uid;
$param['username'] = $this->username;
$param['create_time'] = time();
unset($param['id']);
$this->model->insert($param);
}
} catch (\Exception $e) {
$msg = $e->getMessage();
$this->error("错误提示:".$msg);
}
$this->success('操作成功', url('admin/article/index?cid='.$param['cid']));
} else {
$category_model = new CategoryModel();
if (!$cid) {
$this->error('请选择栏目');
}
if ($id) {
$data = $this->model->find($id);
} else {
$data = [
'id' => 0,
'title' => '',
'writer' => '',
'source' => '',
'title_pic' => '',
'keywords' => '',
'summary' => '',
'content' => '',
'clicks' => 0
];
}
$category_info = $category_model->field('id, pid, name')->find($cid);
$this->assign('cid', $cid);
$this->assign('category_info', $category_info);
$this->assign('data', $data);
return $this->fetch();
}
}
public function doAddOrEdit(Request $request = null)
{
if ($request->isPost()) {
$param = $request->param();
// var_dump($param);
// exit;
if (!$param['cid']) {
$this->error('请选择数据表');
}
if ($param['title'] == '') {
$this->error("标题不能为空");
}
// // 获取表单上传文件
// $file = request()->file('upload_file');
// if($file){
// $info = $file->validate(['size'=>4194304,'ext'=>'png,jpg,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'uploads');
// if($info){
// // 成功上传后 获取上传信息
// $param['title_pic'] = '/uploads/' . $info->getSaveName();
// }else{
// $this->error("错误提示:" . $file->getError());
// }
// }
// unset($param['upload_file']);
$param['content'] = $param['editorValue'];
unset($param['editorValue']);
try {
if ($param['id'] != 0) {
$this->model->update($param);
} else {
$param['userid'] = $this->uid;
$param['username'] = $this->username;
$param['create_time'] = time();
unset($param['id']);
$this->model->insert($param);
}
} catch (\Exception $e) {
$msg = $e->getMessage();
$this->error("错误提示:".$msg);
}
$this->success('操作成功', url('index', ['cid'=>$param['cid']]));
}
}
public function uploadimg()
{
return $this->fetch();
}
public function delete($id = null)
{
if (Request::instance()->isAjax()) {
if ($this->model->destroy($id)) {
return ['code' => 2, 'msg' => "删除成功"];
} else {
return ['code' => 0, 'msg' => "删除失败"];
}
}
}
}
|