Goods.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2025 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\model\GoodsSku as GoodsSkuModel;
  14. use app\api\model\GoodsSpecRel as GoodsSpecRelModel;
  15. use app\api\service\Goods as GoodsService;
  16. use app\api\service\user\Grade as UserGradeService;
  17. use app\common\enum\goods\Status as GoodsStatusEnum;
  18. use app\common\model\Goods as GoodsModel;
  19. use cores\exception\BaseException;
  20. use think\db\exception\DataNotFoundException;
  21. use think\db\exception\DbException;
  22. use think\db\exception\ModelNotFoundException;
  23. /**
  24. * 商品模型
  25. * Class Goods
  26. * @package app\api\model
  27. */
  28. class Goods extends GoodsModel
  29. {
  30. /**
  31. * 隐藏字段
  32. * @var array
  33. */
  34. public $hidden = [
  35. 'images',
  36. 'delivery',
  37. 'deduct_stock_type',
  38. 'sales_initial',
  39. 'sales_actual',
  40. 'sort',
  41. 'is_delete',
  42. 'store_id',
  43. 'create_time',
  44. 'update_time'
  45. ];
  46. // 是否启用会员折扣价
  47. private bool $enableGradeMoney = true;
  48. /**
  49. * 商品详情:HTML实体转换回普通字符
  50. * @param $value
  51. * @return string
  52. */
  53. public function getContentAttr($value): string
  54. {
  55. return \htmlspecialchars_decode((string)$value);
  56. }
  57. /**
  58. * 设置是否启用会员折扣价
  59. * @param bool $value
  60. * @return $this
  61. * @throws DataNotFoundException
  62. * @throws DbException
  63. * @throws ModelNotFoundException
  64. */
  65. public function setEnableGradeMoney(bool $value): Goods
  66. {
  67. $this->enableGradeMoney = $value;
  68. return $this;
  69. }
  70. /**
  71. * 获取是否启用会员折扣价
  72. * @return bool
  73. * @throws DataNotFoundException
  74. * @throws DbException
  75. * @throws ModelNotFoundException
  76. */
  77. private function getEnableGradeMoney(): bool
  78. {
  79. return $this->enableGradeMoney;
  80. }
  81. /**
  82. * 获取商品列表
  83. * @param array $param 查询条件
  84. * @param int $listRows 分页数量
  85. * @return mixed|\think\model\Collection|\think\Paginator
  86. * @throws \think\db\exception\DbException
  87. */
  88. public function getList(array $param = [], int $listRows = 15)
  89. {
  90. // 整理查询参数
  91. $params = \array_merge($param, ['status' => GoodsStatusEnum::ON_SALE]);
  92. // 获取商品列表
  93. $list = parent::getList($params, $listRows);
  94. if ($list->isEmpty()) {
  95. return $list;
  96. }
  97. // 隐藏冗余的属性
  98. $list->hidden(static::getHidden(['content', 'goods_images', 'images']));
  99. // 整理列表数据并返回
  100. return $this->setGoodsListDataFromApi($list);
  101. }
  102. /**
  103. * 获取商品详情 (详细数据用于页面展示)
  104. * @param int $goodsId 商品ID
  105. * @param bool $verifyStatus 是否验证商品状态(上架)
  106. * @return mixed
  107. * @throws BaseException
  108. * @throws \think\db\exception\DataNotFoundException
  109. * @throws \think\db\exception\DbException
  110. * @throws \think\db\exception\ModelNotFoundException
  111. */
  112. public function getDetails(int $goodsId, bool $verifyStatus = true)
  113. {
  114. // 关联查询(商品图片、sku列表)
  115. $with = ['images.file', 'skuList.image', 'video', 'videoCover'];
  116. // 获取商品记录
  117. $goodsInfo = $this->getGoodsMain($goodsId, $with, $verifyStatus);
  118. // 商品规格列表
  119. $goodsInfo['specList'] = GoodsSpecRelModel::getSpecList($goodsInfo['goods_id']);
  120. return $goodsInfo->hidden(static::getHidden(['images']));
  121. }
  122. /**
  123. * 获取商品详情 (仅包含主商品信息和商品图片)
  124. * @param int $goodsId 商品ID
  125. * @param bool $verifyStatus 是否验证商品状态(上架)
  126. * @return mixed
  127. * @throws BaseException
  128. */
  129. public function getBasic(int $goodsId, bool $verifyStatus = true)
  130. {
  131. return $this->getGoodsMain($goodsId, ['images.file'], $verifyStatus);
  132. }
  133. /**
  134. * 获取商品规格数据
  135. * @param int $goodsId
  136. * @return array
  137. * @throws \think\db\exception\DataNotFoundException
  138. * @throws \think\db\exception\DbException
  139. * @throws \think\db\exception\ModelNotFoundException
  140. */
  141. public function getSpecData(int $goodsId): array
  142. {
  143. $data = [];
  144. // 商品SKU列表
  145. $data['skuList'] = GoodsSkuModel::getSkuList($goodsId, true);
  146. // 商品规格列表
  147. $data['specList'] = GoodsSpecRelModel::getSpecList($goodsId);
  148. return $data;
  149. }
  150. /**
  151. * 获取商品主体信息
  152. * @param int $goodsId 商品ID
  153. * @param array $with 关联查询
  154. * @param bool $verifyStatus 是否验证商品状态(上架)
  155. * @return mixed
  156. * @throws BaseException
  157. */
  158. private function getGoodsMain(int $goodsId, array $with = [], bool $verifyStatus = true)
  159. {
  160. // 获取商品记录
  161. $goodsInfo = static::detail($goodsId, $with);
  162. // 判断商品是否存在
  163. if (empty($goodsInfo) || $goodsInfo['is_delete']) {
  164. throwError('很抱歉,商品信息不存在');
  165. }
  166. // 判断商品状态 (上架)
  167. if ($verifyStatus && $goodsInfo['status'] == GoodsStatusEnum::OFF_SALE) {
  168. throwError('很抱歉,当前商品已下架');
  169. }
  170. // 整理商品数据并返回
  171. return $this->setGoodsDataFromApi($goodsInfo);
  172. }
  173. /**
  174. * 根据商品id集获取商品列表
  175. * @param array $goodsIds
  176. * @return mixed
  177. */
  178. public function getListByIdsFromApi(array $goodsIds)
  179. {
  180. // 获取商品列表
  181. $data = $this->getListByIds($goodsIds, GoodsStatusEnum::ON_SALE);
  182. // 整理列表数据并返回
  183. return $this->setGoodsListDataFromApi($data);
  184. }
  185. /**
  186. * 获取商品指定的sku信息并且设置商品的会员价
  187. * @param mixed $goodsInfo 商品信息
  188. * @param string $goodsSkuId 商品SKUID
  189. * @param bool $enableGradeMoney 是否设置会员折扣价
  190. * @return \app\common\model\GoodsSku|array|null
  191. * @throws BaseException
  192. */
  193. public static function getSkuInfo($goodsInfo, string $goodsSkuId, bool $enableGradeMoney = true)
  194. {
  195. $goodsInfo['skuInfo'] = GoodsService::getSkuInfo($goodsInfo['goods_id'], $goodsSkuId);
  196. $enableGradeMoney && (new static)->setGoodsGradeMoney($goodsInfo);
  197. return $goodsInfo['skuInfo'];
  198. }
  199. /**
  200. * 设置商品展示的数据 api模块
  201. * @param $data
  202. * @return mixed
  203. */
  204. private function setGoodsListDataFromApi($data)
  205. {
  206. return $this->setGoodsListData($data, function ($goods) {
  207. // 整理商品数据 api模块
  208. $this->setGoodsDataFromApi($goods);
  209. });
  210. }
  211. /**
  212. * 整理商品数据 api模块
  213. * @param $goodsInfo
  214. * @return mixed
  215. */
  216. private function setGoodsDataFromApi($goodsInfo)
  217. {
  218. return $this->setGoodsData($goodsInfo, function ($goods) {
  219. // 计算并设置商品会员价
  220. $this->getEnableGradeMoney() && $this->setGoodsGradeMoney($goods);
  221. });
  222. }
  223. /**
  224. * 设置商品的会员价
  225. * @param Goods $goods
  226. * @throws BaseException
  227. */
  228. private function setGoodsGradeMoney(self $goods)
  229. {
  230. // 设置当前商品是否使用会员等级折扣价
  231. $goods['is_user_grade'] = false;
  232. // 获取当前登录用户的会员等级信息
  233. $gradeInfo = UserGradeService::getCurrentGradeInfo();
  234. // 判断商品是否参与会员折扣
  235. if (empty($gradeInfo) || !$goods['is_enable_grade']) {
  236. return;
  237. }
  238. // 默认的折扣比例
  239. $discountRatio = $gradeInfo['equity']['discount'];
  240. // 商品单独设置了会员折扣
  241. if ($goods['is_alone_grade'] && isset($goods['alone_grade_equity'][$gradeInfo['grade_id']])) {
  242. $discountRatio = $goods['alone_grade_equity'][$gradeInfo['grade_id']];
  243. }
  244. if (empty($discountRatio)) {
  245. return;
  246. }
  247. // 标记参与会员折扣
  248. $goods['is_user_grade'] = true;
  249. // 会员折扣价: 商品基础价格
  250. $goods['goods_price_min'] = UserGradeService::getDiscountPrice($goods['goods_price_min'], $discountRatio);
  251. $goods['goods_price_max'] = UserGradeService::getDiscountPrice($goods['goods_price_max'], $discountRatio);
  252. // 会员折扣价: 商品sku列表
  253. foreach ($goods['skuList'] as &$skuItem) {
  254. $skuItem['goods_price'] = UserGradeService::getDiscountPrice($skuItem['goods_price'], $discountRatio);
  255. }
  256. // 会员折扣价: 已选择的商品sku(用于购物车)
  257. if ($goods->getAttr('skuInfo')) {
  258. $goods['skuInfo']['goods_price'] = UserGradeService::getDiscountPrice($goods['skuInfo']['goods_price'], $discountRatio);
  259. }
  260. }
  261. }