Просмотр исходного кода

新增:验证当前客户端是否允许访问

Yahoo 4 лет назад
Родитель
Сommit
9ed44d42b1
2 измененных файлов с 33 добавлено и 5 удалено
  1. 22 4
      app/api/controller/Controller.php
  2. 11 1
      app/api/model/h5/Setting.php

+ 22 - 4
app/api/controller/Controller.php

@@ -39,6 +39,8 @@ class Controller extends BaseController
         $this->storeId = $this->getStoreId();
         // 验证当前商城状态
         $this->checkStore();
+        // 验证当前客户端状态
+        $this->checkClient();
     }
 
     /**
@@ -46,7 +48,7 @@ class Controller extends BaseController
      * @return int|null
      * @throws BaseException
      */
-    protected function getStoreId()
+    protected function getStoreId(): ?int
     {
         $storeId = getStoreId();    // app/api/common.php
         if (empty($storeId)) {
@@ -57,10 +59,10 @@ class Controller extends BaseController
 
     /**
      * 验证当前商城状态
-     * @return bool
+     * @return void
      * @throws BaseException
      */
-    private function checkStore()
+    private function checkStore(): void
     {
         // 获取当前商城信息
         $store = StoreModel::detail($this->storeId);
@@ -70,7 +72,23 @@ class Controller extends BaseController
         if ($store['is_recycle'] || $store['is_delete']) {
             throwError('当前商城已删除');
         }
-        return true;
+    }
+
+    /**
+     * 验证当前客户端是否允许访问
+     * @throws BaseException
+     */
+    private function checkClient()
+    {
+        $client = getPlatform();
+        $settingClass = [
+            'H5' => [\app\api\model\h5\Setting::class, 'checkStatus', 'H5端']
+        ];
+        if (!isset($settingClass[$client])) {
+            return;
+        }
+        $status = call_user_func([$settingClass[$client][0], $settingClass[$client][1]]);
+        $status === false && throwError('很抱歉,当前' . $settingClass[$client][2] . '端暂不支持访问');
     }
 
     /**

+ 11 - 1
app/api/model/h5/Setting.php

@@ -21,5 +21,15 @@ use app\common\model\h5\Setting as SettingModel;
  */
 class Setting extends SettingModel
 {
-
+    /**
+     * 验证当前是否允许访问
+     * @return bool
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function checkStatus(): bool
+    {
+        return (bool)static::getItem('basic', static::$storeId)['enabled'];
+    }
 }