<?php
namespace app\admin\controller;
use think\Request;
// 使用模型
use app\admin\model\Commodity as CommModel;
class Supervise extends Common
{
/*--商品信息管理页面--*/
public function supervise($keywords='')
{
//实例化调用model类
$comm = new CommModel();
//查询引擎
if($keywords){
$comm = $comm->where('title','like','%'.$keywords.'%');
}
//分页
$data = $comm->order('id desc')
->paginate(10);
$page = $data->render();
$this->assign('data',$data);
$this->assign('page',$page);
return $this->fetch();
}
//商品信息上传页面
public function add()
{
return $this->fetch();
}
//商品信息上传操作
public function doadd()
{
//判断是否是Ajax传值
if(!request()->isAjax())die();
//接收上传的图片
$images = request()->file('images');
//判断图片上传
if(!empty($images)){
//接收处理后的图片信息
$images = $this->uploadImages($images);
if($images == null){
return ['code' => 400, 'msg' => '图片存储错误'];
}else{
$file = $images;
$str_image = implode("|", $file);
}
}
//整理接收前台数据
$data = $_POST;
$data['images'] = $str_image;
$data['time'] = time();
//验证
$val = $this->validate($data,'Commodity');
if($val !== true){
return ['code'=>500,'msg'=>$val];
}
//数据入库
$sql = new CommModel($data);
$date = $sql->save();
if($date !== true){
return ['code'=>200,'msg'=>'车辆信息添加成功请检查'];
}else{
return ['code'=>400,'msg'=>'车辆信息添加失败请核实'];
}
}
//商品信息修改页面
public function edit($id)
{
if(empty($id))die;
//将信息带入修改页面
$sql = new CommModel();
$date = $sql->where('id',$id)->find();
$this->assign('date',$date);
return $this->fetch();
}
//商品信息修改操作
public function doedit()
{
//接收信息对应ID
$id = trim(request()->param('id'));
//接收上传的图片
$images = request()->file('images');
//判断图片上传
if(!empty($images)){
//接收处理后的图片信息
$images = $this->uploadImages($images);
if($images == null){
return ['code' => 400, 'msg' => '图片存储错误'];
}else{
$file = $images;
$str_image = implode("|", $file);
}
}
//接收表单传值
$date = $_POST;
if(!empty($str_image)){
$date['images'] = $str_image;
}else{
$date['images'] = $_POST['images'];
}
//实例化上传项
$sql = new CommModel();
//执行修改
$bol = $sql->save($date,['id' => $id]);
if($bol){
return ['code'=>200,'msg'=>'资料修改成功请检查'];
}else{
return ['code'=>400,'msg'=>'资料未更新请点取消'];
}
}
//是否发布车辆
public function isPub()
{
//获取标签
$tag = $this->request->param('tag');
//获取id
$id = $this->request->param('id');
if(!isset($id)){
return ['code' => 400,'msg' => '缺少必要更新条件'];
}
switch ($tag){
case 'pub':
$param['ispub'] = '1';
break;
default:
$param['ispub'] = '0';
}
//修改入库
$CommModel = new CommModel();
if($CommModel->save($param,['id'=>$id])){
return ['code' => 200,'msg' => '操作成功'];
}else{
return ['code' => 400,'msg' => '操作失败'];
}
}
//商品信息删除
public function dodel($id)
{
//实例化模型对象
$sql = new CommModel();
$img = $sql->where('id',$id)->field('images')->find();
$link = explode('|',$img['images']);
//循环删除车辆信息中包含的图片文件
foreach($link as $key=>$value){
unlink(ROOT_PATH . 'public' . DS . 'uploads' . DS . $value);
}
//执行删除
$del = $sql->deletes($id);
if($del == 1){
return ['code'=>200,'msg'=>'删除成功'];
}else{
return ['code'=>400,'msg'=>'删除失败'];
}
}
//多图片上传功能
public function uploadImages($files)
{
foreach($files as $file){
// 移动到框架应用根目录/public/uploads/ 目录下
$info = $file->validate(['size' => '2097152','ext' => 'jpeg,jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
if($info){
// 成功上传后 获取上传信息
$arr[] = $info->getSaveName();
}else{
// 上传失败获取错误信息
return $file->getError();
}
}
return $arr;
}