<?php
/**
 *
 * @author haiming
 * Date: 2021/5/21
 * Time: 10:32 PM
 */

namespace  Api\PhpServices\Sensitive;

use Api\PhpUtils\Common\TimeOut;
use Api\PhpUtils\Http\Request;
use App\Exception\custom\CodeSpecialException;

class Sensitive
{

    /**
     * 敏感词
     * 
     *
     */
    public function detect($param)
    {
        
        $url = config('interface','service.sensitive.detect');
        if(!$url){
            throw new CodeSpecialException("failed");
        }

        if (empty($param)) {
            throw new CodeSpecialException("failed");
        }
    
        $params = [
            'businessId' => !empty($param['businessId']) ? $param['businessId'] : 1,
            'text' => !empty($param['text']) ? $param['text'] : '',
            'scene' => !empty($param['scene']) ? $param['scene'] : 3,
        ];

        $headers = [
            'Content-Type' => 'application/json;charset=UTF-8',
        ];
  
        $detect_info  = (new TimeOut())->runPost($url, $params,'json',$headers);
        if(!$detect_info){
            throw new CodeSpecialException("timeout");
        }
        return $detect_info;
    }
    
    /**
     * 敏感词批量
     * 
     *
     */
    public function detectBatch($params)
    {
        $url = config('interface','service.sensitive.detect_batch');
        if(!$url){
            throw new CodeSpecialException("failed");
        }

        if (empty($params)) {
            throw new CodeSpecialException("failed");
        }
    
        $headers = [
            'Content-Type' => 'application/json;charset=UTF-8',
        ];
  
        $detect_info  = (new TimeOut())->runPost($url, $params,'json',$headers);
        if(!$detect_info){
            throw new CodeSpecialException("timeout");
        }
        return $detect_info;
    }
 
}
