StoreSeckillController.php 5.6 KB

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