Hwocr.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\Api\controller;
  4. use think\facade\Cache;
  5. use think\App;
  6. class Hwocr
  7. {
  8. private static $name="hw98795781";
  9. private static $id="0b5f3e336280f3c91f09c01ad29e253e";
  10. private static $password="wyhtQazwsx12";
  11. private static $project=["name"=>"cn-north-4","id"=>"0b5f50097c80f2722fd7c01af96659ce"];
  12. // private static $name="wugg_hw";
  13. // private static $id="0a48c7e67a0026bc1fe3c00d0cc963e6";
  14. // private static $password="wugg123!@#";
  15. // private static $project=["name"=>"cn-north-4","id"=>"0a48cd53cd00f2bc2f4fc00d55aa16b0"];
  16. private static $Api="https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens?nocatalog=1";
  17. private $params=[];
  18. private $token=[];
  19. private $app=[];
  20. private $request=[];
  21. public function __construct(App $app)
  22. {
  23. $this->app = $app;
  24. $this->request = $this->app->request;
  25. }
  26. public function GetToken(){
  27. $this->params=[
  28. "auth"=>[
  29. "identity"=>[
  30. "methods"=>["password"],
  31. "password"=>[
  32. "user"=>[
  33. "domain"=>["name"=>self::$name],
  34. "name"=>self::$name,
  35. "password"=>self::$password
  36. ]
  37. ]
  38. ],
  39. "scope"=>[
  40. "project"=>self::$project
  41. ]
  42. ]
  43. ];
  44. $this->token=Cache::get("x-auth-token");
  45. if($this->token==""){
  46. $result = post(self::$Api,json_encode($this->params));
  47. $data =explode("\r\n",$result);
  48. array_walk($data,function (&$val,$key){
  49. $temp = explode(":",$val);
  50. if(count($temp)>1){
  51. $val=[$temp[0]=>$temp[1]];
  52. }
  53. });
  54. $token=array_column($data,"X-Subject-Token");
  55. $this->token= $token[0];
  56. Cache::set("x-auth-token",$token[0],23*3600);
  57. }
  58. $this->GetImg();
  59. }
  60. public function GetImg(){
  61. $post=$this->request->post();
  62. $img = isset($post['image']) ? $post['image'] : "";
  63. $url = isset($post['url']) ? $post['url'] : "";
  64. if($img==""&&$url==""){
  65. return error_show(1002,"参数有误!");
  66. }
  67. $header=["x-auth-token:" .$this->token,"Content-Type:application/json;charset=UTF-8"];
  68. $urlc ="https://ocr.cn-north-4.myhuaweicloud.com/v2/".self::$project['id']."/ocr/vat-invoice";
  69. $param=["url"=>$url,"image"=>$img];
  70. $key = post2($urlc,json_encode($param),$header);
  71. if($key==false){
  72. $return = ["code"=>1003,"msg"=>"图片识别失败"];
  73. }else{
  74. $return = ["code"=>0,"msg"=>"图片识别成功","data"=>json_decode($key,true)];
  75. }
  76. echo json_encode($return);
  77. }
  78. }