StoreSeckillController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\api\controller\v1\activity;
  12. use app\Request;
  13. use app\services\activity\seckill\StoreSeckillServices;
  14. use app\services\other\QrcodeServices;
  15. use crmeb\services\GroupDataService;
  16. /**
  17. * 秒杀商品类
  18. * Class StoreSeckillController
  19. * @package app\api\controller\activity
  20. */
  21. class StoreSeckillController
  22. {
  23. protected $services;
  24. public function __construct(StoreSeckillServices $services)
  25. {
  26. $this->services = $services;
  27. }
  28. /**
  29. * 秒杀商品时间区间
  30. * @return mixed
  31. */
  32. public function index()
  33. {
  34. //秒杀时间段
  35. $seckillTime = GroupDataService::getData('routine_seckill_time') ?? [];
  36. $seckillTimeIndex = -1;
  37. $timeCount = count($seckillTime);//总数
  38. $unTimeCunt = 0;//即将开始
  39. if ($timeCount) {
  40. $today = strtotime(date('Y-m-d'));
  41. $currentHour = date('H');
  42. foreach ($seckillTime as $key => &$value) {
  43. $activityEndHour = bcadd((int)$value['time'], (int)$value['continued'], 0);
  44. if ($activityEndHour > 24) {
  45. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  46. $value['state'] = '即将开始';
  47. $value['status'] = 2;
  48. $value['stop'] = (int)bcadd($today, bcmul($activityEndHour, 3600, 0));
  49. } else {
  50. if ($currentHour >= (int)$value['time'] && $currentHour < $activityEndHour) {
  51. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  52. $value['state'] = '抢购中';
  53. $value['stop'] = (int)bcadd($today, bcmul($activityEndHour, 3600, 0));
  54. $value['status'] = 1;
  55. if ($seckillTimeIndex == -1) $seckillTimeIndex = $key;
  56. } else if ($currentHour < (int)$value['time']) {
  57. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  58. $value['state'] = '即将开始';
  59. $value['status'] = 2;
  60. $value['stop'] = (int)bcadd($today, bcmul($activityEndHour, 3600, 0));
  61. $unTimeCunt += 1;
  62. } else if ($currentHour >= $activityEndHour) {
  63. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  64. $value['state'] = '已结束';
  65. $value['status'] = 0;
  66. $value['stop'] = (int)bcadd($today, bcmul($activityEndHour, 3600, 0));
  67. }
  68. }
  69. }
  70. //有时间段但是都不在抢购中
  71. if ($seckillTimeIndex == -1 && $currentHour <= (int)$seckillTime[$timeCount - 1]['time'] ?? 0) {
  72. if ($currentHour < (int)$seckillTime[0]['time'] ?? 0) {//当前时间
  73. $seckillTimeIndex = 0;
  74. } elseif ($unTimeCunt) {//存在未开始的
  75. foreach ($seckillTime as $key => $item) {
  76. if ($item['status'] == 2) {
  77. $seckillTimeIndex = $key;
  78. break;
  79. }
  80. }
  81. } else {
  82. $seckillTimeIndex = $timeCount - 1;
  83. }
  84. }
  85. }
  86. $data['lovely'] = sys_config('seckill_header_banner');
  87. if (strstr($data['lovely'], 'http') === false && strlen(trim($data['lovely']))) $data['lovely'] = sys_config('site_url') . $data['lovely'];
  88. $data['lovely'] = str_replace('\\', '/', $data['lovely']);
  89. $data['seckillTime'] = $seckillTime;
  90. $data['seckillTimeIndex'] = $seckillTimeIndex;
  91. return app('json')->success($data);
  92. }
  93. /**
  94. * 秒杀商品列表
  95. * @param $time
  96. * @return mixed
  97. * @throws \think\db\exception\DataNotFoundException
  98. * @throws \think\db\exception\DbException
  99. * @throws \think\db\exception\ModelNotFoundException
  100. */
  101. public function lst($time)
  102. {
  103. if (!$time) return app('json')->fail(100100);
  104. $seckillInfo = $this->services->getListByTime($time);
  105. return app('json')->success(get_thumb_water($seckillInfo));
  106. }
  107. /**
  108. * 秒杀商品详情
  109. * @param Request $request
  110. * @param $id
  111. * @return mixed
  112. * @throws \think\db\exception\DataNotFoundException
  113. * @throws \think\db\exception\DbException
  114. * @throws \think\db\exception\ModelNotFoundException
  115. */
  116. public function detail(Request $request, $id)
  117. {
  118. $data = $this->services->seckillDetail($request, $id);
  119. return app('json')->success($data);
  120. }
  121. /**
  122. * 获取秒杀小程序二维码
  123. * @param Request $request
  124. * @param $id
  125. * @return mixed
  126. */
  127. public function code(Request $request, $id)
  128. {
  129. /** @var QrcodeServices $qrcodeService */
  130. $qrcodeService = app()->make(QrcodeServices::class);
  131. $url = $qrcodeService->getRoutineQrcodePath($id, $request->uid(), 2);
  132. if ($url) {
  133. return app('json')->success(['code' => $url]);
  134. } else {
  135. return app('json')->success(['code' => '']);
  136. }
  137. }
  138. }