SampleValidator.php
3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace App\Http\Validators;
use Validator;
class SampleValidator
{
//保存相关的验证,别问我为什么不用laravel自带的form-request类
//因为控制器那边已经被之前的人魔改的难用的一比,而且控制器那边还接收了一大堆统一变量
public function checkSave($request)
{
$messages = $this->messages();
$validator = Validator::make($request->all(), [
'activity_name' => 'required|max:50',
'start_time' => 'required',
'end_time' => 'required',
'goods_scope' => 'in:1,2',
'allow_coupon' => 'required',
'amount' => 'required|max:9999999999',
'amount.*' => 'required',
'item_name' => 'required',
'item_name.*' => 'required',
'num' => 'required|max:1000',
'num.*' => 'required',
'total' => 'required',
'total.*' => 'required',
'pic' => 'required',
'pic.*' => 'required',
'supplier_ids' => 'required',
], $messages);
if ($validator->fails()) {
return $validator->errors()->first();
}
//开始结束时间的判断
$oneDaySeconds = 3600 * 24;
if (strtotime($request->get('end_time')) - strtotime($request->get('start_time')) < $oneDaySeconds) {
return '结束时间必须大于开始时间一天或以上';
}
//满赠商品规则最多设置3个
if (count($request->get('amount')) > 3) {
return '满赠商品规则最多设置3个';
}
$service = new GiftActivityService();
$itemList = $service->getGiftItemList($request);
if (count($itemList) != 1) {
foreach ($itemList as $key => $item) {
if ($key == 0) {
continue;
}
if ($itemList[$key]['amount'] <= $itemList[$key - 1]['amount']) {
return '设置满赠规则的满赠金额需逐级递增';
}
}
}
//通用逻辑判断
$result = $this->commonLogicCheck($request, 2);
if ($result !== true) {
return $result;
}
return null;
}
private function messages()
{
return [
'activity_name.max' => '展示名称最长50个汉字',
'show_name.max' => '展示名称最长3个汉字',
'sign.max' => '标识最长1个汉字',
'sign_text.max' => '展示名称最长16个汉字',
'ratio.numeric' => '活动系数只能为纯数字',
'amount.required' => '至少要有一个满赠商品规则',
'activity_name.required' => '活动名称不能为空',
'start_time.required' => '活动开始时间不能为空',
'end_time.required' => '活动结束时间不能为空',
'show_name.required' => '活动展示名称不能为空',
'sign.required' => '活动标识不能为空',
'sign_text.required' => '活动标识文本不能为空',
'allow_coupon.required' => '是否能使用优惠券不能为空',
'amount.*.required' => '请补全满赠商品规则的满赠金额',
'amount.*.max' => '请补全满赠商品规则的满赠金额',
'item_name.*.required' => '请补全满赠商品规则的赠品名称',
'item_name.*.between' => '满赠商品规则的赠品名称必须在2到20个字之间',
'num.*.required' => '请补全满赠商品规则的数量',
'total.*.required' => '请补全满赠商品规则的发行量',
'pic.*.required' => '请补全满赠商品规则的商品图片',
'supplier_ids.required' => '供应商不能为空',
'goods_scope.in' => '商品范围不符合规范',
];
}
}