Goods.php 9.2 KB

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