全栈微信小程序商城 学习笔记之四 构建验证层

全栈微信小程序商城 学习笔记之四 构建验证层

banner相关数据表字段一览

表banner_item

id
img_id 外键,关联image表
key_word 执行关键字,根据不同的type含义不同
type 跳转类型,可能导向商品,可能导向专题,可能导向其他。0,无导向;1:导向商品;2:导向专题
delete_time
banner_id 外键,关联banner表
update_time

表banner

id
name Banner名称,通常作为标识
description Banner描述
delete_time
update_time

新建接口

applicationapicontrollerv1Banner.php

class Banner
{
    public function getBanner($id)
    {

    }
}

路由配置

application oute.php

<?php
use thinkRoute;
Route::get('banner/:id', 'api/v1.Banner/getBanner');//第二个参数:模块/控制器/方法

独立验证和验证器

使用独立验证

applicationapicontrollerv1Banner.php

<?php

namespace appapicontrollerv1;

use thinkValidate;

class Banner
{
    public function getBanner($id)
    {
        $data = [
            'name' => 'vendor111111111111',
            'email' => 'vendor@qq.com'
        ];
        $validate = new Validate([
            'name' => 'require|max:10',
            'email' => 'email'
        ]);
        $result = $validate->check($data);
        //可返回错误信息
        echo $validate->getError();
        //批量验证方法
        // $validate->batch()->check($data);
        // var_dump($validate->getError());
    }
}

validate 验证器

和独立验证区别在于封装性
定义一个验证器,使用内置的规则
applicationapivalidateTestValidate.php

use thinkValidate;
class TestValidate extends Validate
{
    protected $rule = [
        'name' => 'require|max:10',
        'email' => 'email'
    ];  
}

使用
applicationapicontrollerv1Banner.php

class Banner
{
    public function getBanner($id)
    {
        $data = [
            'name' => 'vendor111111111111',
            'email' => 'vendor@qq.com'
        ];
        $validate = new TestValidate;
        $validate->batch()->check($data);
        var_dump($validate->getError());
    }
}

通常会需要自定义验证规则

自定义验证规则

定义一个正整数的验证器
applicationapivalidateIDMustBePositiveInt.php

class IDMustBePositiveInt extends Validate
{
    protected $rule = [
      'id' => 'require|isPostiveInteger'
    ];
    protected function isPositiveInteger($value, $rule = '', $data = '', $fieID = '') // tp5会自动传入$value、$rule、$fieId等参数
    {
        if (is_number($value) && is_int($value + 0) && ($value + 0) > 0) {
            return true;
        } else {
            return $fieID.'必须是正整数';
        }
    }
}

使用
applicationapicontrollerv1Banner.php

class Banner
{
    public function getBanner($id)
    {
        $data = [
            'id' => $id
        ];
        $validate = new IDMustBePositiveInt();
        $validate->batch()->check($data);
        var_dump($validate->getError());
    }
}

验证器的这种方式和独立验证并没有太大的改变,下面将用一个方法进行简化

接口参数校验层

新建一个校验基类
applicationapivalidateBaseValidate.php

class BaseValidate extends Validate
{
    // 检测所有客户端发来的参数是否符合验证类规则
    public function goCheck()
    {
        $request = Request::instance();
        $params = $request->param();
        $result = $this->check($params);
        if (!$result) {
            $error = $this->error;
            //抛出一个异常让控制器逻辑中断执行
            throw new Exception($error); 
        } else {
            return true;
        }
    }
}

先更改继承Validate处为继承BaseValidate
applicationapivalidateIDMustBePositiveInt.php

<?php
namespace appapivalidate;

class IDMustBePositiveInt extends BaseValidate
{
    protected $rule = [
        'id' => 'require|isPositiveInteger',
    ];
    protected function isPositiveInteger($value, $rule='', $data='', $field='')// tp5会自动传入$value、$rule、$fieId等参数
    {
        if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
            return true;
        }
        return $field . '必须是正整数';
    }
}

使用
applicationapicontrollerv1Banner.php

class Banner
{
    public function getBanner($id)
    {
      (new IDMustBePositiveInt())->goCheck();
    }
}

最终代码

applicationapicontrollerv1Banner.php

<?php

namespace appapicontrollerv1;

use appapivalidateIDMustBePositiveInt;

class Banner
{
    public function getBanner($id)
    {
        (new IDMustBePositiveInt())->goCheck();
    }
}

applicationapivalidateIDMustBePositiveInt.php

<?php
namespace appapivalidate;

class IDMustBePositiveInt extends BaseValidate
{
    protected $rule = [
        'id' => 'require|isPositiveInteger',
    ];
    protected function isPositiveInteger($value, $rule='', $data='', $field='')
    {
        if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
            return true;
        }
        return $field . '必须是正整数';
    }

}

applicationapivalidateBaseValidate.php

<?php

namespace appapivalidate;


use thinkException;
use thinkRequest;
use thinkValidate;

class BaseValidate extends Validate
{
    // 检测所有客户端发来的参数是否符合验证类规则
    public function goCheck()
    {
        $request = Request::instance();
        $params = $request->param();
        $result = $this->check($params);
        if (!$result) {
            $error = $this->error;
            //抛出一个异常让控制器逻辑中断执行
            throw new Exception($error);
        } else {
            return true;
        }
    }
}