摘要:<?phpnamespace app\admin\controller;//use think\facade\View; //view創(chuàng)建靜態(tài)代理use app\admin\model\PorModel;use think\Controller;use app\admin\controller\Common;use think\facade\Request;//系統(tǒng)控制器use
<?php
namespace app\admin\controller;
//use think\facade\View; //view創(chuàng)建靜態(tài)代理
use app\admin\model\PorModel;
use think\Controller;
use app\admin\controller\Common;
use think\facade\Request;//系統(tǒng)控制器
use think\facade\Session;
class Product extends Common
{
//渲染編輯頁面
public function index()
{
$news = new PorModel();
$new = $news->order('id', 'desc')->paginate(8);
$this->view->new = $new;
return $this->fetch();
}
//渲染添加界面
public function add()
{
return $this->fetch();
}
//上傳方法
public function upload()
{
//獲取上傳的圖片
$file = Request::file('img');
//驗證圖片信息并保存到指定位置
if ($info = $file->validate(['ext'=> 'jpg,jpeg,png,gif'])->move('uploads')) {
//返回上傳成功信息
return json(['errno' => 0,'data' => ['/uploads/' . $info->getSaveName()]]);
}else{
//返回錯誤信息
return $file->getError();
}
}
//縮略圖上傳方法
public function uploads()
{
//獲取上傳文件
$file = Request::file('file');
//對圖片的驗證
if ($info = $file->validate(['ext'=> 'jpg,jpeg,png,gif'])->move('uploads')) {
//返回上傳成功信息
$fileName = '/uploads/' . $info->getSaveName();
return json([1,'上傳成功!','data' => $fileName]);
}else{
//返回錯誤信息
return $file->getError();
}
}
//添加操作
public function DoAdd()
{
//獲取用戶添加信息
$data = Request::param();
//獲取當(dāng)時時間
$data['time'] = time();
//提取session中的用戶名
$data['username'] = Session::get('username');
//獲取添加標(biāo)題
$title = $data['title'];
//查找數(shù)據(jù)庫標(biāo)題是否存在
$news = PorModel::where('title',$title)->find();
if($news == true){
return['res'=>0,'msg'=>'新聞標(biāo)題重復(fù)!'];
}
//實例化模型
$new = new PorModel();
//發(fā)布信息
if($new->save($data)){
return ['res'=>1,'msg'=>'發(fā)布成功!'];
}else{
return['res'=>0,'msg'=>'發(fā)布失??!'];
}
}
//渲染編輯頁面
public function edit()
{
//獲取信息id
$newId = Request::param('id');
//使用得到的id查詢數(shù)據(jù)庫信息
$new = PorModel::get($newId);
//將數(shù)據(jù)賦值到模板
$this->view->new = $new;
//渲染新聞修改頁模板
return $this->fetch();
}
public function DoEdit()
{
//獲取提交的數(shù)據(jù)
$data = Request::param();
//實例化模型
$new = new PorModel();
//修改的內(nèi)容
$res = $new->save([
'title' => $data['title'],
'pic' => $data['pic'],
'desc' => $data['desc'],
'content' => $data['content'],
'username' => Session::get('username'),
'time' => time(),
],['id' => $data['id']]);
if($res) {
return['res'=>1,'msg'=>'修改成功!'];
}else{
return['res'=>0,'msg'=>'修改失??!'];
}
}
public function del()
{
//獲取新聞id
$newId = Request::param('id');
//實例化模型
$new = new PorModel();
//刪除驗證
if($new->destroy($newId)){
return['res'=>1,'msg'=>'刪除成功!'];
}else{
return['res'=>0,'msg'=>'刪除失?。?#39;];
}
}
}
批改老師:查無此人批改時間:2019-06-19 09:18:08
老師總結(jié):完成的不錯。后臺cms管理系統(tǒng),就是對數(shù)據(jù)進(jìn)行操作。操作越簡單越好。繼續(xù)加油。