Goods.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\api\model\sharing;
  10. use app\common\model\sharing\Goods as GoodsModel;
  11. use app\common\library\helper;
  12. use app\common\exception\BaseException;
  13. /**
  14. * 拼团商品模型
  15. * Class Goods
  16. * @package app\api\model\sharing
  17. */
  18. class Goods extends GoodsModel
  19. {
  20. /**
  21. * 隐藏字段
  22. * @var array
  23. */
  24. protected $hidden = [
  25. 'spec_rel',
  26. 'delivery',
  27. 'sales_initial',
  28. 'sales_actual',
  29. 'is_delete',
  30. 'wxapp_id',
  31. 'create_time',
  32. 'update_time'
  33. ];
  34. /**
  35. * 商品详情:HTML实体转换回普通字符
  36. * @param $value
  37. * @return string
  38. */
  39. public function getContentAttr($value)
  40. {
  41. return htmlspecialchars_decode($value);
  42. }
  43. /**
  44. * 获取商品列表
  45. * @param $param
  46. * @param bool $userInfo
  47. * @return mixed
  48. * @throws \think\exception\DbException
  49. */
  50. public function getList($param, $userInfo = false)
  51. {
  52. // 获取商品列表
  53. $data = parent::getList($param)->hidden(['category', 'content', 'image', 'sku']);
  54. // 整理列表数据并返回
  55. return $this->setGoodsListDataFromApi($data, true, ['userInfo' => $userInfo]);
  56. }
  57. /**
  58. * 获取商品详情信息
  59. * @param int $goodsId 商品id
  60. * @param array|bool $userInfo 用户信息
  61. * @return array|false|\PDOStatement|string|\think\Model|static
  62. * @throws BaseException
  63. */
  64. public function getDetails($goodsId, $userInfo = false)
  65. {
  66. // 获取商品详情
  67. $model = new static;
  68. $goods = $model->with([
  69. 'category',
  70. 'image' => ['file'],
  71. 'sku' => ['image'],
  72. 'spec_rel' => ['spec'],
  73. 'delivery' => ['rule'],
  74. 'commentData' => function ($query) {
  75. $query->with('user')->where(['is_delete' => 0, 'status' => 1])->limit(2);
  76. }
  77. ])->withCount(['commentData' => function ($query) {
  78. $query->where(['is_delete' => 0, 'status' => 1]);
  79. }])
  80. ->where('goods_id', '=', $goodsId)
  81. ->find();
  82. // 判断商品的状态
  83. if (!$goods || $goods['is_delete'] || $goods['goods_status']['value'] != 10) {
  84. throw new BaseException(['msg' => '很抱歉,商品信息不存在或已下架']);
  85. }
  86. // 设置商品展示的数据
  87. $goods = $model->setGoodsListDataFromApi($goods, false, ['userInfo' => $userInfo]);
  88. // 多规格商品sku信息
  89. $goods['goods_multi_spec'] = $goods['spec_type'] == 20 ? $model->getManySpecData($goods['spec_rel'], $goods['sku']) : null;
  90. return $goods;
  91. }
  92. /**
  93. * 根据商品id集获取商品列表
  94. * @param $goodsIds
  95. * @param bool $userInfo
  96. * @return mixed
  97. * @throws \think\db\exception\DataNotFoundException
  98. * @throws \think\db\exception\ModelNotFoundException
  99. * @throws \think\exception\DbException
  100. */
  101. public function getListByIdsFromApi($goodsIds, $userInfo = false)
  102. {
  103. // 获取商品列表
  104. $data = parent::getListByIds($goodsIds, 10);
  105. // 整理列表数据并返回
  106. return $this->setGoodsListDataFromApi($data, true, ['userInfo' => $userInfo]);
  107. }
  108. /**
  109. * 设置商品展示的数据 api模块
  110. * @param $data
  111. * @param bool $isMultiple
  112. * @param array $param
  113. * @return mixed
  114. */
  115. private function setGoodsListDataFromApi(&$data, $isMultiple, $param)
  116. {
  117. return parent::setGoodsListData($data, $isMultiple, function ($goods) use ($param) {
  118. // 计算并设置商品会员价
  119. $this->setGoodsGradeMoney($param['userInfo'], $goods);
  120. });
  121. }
  122. /**
  123. * 设置商品的会员价
  124. * @param $user
  125. * @param $goods
  126. */
  127. private function setGoodsGradeMoney($user, &$goods)
  128. {
  129. // 会员等级状态
  130. $gradeStatus = (!empty($user) && $user['grade_id'] > 0 && !empty($user['grade']))
  131. && (!$user['grade']['is_delete'] && $user['grade']['status']);
  132. // 判断商品是否参与会员折扣
  133. if (!$gradeStatus || !$goods['is_enable_grade']) {
  134. $goods['is_user_grade'] = false;
  135. return;
  136. }
  137. // 商品单独设置了会员折扣
  138. if ($goods['is_alone_grade'] && isset($goods['alone_grade_equity'][$user['grade_id']])) {
  139. // 折扣比例
  140. $discountRatio = helper::bcdiv($goods['alone_grade_equity'][$user['grade_id']], 10);
  141. } else {
  142. // 折扣比例
  143. $discountRatio = helper::bcdiv($user['grade']['equity']['discount'], 10);
  144. }
  145. if ($discountRatio > 0) {
  146. // 标记参与会员折扣
  147. $goods['is_user_grade'] = true;
  148. // 会员折扣价
  149. foreach ($goods['sku'] as &$skuItem) {
  150. $skuItem['goods_price'] = helper::number2(helper::bcmul($skuItem['goods_price'], $discountRatio), true);
  151. $skuItem['sharing_price'] = helper::number2(helper::bcmul($skuItem['sharing_price'], $discountRatio), true);
  152. }
  153. }
  154. }
  155. }