我这个注册逻辑If会不会太多?需不需要优化

问题描述

项目有个注册逻辑,由于对注册信息要求比较多,所以if较多,这种需不需要优化,如果需要要怎么做

我这个注册逻辑If会不会太多?需不需要优化

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

确实需要优化的,您 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());
        }
1年前 评论
威尼斯 (楼主) 1年前
liziyu 1年前
讨论数量: 15

确实需要优化的,您 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());
        }
1年前 评论
威尼斯 (楼主) 1年前
liziyu 1年前
╰ゝSakura

搞个验证器吧,把非空的验证逻辑,放在验证器里面

1年前

确实需要优化的,您 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());
        }
1年前 评论
威尼斯 (楼主) 1年前
liziyu 1年前

表单验证可以实现每个 if 对应的判断,还简单些

1年前
 $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);
                }
            }

同样贴个类似的,这种数据校验有没有好的优化?

1年前 评论
lizzj 1年前
lizzj 1年前
微加加的朋友 (作者) 1年前

求求看下文档吧,,,

1年前

框架无关的写法,拆成一个大方法,大方法里呼叫若干个小方法。 小方法里抛异常。

1年前
chowjiawei

@微加加的朋友 可以放在验证器里面 自定义验证 file

1年前 评论
微加加的朋友 1年前
chowjiawei (作者) 1年前

建议楼主使用表单验证器

1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!