InquiryForm.php 3.2 KB
<?php

namespace App\Models\Inquiry;

use App\Models\Base;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;

/**
 * Class InquiryForm
 * @package App\Models\Inquiry
 * @author zbj
 * @date 2023/12/5
 */
class InquiryForm extends Base
{
    use SoftDeletes;

    //设置关联表名
    /**
     * @var mixed
     */
    protected $connection = "custom_mysql";
    protected $table = 'gl_inquiry_form';


    /**
     * 预设字段名称
     * @author zbj
     * @date 2023/12/5
     */
    public static function fieldMap($field = ''){
        $map = [
            'name' => '姓名',
            'email' => '邮箱',
            'phone' => '电话',
            'mobile' => '电话',
            'message' => '询盘内容',
            'company' => '公司名称',
            'file' => '文件'
        ];
        if($field){
            return $map[Str::lower($field)] ?? Str::studly($field);
        }
        return $map;
    }

    /**
     * @author zbj
     * @date 2023/12/5
     */
    public static function getField($form_id){
        $cache_key = 'inquiry_form_field_' . $form_id;
        $field = Cache::get($cache_key);
        if(!$field){
            $field = self::where('id', $form_id)->value('field');
            $field && Cache::set($cache_key, $field, 3600);
        }
        return $field;
    }

    /**
     * 根据提交数据的key获取表单id
     * @param $data
     * @return mixed
     * @author zbj
     * @date 2023/12/4
     */
    public static function getFromId($data){
        unset($data['globalso-domain_host_url']);
        unset($data['globalso-domain']);
        unset($data['globalso-edition']);
        unset($data['globalso-date']);

        foreach ($data as $k => $v){
            if(in_array($k, ['name', 'email', 'message']) && empty($v)){
                //特殊处理1986的表单
                if($k == 'message' && $data['project_id'] == 1986){
                    continue;
                }
                unset($data[$k]);
            }
        }

        ksort($data);
        $field = array_keys($data);
        $sign = md5(json_encode($field));
        $model = self::where('sign', $sign)->first();
        if(!$model){
            $model = new self();
            $model->sign = $sign;
            $model->field = $field;

            if(!empty($data['name']) && !empty($data['email']) && !empty($data['message'])){
                $has_file = false;
                foreach ($data as $v){
                    if (is_array($v)){
                        $has_file = true;
                        break;
                    }
                }
                !$has_file && $model->is_default = 1;
            }

            //移动端询盘表单
            if($sign == md5(json_encode(["__amp_source_origin", "message", "name", "phone"]))){
                $model->title = '移动端询盘';
            }

            $model->save();
        }
        return $model->id;
    }

    public function setFieldAttribute($value)
    {
        $this->attributes['field'] = json_encode($value);
    }

    public function getFieldAttribute($value)
    {
        return json_decode($value, true);
    }
}