Goods.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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\controller;
  13. use app\api\model\Cart as CartModel;
  14. use app\api\model\Goods as GoodsModel;
  15. use app\api\service\User as UserService;
  16. use app\common\enum\goods\GoodsType;
  17. use app\common\library\helper;
  18. use app\common\model\BrowseRecords;
  19. use app\store\model\qc\QcMjSendActivity;
  20. use app\common\service\delivery\Express as ExpressService;
  21. use app\api\model\Category;
  22. use app\api\model\GoodsCategoryRel;
  23. use app\api\model\member\MemberGoods as MemberGoodsModel;
  24. use app\api\model\groupbuylb\GroupBuyLbActivity;
  25. use app\common\model\ms\MsActivity as MsActivityModel;
  26. use app\api\model\Order as OrderModel;
  27. use app\api\model\za\ZaActivity as ZaActivityModel;
  28. /**
  29. * 商品控制器
  30. * Class Goods
  31. * @package app\api\controller
  32. */
  33. class Goods extends Controller
  34. {
  35. /**
  36. * 商品列表`
  37. *
  38. * @return array
  39. * @throws \think\db\exception\DbException
  40. */
  41. public function list()
  42. {
  43. // 获取列表数据
  44. $model = new GoodsModel;
  45. $list = $model->getList($this->request->param());
  46. $goodsType = $this->request->param('goods_type', null);
  47. foreach ($list as $item){
  48. if ($goodsType == GoodsType::EXCHANGE){
  49. $item['za'] = null;
  50. $item['miaosha'] = null;
  51. continue;
  52. }
  53. //买一赠一
  54. $za = (new ZaActivityModel())->getZaGood($item['goods_id']);
  55. if(isset($za)){
  56. $item['za'] = $za;
  57. }else{
  58. $item['za'] = null;
  59. }
  60. $miaosha = (new MsActivityModel)->getMsPrice($item['goods_id'],$item['goods_price_min']);
  61. $item['miaosha'] = $miaosha;
  62. }
  63. return $this->renderSuccess(compact('list'));
  64. }
  65. public function ricelist(){
  66. // 获取列表数据
  67. $model = new GoodsModel;
  68. $category = Category::with(['goods'])->where("status",1)->where("type",2)->select();
  69. $goods_id_array = [];
  70. foreach($category as $row){
  71. $goods = $row['goods'];
  72. foreach($goods as $row){
  73. $goods_id_array[] = $row['goods_id'];
  74. }
  75. }
  76. $param = array_merge($this->request->param(),['goods_id_arr'=>$goods_id_array]);
  77. $list = $model->getRiceList($param);
  78. // return $category;
  79. return $this->renderSuccess(compact('list'));
  80. }
  81. /**
  82. * 获取商品详情
  83. * @param int $id
  84. * @return array
  85. */
  86. public function detail(int $id, int $addressId = 0, int $staffUserId = 0)
  87. {
  88. $user = UserService::getCurrentLoginUser();
  89. // 商品详情
  90. $model = new GoodsModel;
  91. $goodsInfo = $model->getDetails($id, $user, $addressId, $staffUserId);
  92. $goodsInfo->inc('view_num', 1)->update();
  93. if ($user) {
  94. $browseRecord = BrowseRecords::whereDay('create_time')->where(['user_id' => $user->user_id, 'source_type' => 1, 'source_id' => $id])->find();
  95. if (!$browseRecord) {
  96. BrowseRecords::create([
  97. 'user_id' => $user->user_id,
  98. 'source_type' => 1,
  99. 'source_id' => $id
  100. ]);
  101. $goodsInfo->inc('view_distinct_num', 1)->update();
  102. } else {
  103. $browseRecord->update_time = time();
  104. $browseRecord->save();
  105. }
  106. }
  107. $member = MemberGoodsModel::where('goods_id',$id)->where("status",0)->find();
  108. $goodsInfoArray = $goodsInfo->toArray();
  109. $goodsInfoArray['member_price'] = (new MemberGoodsModel)->getGoodsMemberPrice($id,$goodsInfoArray['goods_price_min']);
  110. $groupbuylb = new GroupBuyLbActivity;
  111. $group_buy = $groupbuylb->getGroupBuyLbPrice($id);
  112. $goodsInfoArray['group_buy'] = $group_buy;
  113. //秒杀
  114. $miaosha = (new MsActivityModel)->getMsPrice($id,$goodsInfoArray['goods_price_min']);
  115. if(isset($miaosha)&&$miaosha['type']==4){
  116. $goodsInfoArray['coupon_list'] = [];//秒杀中不返回优惠券
  117. }
  118. if(isset($miaosha)){
  119. $user_id = $user->user_id??0;
  120. if($user_id==0){
  121. $have_buy = 0;
  122. }else{
  123. $have_buy =(new OrderModel())->miaoshaHaveBuyCnt($miaosha['ms_id'],$user_id);
  124. }
  125. //还能买多少件
  126. $miaosha['can_buy_mount'] = $miaosha['limit_mount'] - $have_buy;
  127. }
  128. //买一赠一
  129. $za = (new ZaActivityModel())->getZaGood($id);
  130. if(isset($za)){
  131. $goodsInfoArray['za'] = $za;
  132. }
  133. $goodsInfoArray['miaosha'] = $miaosha;
  134. return $this->renderSuccess($goodsInfoArray);
  135. }
  136. /**
  137. * Notes:计算当前配送模板的运费
  138. * Author: zhangs
  139. * DateTime: 2021/9/24 20:28
  140. * @return array
  141. */
  142. public function calcDeliveryAmount()
  143. {
  144. $params = $this->request->param();
  145. $model = new GoodsModel();
  146. $goodsList = $model->getOrderGoodsListForDelivery(
  147. (int)$params['goodsId'],
  148. (string)$params['goodsSkuId'],
  149. (int)$params['goodsNum']
  150. );
  151. // 设置默认数据:配送费用
  152. helper::setDataAttribute($goodsList, [
  153. 'expressPrice' => 0,
  154. ], true);
  155. // 当前用户收货城市id
  156. $user = UserService::getCurrentLoginUser();
  157. $cityId = $user['address_default'] ? (int)$user['address_default']['city_id'] : 0;
  158. // 初始化配送服务类
  159. $ExpressService = new ExpressService($cityId, $goodsList);
  160. $isIntraRegion = true;
  161. // 验证商品是否在限购配送范围
  162. $isDeliveryLimit = $ExpressService->isDeliveryLimit();
  163. if ($isDeliveryLimit == false) {
  164. $isIntraRegion = false;
  165. } else {
  166. // 验证商品是否在配送范围
  167. $isIntraRegion = $ExpressService->isIntraRegion();
  168. if ($cityId > 0 && $isIntraRegion == false) {
  169. $isIntraRegion = false;
  170. }
  171. }
  172. // 验证商品是否在配送范围
  173. // $isIntraRegion = $ExpressService->isIntraRegion();
  174. // if ($cityId > 0 && $isIntraRegion == false) {
  175. // $notInRuleGoodsName = $ExpressService->getNotInRuleGoodsName();
  176. // return $this->renderError("很抱歉,您的收货地址不在商品 [".limit_str((string)$notInRuleGoodsName)."] 的配送范围内");
  177. // }
  178. // 订单总运费金额
  179. $expressPrice = $ExpressService->getDeliveryFee();
  180. return $this->renderSuccess(compact('isIntraRegion', 'expressPrice'));
  181. }
  182. /**
  183. * 全场满件赠活动数据
  184. *
  185. * @param int $act_id
  186. * @return array|\think\response\Json
  187. */
  188. public function qcMjSendActData(int $act_id)
  189. {
  190. $now = date('Y-m-d H:i:s');
  191. $filter[] = ['start_time', '<', $now];
  192. $filter[] = ['end_time', '>', $now];
  193. $filter[] = ['audit_status', '=', 1];
  194. $filter[] = ['status', '=', 1];
  195. $filter[] = ['id', '=', $act_id];
  196. $activity = QcMjSendActivity::detail($filter,['image','goodsExcept','giftGoods']);
  197. if(empty($activity)){
  198. return $this->renderError("活动不存在");
  199. }
  200. $activity = $activity->toArray();
  201. $activity['activity_status'] = $activity['activity_status']['code'];
  202. $activity['image_url'] = $activity['image']['preview_url'] ?? '';
  203. $cartModel = new CartModel();
  204. $activity['cart_total'] = $cartModel->getCartTotal();
  205. $cartGoods = $cartModel->getCartGoodsIdNums();
  206. if (!empty($activity['goodsExcept'])) {
  207. $num = 0;
  208. $mj_props = array_values(array_sort($activity['mj_props'], 'value'));
  209. foreach ($activity['goodsExcept'] as $key=>&$item) {
  210. if($item['residue_stock']<=0){
  211. unset($activity['goodsExcept'][$key]);
  212. }else{
  213. $num += $item['residue_stock'];
  214. }
  215. $item['cart_num'] = 0;
  216. if (isset($cartGoods[$item['goods_id']])) {
  217. $item['cart_num'] = $cartGoods[$item['goods_id']];
  218. }
  219. }
  220. if($num>0 && $mj_props[0]['value']>$num){//剩余数量小于最小的赠送
  221. $activity['goodsExcept'] = [];
  222. }
  223. }
  224. if (!empty($activity['giftGoods'])) {
  225. foreach ($activity['giftGoods'] as &$item) {
  226. $item['cart_num'] = 0;
  227. if (isset($cartGoods[$item['goods_id']])) {
  228. $item['cart_num'] = $cartGoods[$item['goods_id']];
  229. }
  230. }
  231. }
  232. return $this->renderSuccess($activity);
  233. }
  234. }