diff --git a/app/controller/AdminZoneGoods.php b/app/controller/AdminZoneGoods.php new file mode 100644 index 0000000..eaa4e79 --- /dev/null +++ b/app/controller/AdminZoneGoods.php @@ -0,0 +1,99 @@ +find($zone_id); + if (!$ZoneRes) throw new ValidateException('所属专区不存在'); + + $ZoneGoods = new ZoneGoods(); + + # 填写总概率不能超出 1 + $rule = [ + 'zone_id|所属专区id' => 'require|number|max:10', + 'title|标题' => 'require|number|max:10', + 'price|单价' => 'require|float|max:12', + 'cover_image|封面图'=> 'require|max:127', + 'bg_image|背景图' => 'require|max:127', + 'max_awards_amount|最高中奖金额' => 'max:12', + ]; + + if ($id) { + validate()->rule($rule)->scene('goodsParamEdit')->check($param); + $Save = $ZoneGoods->find($id); + $Save->zone_id = $param['zone_id']; + $Save->title = $param['title']; + $Save->price = $param['price']; + $Save->cover_image = $param['cover_image']; + $Save->bg_image = $param['bg_image']; + if (!empty($param['max_awards_amount'])) $Save->max_awards_amount = $param['max_awards_amount']; + if (!empty($content)) $Save->content = $content; + $Save->update_time = date("Y-m-d H:i:s",time()); + $Save->save(); + } else { + validate()->rule($rule)->scene('goodsParamAdd')->check($param); + + $ZoneGoods->save([ + 'zone_id' => $zone_id, + 'title' => $param['title'], + 'max_awards_amount' => $param['max_awards_amount'], + 'content' => $content, + 'price' => $param['price'], + 'cover_image' => $param['cover_image'], + 'bg_image' => $param['bg_image'], + '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('操作失败'); + } + } + + /** + * 上传轮播图图片 + * @return array + */ + public function upload() + { + $file = request()->file(); + $param = Request::param(); + try { + $rule = [ + 'image' => [ + 'fileSize:10240000', // 文件大小不超过10M (10 * 1024 KB) + 'fileExt:jpeg,jpg,png,gif' + ], + 'type' => 'require|in:1,2' + ]; + validate($rule)->check($file); + $image_path = $param['type'] == 1 ? 'topic' : 'background'; + $path = Upload::uploadImage($file['image'],$image_path); + return $this->renderSuccess('上传成功',['path' => $path,'url' => get_image_url($path)]); + } catch (ValidateException $e) { + return $this->renderError($e->getMessage()); + } + } + +} \ No newline at end of file