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;
- }
-
- 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;
- }
-
- 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)];
- }
- }
- }
|