浏览代码

Merge branch 'develop'

zhangchangchun 5 年之前
父节点
当前提交
603fb48742
共有 4 个文件被更改,包括 78 次插入4 次删除
  1. 71 0
      app/Http/Middleware/VerifyShopMiddleware.php
  2. 2 2
      app/Transformers/ShopsTransformer.php
  3. 1 0
      bootstrap/app.php
  4. 4 2
      routes/api.php

+ 71 - 0
app/Http/Middleware/VerifyShopMiddleware.php

@@ -0,0 +1,71 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019-06-05
+ * Time: 18:30
+ */
+
+namespace App\Http\Middleware;
+
+use App\Shop;
+use Closure;
+use Tymon\JWTAuth\Exceptions\JWTException;
+use Tymon\JWTAuth\Exceptions\TokenExpiredException;
+use Tymon\JWTAuth\Exceptions\TokenInvalidException;
+use Tymon\JWTAuth\Facades\JWTAuth;
+
+class VerifyShopMiddleware {
+    /**
+     * Handle an incoming request.
+     *
+     * @param  \Illuminate\Http\Request $request
+     * @param  \Closure $next
+     * @return mixed
+     */
+    public function handle($request, Closure $next)
+    {
+
+        try {
+            $token =  JWTAuth::decode(JWTAuth::getToken());
+            if(!empty($token)){
+                if($token['type'] == 2){
+                    $shop = Shop::where(['shop_id'=>$token['shop']->shop_id])->first();
+                    if($shop->status == 0){
+                        $error = [
+                            'message' => '商户已禁用,禁止操作',
+                            'code' => 401,
+                        ];
+                        return response()->json($error);
+                    }
+                }
+            }
+            //$data = JWTAuth::decode($token);
+        } catch (TokenExpiredException $e) {
+            $error = [
+                'message' => 'Token is Expired',
+                'code' => 401,
+            ];
+            return response()->json($error);
+        } catch (TokenInvalidException $e) {
+            $error = [
+                'message' => $e->getMessage(),
+                'code' => 401,
+            ];
+            return response()->json($error);
+        } catch (JWTException $e) {
+            $error = [
+                'message' => $e->getMessage(),
+                'code' => 401,
+            ];
+            return response()->json($error);
+        }catch (\Exception $e){
+            $error = [
+                'message' => $e->getMessage(),
+                'code' => 401,
+            ];
+            return response()->json($error);
+        }
+        return $next($request);
+    }
+}

+ 2 - 2
app/Transformers/ShopsTransformer.php

@@ -34,12 +34,12 @@ class ShopsTransformer extends  TransformerAbstract{
             'license_img'    => $shop['license_img'],
             'food_trans_license'    => $shop['food_trans_license'],
             'other_license'    => $shop['other_license'],
-            'proportion'    => $shop['proportion'],
+            'proportion'    => floatval($shop['proportion']),
             'verify_type'    => $shop['verify_type'],
             'star'    => $shop['star'],
             'created_at' => Carbon::parse($shop['created_at'])->format('Y-m-d H:i:s'),
             'updated_at' => Carbon::parse($shop['updated_at'])->format('Y-m-d H:i:s'),
-            'shop_account' => $shop->account->toArray(),
+            'shop_account' => !empty($shop->account) ? $shop->account->toArray() : "",
         ];
     }
 }

+ 1 - 0
bootstrap/app.php

@@ -71,6 +71,7 @@ $app->singleton(
 $app->routeMiddleware([
     'auth' => App\Http\Middleware\Authenticate::class,
     'jwt.chxq_auth' => App\Http\Middleware\JwtAuthMiddleware::class,
+    'chxq.shop_check' => App\Http\Middleware\VerifyShopMiddleware::class,
 ]);
 
 /*

+ 4 - 2
routes/api.php

@@ -23,8 +23,10 @@ $api->version('v1', [
     //获取商户信息
     $api->post('getShopInfo', 'ShopController@getTokenShop');
 
-    $api->get('statistics', 'IndexController@index');
-    $api->get('statistics/sales', 'IndexController@sales');
+    $api->group(['middleware' => 'chxq.shop_check'], function ($api) {
+        $api->get('statistics', 'IndexController@index');
+        $api->get('statistics/sales', 'IndexController@sales');
+    });
 
     $api->group(['middleware' => 'auth:api'], function ($api) {
         //注册