1 changed files with 99 additions and 0 deletions
@ -0,0 +1,99 @@ |
|||
<?php |
|||
|
|||
namespace app\controller; |
|||
|
|||
use app\BaseController; |
|||
use app\logic\Upload; |
|||
use app\model\ZoneGoods; |
|||
use think\exception\ValidateException; |
|||
use think\facade\Request; |
|||
|
|||
class AdminZoneGoods extends BaseController |
|||
{ |
|||
|
|||
public function saveParam() |
|||
{ |
|||
|
|||
$param = Request::param(); |
|||
$content = Request::param('content','','strip_tags'); |
|||
|
|||
try { |
|||
$id = $param['id'] ?? 0; |
|||
$zone_id = $param['zone_id'] ?? 0; |
|||
|
|||
$Zone = new \app\model\Zone(); |
|||
$ZoneRes = $Zone->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()); |
|||
} |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue