Kaynağa Gözat

零售端,登录功能优化

ouyang 9 saat önce
ebeveyn
işleme
8cb9714f7d

+ 5 - 2
app-mall/controllers/BaseController.php

@@ -64,18 +64,21 @@ class BaseController extends PublicController
                 if (!empty($hdId)) {
                     $hd = HdClass::getById($hdId, true);
                     if (!empty($hd)) {
-                        if (!empty($this->userId) && $hd->userId == $this->userId && $shopId == $hd->shopId) { // 用户和店铺匹配校验
+                        if (!empty($this->userId) && $hd->userId == $this->userId && $shopId == $hd->shopId) {
+                            // 已登录且 hd 归属当前用户:绑定门店客户上下文
                             $this->hd = $hd;
                             $this->hdId = $hdId;
                             $customId = $hd->customId ?? 0;
                             $custom = CustomClass::getById($customId, true);
                             $this->custom = $custom;
                             $this->customId = $customId;
-                        } else { // 用户和店铺不匹配,清空店铺信息,防止后续使用(防止用户恶意使用店铺信息)
+                        } elseif (!empty($this->userId)) {
+                            // 已登录但 hd 与用户不匹配:清空店铺,防止恶意越权
                             $this->shop = null;
                             $this->main = null;
                             $this->mainId = 0;
                         }
+                        // 未登录访客:保留 shop/main,仅不绑定 hd/custom,便于浏览分类/首页
                     }
                 }
             }

+ 14 - 12
app-mall/controllers/CategoryController.php

@@ -12,13 +12,17 @@ use common\components\util;
 class CategoryController extends BaseController
 {
 
-    public $guestAccess = ['list', 'use-case-list']; // 'goods-list' ---- 为什么不用认证
+    /**
+     * 游客可浏览分类与商品列表(商城分类页未登录浏览);下单/建客仍走登录后流程
+     * buildRelation 对空 user 直接 return,游客不会建客
+     */
+    public $guestAccess = ['list', 'use-case-list', 'goods-list'];
 
-    //获取分类 ssh 2019.12.2
+    //获取分类 ssh 2019.12.2(游客可浏览,不依赖 user;无店铺上下文时返回空)
     public function actionList()
     {
-        $user = $this->user;
-        if (empty($user)) {
+        // 未登录场景必须有 account→mainId,否则无法定位门店分类
+        if (empty($this->shop) || empty($this->mainId)) {
             util::success([]);
         }
 
@@ -37,7 +41,7 @@ class CategoryController extends BaseController
             }
         }
 
-        //建立客户关系
+        //建立客户关系(未登录时 buildRelation 直接返回,不建客)
         CustomClass::buildRelation($this->shop, $this->user);
         $mainId = $this->mainId ?? 0;
         $where = ['mainId' => $mainId, 'delStatus' => 0, 'status' => 1];
@@ -50,17 +54,15 @@ class CategoryController extends BaseController
         util::success($return);
     }
 
-    //取分类下商品 ssh 2019.12.2
+    //取分类下商品 ssh 2019.12.2(游客可浏览,custom 为空时按散客价组装)
     public function actionGoodsList()
     {
-        $user = $this->user;
-        if (empty($user)) {
-            util::success(['list' => []]);
-        }
-
         $get = Yii::$app->request->get();
         $shop = $this->shop;
-        $custom = $this->custom;
+        if (empty($shop)) {
+            util::success(['list' => []]);
+        }
+        $custom = !empty($this->custom) ? $this->custom : [];
         $params = [];
         //调用花店端的方法,后面再分离 (为了代码维护可以不分离,免得一处改了,另一处没修改到。--- 所以分离好后,又还原了)
         $data = \bizHd\goods\classes\GoodsCategoryClass::getGoodsList($get, $shop, $custom, $params);

+ 6 - 2
app-mall/controllers/CategorySettingController.php

@@ -11,14 +11,18 @@ use Yii;
  */
 class CategorySettingController extends BaseController
 {
+    /** 分类页配置允许未登录读取,便于访客浏览分类菜单 */
+    public $guestAccess = ['detail'];
+
     /**
      * 读取当前门店分类页配置(花材展示名、置顶、广告等)
      * GET /category-setting/detail
+     * 未登录也可读:只要带 account 解析出 mainId/shopId 即可(getOrInit 只读不写库)
      */
     public function actionDetail()
     {
-        $user = $this->user;
-        if (empty($user) || empty($this->mainId) || empty($this->shopId)) {
+        // 无店铺上下文时返回默认空配置,避免游客误报登录
+        if (empty($this->mainId) || empty($this->shopId)) {
             util::success([
                 'id' => 0,
                 'mainId' => intval($this->mainId),

+ 23 - 6
app-mall/controllers/GoodsController.php

@@ -11,20 +11,37 @@ use bizMall\pictext\classes\PicTextClass;
 use Yii;
 use common\components\util;
 
+/**
+ * 商城商品接口
+ * detail:允许游客浏览详情(需带 account 定位门店);加购/下单仍在前端弹登录
+ */
 class GoodsController extends BaseController
 {
-    //获取商品详情 ssh 2019.12.3
+    /** 商品详情允许未登录读取,便于分享落地页浏览 */
+    public $guestAccess = ['detail'];
+
+    /**
+     * 获取商品详情
+     * GET /goods/detail?id=&account=&hdId=
+     * 游客:不强制 custom,价格按散客/无会员价组装;customId 返回 0
+     */
     public function actionDetail()
     {
         $id = Yii::$app->request->get('id', 0);
 
-        $custom = $this->custom;
-        if (empty($custom)) {
-            util::fail('用户信息缺失');
-        }
         $shop = $this->shop;
+        // 必须有店铺上下文,否则无法校验 mainId / 计价
+        if (empty($shop) || empty($this->mainId)) {
+            util::fail('访问门店出错了');
+        }
+
+        // 游客可浏览:custom 为空时 groupGoodsBaseInfo 按无会员价处理,不报「用户信息缺失」
+        $custom = !empty($this->custom) ? $this->custom : [];
         $params = [];
         $goods = GoodsClass::getGoodsInfo($id, $shop, $custom, $params);
+        if (empty($goods)) {
+            util::fail('商品不存在');
+        }
 
         if ($goods['mainId'] != $this->mainId) {
             util::fail('不是你的商品');
@@ -60,7 +77,7 @@ class GoodsController extends BaseController
             $picText = PicTextClass::getById($picTextId, true);
             $goods['picText'] = $picText;
         }
-        $goods['customId'] = $this->customId;
+        $goods['customId'] = $this->customId ?: 0;
 
         util::success($goods);
     }

+ 8 - 4
app-mall/controllers/ItemController.php

@@ -110,6 +110,10 @@ class ItemController extends BaseController
         echo '<div style="font-size:60px;margin-top:50px;margin-left:60px;">' . $className . '</div>';
     }
 
+    /**
+     * 花材分类+商品(商城分类页)
+     * 游客可浏览:guestAccess 含 index;无店铺或非零售店直接失败/空;buildRelation 对空 user 不建客
+     */
     public function actionIndex()
     {
         $py = Yii::$app->request->get('py', '');
@@ -119,12 +123,11 @@ class ItemController extends BaseController
         $delStatus = Yii::$app->request->get('delStatus', 0);
 
         $shop = $this->shop;
-        if (!isset($shop->ptStyle) || $shop->ptStyle != 1) {
+        // 未登录时若未带 account,shop 为空;避免对 null 取 ptStyle 报错
+        if (empty($shop) || !isset($shop->ptStyle) || $shop->ptStyle != 1) {
             util::fail('访问门店出错了');
         }
-
-        $user = $this->user;
-        if (empty($user)) {
+        if (empty($this->mainId)) {
             util::success([]);
         }
 
@@ -157,6 +160,7 @@ class ItemController extends BaseController
         //$where['frontHide'] = 0;
         //零售端没有隐藏
         $where['frontUse'] = 0;
+        // level=0:游客/无会员用标准批发价展示,不依赖 custom
         $level = 0;
         $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
         $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);

+ 6 - 2
app-mall/controllers/ShopNoticeController.php

@@ -11,14 +11,18 @@ use Yii;
  */
 class ShopNoticeController extends BaseController
 {
+    /** 列表允许未登录浏览(详情仍需登录) */
+    public $guestAccess = ['show-list'];
+
     /**
      * 客户端公告列表(仅上架、未删除)
      * GET /shop-notice/show-list?position=2
+     * 未登录可浏览:依赖 account 解析 mainId,与登录态无关
      */
     public function actionShowList()
     {
-        $user = $this->user;
-        if (empty($user) || empty($this->mainId)) {
+        // 无门店主账号时返回空列表(不要求 user,避免分类/首页公告空白或 toast)
+        if (empty($this->mainId)) {
             util::success([
                 'totalNum' => 0,
                 'totalPage' => 0,