chengkun
2025-09-05 4822304b63e1bd6327860af7f3db0133cecf167f
app/admin/controller/Blog.php
@@ -2,6 +2,7 @@
namespace app\admin\controller;
use think\facade\Db;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Filesystem;
@@ -13,10 +14,65 @@
        return View::fetch('index');
    }
    
    public function add(): string {
    public function get_blog_list() {
        try {
            $page   = input('page', 1);
            $limit  = input('limit', 10);
            $where  = [];
            $total  = Db::name('blog')->where($where)->count();
            $list   = Db::name('blog')->where($where)->order('id desc')->page($page, $limit)->select()->toArray();
            $result = [
                'code'    => 200,
                'message' => '获取成功',
                'data'    => [
                    'list'  => $list,
                    'total' => $total,
                ],
            ];
        } catch (Exception $exc) {
            $result = [
                'code'    => $exc->getCode(),
                'message' => $exc->getMessage(),
            ];
        }
        return json($result);
    }
    public function add($id = 0): string {
        header('Content-Type:text/html;charset=utf-8');
        View::assign('menuitem', strtolower('Blog-index'));
        View::assign('id', $id);
        return View::fetch('add');
    }
    /**
     * 获取博客信息
     * @return Json
     */
    public function get_blog_info(): Json {
        try {
            $id = input('id');
            if (!is_numeric($id)) {
                throw new Exception('参数错误');
            }
            $info = Db::name('blog')->field('id,title,en_title,desc,en_desc,cover_img,content,en_content')->where('id', $id)->find();
            if (empty($info)) {
                throw new Exception('信息不存在');
            }
            $result = [
                'code'    => 200,
                'message' => '获取成功',
                'data'    => $info,
            ];
        } catch (Exception $exc) {
            $result = [
                'code'    => $exc->getCode(),
                'message' => $exc->getMessage(),
            ];
        }
        return json($result);
    }
    
    /**
@@ -31,7 +87,7 @@
            
            $file    = request()->file('image');
            $files[] = $file;
            validate(['image' => 'fileSize:10240|fileExt:jpg'])->check($files);
            validate(['image' => 'fileSize:10240|fileExt:jpg,jpeg,png'])->check($files);
            // 上传到本地服务器
            $savename = (new Filesystem)::disk('public')->putFile('/images', $file);
            $savename = (new Filesystem)::disk('public')->url($savename); // 获取上传后的文件路径
@@ -71,7 +127,7 @@
            
            $file    = request()->file('file');
            $files[] = $file;
            validate(['image' => 'fileSize:10240|fileExt:jpg'])->check($files);
            validate(['image' => 'fileSize:10240|fileExt:jpg,jpeg,png'])->check($files);
            // 上传到本地服务器
            $savename = (new Filesystem)::disk('public')->putFile('/images', $file);
            $savename = (new Filesystem)::disk('public')->url($savename); // 获取上传后的文件路径
@@ -99,12 +155,50 @@
        return json($result);
    }
    
    public function save_blog() {
    /**
     * 保存博客
     * @return Json
     */
    public function save_blog(): Json {
        try {
            if (!request()->isPost()) {
                throw new Exception('请求方式错误');
            }
            $data = request()->post();
            ///////验证////////////
            $validate        = new \app\admin\validate\Blog();
            $validate_result = $validate->scene('save_blog')->check($data);
            if (!$validate_result) {
                throw new Exception($validate->getError());
            }
            if ($data['id'] && is_numeric($data['id'])) {
                /////////更新////////////
                $info = Db::name('blog')->field('id')->where('id', $data['id'])->find();
                if (empty($info)) {
                    throw new Exception('信息不存在');
                }
                unset($data['create_time']);
                $update_result = Db::name('blog')->where('id', $data['id'])->save($data);
                if (!$update_result) {
                    throw new Exception('更新失败');
                }
            } else {
                //////////添加////////////
                $data['create_time'] = time();
                $data['admin_id']    = $this->admin_id;
                $new_id = Db::name('blog')->insertGetId($data);
                if (!$new_id) {
                    throw new Exception('保存失败');
                }
            }
            $result = [
                'code'    => 200,
                'message' => '保存成功',
            ];
            
        } catch (Exception $exc) {
            $result = [
@@ -114,4 +208,37 @@
        }
        return json($result);
    }
    /**
     * 修改博客状态
     * @return Json
     */
    public function change_blog_status() {
        try {
            $id = input('id');
            if (!is_numeric($id)) {
                throw new Exception('参数错误');
            }
            $info = Db::name('blog')->field('id,status')->where('id', $id)->find();
            if (empty($info)) {
                throw new Exception('信息不存在');
            }
            $status        = $info['status'] == 1 ? 0 : 1;
            $update_result = Db::name('blog')->where('id', $id)->save(['status' => $status]);
            if (!$update_result) {
                throw new Exception('更新失败');
            }
            $result = [
                'code'    => 200,
                'message' => '更新成功',
            ];
        } catch (Exception $exc) {
            $result = [
                'code'    => $exc->getCode(),
                'message' => $exc->getMessage(),
            ];
        }
        return json($result);
    }
}