<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\common\model\bbfxshop;

use AlibabaCloud\SDK\Green\V20220302\Models\ImageModerationResponse;
use Darabonba\OpenApi\Models\Config;
// use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use AlibabaCloud\Dara\Models\RuntimeOptions;
use AlibabaCloud\SDK\Green\V20220302\Green;
use AlibabaCloud\SDK\Green\V20220302\Models\ImageModerationRequest;
use AlibabaCloud\Tea\Utils\Utils;


use think\Model;
use think\Db;


class ImageCheck extends Model
{
    public static function getSet(){
        global $_W;
        $base = $_W['base'];
        if(empty($base)){
            $base = \app\common\model\bbfxshop\Common::getConfig(true);
        }

        if(!in_array('image_check',$base['pay_plugin'])){
            return []; 
        }

        return $base['image_check'];
    }

    public static function createClient(){

        $config = self::getSet();

        // $config['endpoint'] = "green-cip.cn-beijing.aliyuncs.com";

        $config = new Config([
            "accessKeyId" => $config['accessKeyId'],
            "accessKeySecret" => $config['accessKeySecret'],
            // 设置HTTP代理。
            // "httpProxy" => "http://10.10.xx.xx:xxxx",
            // 设置HTTPS代理。
            // "httpsProxy" => "https://10.10.xx.xx:xxxx",
            "endpoint" => $config['endpoint'],
        ]);
        return new Green($config);
    }

    public static function check($id){
        $config = self::getSet();
        if($config['status'] != 1){
            return __json(-1,"未开启");
        }
        $attachment = Db::name("attachment")->where(['id'=>$id])->find();
        if(empty($attachment)){
            return __json(-2,"图片不存在");
        }

        $data = [
            'uniacid'=>UNIACID,
            'user_id'=>$attachment['user_id'],
            'pid'=>$attachment['id'],
            'url'=>$attachment['url'],
            'createtime'=>time()
        ];

        $id = Db::name("bbfx_image_check_log")->insertGetId($data);

        $url = cdnurl($attachment['url']);

        $result = self::checkResult($url);
        if($result['code'] != 1){

            Db::name("attachment")->where(['id'=>$id])->update(['check_status'=>0]);
           
            return $result;
        }
        $check_result = $result['data']['result'];
        if($result['code'] == 1 && $result['data']['riskLevel'] == 'none'){
            Db::name("bbfx_image_check_log")->where(['id'=>$id])->update(['status'=>1,'checktime'=>time(),'result'=>iserializer($check_result)]);
            return __json(1,"图片无违规内容",$result['data']);
        }

        Db::name("attachment")->where(['id'=>$id])->update(['check_status'=>0]);
        Db::name("bbfx_image_check_log")->where(['id'=>$id])->update(['status'=>2,'checktime'=>time(),'result'=>iserializer($check_result)]);

        return __json(0,$check_result[0]['description'],$result['data']);
    }
 
    public static function checkResult($url){
        $config = self::getSet();
        if($config['status'] != 1){
            return __json(0,"未开启");
        }
 
        $client = self::createClient();
        // 创建RuntimeObject实例并设置运行参数。
        $runtime = new RuntimeOptions([]);
        
        // 检测参数构造。
        $request = new ImageModerationRequest();
        $serviceParameters = array(
            // 待检测图片，公网可访问的URL。
            'imageUrl' => $url,
            // 检测数据唯一标识。
            'dataId' => uniqid());
        // 图片检测service：内容安全控制台图片增强版规则配置的serviceCode，示例：baselineCheck
        // 支持service请参考：https://help.aliyun.com/document_detail/467826.html?0#p-23b-o19-gff  
        $request->service = "baselineCheck";
        $request->serviceParameters = json_encode($serviceParameters);
        

        $result = $client->imageModerationWithOptions($request, $runtime);

        $result = json_decode(json_encode($result),true);

        if($result['statusCode'] !== 200){
            return __json(0,"图片检测接口调用失败",$result);
        }

        if($result['body']['code'] !== 200){
            return __json(0,$result['body']['msg'],$result['body']);
        }

        return __json(1,"图片检测成功",$result['body']['data']);
    }
}
