123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- namespace App\Service;
- use Green\Request\V20170112 as Green;
- class DetectionService
- {
-
- private function getClient()
- {
- date_default_timezone_set("PRC");
- $iClientProfile = \DefaultProfile::getProfile("cn-shanghai", config('aliyun.access_key'), config('aliyun.access_secret'));
- \DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
- $client = new \DefaultAcsClient($iClientProfile);
- return $client;
- }
-
- private function processResponse($request)
- {
- $client = $this->getClient();
- try {
- $response = $client->getAcsResponse($request);
- if(200 == $response->code){
- $taskResults = $response->data;
- $detail = [];
- $flag = true;
- foreach ($taskResults as $taskResult) {
- if(200 == $taskResult->code){
- $this->processSceneResult($taskResult, $flag);
- }else{
- return $this->echoStr(-2000, 'task process fail:'.$response->code);
- }
- if (isset($taskResult->filteredContent)) {
- array_push($detail, $taskResult->filteredContent);
- }
- }
- if($flag == false){
- return $this->echoStr(-10000, 'the scene is not normal', $detail);
- }else{
- return $this->echoStr(10000, 'the scene is normal');
- }
- }else{
- return $this->echoStr(-2000, 'detect not success. code:'.$response->code);
- }
- } catch (Exception $e) {
- return $this->echoStr(-2000, $e);
- }
- }
-
- private function echoStr($code, $msg, $detail = []){
- return array(
- 'code' => $code,
- 'msg' => $msg,
- 'detail' => $detail
- );
- }
-
- private function processSceneResult($taskResult, &$flag){
- $sceneResults = $taskResult->results;
- foreach ($sceneResults as $sceneResult) {
-
- $suggestion = $sceneResult->suggestion;
- $rate = $sceneResult->rate;
- if($suggestion!='pass' && $rate>80){
- $flag = false;
- }
- }
- }
-
- public function checkText($text){
- if(empty($text)){
- return null;
- }
- $request = new Green\TextScanRequest();
- $request->setMethod("POST");
- $request->setAcceptFormat("JSON");
- if(is_array($text)){
- $taskArr = [];
- foreach($text as $k => $v){
- $task = 'task'.$k;
- $$task = array('dataId' => md5(uniqid($task)),
- 'content' => $v,
- 'category' => 'post',
- 'time' => round(microtime(true)*1000)
- );
- array_push($taskArr, $$task);
- }
- $request->setContent(json_encode(array("tasks" => $taskArr,
- "scenes" => array("antispam"))));
- }else if(is_string($text)){
- $task1 = array('dataId' => md5(uniqid()),
- 'content' => $text
- );
- $request->setContent(json_encode(array("tasks" => array($task1),
- "scenes" => array("antispam"))));
- }
- return $this->processResponse($request);
- }
-
- public function checkImg($img){
- if(empty($img)){
- return null;
- }
- $request = new Green\ImageSyncScanRequest();
- $request->setMethod("POST");
- $request->setAcceptFormat("JSON");
- if(is_array($img)){
- $taskArr = array();
- foreach($img as $k => $v){
- $task = 'task'.$k;
- $$task = array('dataId' => md5(uniqid($task)),
- 'url' => $v,
- 'time' => round(microtime(true)*1000)
- );
- array_push($taskArr, $$task);
- }
- $request->setContent(json_encode(array("tasks" => $taskArr,
- "scenes" => array("porn", "terrorism"))));
- }else if(is_string($img)){
- $task1 = array('dataId' => md5(uniqid()),
- 'url' => $img,
- 'time' => round(microtime(true)*1000)
- );
- $request->setContent(json_encode(array("tasks" => array($task1),
- "scenes" => array("porn", "terrorism"))));
- }
- return $this->processResponse($request);
- }
- }
|