ouyang 2 дней назад
Родитель
Сommit
8e4d9950f7

+ 2 - 2
app-ghs/controllers/MainInviteController.php

@@ -28,7 +28,7 @@ class MainInviteController extends BaseController
     public function actionIndex()
     {
         $mainId = (int) $this->mainId;
-        $invite = MainInviteClass::getByMainId($mainId);
+        $invite = MainInviteClass::ensureByMainId($mainId);
         $inviteData = [
             'inviteCode' => '',
             'inviteCodeMask' => '',
@@ -111,7 +111,7 @@ class MainInviteController extends BaseController
     public function actionGetInviteHdPoster()
     {
         $mainId = (int) $this->mainId;
-        $invite = MainInviteClass::getByMainId($mainId);
+        $invite = MainInviteClass::ensureByMainId($mainId);
         $inviteCode = trim((string) ($invite['inviteCode'] ?? ''));
         if ($inviteCode === '') {
             util::fail('暂无邀请码');

+ 1 - 1
app-hd/controllers/MainInviteController.php

@@ -21,7 +21,7 @@ class MainInviteController extends BaseController
     public function actionIndex()
     {
         $mainId = (int) $this->mainId;
-        $invite = MainInviteClass::getByMainId($mainId);
+        $invite = MainInviteClass::ensureByMainId($mainId);
         $inviteData = [
             'inviteCode' => '',
             'inviteCodeMask' => '',

+ 29 - 1
biz-hd/shop/classes/MainInviteClass.php

@@ -30,6 +30,34 @@ class MainInviteClass extends BaseClass
         return self::getByCondition(['mainId' => (int)$mainId], false);
     }
 
+    /**
+     * 确保 xhMainInvite 存在(邀请人账户);无记录则按 dict 默认规则自动初始化
+     * 供 shopInviteHub 首页、生成邀请码等入口调用;并发下 uk_mainId 冲突时再查一次
+     *
+     * @param int $mainId 当前登录 xhMain.id
+     * @return array|null 邀请主数据行
+     */
+    public static function ensureByMainId($mainId)
+    {
+        $mainId = (int) $mainId;
+        if ($mainId <= 0) {
+            return null;
+        }
+        $row = self::getByMainId($mainId);
+        if (!empty($row)) {
+            return $row;
+        }
+        try {
+            return self::createInviteeRecord($mainId, 0);
+        } catch (\Throwable $e) {
+            $row = self::getByMainId($mainId);
+            if (!empty($row)) {
+                return $row;
+            }
+            throw $e;
+        }
+    }
+
     /**
      * 按邀请码查 xhMainInvite
      */
@@ -320,7 +348,7 @@ class MainInviteClass extends BaseClass
      */
     protected static function depositAbleCommissionToWalletInTransaction($mainId, $shopId, $shopName)
     {
-        $preview = self::getByMainId($mainId);
+        $preview = self::ensureByMainId($mainId);
         if (empty($preview)) {
             throw new \Exception('暂无邀请账户');
         }