Selaa lähdekoodia

wesmiler 报恩寺项目

wesmiler 3 vuotta sitten
vanhempi
commit
58f9a352e5

+ 32 - 0
app/Helpers/common.php

@@ -339,6 +339,38 @@ if (!function_exists('httpRequest')) {
     }
 }
 
+/**
+ * HTTP请求
+ * @param $url 链接
+ * @param $data 提交数据
+ * @param string $type 请求类型:get post
+ * @param string $dataType 返回数据类型 array json
+ * @param int $timeout
+ * @return mixed
+ */
+function httpHeaderRequest($url, $data=[], $type='post', $headers=[], $dataType='array', $timeout=60){
+    $data = $data && is_array($data)? http_build_query($data) : $data;
+    $url = strtolower($type) == 'get'? $url.(strpos($url, '?') === false ? '?' : ''). $data : $url;
+    $ch = curl_init($url);
+    if($headers){
+        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+    }
+    curl_setopt($ch, CURLOPT_POST, 1);
+    curl_setopt($ch, CURLOPT_HEADER,0 );
+    curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
+    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
+    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查,0-规避ssl的证书检查
+    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
+    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
+    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
+    $ret = curl_exec($ch);
+    curl_close($ch);
+    if(strtolower($dataType) == 'array' && !is_array($ret)){
+        $ret = json_decode($ret, true);
+    }
+    return $ret;
+}
 if (!function_exists('datetime')) {
 
     /**

+ 27 - 0
app/Http/Controllers/Api/TestController.php

@@ -6,6 +6,7 @@ use App\Helpers\Jwt;
 use App\Http\Controllers\Api\v1\NotifyController;
 use App\Models\MemberModel;
 use App\Models\UserModel;
+use App\Services\ConfigService;
 use App\Services\NotifyService;
 use App\Services\RedisService;
 use App\Services\SnapshotService;
@@ -74,6 +75,32 @@ $json = '{
 
     }
 
+    public function lamp(){
+        $colors = ['黄色','红色','绿色','白色'];
+        shuffle($colors);
+        $data = [
+            'CommandType'=> 'OpenLed', // 执行命令方式:OpenLed-开灯,CloseLed-关灯,QuitStatus-查询状态
+            'MD'=> 1, // 机器号设备号
+            'MDPORT'=> 1, // 设备端口:暂与设备号一致
+            'LEDID'=> 4, // 亮灯灯号1-498对应1号设备,498后为设备2
+            'Color'=> urlencode($colors[0]), // 颜色
+            'OrderNo'=> 'G202107152132315', // 订单号
+        ];
+        $dataStr = [];
+        foreach($data as $k => $v){
+            $dataStr[] = $k.'='.$v;
+        }
+
+        // 调用亮灯接口处理
+        $dataStr = implode('&', $dataStr);
+        var_dump($dataStr);
+        $headers = ["Content-Type: application/x-www-form-urlencoded; charset=UTF-8"];
+        $apiUrl = ConfigService::make()->getConfigByCode('gd_api_url').'/LEDControl.ashx';
+        $result = httpHeaderRequest($apiUrl, $dataStr,'post',$headers,'text');
+        var_dump($result);
+        return $result;
+    }
+
     /**
      * 支付回调
      */

+ 14 - 5
app/Services/DevicesService.php

@@ -170,7 +170,7 @@ class DevicesService extends BaseService
         // 亮灯处理
         unset($deviceNums[0]);
         RedisService::set("caches:devices:nums_{$deviceCode}", $deviceNums, rand(10,30));
-        $colors = ['黄','红','绿','白'];
+        $colors = ['黄','红','绿','白'];
         shuffle($colors);
         $data = [
             'CommandType'=> 'OpenLed', // 执行命令方式:OpenLed-开灯,CloseLed-关灯,QuitStatus-查询状态
@@ -180,17 +180,26 @@ class DevicesService extends BaseService
             'Color'=> $colors[0], // 颜色
             'OrderNo'=> $orderSn, // 订单号
         ];
+        $dataStr = [];
+        foreach($data as $k => $v){
+            $dataStr[] = $k.'='.$v;
+        }
 
         // 调用亮灯接口处理
+        $dataStr = implode('&', $dataStr);
+        $headers = ["Content-Type:application/x-www-form-urlencoded; charset=UTF-8"];
         $apiUrl = ConfigService::make()->getConfigByCode('gd_api_url').'/LEDControl.ashx';
         RedisService::set($errorKey.':request', ['url'=> $apiUrl,'data'=> $data], 3600);
-        $result = httpRequest($apiUrl, $data,'post','',3);
+        $result = httpHeaderRequest($apiUrl, $dataStr,'post',$headers,'text');
         RedisService::set($errorKey.':result', $result, 3600);
-        $code = isset($result['code'])? $result['code'] : '';
 
         // 更新灯状态
-        GongdengOrderModel::where('order_sn', $orderSn)->update(['lamp_status'=> 2,'device_num'=> $deviceLampNo,'params'=> json_encode($data, 256)]);
+        if($result == 'True'){
+            GongdengOrderModel::where('order_sn', $orderSn)->update(['lamp_status'=> 2,'device_num'=> $deviceLampNo,'params'=> json_encode($data, 256)]);
+            return true;
+        }else{
+            return false;
+        }
 
-        return true;
     }
 }

+ 3 - 2
routes/api.php

@@ -20,7 +20,9 @@ Route::middleware('auth:api')->get('/user', function (Request $request) {
 
 // 接口路由
 Route::any('/test', [\App\Http\Controllers\Api\TestController::class, 'index']);
-Route::get('/logout', [\App\Http\Controllers\Api\TestController::class, 'logout']);
+Route::any('/test/pay/{scene}', [\App\Http\Controllers\Api\TestController::class, 'pay']);
+Route::any('/test/lamp', [\App\Http\Controllers\Api\TestController::class, 'lamp']);
+
 
 // 授权登陆
 Route::post('/auth', [\App\Http\Controllers\Api\AuthController::class, 'index']);
@@ -32,7 +34,6 @@ Route::post('/index/jssdk', [\App\Http\Controllers\Api\v1\IndexController::class
 
 
 // 支付回调
-Route::any('/test/pay/{scene}', [\App\Http\Controllers\Api\TestController::class, 'pay']);
 Route::any('/notify/pay/{scene}', [\App\Http\Controllers\Api\v1\NotifyController::class, 'pay']);
 Route::any('/notify/refund/{scene}', [\App\Http\Controllers\Api\v1\NotifyController::class, 'check']);
 

+ 3 - 1
开发文档功能.md

@@ -137,4 +137,6 @@ data: {
     LEDID: LEDID,
     Color: Color
 }
-})
+})
+
+5.获取监控图