abstrait:<?php /** * Created by PhpStorm. * User: Jason * Date: 2019/4/27 * Time: 20:26 */ namespace app\admin\controller; u
<?php /** * Created by PhpStorm. * User: Jason * Date: 2019/4/27 * Time: 20:26 */ namespace app\admin\controller; use app\admin\controller\Common; use think\facade\Request; use think\facade\Session; use app\admin\model\SlideModel; class Slide extends Common { // 渲染列表 public function index() { // 獲取所有的輪播圖片信息 $slide = SlideModel::order('id','desc')->paginate(3); // 模板賦值 $this->assign('slide',$slide); // 渲染模板 return $this->fetch(); } // 渲染添加模板 public function add() { return $this->fetch(); } // 添加操作 public function DoAdd() { // 獲取上傳的數(shù)據(jù) $data = Request::param(); // 拼接時間,發(fā)布用戶 $data['username'] = Session::get('username'); $data['time'] = time(); // 驗證是否寫入成功 $ins = SlideModel::create($data); // 判斷 if($ins) { return json(['code'=>1,'msg'=>'添加成功']); } return json(['code'=>0,'msg'=>'添加失敗']); } // 刪除操作 public function DoDel() { // 獲取刪除條件 $slide_id = Request::param('id'); // 使用模型刪除數(shù)據(jù) $del = SlideModel::destroy($slide_id); if($del) { // 返回刪除成功信息 return json(['code'=>1,'msg'=>'數(shù)據(jù)刪除成功']); } // 返回刪除失敗信息 return json(['code'=>0,'msg'=>'數(shù)據(jù)刪除失敗']); } // 圖片上傳 public function upload() { // 獲取上傳圖片信息 $file = Request::file('file'); // 驗證并移動文件到指定目錄 if($info = $file->validate(['ext'=>'jpg,jpeg,png,gif'])->move('upload')) { // 拼接上傳完成的圖片地址 $filePath = '/upload/'.$info->getSaveName(); // 返回上傳成功信息 return json([1,'文件上傳成功','data'=>$filePath]); } // 返回錯誤信息 return $file->getError(); } }
實現(xiàn)輪播圖的添加刪操作
Professeur correcteur:天蓬老師Temps de correction:2019-04-28 08:49:40
Résumé du professeur:json(['code'=>1,'msg'=>'數(shù)據(jù)刪除成功']); 如果你是ajax調(diào)用 的話, 默認返回 的就是json格式字符串, 這里的json()函數(shù)是多余 的