4 changed files with 228 additions and 0 deletions
@ -0,0 +1,137 @@ |
|||
<?php |
|||
declare (strict_types = 1); |
|||
|
|||
namespace app\controller; |
|||
|
|||
use app\BaseController; |
|||
use app\logic\Upload; |
|||
use app\model\RotationChart as RotationChartModel; |
|||
use app\validate\RotationChart as RotationChartValidate; |
|||
use think\exception\ValidateException; |
|||
use think\facade\Request; |
|||
|
|||
|
|||
class RotationChart extends BaseController |
|||
{ |
|||
/** |
|||
* 显示资源列表 |
|||
*/ |
|||
public function list() |
|||
{ |
|||
$request = Request::param(); |
|||
|
|||
$limit = $request['limit'] ?? 10; |
|||
|
|||
$where = []; |
|||
|
|||
$CustomerServiceModel = new RotationChartModel(); |
|||
|
|||
# 查询用户列表 |
|||
$field = 'id,image,sort,title'; |
|||
$res = $CustomerServiceModel->field($field)->where($where)->order('sort asc,id desc')->paginate($limit); |
|||
|
|||
$list = $res->items(); |
|||
foreach ($list as &$item) { |
|||
$item['image'] = get_image_url($item['image']); |
|||
} |
|||
$total = $res->total(); |
|||
|
|||
return $this->renderSuccess('数据返回成功', ['list' => $list, 'total' => $total]); |
|||
} |
|||
|
|||
/** |
|||
* 保存新建的资源 |
|||
*/ |
|||
public function save() |
|||
{ |
|||
$param = Request::param(); |
|||
|
|||
try { |
|||
$id = $param['id'] ?? 0; |
|||
|
|||
if ($id) { |
|||
validate(RotationChartValidate::class)->scene('edit')->check($param); |
|||
$CustomerServiceModel = RotationChartModel::find($id); |
|||
$CustomerServiceModel->title = $param['title']; |
|||
$CustomerServiceModel->image = $param['image']; |
|||
$CustomerServiceModel->sort = $param['sort'] ?? 1; |
|||
$CustomerServiceModel->update_time = date("Y-m-d H:i:s",time()); |
|||
$CustomerServiceModel->save(); |
|||
} else { |
|||
validate(RotationChartValidate::class)->scene('add')->check($param); |
|||
|
|||
$CustomerService = new RotationChartModel(); |
|||
$CustomerService->save([ |
|||
'title' => $param['title'], |
|||
'image' => $param['image'], |
|||
'sort' => $param['sort'] ?? 1, |
|||
'create_time' => date("Y-m-d H:i:s",time()) |
|||
]); |
|||
} |
|||
|
|||
return $this->renderSuccess($id ? '编辑成功' : '添加成功'); |
|||
} catch (ValidateException $validateException) { |
|||
return $this->renderError($validateException->getMessage()); |
|||
} catch (\Exception $e) { |
|||
return $this->renderError('操作失败'); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 删除指定资源 |
|||
*/ |
|||
public function delete() |
|||
{ |
|||
$param = Request::param(); |
|||
|
|||
try { |
|||
|
|||
validate(RotationChartValidate::class)->scene('del')->check($param); |
|||
|
|||
$result = RotationChartModel::destroy($param['id']); |
|||
|
|||
if (!$result) throw new ValidateException('删除失败'); |
|||
|
|||
return $this->renderSuccess('已删除'); |
|||
} catch (ValidateException $validateException) { |
|||
return $this->renderError($validateException->getMessage()); |
|||
} catch (\Exception $e) { |
|||
return $this->renderError('操作失败'); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 上传轮播图图片 |
|||
* @return array |
|||
*/ |
|||
public function upload() |
|||
{ |
|||
$file = request()->file(); |
|||
|
|||
try { |
|||
$rule = [ |
|||
'image' => [ |
|||
'fileSize:10240', // 文件大小不超过10M (10 * 1024 KB) |
|||
'fileExt:jpeg,jpg,png,gif', // 文件后缀只允许jpeg,png,jpg,gif |
|||
function ($value) { |
|||
// 验证图片宽度在350 |
|||
list($width, $height) = getimagesize($value->getPathName()); |
|||
|
|||
if ($width < 345 || $width > 355) { |
|||
return '图片宽度不能小于345,且不能大于355像素'; |
|||
} |
|||
if ($height < 145 || $height > 155) { |
|||
return '图片高度不能小于145,且不能大于155像素'; |
|||
} |
|||
return true; |
|||
} |
|||
] |
|||
]; |
|||
validate($rule)->check($file); |
|||
$path = Upload::uploadImage($file['image'],'rotation_chart'); |
|||
return $this->renderSuccess('上传成功',['path' => $path,'url' => get_image_url($path)]); |
|||
} catch (ValidateException $e) { |
|||
return $this->renderError($e->getMessage()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,40 @@ |
|||
<?php |
|||
|
|||
namespace app\logic; |
|||
|
|||
use think\facade\Filesystem; |
|||
|
|||
class Upload |
|||
{ |
|||
|
|||
/** |
|||
* 图片上传 |
|||
* @param $file |
|||
* @param $path |
|||
* @return bool|string |
|||
*/ |
|||
public static function uploadImage($file,$path = 'topic') |
|||
{ |
|||
// 上传到本地服务器 |
|||
// $file = request()->file('image'); |
|||
|
|||
return Filesystem::putFile( $path, $file); |
|||
} |
|||
|
|||
/** |
|||
* 多图片上传 |
|||
* @param array $files |
|||
* @param $path |
|||
* @return array |
|||
*/ |
|||
public static function uploadImageAll(array $files,$path = 'topic') |
|||
{ |
|||
//$files = request()->file('image'); |
|||
$saveName = []; |
|||
foreach($files as $file){ |
|||
$saveName[] = Filesystem::putFile( $path, $file); |
|||
} |
|||
return $saveName; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
<?php |
|||
declare (strict_types = 1); |
|||
|
|||
namespace app\validate; |
|||
|
|||
use think\Validate; |
|||
|
|||
class RotationChart extends Validate |
|||
{ |
|||
/** |
|||
* 定义验证规则 |
|||
* 格式:'字段名' => ['规则1','规则2'...] |
|||
* |
|||
* @var array |
|||
*/ |
|||
protected $rule = [ |
|||
'id|轮播图id' => 'require|number', |
|||
'title|标题' => 'require|max:40', |
|||
'image|图片地址' => 'require|max:200', |
|||
'sort|排序' => 'number', |
|||
]; |
|||
|
|||
/** |
|||
* 定义错误信息 |
|||
* 格式:'字段名.规则名' => '错误信息' |
|||
* |
|||
* @var array |
|||
*/ |
|||
protected $message = []; |
|||
|
|||
protected $scene = [ |
|||
'add' => ['image','sort','title'], |
|||
'edit' => ['image','sort','title','id'], |
|||
'del' => ['id'] |
|||
]; |
|||
} |
|||
Loading…
Reference in new issue