1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- declare (strict_types = 1);
- namespace app\Api\controller;
- use think\facade\Cache;
- use think\App;
- class Hwocr
- {
- private static $name="hw98795781";
- private static $id="0b5f3e336280f3c91f09c01ad29e253e";
- private static $password="wyhtQazwsx12";
- private static $project=["name"=>"cn-north-4","id"=>"0b5f50097c80f2722fd7c01af96659ce"];
- // private static $name="wugg_hw";
- // private static $id="0a48c7e67a0026bc1fe3c00d0cc963e6";
- // private static $password="wugg123!@#";
- // private static $project=["name"=>"cn-north-4","id"=>"0a48cd53cd00f2bc2f4fc00d55aa16b0"];
- private static $Api="https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens?nocatalog=1";
- private $params=[];
- private $token=[];
- private $app=[];
- private $request=[];
- public function __construct(App $app)
- {
- $this->app = $app;
- $this->request = $this->app->request;
- }
- public function GetToken(){
- $this->params=[
- "auth"=>[
- "identity"=>[
- "methods"=>["password"],
- "password"=>[
- "user"=>[
- "domain"=>["name"=>self::$name],
- "name"=>self::$name,
- "password"=>self::$password
- ]
- ]
- ],
- "scope"=>[
- "project"=>self::$project
- ]
- ]
- ];
- $this->token=Cache::get("x-auth-token");
- if($this->token==""){
- $result = post(self::$Api,json_encode($this->params));
- $data =explode("\r\n",$result);
- array_walk($data,function (&$val,$key){
- $temp = explode(":",$val);
- if(count($temp)>1){
- $val=[$temp[0]=>$temp[1]];
- }
- });
- $token=array_column($data,"X-Subject-Token");
- $this->token= $token[0];
- Cache::set("x-auth-token",$token[0],23*3600);
- }
- $this->GetImg();
- }
- public function GetImg(){
- $post=$this->request->post();
- $img = isset($post['image']) ? $post['image'] : "";
- $url = isset($post['url']) ? $post['url'] : "";
- if($img==""&&$url==""){
- return error_show(1002,"参数有误!");
- }
- $header=["x-auth-token:" .$this->token,"Content-Type:application/json;charset=UTF-8"];
- $urlc ="https://ocr.cn-north-4.myhuaweicloud.com/v2/".self::$project['id']."/ocr/vat-invoice";
- $param=["url"=>$url,"image"=>$img];
- $key = post2($urlc,json_encode($param),$header);
- if($key==false){
- $return = ["code"=>1003,"msg"=>"图片识别失败"];
- }else{
- $return = ["code"=>0,"msg"=>"图片识别成功","data"=>json_decode($key,true)];
- }
- echo json_encode($return);
- }
- }
|