Goods.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\model;
  13. use app\api\service\Goods as GoodsService;
  14. use app\api\service\user\Grade as UserGradeService;
  15. use app\api\model\GoodsSpecRel as GoodsSpecRelModel;
  16. use app\common\model\Goods as GoodsModel;
  17. use app\common\enum\goods\Status as GoodsStatusEnum;
  18. use cores\exception\BaseException;
  19. /**
  20. * 商品模型
  21. * Class Goods
  22. * @package app\api\model
  23. */
  24. class Goods extends GoodsModel
  25. {
  26. /**
  27. * 隐藏字段
  28. * @var array
  29. */
  30. public $hidden = [
  31. 'images',
  32. 'delivery',
  33. 'deduct_stock_type',
  34. 'sales_initial',
  35. 'sales_actual',
  36. 'sort',
  37. 'is_delete',
  38. 'store_id',
  39. 'create_time',
  40. 'update_time'
  41. ];
  42. // 是否设置会员折扣价
  43. private $isGoodsGradeMoney = true;
  44. /**
  45. * 商品详情:HTML实体转换回普通字符
  46. * @param $value
  47. * @return string
  48. */
  49. public function getContentAttr($value): string
  50. {
  51. return htmlspecialchars_decode((string)$value);
  52. }
  53. /**
  54. * 是否设置会员折扣价
  55. * @param bool $value
  56. * @return $this
  57. */
  58. public function isGoodsGradeMoney(bool $value): Goods
  59. {
  60. $this->isGoodsGradeMoney = $value;
  61. return $this;
  62. }
  63. /**
  64. * 获取商品列表
  65. * @param array $param 查询条件
  66. * @param int $listRows 分页数量
  67. * @return mixed|\think\model\Collection|\think\Paginator
  68. * @throws \think\db\exception\DbException
  69. */
  70. public function getList(array $param = [], int $listRows = 15)
  71. {
  72. // 整理查询参数
  73. $params = array_merge($param, ['status' => GoodsStatusEnum::ON_SALE]);
  74. // 获取商品列表
  75. $list = parent::getList($params, $listRows);
  76. if ($list->isEmpty()) {
  77. return $list;
  78. }
  79. // 隐藏冗余的字段
  80. $list->hidden(array_merge($this->hidden, ['content', 'goods_images', 'images']));
  81. // 整理列表数据并返回
  82. return $this->setGoodsListDataFromApi($list);
  83. }
  84. /**
  85. * 获取商品详情 (详细数据用于页面展示)
  86. * @param int $goodsId 商品id
  87. * @return mixed
  88. * @throws BaseException
  89. * @throws \think\db\exception\DataNotFoundException
  90. * @throws \think\db\exception\DbException
  91. * @throws \think\db\exception\ModelNotFoundException
  92. */
  93. public function getDetails(int $goodsId)
  94. {
  95. // 关联查询
  96. $with = ['images.file', 'skuList.image', 'video', 'videoCover'];
  97. // 获取商品记录
  98. $goodsInfo = static::detail($goodsId, $with);
  99. // 判断商品的状态
  100. if (empty($goodsInfo) || $goodsInfo['is_delete'] || $goodsInfo['status'] != GoodsStatusEnum::ON_SALE) {
  101. throwError('很抱歉,商品信息不存在或已下架');
  102. }
  103. // 设置商品展示的数据
  104. $goodsInfo = $this->setGoodsDataFromApi($goodsInfo);
  105. // 商品规格列表
  106. $goodsInfo['specList'] = GoodsSpecRelModel::getSpecList($goodsInfo['goods_id']);
  107. return $goodsInfo;
  108. }
  109. /**
  110. * 根据商品id集获取商品列表
  111. * @param array $goodsIds
  112. * @return mixed
  113. */
  114. public function getListByIdsFromApi(array $goodsIds)
  115. {
  116. // 获取商品列表
  117. $data = $this->getListByIds($goodsIds, GoodsStatusEnum::ON_SALE);
  118. // 整理列表数据并返回
  119. return $this->setGoodsListDataFromApi($data);
  120. }
  121. /**
  122. * 获取商品指定的sku信息并且设置商品的会员价
  123. * @param mixed $goodsInfo 商品信息
  124. * @param string $goodsSkuId 商品SKUID
  125. * @param bool $isGoodsGradeMoney 是否设置会员折扣价
  126. * @return \app\common\model\GoodsSku|array|null
  127. * @throws BaseException
  128. */
  129. public static function getSkuInfo($goodsInfo, string $goodsSkuId, bool $isGoodsGradeMoney = true)
  130. {
  131. $goodsInfo['skuInfo'] = GoodsService::getSkuInfo($goodsInfo['goods_id'], $goodsSkuId);
  132. $isGoodsGradeMoney && (new static)->setGoodsGradeMoney($goodsInfo);
  133. return $goodsInfo['skuInfo'];
  134. }
  135. /**
  136. * 设置商品展示的数据 api模块
  137. * @param $data
  138. * @return mixed
  139. */
  140. private function setGoodsListDataFromApi($data)
  141. {
  142. return $this->setGoodsListData($data, function ($goods) {
  143. // 整理商品数据 api模块
  144. $this->setGoodsDataFromApi($goods);
  145. });
  146. }
  147. /**
  148. * 整理商品数据 api模块
  149. * @param $goodsInfo
  150. * @return mixed
  151. */
  152. private function setGoodsDataFromApi($goodsInfo)
  153. {
  154. return $this->setGoodsData($goodsInfo, function ($goods) {
  155. // 计算并设置商品会员价
  156. $this->isGoodsGradeMoney && $this->setGoodsGradeMoney($goods);
  157. });
  158. }
  159. /**
  160. * 设置商品的会员价
  161. * @param Goods $goods
  162. * @throws BaseException
  163. */
  164. private function setGoodsGradeMoney(self $goods)
  165. {
  166. // 设置当前商品是否使用会员等级折扣价
  167. $goods['is_user_grade'] = false;
  168. // 获取当前登录用户的会员等级信息
  169. $gradeInfo = UserGradeService::getCurrentGradeInfo();
  170. // 判断商品是否参与会员折扣
  171. if (empty($gradeInfo) || !$goods['is_enable_grade']) {
  172. return;
  173. }
  174. // 默认的折扣比例
  175. $discountRatio = $gradeInfo['equity']['discount'];
  176. // 商品单独设置了会员折扣
  177. if ($goods['is_alone_grade'] && isset($goods['alone_grade_equity'][$gradeInfo['grade_id']])) {
  178. $discountRatio = $goods['alone_grade_equity'][$gradeInfo['grade_id']];
  179. }
  180. if (empty($discountRatio)) {
  181. return;
  182. }
  183. // 标记参与会员折扣
  184. $goods['is_user_grade'] = true;
  185. // 会员折扣价: 商品基础价格
  186. $goods['goods_price_min'] = UserGradeService::getDiscountPrice($goods['goods_price_min'], $discountRatio);
  187. $goods['goods_price_max'] = UserGradeService::getDiscountPrice($goods['goods_price_max'], $discountRatio);
  188. // 会员折扣价: 商品sku列表
  189. if ($goods->getRelation('skuList')) {
  190. foreach ($goods['skuList'] as &$skuItem) {
  191. $skuItem['goods_price'] = UserGradeService::getDiscountPrice($skuItem['goods_price'], $discountRatio);
  192. }
  193. }
  194. // 会员折扣价: 已选择的商品sku(用于购物车)
  195. if ($goods->getAttr('skuInfo')) {
  196. $goods['skuInfo']['goods_price'] = UserGradeService::getDiscountPrice($goods['skuInfo']['goods_price'], $discountRatio);
  197. }
  198. }
  199. }