123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- declare (strict_types = 1);
- /**
- * +----------------------------------------------------------------------
- * 后台文章控制制器
- * @author huwhis@163.com
- * @version 0.0.1
- * +----------------------------------------------------------------------
- */
- namespace app\sys\controller;
- // 引入框架内置类
- use think\facade\View;
- use app\common\model\Category as CategoryModel;
- /**
- * 文章管理
- */
- class Article extends Base
- {
- protected $modelName = "Article";
- public function index()
- {
- $categories = CategoryModel::articleCategoryies();
- $categories_url = [];
- $categories_name = [];
- foreach ($categories as $value) {
- $categories_url[$value->id] = $value->url;
- $categories_name[$value->id] = $value->category_name;
- }
- $category_tree = obj_tree($categories);
- $where = [];
- if ($cid = $this->app->request->param('cid')) {
- $where['cid'] = $cid;
- }
- $data = $this->modelName::where($where)->field('id,cid,title,username,hits,sort,create_time')->order(['sort desc', 'id desc'])->paginate();
- View::assign('category_tree', $category_tree);
- View::assign('categories_url', $categories_url);
- View::assign('categories_name', $categories_name);
- View::assign('data', $data);
- View::assign('cid', $cid);
- return View::fetch();
- }
- public function save($id = 0, $cid = 0)
- {
- if ($this->app->request->isPost()) {
- $params = $this->app->request->param();
- if (!$params['cid']) {
- $this->error('请选择栏目');
- }
- if ($params['title'] == '') {
- $this->error("标题不能为空");
- }
- $params['content'] = $params['editorValue'];
- unset($params['editorValue']);
- try {
- if ($params['id'] != 0) {
- $this->modelName::update($params);
- } else {
- $params['userid'] = $this->getSysUser()->userid;
- $params['username'] = $this->getSysUser()->username;
- unset($params['id']);
- $this->modelName::create($params);
- }
- } catch (\Exception $e) {
- $msg = $e->getMessage();
- $this->error("错误提示:".$msg);
- }
- $this->success('操作成功', (string) url('index?cid='.$params['cid']));
- } else {
- if (!$cid) {
- $this->error('请选择栏目');
- }
- if ($id) {
- $data = $this->modelName::find($id);
- } else {
- $data = null;
- }
-
- $category_info = CategoryModel::field('id, parent_id, category_name')->find($cid);
-
- View::assign('cid', $cid);
- View::assign('category_info', $category_info);
- View::assign('data', $data);
-
- return View::fetch();
- }
- }
- }
|