GoodsController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace addons\Happiness\api\modules\v1\controllers;
  3. use addons\Clinic\common\enums\ClinicEnum;
  4. use addons\Clinic\common\models\Clinic;
  5. use addons\Clinic\common\models\ClinicCoupon;
  6. use addons\Clinic\common\models\ClinicCouponCatRelate;
  7. use addons\Clinic\common\models\ClinicCouponGoodsRelate;
  8. use addons\Clinic\common\models\ClinicGoods;
  9. use addons\Clinic\common\services\ClinicGoodsTrackingService;
  10. use addons\Coupon\common\enums\AddonsCouponEnum;
  11. use addons\Happiness\common\enums\HappinessEnum;
  12. use addons\Happiness\common\service\GoodsService;
  13. use addons\Happiness\common\service\StoreService;
  14. use common\enums\GoodsTypeEnum;
  15. use common\enums\OrderCommentEnum;
  16. use common\enums\StatusEnum;
  17. use common\events\census\ViewsEvent;
  18. use common\globals\GlobalInvoke;
  19. use common\helpers\LocationHelper;
  20. use common\models\common\Region;
  21. use common\models\goods\Goods;
  22. use common\models\goods\GoodsCateRelate;
  23. use common\models\order\OrderComments;
  24. use common\models\user\User;
  25. use common\models\user\UserLevel;
  26. use common\logic\common\GoodsLogic;
  27. use common\models\user\UserPartitionRelationship;
  28. use Yii;
  29. use Exception;
  30. use api\controllers\OnAuthController;
  31. /**
  32. * 幸福小店商品
  33. *
  34. * Class DefaultController
  35. * @package addons\Store\api\modules\v1\controllers
  36. */
  37. class GoodsController extends OnAuthController
  38. {
  39. public $modelClass = '';
  40. /**
  41. * 不用进行登录验证的方法
  42. *
  43. * 例如: ['index', 'update', 'create', 'view', 'delete']
  44. * 默认全部需要验证
  45. *
  46. * @var array
  47. */
  48. protected $authOptional = ['index', 'detail', 'cate-goods-list', 'list', 'recommend', 'comments-list', 'get-platform-goods'];
  49. //获取门店商品列表
  50. public function actionCateGoodsList()
  51. {
  52. $request = Yii::$app->request;
  53. if ($request->isGet) {
  54. try {
  55. $get = $request->get();
  56. $res = Yii::$app->services->validate->validate($get, [
  57. [['cate_id', 'page', 'store_id', 'limit'], 'integer'],
  58. [['keyword'], 'string'],
  59. [['type'],'safe']
  60. ]);
  61. if (!$res) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  62. $list = (new GoodsService(['mall_id' => $this->mall_id, 'user_id' => $this->user_id]))->cateGoodsList($get);
  63. $this->success('获取成功', $list);
  64. } catch (\Exception $e) {
  65. return $this->error($e->getMessage());
  66. }
  67. }
  68. }
  69. //最近的门店商品
  70. public function actionGetNearStoreGoodsList()
  71. {
  72. $request = Yii::$app->request;
  73. if ($request->isGet) {
  74. try {
  75. $get = $request->get();
  76. $res = Yii::$app->services->validate->validate($get, [
  77. [['longitude', 'latitude', 'shipping_type', 'attr_id', 'attr_ids', 'goods_ids'], 'string'],
  78. ]);
  79. if (!$res) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  80. $get['mall_id'] = $this->mall_id;
  81. $get['user_id'] = $this->user_id;
  82. if (!empty($get['attr_ids'])) $get['attr_ids'] = explode(',', $get['attr_ids']);
  83. if (!empty($get['goods_ids'])) $get['goods_ids'] = explode(',', $get['goods_ids']);
  84. $info = (new StoreService())->getStore($get);
  85. $search['store_id']= $info['id'];
  86. $search['page'] = $get['page'];
  87. $search['limit'] = $get['limit'];
  88. if (!$res) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  89. $list = (new GoodsService(['mall_id' => $this->mall_id, 'user_id' => $this->user_id]))->cateGoodsList
  90. ($search);
  91. $list['store']=(new StoreService(['mall_id' => $this->mall_id]))->getStoreDetail(['store_id'=>$info['id']]);
  92. $this->success('获取成功', $list);
  93. } catch (\Exception $e) {
  94. return $this->error($e->getMessage());
  95. }
  96. }
  97. }
  98. /**
  99. * 首页
  100. *
  101. * @return string
  102. */
  103. public function actionDetail()
  104. {
  105. $params = \Yii::$app->request->get();
  106. $res = \Yii::$app->services->validate->validate($params, [
  107. [['goods_id'], 'integer'],
  108. [['scene'], 'string'],
  109. ['store_id', 'integer'],
  110. [['addonsFields','latitude','longitude'], 'safe']
  111. ]);
  112. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  113. $goods = GoodsLogic::GetDetail($this->mall_id, $params, $this->user);
  114. if (empty($goods)) return $this->error(GoodsLogic::getStaticError());
  115. if ($params['addonsFields']) {
  116. if($this->platform == User::PLATFORM_MP_WX){
  117. is_string($params['addonsFields']) && $params['addonsFields'] = json_decode($params['addonsFields'], true);
  118. }else{
  119. $params['addonsFields'] = is_string($params['addonsFields']) ? explode(',', $params['addonsFields']) : $params['addonsFields'];
  120. }
  121. $goods = Goods::getAddonsGoodsSetting($this->mall_id, $params['addonsFields'], $goods, 'detail');
  122. }
  123. $goods['is_bind_popups'] = UserPartitionRelationship::getIsNeedPopups($this->mall_id, $this->user_id ?? 0);
  124. //浏览
  125. if ($goods) {
  126. $event = new ViewsEvent($this->mall_id);
  127. $data['mall_id'] = $this->mall_id;
  128. $data['user_id'] = $this->user_id;
  129. $data['goods_id'] = $params['goods_id'];
  130. \Yii::$app->services->events->dispatch($event, $data, $event->listeners);
  131. $smart_form_template = GlobalInvoke::exec(GlobalInvoke::GOODS_NEED_SMARTFORM, $this->mall_id, ['user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'goods_id' => $params['goods_id']]);
  132. if (!empty($smart_form_template)) $goods = array_merge($goods, $smart_form_template);
  133. // 商品详情公共调用
  134. $invoke_data = GlobalInvoke::exec(
  135. GlobalInvoke::API_GOODS_DETAIL,
  136. $this->mall_id,
  137. ['mall_id' => $this->mall_id, 'goods' => $goods, 'user' => $this->user,'latitude'=>$params['latitude']??0,'longitude'=>$params['longitude']??0]
  138. );
  139. if (!empty($invoke_data['goods'])) $goods = $invoke_data['goods'];
  140. }
  141. // 首页文本命名
  142. $home_text = Yii::$app->services->setting->mall->get($this->mall_id, "goodsdetail.home_text");
  143. $goods['home_text'] = !empty($home_text) ? $home_text : '首页';
  144. $res = GoodsLogic::getGoodsAssociationList($this->mall_id,$params['goods_id']);
  145. $goods['extra1_goods_list'] = $res['extra1_goods_list']??[];
  146. $goods['extra2_goods_list'] = $res['extra2_goods_list']??[];
  147. $pension_scale = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME,
  148. 'basic.pension_scale')??0;
  149. $goods['pension_price']=bcmul($goods['price'],bcdiv($pension_scale,100,4),2);
  150. return $this->success('获取成功', $goods);
  151. }
  152. }