<?php
namespace Home\Controller;
vendor('tencentsdk.TCloudAutoLoader');

use Think\Controller;
use SignedCookie;
use Zhida\OAuthHelperV3;
use Think\Upload\Driver\Qiniu\QiniuStorage;

use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Nlp\V20190408\NlpClient;
use TencentCloud\Nlp\V20190408\Models\SensitiveWordsRecognitionRequest;

use Think\Log;

class IndexController extends Controller
{
    private $auth_type = OAuthHelperV3::SCOPE_USERINFO;
    private $cookie_uid_key = 'skp-rotor-v1';
    private $debug = 1;
    private $today;
    use ControllerTrait;
    
    public function index()
    {
        $this->display();
    }

    public function detectSensitiveWords(){
      $text = $_GET['text'];
      $cred = new Credential("AKID9B4pPq5YBgz9Yo7qDRkspgOQK3c89PrD", "MXWK3DKNFzYgpvWwCKH4kKoma0O1Yu8A");
      $httpProfile = new HttpProfile();
      $httpProfile->setEndpoint("nlp.tencentcloudapi.com");
        
      $clientProfile = new ClientProfile();
      $clientProfile->setHttpProfile($httpProfile);
      $client = new NlpClient($cred, "ap-guangzhou", $clientProfile);
      $req = new SensitiveWordsRecognitionRequest();
      
      $params = '{"Text":"+'. $text .'+"}';
      $req->fromJsonString($params);
      $resp = $client->SensitiveWordsRecognition($req);
      $resp = $resp->toJsonString();
      echo $resp;
    }
    public function checkWord() {
      $name = $_POST['name'];
      if($this->utf8_strlen($name) > 4 || empty($name))
      {
        $this->ajaxReturn(array(
          'status' => 0,
          'msg' => '非法',
          'step' => 3
        ));
        return;
      }
      $detectResult = json_decode($this->detectSensitiveWords($name),1);
      if($detectResult['SensitiveWords'] != null)
      {
        $this->ajaxReturn(array(
            'status' => -1,
            'msg' => '您提交的姓名包含敏感词',
            'step' => 3
        ));
        return;
      }
      else 
      {
        $this->ajaxReturn(array(
            'status' => 1,
            'msg' => '通过',
            'step' => 3
        ));
        return;
      }
    }

    // private function detectSensitiveWords($name){
    //   $text = $name;
    //   $resp = file_get_contents('http://nex3-avatar.dev.izhida.cn/index/detectSensitiveWords?text='.$text);
    //   // $text = $name;
    //   // $text = '胡锦涛';
    //   // $cred = new Credential("AKID9B4pPq5YBgz9Yo7qDRkspgOQK3c89PrD", "MXWK3DKNFzYgpvWwCKH4kKoma0O1Yu8A");
    //   // $httpProfile = new HttpProfile();
    //   // $httpProfile->setEndpoint("nlp.tencentcloudapi.com");
        
    //   // $clientProfile = new ClientProfile();
    //   // $clientProfile->setHttpProfile($httpProfile);
    //   // $client = new NlpClient($cred, "ap-guangzhou", $clientProfile);
    //   // $req = new SensitiveWordsRecognitionRequest();
      
    //   // $params = '{"Text":"+'. $text .'+"}';
    //   // $req->fromJsonString($params);
    //   // $resp = $client->SensitiveWordsRecognition($req);
    //   // $resp = $resp->toJsonString();
    //   // return $resp;

    //   return json_decode($resp,1);
    // }

    // public function checkWord() {
    //   $name = $_POST['name'];
    //   if(empty($name))
    //   {
    //     $this->ajaxReturn(array(
    //       'status' => 0,
    //       'msg' => '非法',
    //       'step' => 3
    //     ));
    //     return;
    //   }
    //   $detectResult = $this->detectSensitiveWords($name);

    //   if($detectResult['SensitiveWords'] != null)
    //   {
    //     $this->ajaxReturn(array(
    //         'status' => -1,
    //         'msg' => '您提交的姓名包含敏感词',
    //         'step' => 3
    //     ));
    //     return;
    //   }
    //   else 
    //   {
    //     $this->ajaxReturn(array(
    //         'status' => 1,
    //         'msg' => '通过',
    //         'step' => 3
    //     ));
    //     return;
    //   }
    // }

    // 计算中文字符串长度
    function utf8_strlen($string = null) {
      // 将字符串分解为单元
      preg_match_all("/./us", $string, $match);
      // 返回单元个数
      return count($match[0]);
    }
    
    public function cookie()
    {
        SignedCookie::set_cookie($this->cookie_uid_key, 'oVsrlt0zUWMLvMx2-KPGVmY2I-Xc', C('cookie_sign'), 86400 * 60);
    }
}