Explorar el Código

模型层代码优化,去除掉 models 层,只需要 class (历史代码保留)

shizhongqi hace 9 meses
padre
commit
08697f41d6

+ 16 - 14
app-ghs/controllers/DeliveryController.php

@@ -26,26 +26,27 @@ class DeliveryController extends BaseController
     {
         $platformList = [];
         $platformLogos = [
-            'data' => '📦',
-            'shunfeng' => '🚚',
-            'meituan' => '🍱' ,
-            'fengniao' => '🍜',
-            'huolala' => '🚛',
-            'shansong' => '⚡',
-            'didi' => '🚗',
-			'uu' => '🛵'
+            'data' => ['name'=>'达达', 'logo'=>'📦'],
+            'shunfeng' => ['name'=>'顺丰同城', 'logo'=>'🚚'],
+            'meituan' => ['name'=>'美团', 'logo'=>'🍱'] ,
+            'fengniao' => ['name'=>'蜂鸟', 'logo'=>'🍜'],
+            'huolala' => ['name'=>'货拉拉', 'logo'=>'🚛'],
+            'shansong' => ['name'=>'闪送', 'logo'=>'⚡'],
+            'didi' => ['name'=>'滴滴', 'logo'=>'🚗'],
+			'uu' => ['name'=>'UU跑腿', 'logo'=>'🛵']
         ];
         $mainId = $this->mainId;
         $allDeliveries = DeliveryAuthTokenClass::getAllByCondition(['mainId'=>$mainId]);
         foreach($allDeliveries as $d){
             $item = [
                 'id'=>$d['platform'],
-                'logo'=>$platformLogos[$d['platform']],
+                'logo'=>$platformLogos[$d['platform']]['logo'],
+                'name'=>$platformLogos[$d['platform']]['name'],
                 'is_authorized' => true,
                 'balance' => 0,
                 'access_type' => 'oauth'
             ];
-            $platformList = $item;
+            $platformList[] = $item;
         }
         util::success(['platformList'=>$platformList]);
 //        [
@@ -306,10 +307,11 @@ class DeliveryController extends BaseController
     // 创建订单(真实下单)
     public function actionCreateOrder()
     {
-        $form = new CreateOrderForm();
-        $form->loadAndValidate();
-
-        $post = $form->getAttributes();
+//        $form = new CreateOrderForm();
+//        $form->loadAndValidate();
+//
+//        $post = $form->getAttributes();
+        $post = Yii::$app->request->post();
         $orderId = $post['orderId']; // xhGhsOrder.id
         $params = $post['allParams'];
         $platform = $params['en_name'];// $post['platform'] 是中文

+ 4 - 4
biz-ghs/express/classes/DeliveryAuthTokenClass.php

@@ -1,11 +1,11 @@
 <?php
-
-
 namespace bizGhs\express\classes;
 
 use bizGhs\base\classes\BaseClass;
 
 class DeliveryAuthTokenClass extends BaseClass
 {
-    public static $baseFile = '\bizGhs\express\models\DeliveryAuthToken';
-}
+    //public static $baseFile = '\bizGhs\express\models\DeliveryAuthToken'; // DeliveryAuthToken::class;
+    // 通过 baseTable 指定表名,无需单独定义 AR
+    public static $baseTable = 'xhDeliveryAuthToken';
+}

+ 2 - 1
biz-ghs/express/classes/GhsDeliveryOrderClass.php

@@ -5,5 +5,6 @@ use bizGhs\base\classes\BaseClass;
 
 class GhsDeliveryOrderClass extends BaseClass
 {
-    public static $baseFile = '\bizGhs\express\models\GhsDelivery';
+    //public static $baseFile = '\bizGhs\express\models\GhsDelivery';
+    public static $baseTable = 'xhGhsDeliveryOrder';
 }

+ 0 - 13
biz-ghs/express/models/DeliveryAuthToken.php

@@ -1,13 +0,0 @@
-<?php
-
-
-namespace bizGhs\express\models;
-use bizGhs\base\models\Base;
-
-class DeliveryAuthToken extends Base
-{
-    public static function tableName()
-    {
-        return 'xhDeliveryAuthToken';
-    }
-}

+ 48 - 2
common/base/classes/BaseClass.php

@@ -7,14 +7,60 @@ use \common\base\models\Base;
 
 class BaseClass
 {
-    public static $baseFile;
+    public static $baseFile; // 模型类名的路径字符串
+    public static $baseTable; // 表名(新增属性,表名和模型类名二选一,如果同时存在,则优先使用模型类名)
+    public static $baseDb; // 数据库组件ID
 
     /**
      * @return Base
      */
     protected static function getModel()
     {
-        return Yii::createObject(['class' => static::$baseFile]);
+        if (isset(static::$baseFile) && !empty(static::$baseFile)) {
+            if (is_string(static::$baseFile)) {
+                return Yii::createObject(['class' => static::$baseFile]);
+            } else {
+                return new static::$baseFile;
+            }
+        }
+
+        // 兼容:通过在 Class 上声明 static $baseTable 来动态指定表名
+        // 可选:再通过 static $baseDb 指定使用的 DB 组件 ID(如不设置则走默认)
+        if (property_exists(static::class, 'baseTable') && !empty(static::$baseTable)) {
+            $tableName = static::$baseTable;
+            $dbId = property_exists(static::class, 'baseDb') ? static::$baseDb : null;
+
+            $model = new class($tableName, $dbId) extends \common\base\models\Base {
+                private static $_tableName;
+                private static $_dbId;
+
+                public function __construct($tableName, $dbId = null)
+                {
+                    self::$_tableName = $tableName;
+                    self::$_dbId = $dbId;
+                    parent::__construct();
+                }
+
+                public static function tableName()
+                {
+                    return self::$_tableName;
+                }
+
+                public static function getDb()
+                {
+                    if (!empty(self::$_dbId)) {
+                        return \Yii::$app->get(self::$_dbId);
+                    }
+                    return parent::getDb();
+                }
+            };
+            return $model;
+        }
+
+        // 未设置 $baseFile / $baseTable,配置不完整
+        throw new \yii\base\InvalidConfigException(
+            'Missing model binding for ' . static::class . ': set static $baseFile or static $baseTable.'
+        );
     }
 
     //查询全部 ssh 2019.11.27