StoreSeckillController.php 7.2 KB

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