<?php
declare (strict_types=1);

namespace app\api\service;

use app\api\service\User as UserService;
use app\common\library\baidu\OcrCard;
use app\common\library\baidu\OcrLicense;
use app\common\service\BaseService;
use cores\exception\BaseException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class Ocr extends BaseService
{
    /**
     * @notes:获取配置
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author: wanghousheng
     */

    /**
     * @notes:
     * @param string $img_url //证件地址 可外网访问的
     * @param int $type //1 正面 2反面
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException|BaseException
     * @author: wanghousheng
     */
    public static function checkCard(string $img_url, int $type = 1): array
    {
        // 当前用户ID
        UserService::getCurrentLoginUserId();
        $config = Setting::getOcr();
        $clientSecret = !empty($config['secret_key']) ? $config['secret_key'] : '';
        $clientId = !empty($config['access_key']) ? $config['access_key'] : '';
        $obj = new OcrCard($clientId, $clientSecret);
        return $obj->checkCard($img_url, $type);
    }

    /**
     * @notes:营业执照识别
     * @param string $img_url
     * @return array
     * @throws BaseException
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author: wanghousheng
     */
    public static function checkLicense(string $img_url): array
    {
        // 当前用户ID
        UserService::getCurrentLoginUserId();
        $config = Setting::getOcr();
        $clientSecret = !empty($config['secret_key']) ? $config['secret_key'] : '';
        $clientId = !empty($config['access_key']) ? $config['access_key'] : '';
        $obj = new OcrLicense($clientId, $clientSecret);
        return $obj->check($img_url);
    }
}