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); } /** * 上传图片 * @return Json */ public function upload_img(): Json { try { if (!request()->isPost()) { throw new Exception('请求方式错误'); } $file = request()->file('image'); $files[] = $file; 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); // 获取上传后的文件路径 if (!$savename) { throw new Exception('上传失败'); } $result = [ 'errno' => 0, 'data' => [ 'url' => $savename, ], ]; } catch (ValidateException $e) { return Json([ 'errno' => 1, 'message' => $e->getMessage(), ]); } catch (Exception $exc) { $result = [ 'errno' => 1, 'message' => $exc->getMessage(), ]; } return json($result); } /** * 上传封面图片 * @return Json */ public function upload_cover_img(): Json { try { if (!request()->isPost()) { throw new Exception('请求方式错误'); } $file = request()->file('file'); $files[] = $file; 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); // 获取上传后的文件路径 if (!$savename) { throw new Exception('上传失败'); } $result = [ 'code' => 200, 'data' => [ 'url' => $savename, ], ]; } catch (ValidateException $e) { return Json([ 'code' => $e->getCode(), 'message' => $e->getMessage(), ]); } catch (Exception $exc) { $result = [ 'code' => $exc->getCode(), 'message' => $exc->getMessage(), ]; } return json($result); } /** * 保存博客 * @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 = [ 'code' => $exc->getCode(), 'message' => $exc->getMessage(), ]; } 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); } }