StoreSeckillController.php 8.1 KB

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