作者 王晓刚
1 个管道 的构建 通过 耗费 0 秒

合并分支 'wxg' 到 'master'

数管接口



查看合并请求 !2
... ... @@ -6,6 +6,7 @@ namespace app\api\controller;
use app\api\model\Equipment;
use app\api\model\SubscribLog;
use app\common\controller\Api;
use think\Db;
/**
* 应用端接口
... ... @@ -261,9 +262,229 @@ class App extends Api
if(!$apiRequestInfo) {
$this->error('access_token不存在');
}
if($apiRequestInfo['over_time'] < time()) {
if($apiRequestInfo['overtime'] < time()) {
$this->error('access_token已过期');
}
return $apiRequestInfo['api_id'];
return $apiRequestInfo['id'];
}
/**
* 数管图片识别
*
* @ApiTitle (数管图片识别)
* @ApiSummary (数管图片识别)
* @ApiMethod (POST/GET)
* @ApiRoute (/api/app/getImageInfo)
* @ApiParams (name="app_id", type="integer", required=true, description="应用appid")
* @ApiParams (name="access_token", type="string", required=true, description="身份验证token")
* @ApiParams (name="customer_id", type="integer", required=true, description="用户id")
* @ApiParams (name="url", type="string", required=true, description="图片地址")
* @ApiParams (name="image_id", type="string", required=true, description="图片id")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="SUCCESS")
* @ApiReturnParams (name="data", type="object", sample="{'image_id':'int','timestamp':'string','result':{'id':'int','type':'string','total':'int','confirmation':'int','distrust':'int','image_path':'int'}}", description="扩展数据返回")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
'data':'{
'image_id':'图片id',
'timestamp':'时间戳',
'result':{
'id':'ID',
'type':'识别出的管的类型及大小',
'total':'识别总数量',
'confirmation':'确认数量',
'distrust':'疑似数量',
'image_path':'图片路径'
},
}'
})
*/
public function getImageInfo(){
$param = $this->request->param();
$validate = new \think\Validate([
'app_id' => 'require',
'customer_id' => 'require',
'access_token' => 'require',
'url' => 'require',
'image_id' => 'require',
]);
if (!$validate->check($param)) {
$this->error($validate->getError());
}
$app_id = $param['app_id'];
$customer_id = $param['customer_id'];
$access_token = $param['access_token'];
$url = $param['url'];
$image_id = $param['image_id'];
//验证token是否过期
$api_id = $this->check($app_id,$access_token);
//保存图片
$file = $this->saveImage($url);
//请求地址
$url = "http://49.234.158.29:80/get_img_info/";
$files = realpath(ltrim("./uploads/".$file, '@'));
$files = new \CURLFile($files);
$key = $this->getKey(20,false);
$param['files'] = $files;
$param['timestamp'] = date('Y-m-d H:i:s');
$param['key'] = $key;
$param['image_id'] = $image_id;
$param['sign'] = $this->getSign($param);
$result = $this->httpPost($url,$param);
$data = json_decode($result,true);
Db::startTrans();
//保存数管信息
$arr = [
'api_id' => $api_id,
'customer_id' => $customer_id,
'image_id' => $image_id,
'files' => $file,
'timestamp' => $param['timestamp'],
'key' => $key,
'sign' => $param['sign'],
'createtime' => time(),
];
$result1 = Db::name('shuguan')->insertGetId($arr);
if(!$result1){
Db::rollback();
$this->error('sql执行失败');
}
//保存数管接口返回数据
if(!empty($data['result'])){
$arr2 = [];
foreach($data['result'] as $key => $r){
$arr1 = [
'shuguan_id' => $result1,
'type' => $r['type'],
'total' => $r['total'],
'confirmation' => $r['confirmation'],
'distrust' => $r['distrust'],
'image_path' => $r['image_path'],
'createtime' => time(),
];
$arr2[] = $arr1;
}
$result2 = Db::name('shuguan_log')->insertAll($arr2);
if(!$result2){
Db::rollback();
$this->error('sql执行失败');
}
}
Db::commit();
$this->success('SUCCESS',$result);
}
/**
* 模拟调用
*/
public function test(){
$url = "http://gangjuren.net/api/app/getImageInfo";
$arr = [
'url' => "http://company.zzspjob.com/6b408e79f3a21ac02746b218f6cbc99@3x.png",
'app_id' => 'gjra2g11dg56ad7bb71',
'customer_id' => '1',
'access_token' => 'e508a6c25ed2ec1237fa8a0ba8a09357ff83b8c8',
'image_id' => '1',
];
$result = $this->httpPost($url,$arr);
$this->success('',$result);
}
/**
* 保存图片
* @param $url
* @return string
*/
public function saveImage($url){
$date = date('Ymd');
$savePath = "./uploads/shuguan/$date/";
if (!file_exists($savePath)){
mkdir($savePath, 0777,true);
}
$image = file_get_contents($url);
$file_name = md5(uniqid(rand())).".png";
file_put_contents($savePath.$file_name,$image);
return "shuguan/$date/$file_name";
}
/**
* 仿form-data提交表单
* @param $url
* @param $data
* @return mixed
*/
private function httpPost($url,$data){ // 模拟提交数据函数
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
//curl_setopt($curl, CURLOPT_TIMEOUT, 120); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, false); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
$result = curl_exec($curl); // 执行操作
if (curl_errno($curl)) {
echo 'Error POST'.curl_error($curl);
}
curl_close($curl); // 关键CURL会话
return $result; // 返回数据
}
/**
* 获取随机字符串
* @param $len 长度
* @param bool $special 是否需要特殊字符
* @return string 返回随机字符串
*/
public function getKey($len, $special=true){
$chars = array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
"3", "4", "5", "6", "7", "8", "9"
);
if($special){
$chars = array_merge($chars, array(
"!", "@", "#", "$", "?", "|", "{", "/", ":", ";",
"%", "^", "&", "*", "(", ")", "-", "_", "[", "]",
"}", "<", ">", "~", "+", "=", ",", "."
));
}
$charsLen = count($chars) - 1;
shuffle($chars);//打乱数组顺序
$str = '';
for($i=0; $i<$len; $i++){
$str .= $chars[mt_rand(0, $charsLen)];//随机取出一位
}
return $str;
}
/**
* 生成签名
* @param array $param 要加密的数组
* @return string 返回签名
*/
protected function getSign($param){
$key = $param['key'];
unset($param['key']);
unset($param['files']);
//按照参数名ASCII字典序排序
ksort($param);
$arr = [];
foreach($param as $k => $p){
$arr[] = $k."=".$p;
}
//拼接成字符串
$str = implode('&',$arr);
//与key拼接
$str = $str."&key=".$key;
//进行加密
$signature = MD5($str);
//转换成大写
$signature = strtoupper($signature);
return $signature;
}
}
\ No newline at end of file
... ...
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/8/31
* Time: 15:37
*/
namespace app\api\controller;
use app\api\model\Api;
class Recognition extends Api
{
public function get_image_info($url=null,$image_id="1"){
//请求地址
$url = "http://49.234.158.29:80/get_img_info/";
$files = "./uploads/20190821/6b408e79f3a21ac02746b218f6cbc99@3x.png";
$files = realpath(ltrim($files, '@'));
$files = new \CURLFile($files);
$key = $this->getKey(20,false);
$param['files'] = $files;
$param['timestamp'] = date('Y-m-d H:i:s');
$param['key'] = $key;
$param['image_id'] = $image_id;
$param['sign'] = $this->getSign($param);
$result = $this->httpPost($url,$param);
$data = json_decode($result,true);
dump($data);
}
private function httpPost($url,$data){ // 模拟提交数据函数
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
//curl_setopt($curl, CURLOPT_TIMEOUT, 120); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, false); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
$result = curl_exec($curl); // 执行操作
if (curl_errno($curl)) {
echo 'Error POST'.curl_error($curl);
}
curl_close($curl); // 关键CURL会话
return $result; // 返回数据
}
/**
* 获取司机字符串
* @param $len 长度
* @param bool $special 是否需要特殊字符
* @return string 返回随机字符串
*/
public function getKey($len, $special=true){
$chars = array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
"3", "4", "5", "6", "7", "8", "9"
);
if($special){
$chars = array_merge($chars, array(
"!", "@", "#", "$", "?", "|", "{", "/", ":", ";",
"%", "^", "&", "*", "(", ")", "-", "_", "[", "]",
"}", "<", ">", "~", "+", "=", ",", "."
));
}
$charsLen = count($chars) - 1;
shuffle($chars);//打乱数组顺序
$str = '';
for($i=0; $i<$len; $i++){
$str .= $chars[mt_rand(0, $charsLen)];//随机取出一位
}
return $str;
}
/**
* 生成签名
* @param array $param 要加密的数组
* @return string 返回签名
*/
protected function getSign($param){
$key = $param['key'];
unset($param['key']);
unset($param['files']);
//按照参数名ASCII字典序排序
ksort($param);
$arr = [];
foreach($param as $k => $p){
$arr[] = $k."=".$p;
}
//拼接成字符串
$str = implode('&',$arr);
//与key拼接
$str = $str."&key=".$key;
//进行加密
$signature = MD5($str);
//转换成大写
$signature = strtoupper($signature);
return $signature;
}
}
\ No newline at end of file
... ...