Goods.php 9.6 KB

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