GoodsController.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace mall\controllers;
  3. use bizHd\goods\classes\GoodsCategoryClass;
  4. use bizMall\goods\classes\GoodsClass;
  5. use bizMall\goods\classes\GoodsSettingClass;
  6. use bizMall\pictext\classes\PicTextGoodsClass;
  7. use bizMall\goods\classes\CategoryClass;
  8. use bizMall\pictext\classes\PicTextClass;
  9. use Yii;
  10. use common\components\util;
  11. /**
  12. * 商城商品接口
  13. * detail:允许游客浏览详情(需带 account 定位门店);加购/下单仍在前端弹登录
  14. */
  15. class GoodsController extends BaseController
  16. {
  17. /** 商品详情允许未登录读取,便于分享落地页浏览 */
  18. public $guestAccess = ['detail'];
  19. /**
  20. * 获取商品详情
  21. * GET /goods/detail?id=&account=&hdId=
  22. * 游客:不强制 custom,价格按散客/无会员价组装;customId 返回 0
  23. */
  24. public function actionDetail()
  25. {
  26. $id = Yii::$app->request->get('id', 0);
  27. $shop = $this->shop;
  28. // 必须有店铺上下文,否则无法校验 mainId / 计价
  29. if (empty($shop) || empty($this->mainId)) {
  30. util::fail('访问门店出错了');
  31. }
  32. // 游客可浏览:custom 为空时 groupGoodsBaseInfo 按无会员价处理,不报「用户信息缺失」
  33. $custom = !empty($this->custom) ? $this->custom : [];
  34. $params = [];
  35. $goods = GoodsClass::getGoodsInfo($id, $shop, $custom, $params);
  36. if (empty($goods)) {
  37. util::fail('商品不存在');
  38. }
  39. if ($goods['mainId'] != $this->mainId) {
  40. util::fail('不是你的商品');
  41. }
  42. $goods = \bizHd\goods\classes\GoodsClass::appendUseCasesAndSpecs($goods, $this->mainId);
  43. // $setting = GoodsSettingClass::getByCondition(['mainId' => $this->mainId]);
  44. // $intro = $setting['goodsIntroduce'] ?? '';
  45. // $info['commonIntro'] = $intro;
  46. // 商品详情图文
  47. if (empty($goods['masterId'])) {
  48. $picTextGoodsId = $id;
  49. } else {
  50. $picTextGoodsId = $goods['masterId'];
  51. }
  52. $pixTextGoods = PicTextGoodsClass::getByCondition(['mainId' => $this->mainId, 'goodsId' => $picTextGoodsId], true);
  53. $goods['picTextGoods'] = $pixTextGoods;
  54. $categoryId = intval(Yii::$app->request->get('categoryId'));
  55. if ($categoryId <= 0 && $id > 0) {
  56. $categoryMap = GoodsCategoryClass::getGoodsHasCategoryIdsBatch([$picTextGoodsId]);
  57. $categoryIds = $categoryMap[$picTextGoodsId] ?? [];
  58. if (!empty($categoryIds)) {
  59. $categoryId = intval($categoryIds[0]);
  60. }
  61. }
  62. // 商品所属分类图文
  63. $goods['picText'] = [];
  64. $category = CategoryClass::getById($categoryId);
  65. if (!empty($category)) {
  66. $picTextId = $category['picTextId'];
  67. $picText = PicTextClass::getById($picTextId, true);
  68. $goods['picText'] = $picText;
  69. }
  70. $goods['customId'] = $this->customId ?: 0;
  71. util::success($goods);
  72. }
  73. }