StoreExchangeController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\StoreExchange;
  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 StoreExchangeController
  21. {
  22. /**
  23. * 秒杀产品列表
  24. * @param Request $request
  25. * @param $time
  26. * @return mixed
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\ModelNotFoundException
  29. * @throws \think\db\exception\DbException
  30. */
  31. public function lst(Request $request)
  32. {
  33. list($page, $limit, $money_type) = UtilService::getMore([
  34. ['page', 0],
  35. ['limit', 0],
  36. ['money_type', ''],
  37. ], $request, true);
  38. $seckillInfo = StoreExchange::exchangeList($money_type, $page, $limit);
  39. if (count($seckillInfo)) {
  40. foreach ($seckillInfo as $key => &$item) {
  41. if ($item['quota'] > 0) {
  42. $quota = StoreProductAttrValue::where('product_id', $item['id'])->where('type', 4)->value('SUM(quota)');
  43. $percent = (int)bcmul(bcdiv(bcsub($item['quota'], $quota), $item['quota'], 2), 100, 0);
  44. $item['percent'] = $percent;
  45. $item['stock'] = $quota;
  46. $item['money_type'] = get_money_name($item['money_type']);
  47. } else {
  48. $item['percent'] = 100;
  49. $item['stock'] = 0;
  50. $item['money_type'] = get_money_name($item['money_type']);
  51. }
  52. }
  53. }
  54. return app('json')->successful($seckillInfo);
  55. }
  56. /**
  57. * 秒杀产品详情
  58. * @param Request $request
  59. * @param $id
  60. * @return mixed
  61. */
  62. public function detail(Request $request, $id)
  63. {
  64. if (!$id || !($storeInfo = StoreExchange::getValidProduct($id))) return app('json')->fail('商品不存在或已下架!');
  65. $storeInfo = $storeInfo->hidden(['cost', 'add_time', 'is_del'])->toArray();
  66. $siteUrl = sys_config('site_url');
  67. $storeInfo['image'] = set_file_url($storeInfo['image'], $siteUrl);
  68. $storeInfo['image_base'] = set_file_url($storeInfo['image'], $siteUrl);
  69. $storeInfo['code_base'] = QrcodeService::getWechatQrcodePath($id . '_exchange_detail_wap.jpg', '/activity/exchange_detail/' . $id);
  70. $uid = $request->uid();
  71. $storeInfo['uid'] = $uid;
  72. $storeInfo['money_type'] = get_money_name($storeInfo['money_type']);
  73. $storeInfo['description'] = htmlspecialchars_decode(StoreDescription::getDescription($id, 4));
  74. $data['storeInfo'] = $storeInfo;
  75. StoreVisit::setView($uid, $id, $storeInfo['product_id'], 'viwe');
  76. $data['reply'] = StoreProductReply::getRecProductReply($storeInfo['product_id']);
  77. $data['replyCount'] = StoreProductReply::productValidWhere()->where('product_id', $storeInfo['product_id'])->count();
  78. if ($data['replyCount']) {
  79. $goodReply = StoreProductReply::productValidWhere()->where('product_id', $storeInfo['product_id'])->where('product_score', 5)->count();
  80. $data['replyChance'] = $goodReply;
  81. if ($goodReply) {
  82. $data['replyChance'] = bcdiv($goodReply, $data['replyCount'], 2);
  83. $data['replyChance'] = bcmul($data['replyChance'], 100, 3);
  84. }
  85. } else $data['replyChance'] = 0;
  86. list($productAttr, $productValue) = StoreProductAttr::getProductAttrDetail($id, $uid, 0, 4);
  87. foreach ($productValue as $k => $v) {
  88. $productValue[$k]['product_stock'] = StoreProductAttrValue::where('product_id', $storeInfo['product_id'])->where('suk', $v['suk'])->where('type', 0)->value('stock');
  89. $productValue[$k]['money_type_name'] = get_money_name($productValue[$k]['money_type']);
  90. }
  91. $data['productAttr'] = $productAttr;
  92. $data['productValue'] = $productValue;
  93. return app('json')->successful($data);
  94. }
  95. }