FormField.php 5.67 KB
<?php

namespace addons\antis\model;

use think\Model;
use traits\model\SoftDelete;

/**
 * 表单字段
 */
class FormField extends Model {
    use SoftDelete;

    const LEADS_TYPE = 'leads';
    const CUSTOMER_TYPE = 'customer';

    const CONTACTS_TYPE = 'contacts';
    const CONTRACT_TYPE = 'contract';
    const RECEIVABLES_TYPE = 'examine';
    const BUSINESS_TYPE = 'business';
	// 表名,不含前缀
	protected $name = 'qingdong_form_field';
	// 自动写入时间戳字段
	protected $autoWriteTimestamp = 'int';
	// 定义时间戳字段名
	protected $createTime = 'createtime';
	protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

	public function getUpdatetimeAttr($value){
		return date('Y-m-d H:i',$value);
	}

    /**
     * 获取表单字段
     */
    public static function getFields($type){
        $fields=self::where(['types'=>$type,'field'=>['like',"main_%"],'list_show'=>1,'info_type'=>'main'])->select();
        $js=[];
        foreach ($fields as $v){
            $d=[
                'field' => $v['field'],
                'title' => $v['name'],
                'operate' => 'like',
                'autocomplete' => false,
            ];
            if($v['form_type'] == 'Cascader'){
                $d['extend']='data-toggle="city-picker"';
            }else if($v['form_type'] == 'uploadImage'){//图片
                continue;
            }else if($v['form_type'] == 'uploadFile'){//文件
                continue;
            }else if($v['form_type'] == 'DatePicker' ){//日期
                $d['operate']='RANGE';
                $d['addclass']='datetimerange';
                $d['formatter']='Table.api.formatter.datetime';
                $d['extend']='data-locale="{format:\"YYYY-MM-DD\"}"';
            }else if($v['form_type'] == 'TimePicker' ){//时间
                $d['operate']='RANGE';
                $d['addclass']='datetimerange';
                $d['formatter']='Table.api.formatter.datetime';
            }else if($v['form_type'] == 'textarea'){//文本
                $d['operate']=false;
            }else if(in_array($v['form_type'],['select','checkbox','radio'])){//文件
                $d['searchList']=json_encode(explode('|',$v['setting']),JSON_UNESCAPED_UNICODE);
            }else if(in_array($v['form_type'],['Rate'])){//文件
                $d['searchList']=json_encode([1=>1,2=>2,3=>3,4=>4,5=>5],JSON_UNESCAPED_UNICODE);
            }

            $js[] = $d;
        }
        return json_encode($js,JSON_UNESCAPED_UNICODE);
    }

    /**
     * 验证表单字段
     */
    public static function checkFields($types,$params,$id=null)
    {
        switch ($types){
            case 'leads'://线索
                $model=new Leads();
                break;
            case 'customer'://客户
                $model=new Customer();
                break;
            case 'contract'://合同
                $model=new Contract();
                break;
            case 'contacts'://联系人
                $model=new Contacts();
                break;
            case 'receivabels'://回款
                $model=new Receivables();
                break;

        }
        $fields=self::where(['types'=>$types,'info_type'=>'main'])->select();
        foreach ($fields as $v){
            //必填
            if($v['is_null'] == 1){
                if (!isset($params[$v['field']]) || empty($params[$v['field']])) {
                    return $v['name'] . '不能为空!';
                }
            }
            //唯一
            if($v['is_unique'] == 1){
                if (!isset($params[$v['field']]) || empty($params[$v['field']])) {
                    continue;
                }
                $where=[];
                if(!empty($id)){
                    $where['id']=['neq',$id];
                }
                if(is_array($params[$v['field']])){
                    $params[$v['field']]=implode(',',$params[$v['field']]);
                }
                $where[$v['field']]=$params[$v['field']];
                if($model->where($where)->count() > 0){
                    return $v['name'] ."【{$params[$v['field']]}】". '已存在!';
                }
            }

        }
        return true;
    }

    /**
     * 获取搜索条件
     */
    public static function updateWhereField($types,$params){
        $fields=self::where(['types'=>$types,'info_type'=>'main'])->select();
        $where=[];
        foreach ($fields as $v){
            if(!isset($params[$v['field']]) || $params[$v['field']] === ''){
                continue;
            }
            if($v['form_type'] == 'uploadImage'){//图片
                continue;
            }else if($v['form_type'] == 'uploadFile'){//文件
                continue;
            }else if($v['form_type'] == 'DatePicker' ){//日期
                $times=explode(',',$params[$v['field']]);
                $where[$v['field']]=['between',$times ];
            }else if($v['form_type'] == 'TimePicker' ){//时间
                $times=explode(',',$params[$v['field']]);
                $where[$v['field']]=['between',$times ];
            }else if($v['form_type'] == 'textarea'){//文本
                continue;
            }else if(in_array($v['form_type'],['select','checkbox','radio'])){//
                $where[$v['field']]=$params[$v['field']];
            }else if(in_array($v['form_type'],['Rate'])){//星级
                $where[$v['field']]=$params[$v['field']];
            }else{
               $where[$v['field']]=['like',"%{$params[$v['field']]}%" ];
            }
        }
        return $where;
    }
}