Goods.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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\GoodsSku as GoodsSkuModel;
  16. use app\api\model\GoodsSpecRel as GoodsSpecRelModel;
  17. use app\common\model\Goods as GoodsModel;
  18. use app\common\enum\goods\Status as GoodsStatusEnum;
  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. // 关联查询(商品图片)
  132. $with = ['images.file'];
  133. // 获取商品记录
  134. return $this->getGoodsMain($goodsId, $with, $verifyStatus);
  135. }
  136. /**
  137. * 获取商品规格数据
  138. * @param int $goodsId
  139. * @return array
  140. * @throws \think\db\exception\DataNotFoundException
  141. * @throws \think\db\exception\DbException
  142. * @throws \think\db\exception\ModelNotFoundException
  143. */
  144. public function getSpecData(int $goodsId): array
  145. {
  146. $data = [];
  147. // 商品SKU列表
  148. $data['skuList'] = GoodsSkuModel::getSkuList($goodsId, true);
  149. // 商品规格列表
  150. $data['specList'] = GoodsSpecRelModel::getSpecList($goodsId);
  151. return $data;
  152. }
  153. /**
  154. * 获取商品主体信息
  155. * @param int $goodsId 商品ID
  156. * @param array $with 关联查询
  157. * @param bool $verifyStatus 是否验证商品状态(上架)
  158. * @return mixed
  159. * @throws BaseException
  160. */
  161. private function getGoodsMain(int $goodsId, array $with = [], bool $verifyStatus = true)
  162. {
  163. // 获取商品记录
  164. $goodsInfo = static::detail($goodsId, $with);
  165. // 判断商品是否存在
  166. if (empty($goodsInfo) || $goodsInfo['is_delete']) {
  167. throwError('很抱歉,商品信息不存在');
  168. }
  169. // 判断商品状态(上架)
  170. if ($verifyStatus && $goodsInfo['status'] == GoodsStatusEnum::OFF_SALE) {
  171. throwError('很抱歉,当前商品已下架');
  172. }
  173. // 整理商品数据并返回
  174. return $this->setGoodsDataFromApi($goodsInfo);
  175. }
  176. /**
  177. * 根据商品id集获取商品列表
  178. * @param array $goodsIds
  179. * @return mixed
  180. */
  181. public function getListByIdsFromApi(array $goodsIds)
  182. {
  183. // 获取商品列表
  184. $data = $this->getListByIds($goodsIds, GoodsStatusEnum::ON_SALE);
  185. // 整理列表数据并返回
  186. return $this->setGoodsListDataFromApi($data);
  187. }
  188. /**
  189. * 获取商品指定的sku信息并且设置商品的会员价
  190. * @param mixed $goodsInfo 商品信息
  191. * @param string $goodsSkuId 商品SKUID
  192. * @param bool $enableGradeMoney 是否设置会员折扣价
  193. * @return \app\common\model\GoodsSku|array|null
  194. * @throws BaseException
  195. */
  196. public static function getSkuInfo($goodsInfo, string $goodsSkuId, bool $enableGradeMoney = true)
  197. {
  198. $goodsInfo['skuInfo'] = GoodsService::getSkuInfo($goodsInfo['goods_id'], $goodsSkuId);
  199. $enableGradeMoney && (new static)->setGoodsGradeMoney($goodsInfo);
  200. return $goodsInfo['skuInfo'];
  201. }
  202. /**
  203. * 设置商品展示的数据 api模块
  204. * @param $data
  205. * @return mixed
  206. */
  207. private function setGoodsListDataFromApi($data)
  208. {
  209. return $this->setGoodsListData($data, function ($goods) {
  210. // 整理商品数据 api模块
  211. $this->setGoodsDataFromApi($goods);
  212. });
  213. }
  214. /**
  215. * 整理商品数据 api模块
  216. * @param $goodsInfo
  217. * @return mixed
  218. */
  219. private function setGoodsDataFromApi($goodsInfo)
  220. {
  221. return $this->setGoodsData($goodsInfo, function ($goods) {
  222. // 计算并设置商品会员价
  223. $this->getEnableGradeMoney() && $this->setGoodsGradeMoney($goods);
  224. });
  225. }
  226. /**
  227. * 设置商品的会员价
  228. * @param Goods $goods
  229. * @throws BaseException
  230. */
  231. private function setGoodsGradeMoney(self $goods)
  232. {
  233. // 设置当前商品是否使用会员等级折扣价
  234. $goods['is_user_grade'] = false;
  235. // 获取当前登录用户的会员等级信息
  236. $gradeInfo = UserGradeService::getCurrentGradeInfo();
  237. // 判断商品是否参与会员折扣
  238. if (empty($gradeInfo) || !$goods['is_enable_grade']) {
  239. return;
  240. }
  241. // 默认的折扣比例
  242. $discountRatio = $gradeInfo['equity']['discount'];
  243. // 商品单独设置了会员折扣
  244. if ($goods['is_alone_grade'] && isset($goods['alone_grade_equity'][$gradeInfo['grade_id']])) {
  245. $discountRatio = $goods['alone_grade_equity'][$gradeInfo['grade_id']];
  246. }
  247. if (empty($discountRatio)) {
  248. return;
  249. }
  250. // 标记参与会员折扣
  251. $goods['is_user_grade'] = true;
  252. // 会员折扣价: 商品基础价格
  253. $goods['goods_price_min'] = UserGradeService::getDiscountPrice($goods['goods_price_min'], $discountRatio);
  254. $goods['goods_price_max'] = UserGradeService::getDiscountPrice($goods['goods_price_max'], $discountRatio);
  255. // 会员折扣价: 商品sku列表
  256. if ($goods->getRelation('skuList')) {
  257. foreach ($goods['skuList'] as &$skuItem) {
  258. $skuItem['goods_price'] = UserGradeService::getDiscountPrice($skuItem['goods_price'], $discountRatio);
  259. }
  260. }
  261. // 会员折扣价: 已选择的商品sku(用于购物车)
  262. if ($goods->getAttr('skuInfo')) {
  263. $goods['skuInfo']['goods_price'] = UserGradeService::getDiscountPrice($goods['skuInfo']['goods_price'], $discountRatio);
  264. }
  265. }
  266. }