diff --git a/app/controller/RotationChart.php b/app/controller/RotationChart.php new file mode 100644 index 0000000..1a83125 --- /dev/null +++ b/app/controller/RotationChart.php @@ -0,0 +1,137 @@ +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()); + } + } +} diff --git a/app/logic/Upload.php b/app/logic/Upload.php new file mode 100644 index 0000000..fce6780 --- /dev/null +++ b/app/logic/Upload.php @@ -0,0 +1,40 @@ +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; + } + +} \ No newline at end of file diff --git a/app/validate/RotationChart.php b/app/validate/RotationChart.php new file mode 100644 index 0000000..153104a --- /dev/null +++ b/app/validate/RotationChart.php @@ -0,0 +1,36 @@ + ['规则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'] + ]; +} diff --git a/route/app.php b/route/app.php index d7e35b6..b430e6e 100644 --- a/route/app.php +++ b/route/app.php @@ -103,6 +103,21 @@ Route::group('customerService',function() { Route::post('save','customerService/save'); Route::post('delete','customerService/delete'); })->middleware(CheckAdmin::class)->allowCrossDomain(); + +Route::group('notice',function() { + Route::post('list','notice/list'); + Route::post('save','notice/save'); + Route::post('delete','notice/delete'); +})->middleware(CheckAdmin::class)->allowCrossDomain(); + +Route::group('rotationChart',function() { + Route::post('list','rotationChart/list'); + Route::post('save','rotationChart/save'); + Route::post('delete','rotationChart/delete'); + Route::post('upload','rotationChart/upload'); +})->middleware(CheckAdmin::class)->allowCrossDomain(); + + # 支付(待开发) //Route::group('pay',function (){ // Route::post('pay','pay/pay');