소스 검색

接口对接

cqpCow 9 달 전
부모
커밋
34eea25857

+ 41 - 0
app/Http/Controllers/Api/AssetController.php

@@ -0,0 +1,41 @@
+<?php
+namespace App\Http\Controllers\Api;
+
+use App\Service\AssetServerService;
+use Illuminate\Http\Request;
+
+class AssetController extends BaseController
+{
+    public function getSite(Request $request){
+        list($bool, $data) = (new AssetServerService())->getSite($request->all());
+
+        if($bool){
+            return $this->json_return(200,'',$data);
+        }else{
+            if($bool === 0) return $this->json_return(401,$data);
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function pdList(Request $request){
+        list($bool, $data) = (new AssetServerService())->pdList($request->all(),$request->common_param);
+
+        if($bool){
+            return $this->json_return(200,'',$data);
+        }else{
+            if($bool === 0) return $this->json_return(401,$data);
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function pdDetail(Request $request){
+        list($bool, $data) = (new AssetServerService())->pdDetail($request->all(),$request->common_param);
+
+        if($bool){
+            return $this->json_return(200,'',$data);
+        }else{
+            if($bool === 0) return $this->json_return(401,$data);
+            return $this->json_return(201,$data);
+        }
+    }
+}

+ 11 - 0
app/Http/Controllers/Api/LoginController.php

@@ -1,6 +1,7 @@
 <?php
 namespace App\Http\Controllers\Api;
 
+use App\Service\AssetServerService;
 use App\Service\JRFIDServerService;
 use Illuminate\Http\Request;
 
@@ -16,4 +17,14 @@ class LoginController extends BaseController
             return $this->json_return(201,$msg);
         }
     }
+
+    public function assetlogin(Request $request){
+        $data = $request->only("name","password");
+        list($bool, $msg) = (new AssetServerService())->loginRule($data);
+        if($bool){
+            return $this->json_return(200,'',$msg);
+        }else{
+            return $this->json_return(201,$msg);
+        }
+    }
 }

+ 1 - 0
app/Http/Kernel.php

@@ -65,6 +65,7 @@ class Kernel extends HttpKernel
         'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
         'checkLogin' => \App\Http\Middleware\CheckLogin::class,
         'CheckJRFIDLogin' => \App\Http\Middleware\CheckJRFIDLogin::class,
+        'CheckAssetLogin' => \App\Http\Middleware\CheckAssetLogin::class,
         'checkWeixin' => \App\Http\Middleware\CheckWeinxin::class,
     ];
 

+ 30 - 0
app/Http/Middleware/CheckAssetLogin.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace App\Http\Middleware;
+
+use App\Service\EmployeeService;
+use App\Service\JRFIDServerService;
+use Closure;
+
+class CheckAssetLogin
+{
+    const account = "admin";
+    const password = "admin,,bpm";
+
+    /**
+     * Handle an incoming request.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \Closure  $next
+     * @return mixed
+     */
+    public function handle($request, Closure $next)
+    {
+        $base = base64_encode(Self::account . ':' . Self::password);
+        $token = "Basic " . $base;
+        $data['header'] = ["Authorization: {$token}",'Content-Type:application/json'];
+
+        $request->common_param = $data;
+        return $next($request);
+    }
+}

+ 10 - 0
app/Providers/RouteServiceProvider.php

@@ -47,6 +47,8 @@ class RouteServiceProvider extends ServiceProvider
         $this->mapWebRoutes();
 
         $this->mapWeixinRoutes();
+
+        $this->mapAssetApiRoutes();
         //
     }
 
@@ -79,6 +81,14 @@ class RouteServiceProvider extends ServiceProvider
              ->group(base_path('routes/api.php'));
     }
 
+    protected function mapAssetApiRoutes()
+    {
+        Route::prefix('assetapi')
+            ->middleware('api')
+            ->namespace($this->namespace)
+            ->group(base_path('routes/asset.php'));
+    }
+
     protected function mapWeixinRoutes()
     {
         Route::prefix('wxapi')

+ 153 - 0
app/Service/AssetServerService.php

@@ -0,0 +1,153 @@
+<?php
+
+namespace App\Service;
+
+
+use Illuminate\Support\Facades\Log;
+
+class AssetServerService extends Service
+{
+    public function loginRule($data){
+        if(empty($data['name'])) return [false, '请输入账号!'];
+        if(empty($data['password'])) return [false, '请输入密码!'];
+
+        return [true, ''];
+
+        list($status, $msg) = $this->getToken($data);
+        if(! $status) return [false, $msg];
+
+        return [true, ['data' => $msg]];
+    }
+
+    public function getToken($data){
+        $account = $data['name'];
+        $password = $data['password'];
+
+        $url = config("asset.login");
+        $post = [
+            "name" => $account,
+            "password" => $password,
+            "rememberMe" => true
+        ];
+        $header = ['Content-Type:application/json'];
+        list($status, $result) = $this->post_helper($url,$post, $header);
+        if(! $status) return [$status, $result];
+
+        //登录失败
+        if(! empty($result['errorMessage'])) return [false, $result['errorMessage']];
+
+        return [true, $result];
+    }
+
+    public function pdList($data,$param){
+        $url = config("asset.pdList");
+
+        $post['size'] = $data['size'] ?? 10;
+        $post['number'] = ($data['number'] ?? 1) - 1;
+
+        list($status,$result) = $this->post_helper($url,$post,$param['header']);
+        if(! $status) return [$status, $result];
+
+        if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['msg']];
+        if(! empty($result['type']) && $result['type'] == 'errorVm') return [false, $result['message']];
+
+        if(! isset($result['content'])) {
+            $error = $result[0]['message'] ?? "操作失败,请刷新页面";
+            return [false, $error];
+        }
+
+        return [true, $result];
+    }
+
+    public function pdDetail($data,$param){
+        if(empty($data['id'])) return [false, '请选择盘点单!'];
+        $url = config("asset.pdDetail");
+
+        $url .= $data['id'];
+        list($status,$result) = $this->get_helper($url,$param['header']);
+        if(! $status) return [$status, $result];
+
+        if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['msg']];
+        if(! empty($result['type']) && $result['type'] == 'errorVm') return [false, $result['message']];
+
+        if(! isset($result['content'])) {
+            $error = $result[0]['message'] ?? "操作失败,请刷新页面";
+            return [false, $error];
+        }
+
+        return [true, $result];
+    }
+
+    public function post_helper($url, $data, $header = [], $timeout = 20){
+        Log::channel('apiLog')->info('资产盘点POST', ["api" => $url , "param" => $data ,"header" => $header]);
+
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch,  CURLOPT_RETURNTRANSFER, true);
+        curl_setopt($ch, CURLOPT_ENCODING, '');
+        curl_setopt($ch, CURLOPT_POST, 1);
+        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
+        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
+
+        if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
+        $r = curl_exec($ch);
+
+        if ($r === false) {
+            // 获取错误号
+            $errorNumber = curl_errno($ch);
+            // 获取错误信息
+            $errorMessage = curl_error($ch);
+            $message = "cURL Error #{$errorNumber}: {$errorMessage}";
+
+            Log::channel('apiLog')->info('资产盘点POST结果', ["message" => $message]);
+            return [false, $message];
+        }
+        curl_close($ch);
+
+        $return = json_decode($r, true);
+        Log::channel('apiLog')->info('资产盘点POST结果', ["message" => $return]);
+
+        if(! empty($return['message']) && $return['message'] == 'error.unAuthorized') return [0, '登录凭证已失效或者不正确'];
+
+        return [true, $return];
+    }
+
+    public function get_helper($url,$header=[],$timeout = 20){
+        Log::channel('apiLog')->info('资产盘点GET', ["api" => $url ,"header" => $header]);
+        $ch = curl_init();
+        curl_setopt_array($ch, array(
+            CURLOPT_URL => $url,
+            CURLOPT_RETURNTRANSFER => true,
+            CURLOPT_ENCODING => '',
+            CURLOPT_MAXREDIRS => 10,
+            CURLOPT_TIMEOUT => $timeout,
+            CURLOPT_FOLLOWLOCATION => true,
+            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
+            CURLOPT_CUSTOMREQUEST => 'GET',
+            CURLOPT_SSL_VERIFYPEER => false,
+            CURLOPT_HTTPHEADER => $header,
+        ));
+        $r = curl_exec($ch);
+
+        if ($r === false) {
+            // 获取错误号
+            $errorNumber = curl_errno($ch);
+            // 获取错误信息
+            $errorMessage = curl_error($ch);
+            $message = "cURL Error #{$errorNumber}: {$errorMessage}";
+            Log::channel('apiLog')->info('资产盘点GET', ["message" => $message]);
+            return [false, $message];
+        }
+
+        curl_close($ch);
+
+        $return = json_decode($r, true);
+        Log::channel('apiLog')->info('资产盘点GET', ["message" => $return]);
+
+        if(! empty($return['message']) && $return['message'] == 'error.unAuthorized') return [0, '登录凭证已失效或者不正常'];
+
+        return [true, $return];
+    }
+}

+ 14 - 0
config/asset.php

@@ -0,0 +1,14 @@
+<?php
+//状态码
+return [
+    //登录标识
+    'login_redis_topic' => 'ASSETLOGIN',
+    //登录过期时间
+    'login_expire_time' => 3600, // 3600
+    //登录
+    'login' => 'http://bpmjiuyunfang.net/ibl/api/login/?allowanonymous=true',
+    //盘点单列表
+    'pdList' => 'http://bpm.jiuyunfang.net/jbl/api/module-data/tourism_inventory_sheet_list/page',
+    //获取单个盘点单管理详情
+    'pdDetail' => 'http://bpm.jiuyunfang.net/jbl/api/module-data/tourism_inventory_sheet/tourism_inventory_sheet/611847814397710337/',
+];

+ 1 - 1
config/cors.php

@@ -21,7 +21,7 @@ return [
      * You can enable CORS for 1 or multiple paths.
      * Example: ['api/*']
      */
-    'paths' => ['api/*'],
+    'paths' => ['api/*','assetapi/*'],
 
     /*
     * Matches the request method. `[*]` allows all methods.

+ 25 - 0
routes/asset.php

@@ -0,0 +1,25 @@
+<?php
+
+use Illuminate\Http\Request;
+
+/*
+|--------------------------------------------------------------------------
+| API Routes
+|--------------------------------------------------------------------------
+|
+| Here is where you can register API routes for your application. These
+| routes are loaded by the RouteServiceProvider within a group which
+| is assigned the "api" middleware group. Enjoy building your API!
+|
+*/
+
+Route::middleware('auth:api')->get('/user', function (Request $request) {
+    return $request->user();
+});
+
+Route::any('login', 'Api\LoginController@assetlogin');
+
+Route::group(['middleware'=> ['CheckAssetLogin']],function ($route){
+    $route->any('inventoryList', 'Api\AssetController@pdList');
+    $route->any('inventoryDetail', 'Api\AssetController@pdDetail');
+});