|
@@ -0,0 +1,104 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\common\util\SysSDK\Baidu;
|
|
|
+
|
|
|
+use think\facade\Log;
|
|
|
+
|
|
|
+class Identify {
|
|
|
+
|
|
|
+ private $vi_url = Config::get('baidu.request.vi_url');
|
|
|
+ private $viv_url = Config::get('baidu.request.viv_url');
|
|
|
+
|
|
|
+ private $url = Config::get('baidu.request.oauth_url');
|
|
|
+ private $client_id = Config::get('baidu.request.client_id');
|
|
|
+ private $client_secret = Config::get('baidu.request.client_secret');
|
|
|
+
|
|
|
+
|
|
|
+ public function refresh(){
|
|
|
+ $post_data['grant_type'] = 'client_credentials';
|
|
|
+ $post_data['client_id'] = $this->client_id;
|
|
|
+ $post_data['client_secret'] = $this->client_secret;
|
|
|
+ $o = "";
|
|
|
+ foreach ( $post_data as $k => $v )
|
|
|
+ {
|
|
|
+ $o.= "$k=" . urlencode( $v ). "&" ;
|
|
|
+ }
|
|
|
+ $post_data = substr($o,0,-1);
|
|
|
+
|
|
|
+ $data = $this->curl($this->url, $post_data);
|
|
|
+ Log::info(' get access_token result:'.json_encode($data));
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 发票识别
|
|
|
+ * access_token 百度 access_token 鉴权返回值
|
|
|
+ * files 发票图片文件
|
|
|
+ */
|
|
|
+ public function vat_invoice($access_token, $files){
|
|
|
+ $file_name_array = explode('.', $files);
|
|
|
+ if(count($file_name_array) < 2){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $suffix = $file_name_array[count($file_name_array)-1];
|
|
|
+
|
|
|
+ if(in_array(strtolower($suffix), ['jpg','jpeg','png','bmp'])){
|
|
|
+ $param_type = 'image';
|
|
|
+ }
|
|
|
+ else if(strtolower($suffix) == 'pdf'){
|
|
|
+ $param_type = 'pdf_file';
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $img = file_get_contents($files);
|
|
|
+ $img = base64_encode($img);
|
|
|
+ $param = array(
|
|
|
+ $param_type => $img
|
|
|
+ );
|
|
|
+
|
|
|
+ $data = $this->curl($this->vi_url.'?access_token='.$access_token, $param);
|
|
|
+ Log::info(' get result:'.json_encode($data));
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public function vat_invoice_verification($access_token, $invoice_code, $invoice_num, $invoice_date, $invoice_type, $check_code, $total_amount){
|
|
|
+ $bodys = array(
|
|
|
+ 'invoice_code' => $invoice_code,
|
|
|
+ 'invoice_num' => $invoice_num,
|
|
|
+ 'invoice_date' => $invoice_date,
|
|
|
+ 'check_code' => $check_code,
|
|
|
+ 'invoice_type' => $invoice_type,
|
|
|
+ 'total_amount' => $total_amount,
|
|
|
+ );
|
|
|
+
|
|
|
+ $data = $this->curl($this->viv_url.'?access_token='.$access_token, $bodys);
|
|
|
+ Log::info(' get result:'.json_encode($data));
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function curl($url = '', $param = '')
|
|
|
+ {
|
|
|
+ if (empty($url) || empty($param)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $postUrl = $url;
|
|
|
+ $curlPost = $param;
|
|
|
+ $curl = curl_init();
|
|
|
+ curl_setopt($curl, CURLOPT_URL, $postUrl);
|
|
|
+ curl_setopt($curl, CURLOPT_HEADER, 0);
|
|
|
+ curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
|
+ curl_setopt($curl, CURLOPT_POST, 1);
|
|
|
+ curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
|
|
|
+ $data = curl_exec($curl);
|
|
|
+
|
|
|
+ Log::info(' get curl result:' . json_encode($data));
|
|
|
+ curl_close($curl);
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|