StoreSeckillController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace app\api\controller\activity;
  3. use app\admin\model\system\SystemAttachment;
  4. use app\models\store\StoreProductRelation;
  5. use app\models\store\StoreProductReply;
  6. use app\models\store\StoreSeckill;
  7. use app\Request;
  8. use crmeb\services\GroupDataService;
  9. use crmeb\services\SystemConfigService;
  10. use crmeb\services\UtilService;
  11. /**
  12. * 秒杀产品类
  13. * Class StoreSeckillController
  14. * @package app\api\controller\activity
  15. */
  16. class StoreSeckillController
  17. {
  18. /**
  19. * 秒杀产品时间区间
  20. * @return mixed
  21. * @throws \think\db\exception\DataNotFoundException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. * @throws \think\exception\DbException
  24. */
  25. public function index()
  26. {
  27. //秒杀时间段
  28. $seckillTime = GroupDataService::getData('routine_seckill_time') ?? [];
  29. $seckillTimeIndex = 0;
  30. if(count($seckillTime)){
  31. foreach($seckillTime as $key=>&$value){
  32. $currentHour = date('H');
  33. $activityEndHour = bcadd((int)$value['time'],(int)$value['continued'],0);
  34. if($activityEndHour > 24){
  35. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'].':00' : '0'.(int)$value['time'].':00';
  36. $value['state'] = '即将开始';
  37. $value['status'] = 2;
  38. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')),bcmul($activityEndHour,3600,0));
  39. }else{
  40. if($currentHour >= (int)$value['time'] && $currentHour < $activityEndHour){
  41. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'].':00' : '0'.(int)$value['time'].':00';
  42. $value['state'] = '抢购中';
  43. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')),bcmul($activityEndHour,3600,0));
  44. $value['status'] = 1;
  45. if(!$seckillTimeIndex) $seckillTimeIndex = $key;
  46. }else if($currentHour < (int)$value['time']){
  47. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'].':00' : '0'.(int)$value['time'].':00';
  48. $value['state'] = '即将开始';
  49. $value['status'] = 2;
  50. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')),bcmul($activityEndHour,3600,0));
  51. }else if($currentHour >= $activityEndHour){
  52. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'].':00' : '0'.(int)$value['time'].':00';
  53. $value['state'] = '已结束';
  54. $value['status'] = 0;
  55. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')),bcmul($activityEndHour,3600,0));
  56. }
  57. }
  58. }
  59. }
  60. $data['lovely'] = sysConfig('seckill_header_banner');
  61. if(strstr($data['lovely'],'http') === false && strlen(trim($data['lovely']))) $data['lovely'] = sysConfig('site_url').$data['lovely'];
  62. $data['lovely'] = str_replace('\\','/',$data['lovely']);
  63. $data['seckillTime'] = $seckillTime;
  64. $data['seckillTimeIndex'] = $seckillTimeIndex;
  65. return app('json')->successful($data);
  66. }
  67. /**
  68. * 秒杀产品列表
  69. * @param Request $request
  70. * @param $time
  71. * @return mixed
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. * @throws \think\exception\DbException
  75. */
  76. public function lst(Request $request, $time)
  77. {
  78. list($page, $limit) = UtilService::getMore([
  79. ['page',0],
  80. ['limit',0],
  81. ],$request, true);
  82. if(!$time) return app('json')->fail('参数错误');
  83. $timeInfo = GroupDataService::getDataNumber((int)$time);
  84. $activityEndHour = bcadd((int)$timeInfo['time'],(int)$timeInfo['continued'],0);
  85. $startTime = bcadd(strtotime(date('Y-m-d')),bcmul($timeInfo['time'],3600,0));
  86. $stopTime = bcadd(strtotime(date('Y-m-d')),bcmul($activityEndHour,3600,0));
  87. $seckillInfo = StoreSeckill::seckillList($startTime, $stopTime, $page, $limit);
  88. if(count($seckillInfo)){
  89. foreach ($seckillInfo as $key=>&$item){
  90. $percent = (int)bcmul(bcdiv($item['sales'],bcadd($item['stock'],$item['sales'],0),2),100,0);
  91. $item['percent'] = $percent ? $percent : 10;
  92. }
  93. }
  94. return app('json')->successful($seckillInfo);
  95. }
  96. /**
  97. * 秒杀产品详情
  98. * @param Request $request
  99. * @param $id
  100. * @return mixed
  101. */
  102. public function detail(Request $request, $id,$time = 0){
  103. if(!$id || !($storeInfo = StoreSeckill::getValidProduct($id))) return app('json')->fail('商品不存在或已下架!');
  104. $storeInfo = $storeInfo->hidden(['cost','add_time','is_del'])->toArray();
  105. //公众号
  106. $name = $id.'_seckill_detail_wap.jpg';
  107. $imageInfo = SystemAttachment::getInfo($name,'name');
  108. $siteUrl = sysConfig('site_url');
  109. if(!$imageInfo){
  110. $codeUrl = UtilService::setHttpType($siteUrl.'/activity/seckill_detail/'.$id.'/'.$time, 1);//二维码链接
  111. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  112. if(is_array($imageInfo)){
  113. SystemAttachment::attachmentAdd($imageInfo['name'],$imageInfo['size'],$imageInfo['type'],$imageInfo['dir'],$imageInfo['thumb_path'],1,$imageInfo['image_type'],$imageInfo['time'],2);
  114. $url = $imageInfo['dir'];
  115. }else {
  116. $url = '';
  117. $imageInfo = ['image_type'=>0];
  118. }
  119. }else $url = $imageInfo['att_dir'];
  120. if($imageInfo['image_type'] == 1) $url = $siteUrl.$url;
  121. $storeInfo['image'] = UtilService::setSiteUrl($storeInfo['image'], $siteUrl);
  122. $storeInfo['image_base'] = UtilService::setSiteUrl($storeInfo['image'], $siteUrl);
  123. $storeInfo['code_base'] = $url;
  124. $uid = $request->uid();
  125. $storeInfo['userLike'] = StoreProductRelation::isProductRelation($id, $uid, 'like', 'product_seckill');
  126. $storeInfo['like_num'] = StoreProductRelation::productRelationNum($id,'like','product_seckill');
  127. $storeInfo['userCollect'] = StoreProductRelation::isProductRelation($id, $uid,'collect','product_seckill');
  128. $storeInfo['uid'] = $uid;
  129. $data['storeInfo'] = $storeInfo;
  130. setView($uid,$id,$storeInfo['product_id'],'viwe');
  131. $data['reply'] = StoreProductReply::getRecProductReply($storeInfo['product_id']);
  132. $data['replyCount'] = StoreProductReply::productValidWhere()->where('product_id',$storeInfo['product_id'])->count();
  133. return app('json')->successful($data);
  134. }
  135. }