Goods.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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($goodsId = null)
  86. {
  87. if (empty($goodsId)){
  88. return self::field('goods_id')->where('is_delete', '=', 0)
  89. ->select();
  90. }else{
  91. return self::field('goods_id')->where('goods_id', $goodsId)->where('is_delete', '=', 0)
  92. ->select();
  93. }
  94. }
  95. /**
  96. * 获取商品详情 (详细数据用于页面展示)
  97. * @param int $goodsId 商品ID
  98. * @param bool $verifyStatus 是否验证商品状态(上架)
  99. * @return mixed
  100. * @throws BaseException
  101. * @throws \think\db\exception\DataNotFoundException
  102. * @throws \think\db\exception\DbException
  103. * @throws \think\db\exception\ModelNotFoundException
  104. */
  105. public function getDetails(int $goodsId, bool $verifyStatus = true)
  106. {
  107. // 关联查询(商品图片、sku列表)
  108. $with = ['images.file', 'skuList.image', 'video', 'videoCover'];
  109. // 获取商品记录
  110. $goodsInfo = $this->getGoodsMain($goodsId, $with, $verifyStatus);
  111. if(empty($goodsInfo['goods_id'])){
  112. return $goodsInfo;
  113. }
  114. // 商品规格列表
  115. $goodsInfo['specList'] = GoodsSpecRelModel::getSpecList($goodsInfo['goods_id']);
  116. return $goodsInfo->hidden(array_merge($this->hidden, ['images']));
  117. }
  118. /**
  119. * 获取商品详情 (仅包含主商品信息和商品图片)
  120. * @param int $goodsId 商品ID
  121. * @param bool $verifyStatus 是否验证商品状态(上架)
  122. * @return mixed
  123. * @throws BaseException
  124. */
  125. public function getBasic(int $goodsId, bool $verifyStatus = true)
  126. {
  127. // 关联查询(商品图片)
  128. $with = ['images.file'];
  129. // 获取商品记录
  130. return $this->getGoodsMain($goodsId, $with, $verifyStatus);
  131. }
  132. /**
  133. * 获取商品规格数据
  134. * @param int $goodsId
  135. * @return array
  136. * @throws \think\db\exception\DataNotFoundException
  137. * @throws \think\db\exception\DbException
  138. * @throws \think\db\exception\ModelNotFoundException
  139. */
  140. public function getSpecData(int $goodsId): array
  141. {
  142. $data = [];
  143. // 商品SKU列表
  144. $data['skuList'] = GoodsSkuModel::getSkuList($goodsId);
  145. // 商品规格列表
  146. $data['specList'] = GoodsSpecRelModel::getSpecList($goodsId);
  147. return $data;
  148. }
  149. /**
  150. * 获取商品主体信息
  151. * @param int $goodsId 商品ID
  152. * @param array $with 关联查询
  153. * @param bool $verifyStatus 是否验证商品状态(上架)
  154. * @return mixed
  155. * @throws BaseException
  156. */
  157. private function getGoodsMain(int $goodsId, array $with = [], bool $verifyStatus = true)
  158. {
  159. // 获取商品记录
  160. $goodsInfo = static::detail($goodsId, $with);
  161. // 判断商品是否存在
  162. if (empty($goodsInfo) || $goodsInfo['is_delete']) {
  163. return throwErrorInfo('Non-existent goods.');
  164. }
  165. // 判断商品状态(上架)
  166. if ($verifyStatus && $goodsInfo['status'] == GoodsStatusEnum::OFF_SALE) {
  167. return throwErrorInfo('Products that have been removed from the shelves.');
  168. }
  169. // 整理商品数据并返回
  170. return $this->setGoodsDataFromApi($goodsInfo);
  171. }
  172. /**
  173. * 根据商品id集获取商品列表
  174. * @param array $goodsIds
  175. * @return mixed
  176. */
  177. public function getListByIdsFromApi(array $goodsIds)
  178. {
  179. // 获取商品列表
  180. $data = $this->getListByIds($goodsIds, GoodsStatusEnum::ON_SALE);
  181. // 整理列表数据并返回
  182. return $this->setGoodsListDataFromApi($data);
  183. }
  184. /**
  185. * 获取商品指定的sku信息并且设置商品的会员价
  186. * @param mixed $goodsInfo 商品信息
  187. * @param string $goodsSkuId 商品SKUID
  188. * @param bool $isGoodsGradeMoney 是否设置会员折扣价
  189. * @return \app\common\model\GoodsSku|array|null
  190. * @throws BaseException
  191. */
  192. public static function getSkuInfo($goodsInfo, string $goodsSkuId, bool $isGoodsGradeMoney = true)
  193. {
  194. $goodsInfo['skuInfo'] = GoodsService::getSkuInfo($goodsInfo['goods_id'], $goodsSkuId);
  195. $isGoodsGradeMoney && (new static)->setGoodsGradeMoney($goodsInfo);
  196. return $goodsInfo['skuInfo'];
  197. }
  198. /**
  199. * 设置商品展示的数据 api模块
  200. * @param $data
  201. * @return mixed
  202. */
  203. private function setGoodsListDataFromApi($data)
  204. {
  205. return $this->setGoodsListData($data, function ($goods) {
  206. // 整理商品数据 api模块
  207. $this->setGoodsDataFromApi($goods);
  208. });
  209. }
  210. /**
  211. * 整理商品数据 api模块
  212. * @param $goodsInfo
  213. * @return mixed
  214. */
  215. private function setGoodsDataFromApi($goodsInfo)
  216. {
  217. return $this->setGoodsData($goodsInfo, function ($goods) {
  218. // 计算并设置商品会员价
  219. $this->isGoodsGradeMoney && $this->setGoodsGradeMoney($goods);
  220. });
  221. }
  222. /**
  223. * 设置商品的会员价
  224. * @param Goods $goods
  225. * @throws BaseException
  226. */
  227. private function setGoodsGradeMoney(self $goods)
  228. {
  229. // 设置当前商品是否使用会员等级折扣价
  230. $goods['is_user_grade'] = false;
  231. // 获取当前登录用户的会员等级信息
  232. $gradeInfo = UserGradeService::getCurrentGradeInfo();
  233. // 判断商品是否参与会员折扣
  234. if (empty($gradeInfo) || !$goods['is_enable_grade']) {
  235. return;
  236. }
  237. // 默认的折扣比例
  238. $discountRatio = $gradeInfo['equity']['discount'];
  239. // 商品单独设置了会员折扣
  240. if ($goods['is_alone_grade'] && isset($goods['alone_grade_equity'][$gradeInfo['grade_id']])) {
  241. $discountRatio = $goods['alone_grade_equity'][$gradeInfo['grade_id']];
  242. }
  243. if (empty($discountRatio)) {
  244. return;
  245. }
  246. // 标记参与会员折扣
  247. $goods['is_user_grade'] = true;
  248. // 会员折扣价: 商品基础价格
  249. $goods['goods_price_min'] = UserGradeService::getDiscountPrice($goods['goods_price_min'], $discountRatio);
  250. $goods['goods_price_max'] = UserGradeService::getDiscountPrice($goods['goods_price_max'], $discountRatio);
  251. // 会员折扣价: 商品sku列表
  252. if ($goods->getRelation('skuList')) {
  253. foreach ($goods['skuList'] as &$skuItem) {
  254. $skuItem['goods_price'] = UserGradeService::getDiscountPrice($skuItem['goods_price'], $discountRatio);
  255. }
  256. }
  257. // 会员折扣价: 已选择的商品sku(用于购物车)
  258. if ($goods->getAttr('skuInfo')) {
  259. $goods['skuInfo']['goods_price'] = UserGradeService::getDiscountPrice($goods['skuInfo']['goods_price'], $discountRatio);
  260. }
  261. }
  262. }