<?php
/**
 * secCheckApi.php  UTF-8
 * 铭感检测接口
 *
 * @date    : 2018/12/3 13:50
 *
 * @license 这不是一个自由软件,未经授权不许任何使用和传播。
 * @author  : chenbingling <cbl@huosdk.com>
 * @version : HuoMp 1.0
 */

namespace huomp\controller\sec;

use huo\controller\common\Base;
use huolib\status\CommonStatus;
use huoMpMsg\controller\Common;

class secCheckApi extends Base {
    /**
     * 检查一段文本是否含有违法违规内容
     * https://developers.weixin.qq.com/minigame/dev/api/msgSecCheck.html
     *
     * @param string $wx_app_id     第三方用户唯一凭证
     * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
     * @param string $content       检测文本内容
     *
     * @return array
     */
    public function msgSecCheck($wx_app_id, $wx_app_secret, $content) {
        $_url = 'https://api.weixin.qq.com/wxa/msg_sec_check';
        $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
        $_url = $_url.'?access_token='.$_access_token;
        $_param = ['content' => $content];
        $_param_json = json_encode($_param, JSON_UNESCAPED_UNICODE);
        $_header = ['Content-Type: application/octet-stream; charset=utf-8'];
        $_ret = Common::curl($_url, $_param_json, 'POST', $_header);
        $_rdata = [];
        if (Common::isJson($_ret)) {
            $_rdata = json_decode($_ret, true);
        }
        if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
            $_code = $_rdata['errcode'];

            return $this->huoError($_code, $_rdata['errmsg']);
        }
        $_code = CommonStatus::NO_ERROR;

        return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
    }

    /**
     * 校验一张图片是否含有违法违规内容。
     * https://developers.weixin.qq.com/minigame/dev/api/imgSecCheck.html
     *
     * @param string $wx_app_id     第三方用户唯一凭证
     * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
     * @param string $file          要检测的图片文件
     *
     * @return array
     */
    public function imgSecCheck($wx_app_id, $wx_app_secret, $file) {
        $_url = 'https://api.weixin.qq.com/wxa/img_sec_check';
        $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
        $_url = $_url.'?access_token='.$_access_token;
        $cfile = curl_file_create($file);
        $_img_data = array('media' => $cfile);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $_url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $_img_data);
        /* https 请求 */
        if (strlen($_url) > 5 && strtolower(substr($_url, 0, 5)) == "https") {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $_ret = curl_exec($curl);
        if (false === $_ret) {
            $_ret = curl_errno($curl);
        }
        curl_close($curl);
        if (Common::isJson($_ret)) {
            $_rdata = json_decode($_ret, true);
        }
        if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
            $_code = $_rdata['errcode'];

            return $this->huoError($_code, $_rdata['errmsg']);
        }
        $_code = CommonStatus::NO_ERROR;

        return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
    }
}