123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- namespace app\common;
- use HuaweiCloud\SDK\Core\Auth\BasicCredentials;
- use HuaweiCloud\SDK\Core\Exceptions\{ConnectionException,RequestTimeoutException,ServiceResponseException};
- use HuaweiCloud\SDK\Core\Http\HttpConfig;
- use HuaweiCloud\SDK\Ocr\V1\Model\{BusinessLicenseRequestBody,InvoiceVerificationRequestBody,RecognizeBusinessLicenseRequest,RecognizeInvoiceVerificationRequest};
- use HuaweiCloud\SDK\Ocr\V1\OcrClient;
- class Ocr {
- private static $endpoint = 'https://ocr.cn-north-4.myhuaweicloud.com';
- private static $projectId = "f3818e89528d4b0db0c6353195653f04";
- private static $credentials;
- private static $instance;
- public function __construct() {
- $ak=env('huawei.ak');
- $sk=env('huawei.sk');
- self::$credentials = new BasicCredentials($ak,$sk,self::$projectId);
- }
- public static function getInstance() {
- if (null === self::$instance) {
- self::$instance = new self();
- }
- return self::$instance;
- }
- /**
- * @param string $url
- * @param string $image
- * @return array
- */
- public static function businessLicense($url='',$image='') {
- $client = self::OcrInfo();
- // 业务公司营业执照识别 为例
- if ($image!==""&&$url!=="") $url="";//图片数据流优先级最高。链接上传需要下载图片到识别服务器钟速度更慢
- $request = new RecognizeBusinessLicenseRequest();
- $body = new BusinessLicenseRequestBody();
- if($image!="")$body->setImage($image);
- if($url!="")$body->setUrl($url);
- $request->setBody($body);
- $msg = '';
- try {
- $response = $client->RecognizeBusinessLicense($request);
- } catch (ConnectionException $e) {
- $msg = $e->getMessage();
- } catch (RequestTimeoutException $e) {
- $msg = $e->getMessage();
- } catch (ServiceResponseException $e) {
- $msg = $e->getErrorMsg();
- }
- if ($msg) {
- return ["isResult"=>false,"msg"=>$msg];
- } else {
- return ['isResult'=>true,'msg'=>"获取成功","data"=>json_decode($response->getResult()->__toString(),true)];
- }
- }
- protected static function OcrInfo() {
- $config = HttpConfig::getDefaultConfig();
- $config->setIgnoreSslVerification(true);
- $client = OcrClient::newBuilder()
- ->withHttpConfig($config)
- ->withEndpoint(self::$endpoint)
- ->withCredentials(self::$credentials)
- ->build();
- return $client;
- }
- /**
- * Array of property to type mappings. Used for (de)serialization
- * code 发票代码。发票种类为全电发票时,该参数须为空字符串。
- * number 发票号码
- * issueDate 发票日期格式YYYY-MM-DD
- * checkCode 校验码后六位。 - 以下种类发票,参数不可为空 增值税普通发票、增值税电子普通发票、增值税普通发票(卷式)、增值税电子普通发票(通行费)、区块链电子发票。
- * - 区块链电子发票需要填写5位校验码。
- * subtotalAmount 合计金额。和票据上的金额的有效数字保持一致,例如票据上的金额为88.00,则需要输入字符串为“88.00”,才能验真成功。如果输入“88”或“88.0”可能会产生\"result_code\": \"1010\", \" Parameter error.\"报错。
- * 发票种类为全电发票时,该参数须为价税合计金额,其他票种使用不含税合计金额。 - 以下种类发票,参数不可为空 增值税专用发票、增值税电子专用发票、机动车销售统一发票、二手车销售统一发票、区块链电子发票、全电发票。 - 填写发票合计金额(不含税) 增值税专用发票、增值税电子专用发票、机动车销售统一发票、区块链电子发票。 - 二手车发票需要填写发票车价合计金额
- * @var string[]
- */
- public static function InvoiceVerification($code='',$number='',$issueDate='',$checkCode='',$subtotalAmount='') {
- $client = self::OcrInfo();
- $request=new RecognizeInvoiceVerificationRequest();
- $body = new InvoiceVerificationRequestBody();
- $issueDate = date('Y-m-d',strtotime($issueDate));
- if($checkCode!=""&&strlen($checkCode)!=6)$checkCode=substr($checkCode,0,6);
- if($subtotalAmount!="")$subtotalAmount= number_format($subtotalAmount, 2, '.', '');
- $body->setCode($code);
- $body->setNumber($number);
- $body->setIssueDate($issueDate);
- $body->setCheckCode($checkCode);
- $body->setSubtotalAmount($subtotalAmount);
- $request->setBody($body);
- $msg = '';
- try {
- $response = $client->RecognizeInvoiceVerification($request);
- }catch (ConnectionException $e) {
- $msg = $e->getMessage();
- } catch (RequestTimeoutException $e) {
- $msg = $e->getMessage();
- } catch (ServiceResponseException $e) {
- $msg = $e->getErrorMsg();
- }
- if ($msg) {
- return ["isResult"=>false,"msg"=>$msg];
- } else {
- return ['isResult'=>true,'msg'=>"获取成功","data"=>json_decode($response,true)];
- }
- }
- }
|