讨论数量:
确实需要优化的,您 if 这里做的事情都是数据校验,应该交给 表单验证来搞
表单验证《Laravel 9 中文文档》
$aParam = $request->only(['name','email','password','password_confirmation']);
$oValidator = Validator::make(
$aParam,
[
'name' => 'required|max:10',
'email' => 'required|email|exists:users,email',
'password' => ['required','regex:/^[\d\w]*$/','confirmed']
],
[
'name.required' => '请输入姓名',
'name.max' => '最长10位',
'等等' => '等等'
]
);
if($oValidator->fails()){
dd($oValidator->errors()->first());
}
确实需要优化的,您 if 这里做的事情都是数据校验,应该交给 表单验证来搞
表单验证《Laravel 9 中文文档》
$aParam = $request->only(['name','email','password','password_confirmation']);
$oValidator = Validator::make(
$aParam,
[
'name' => 'required|max:10',
'email' => 'required|email|exists:users,email',
'password' => ['required','regex:/^[\d\w]*$/','confirmed']
],
[
'name.required' => '请输入姓名',
'name.max' => '最长10位',
'等等' => '等等'
]
);
if($oValidator->fails()){
dd($oValidator->errors()->first());
}
$VoucherTemplate = VoucherTemplate::findOrEmpty($params['tid']);
if ($VoucherTemplate->isEmpty()) {
throw new Exception('代金卷不存在或者失效');
}
if ($VoucherTemplate->vouchertemplate_total <= $VoucherTemplate->vouchertemplate_giveout) {
throw new Exception('代金券已兑换完');
}
if ($VoucherTemplate->vouchertemplate_state == VoucherTemplate::VOUCHER_TEMPLATE_STATE_OFF) {
throw new Exception('代金券模版状态失效');
}
if ($VoucherTemplate->vouchertemplate_startdate > TIMESTAMP || $VoucherTemplate->vouchertemplate_enddate < TIMESTAMP) {
throw new Exception('代金券未开启');
}
if ($request->memberBiz->checkIfYourStore($VoucherTemplate->vouchertemplate_store_id)) {
throw new Exception('不可以兑换自己店铺的代金券');
}
if ($request->memberInfo->member_points < $VoucherTemplate->vouchertemplate_points) {
throw new Exception('您的积分不足,暂时不能兑换该代金券');
}
$voucherList = Voucher::where('vouchertemplate_id', $params['tid'])
->where('voucher_owner_id', $request->member_id)
->where('voucher_store_id', $VoucherTemplate->vouchertemplate_store_id)
->select();
if (!$voucherList->isEmpty()) {
$voucher_count = $voucherList->count();
if ($voucher_count >= $VoucherTemplate->vouchertemplate_eachlimit) {
throw new Exception("该代金券您已兑换{$voucher_count}次,不可再兑换了");
}
$voucher_count = $voucherList->where('voucher_state', Voucher::VOUCHER_STATE_NOT_USED)->count();
//买家最多只能拥有同一个店铺尚未消费抵用的店铺代金券最大数量的验证
if ($voucher_count >= intval(config('ds_config.voucher_buyertimes_limit'))) {
$message = sprintf('您的可用代金券已有%s张,不可再兑换了', config('ds_config.voucher_buyertimes_limit'));
throw new Exception($message);
}
}
同样贴个类似的,这种数据校验有没有好的优化?
确实需要优化的,您 if 这里做的事情都是数据校验,应该交给 表单验证来搞
表单验证《Laravel 9 中文文档》