刮刮后端接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.5 KiB

<?php
declare (strict_types = 1);
namespace app\controller;
use app\BaseController;
use app\logic\Upload;
use app\middleware\CheckAdmin;
use app\model\RotationChart as RotationChartModel;
use app\validate\RotationChart as RotationChartValidate;
use think\exception\ValidateException;
use think\facade\Request;
class RotationChart extends BaseController
{
protected $middleware = [CheckAdmin::class];
/**
* 显示资源列表
*/
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['up_image'] = $item['image'];
$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:10240000', // 文件大小不超过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());
}
}
}