摘要:<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/12/6 0005 * Time: 上午 9:43 */ namespace 
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/12/6 0005
* Time: 上午 9:43
*/
namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\ProductModel;
use think\facade\Request;
use think\facade\Session;
class Product extends Base
{
public function lst()
{
$products = ProductModel::order('id','desc')->paginate(2);
$this->assign('products',$products);
return $this->fetch();
}
//富文本中,图片上传方法
public function upload()
{
$file = request()->file('img');
//img 这个取决于 wangEditor 这个富文本编辑器,设置图片上传的控件名称:类似于input的name属性,供接口获取图片信息使用
//editor.customConfig.uploadFileName = 'img'
// 移动到框架应用根目录uploads/images 目录下
$info = $file->validate(['ext'=>'jpg,jpeg,png,gif'])->move( 'uploads/images/');
if($info)
{
// 返回上传成功信息,返回格式也跟 wangEditor 这个富文本编辑器 接口返回的数据格式 有关系,详情查看官方文档
return json(['errno' => 0, 'data' => ['/uploads/images/' . $info->getSaveName()]]);
}else {
return $file->getError();
}
}
public function add()
{
if(request()->isPost())
{
$data = Request::param();
$data['time'] = time();
$data['username'] = Session::get('username');
// 存数据
$res = ProductModel::create($data);
// 存储验证
if($res)
{
return ['res' => 1, 'msg' => '上传成功!'];
}else{
return ['res' => 0, 'msg' => '上传失败!'];
}
}
return $this->fetch();
}
public function edit()
{
// 获取前台列表页面提交过来的id数据
$proId = Request::param('id');
// 通过id 查询数据
$products = ProductModel::get($proId);
if(request()->isPost())
{
$data = Request::param();
$data['time'] = time();
$data['username'] = Session::get('username');
$res = ProductModel::where('id',$data['id'])->update($data);
if ($res)
{
return ['res' => 1, 'msg' =>'修改成功!'];
}else{
return ['res' => 0, 'msg' =>'修改失败!'];
}
}
$this->assign('products',$products);
return $this->fetch();
}
public function del()
{
$delId = Request::param('id');
$res = ProductModel::destroy($delId);
if ($res)
{
return ['res' => 1, 'msg' => '删除成功!'];
}else{
return ['res' => 0, 'msg' => '删除失败!'];
}
}
}
批改老师:天蓬老师批改时间:2018-12-06 13:27:38
老师总结:下次提交表单时, 不必再判断类型,其实,框架会自动判断的