cqp 3 månader sedan
förälder
incheckning
86e8a71719
5 ändrade filer med 175 tillägg och 0 borttagningar
  1. 10 0
      app/Http/Controllers/Api/TestController.php
  2. 12 0
      app/Service/Service.php
  3. 122 0
      app/Service/TestService.php
  4. 29 0
      config/database.php
  5. 2 0
      routes/api.php

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

@@ -36,4 +36,14 @@ class TestController extends BaseController
             return $this->json_return(201,$data);
         }
     }
+
+    public function updateTopStock(Request $request){
+        list($bool, $data) = (new TestService())->updateTopStock($request->all());
+
+        if($bool){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
 }

+ 12 - 0
app/Service/Service.php

@@ -267,4 +267,16 @@ class Service
         $key = $prefix . $key;
         Redis::del($key);
     }
+
+    // 校验域名是否通畅可达
+    // $domain baidu.com 不要带http这些协议头
+    // gethostbyname() 函数可能会受到 PHP 配置中的 allow_url_fopen 和 disable_functions 选项的限制
+    function isDomainAvailable($domain) {
+        $ip = gethostbyname($domain);
+
+        // 如果解析失败或者返回的 IP 地址与输入的域名相同,则说明域名无效
+        if ($ip === $domain || filter_var($ip, FILTER_VALIDATE_IP) === false)  return false;
+
+        return true;
+    }
 }

+ 122 - 0
app/Service/TestService.php

@@ -2,6 +2,8 @@
 
 namespace App\Service;
 
+use Illuminate\Support\Facades\Config;
+use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
 
 class TestService extends Service
@@ -51,6 +53,126 @@ class TestService extends Service
         return [true, $result];
     }
 
+    public function updateTopStock($data){
+        list($status, $msg) = $this->rule();
+        if(! $status) return [false, 'IP未入白名单'];
+        if(empty($data['urlFromT9']) || $data['urlFromT9'] != "getStock") return [false,'API请求参数不能为空'];
+        if(empty($data['code'])) return [false,'API请求参数不能为空'];
+        if(empty($data['warehouse'])) return [false,'API请求参数不能为空'];
+
+        list($status, $msg) = $this->connectYy();
+        if(! $status) return [false, $msg];
+
+        //数据库
+        $db = $msg[0];
+        $u8 = $msg[1];
+        list($status, $msg) = $this->getStock($db, $u8,$data['code'],$data['warehouse']);
+        return [$status, $msg];
+    }
+
+    public function rule(){
+        // 获取用户的IP地址
+        $userIP = $_SERVER['REMOTE_ADDR'];
+        // 获取设置的IP地址
+        $ip = env("AliYUN");
+        $allowedIPs = [$ip];
+        $allowedIPs = array_filter($allowedIPs);
+
+        if(empty($allowedIPs)) return [false, $userIP];
+        // 校验用户IP是否在允许的范围内
+        $isValidIP = false;
+        foreach ($allowedIPs as $allowedIP) {
+            if (strpos($allowedIP, '/') !== false) {
+                // IP段表示法校验
+                list($subnet, $mask) = explode('/', $allowedIP);
+                if ((ip2long($userIP) & ~((1 << (32 - $mask)) - 1)) == ip2long($subnet)) {
+                    $isValidIP = true;
+                    break;
+                }
+            } else {
+                // 单个IP地址校验
+                if ($allowedIP === $userIP) {
+                    $isValidIP = true;
+                    break;
+                }
+            }
+        }
+
+        return [$isValidIP, $userIP];
+    }
+
+    public function connectYy(){
+        $model_box = DB::connection("mysqlT9");
+        $u8 = $model_box->table('setting')
+            ->where('setting_name','u8')
+            ->where('setting_value','<>','')
+            ->first();
+        if(empty($u8)) return [false, 'u8配置参数不存在!'];
+
+        $u8 = $u8->setting_value;
+        // 使用 eval() 函数执行字符串并转换为数组
+        $u8 = eval("return {$u8};");
+        if(empty($u8['domain'])) return [false, '外部域名不能为空!'];
+        if(empty($u8['u8_api_port'])) return [false, 'u8程序API端口不能为空!'];
+        if(empty($u8['u8_database_port'])) return [false, 'u8程序数据库端口不能为空!'];
+        if(empty($u8['database'])) return [false, 'u8程序数据库不能为空!'];
+        if(empty($u8['database_account'])) return [false, 'u8程序数据库登录账号不能为空!'];
+        if(empty($u8['database_password'])) return [false, 'u8程序数据库登录密码不能为空!'];
+        if(empty($u8['sAccID'])) return [false, 'u8程序sAccID不能为空!'];
+        if(empty($u8['sServer'])) return [false, 'u8程序sServer不能为空!'];
+        if(empty($u8['sUserID'])) return [false, 'u8程序sUserID不能为空!'];
+        if(empty($u8['sPassword'])) return [false, 'u8程序sPassword不能为空!'];
+
+        $config = [
+            'driver' => 'sqlsrv',
+            'host' =>  $u8['domain'],
+            'port' => $u8['u8_database_port'],
+            'database' => $u8['database'],
+            'username' => $u8['database_account'],
+            'password' => $u8['database_password'],
+        ];
+
+        // 数据库配置设置
+        Config::set('database.connections.sqlsrvs', $config);
+
+        // 连接
+        try {
+            $pdo = DB::connection('sqlsrvs')->getPdo();
+            if ($pdo instanceof \PDO) {
+                // 连接成功的逻辑代码
+                $db = DB::connection('sqlsrvs');
+                return [true, [$db, $u8]];
+            } else {
+                return [false, '连接失败!'];
+            }
+        } catch (\Throwable $e) {
+            return [false, $e->getMessage()];
+        }
+    }
+
+    public function getStock($db, $u8, $code = [], $warehouse = ""){
+        if(empty($code) || empty($warehouse)) return [false, '存货以及仓库不能为空'];
+
+        //映射ip是否通畅
+        $bool = $this->isDomainAvailable($u8['domain']);
+        if(! $bool) return [false, 'U8程序外部域名不可达'];
+
+        $result = $db->table('CurrentStock')
+            ->where("cWhCode", $warehouse)
+            ->whereIn("cInvCode", $code)
+            ->select('iQuantity as number', 'cInvCode as product_no')
+            ->get()->toArray();
+        $return = [];
+        foreach ($result as $value){
+            $return[] = [
+                'number' => floatval($value->number),
+                'product_no' => $value->product_no
+            ];
+        }
+
+        return [true, $return];
+    }
+
     public function post_helper($url, $data, $header = [], $timeout = 20){
         Log::channel('apiLog')->info('朗峰POST', ["api" => $url , "param" => $data ,"header" => $header]);
 

+ 29 - 0
config/database.php

@@ -91,6 +91,35 @@ return [
             'prefix_indexes' => true,
         ],
 
+        'mysqlT9' => [
+            'driver' => 'mysql',
+            'url' => env('DATABASE_URL'),
+            'host' => '121.37.161.210',
+            'port' => env('DB_PORT', '3306'),
+            'database' => 'tj',
+            'username' => 'tj',
+            'password' => 'tj1@Q3wa$ESZ',
+            'unix_socket' => env('DB_SOCKET', ''),
+            'charset' => 'utf8mb4',
+            'collation' => 'utf8mb4_unicode_ci',
+            'prefix' => '',
+            'prefix_indexes' => true,
+            'strict' => false,
+            'engine' => null,
+            'options' => extension_loaded('pdo_mysql') ? array_filter([
+                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
+            ]) : [],
+        ],
+
+        //sqlserver t9 用友数据库
+        'sqlsrvs' => [
+            'driver' => 'sqlsrv',
+            'host' =>  '',
+            'port' => '',
+            'database' => '',
+            'username' => '',
+            'password' => '',
+        ],
     ],
 
     /*

+ 2 - 0
routes/api.php

@@ -35,6 +35,8 @@ Route::any('getFileData','Api\ImportController@getFileData');
 Route::any('deviceStatisticsReportChart', 'Api\ReportFormsController@deviceStatisticsReportChart');
 Route::any('deviceStatisticsReportOEEChart', 'Api\ReportFormsController@deviceStatisticsReportOEEChart');
 
+//更新t9总社库存
+Route::any('updateTopStock', 'Api\TestController@updateTopStock');
 Route::group(['middleware'=> ['CheckJRFIDLogin']],function ($route){
     //站点获取登录后
     $route->any('getSiteByLogin', 'Api\JRFIDController@getSite2');