如何构建一个输入控制
幻冰小站 浏览次数:601 2020-08-21 11:29:46
namespace    Input\Model;
class    Apijtv2Model extends Base{
    static protected $errMessage = '';
    
    //输出异常信息
    static public function getError(){
    return self::$errMessage;
    }
    //输入异常类
    static public function setError($message){
    return self::$errMessage = $message;
    }
    
    static public $_roultId;
    static public $roult = '';
    //某个验证中使用的场景
    static public $_scale = array();
    //参数信息
    public $_params = array(
    'user'=>array('require','用户名为必填'),
    'pwd'=>array('require','用户密码为必填'),
    'mac'=>array('require','设备mac为必填'),
    'sessid'=>array('require','sessid为必填'),
    'store'=>array('require','store为必填'),
    'kind'=>array('require','store为必填'),
    'level'=>array('require','level为必填'),
    'number'=>array('require','number为必填'),
    'price'=>array('require','price为必填'),
    'originCode'=>array('require','originCode为必填'),
    'origin'=>array('require','origin为必填'),
    'date'=>array('require','date为必填'),
    'entryOrder'=>array('require','entryOrder为必填'),
    'addressCode'=>array('require','addressCode为必填'),
    'address'=>array('require','address为必填'),
    );
    
    //场景信息
    public $_scales = array(
    'loginexpand'=>array('user','pwd','mac'),
    'warehouselist'=>array('sessid'),
    'variety'=>array('sessid'),
    'storestockin'=>array('sessid','store','kind','level','number','price','originCode','origin','date','mac'),
    'storestockout'=>array('sessid','entryOrder','number','price','addressCode','address','date','mac'),
    'base24'=>array('sessid'),
    'admdivlist'=>array('sessid'),
    );
    
    //选择使用场景
    public function scale($scaleID){
    $this->_roultId = strtolower($scaleID);
    $this->_scale = isset($this->_scales[$this->_roultId])?$this->_scales[$this->_roultId]:array();
    return $this;
    }
    
    //使用判定
    public function check($data){
    //使用场景
    $scale = $this->_scale;
    if($scale){
    return $this->checkScaledata($data,$scale,$this->_params);
    }else{
    return true;
    }
    }
    
    //判定执行函数
    public function checkScaledata($input,$scale,$params){
    foreach($scale as $v){
    $param = isset($params[$v])?$params[$v]:NULL;
    if($param){
    if($param[0]=='require'){
    //这里是必填选项
    if($input[$v]){
    //核验通过
    }else{
    self::setError($param[1]);
    return false;
    }
    }else{
    //这里是其它字符串过滤信息
    }
    }else{
    //todo--没有该字段怎么办,这个地方自己写吧
    }
    }
    return true;
    }
}