diff --git a/addon/futures/api/controller/Futures.php b/addon/futures/api/controller/Futures.php index 80a66e7c..0522e792 100644 --- a/addon/futures/api/controller/Futures.php +++ b/addon/futures/api/controller/Futures.php @@ -14,48 +14,29 @@ class Futures extends BaseApi{ $token = $this->checkToken(); if ($token['code'] < 0) return $this->response($token); - //判断当前时间是否在秒杀开启时间,并区分有无钻石用户 - $status = input('status', 'miaosha'); $condition = [ ['a.site_id','=',$this->site_id] ]; + $futuresModel = new FuturesModel; switch($status){ case 'miaosha': $condition[] = ['a.status', '=', '2']; - $startTimeKey = 'miaosha_start_time'; - $endTimeKey = 'miaosha_end_time'; - $fenxiao = model('fenxiao')->getInfo([['member_id', '=', $this->member_id]]); - if(!empty($fenxiao) && $fenxiao['diamond'] > 0){ - $startTimeKey = 'miaosha_early_start_time'; - } + $times = $futuresModel->checkTimes($this->site_id, $this->member_id, 2); break; case 'jianlou': $condition[] = ['a.status', '=', '7']; - $startTimeKey = 'jianlou_start_time'; - $endTimeKey = 'jianlou_end_time'; + $times = $futuresModel->checkTimes($this->site_id, $this->member_id, 7); break; default: return $this->response($this->error('','参数不合法')); break; } - $nowTime = time() - strtotime("today"); - $is_read = FALSE; - - $futuresModel = new FuturesModel; - $basics = $futuresModel->getBasicsConfig($this->site_id)['data']['value']; - foreach($basics['times'] as $times){ - if($times[$startTimeKey] <= $nowTime && $times[$endTimeKey] >= $nowTime){ - $is_read = TRUE; - break; - } - } - - if(!$is_read){ - return $this->response($this->error('','未到秒杀时间')); + if($times['code'] != 0){ + return $this->response($times); } // 获取内容 @@ -85,6 +66,7 @@ class Futures extends BaseApi{ $page = input('page', 1); $page_size = input('page_size', PAGE_LIST_ROWS); $list = $futuresModel->getPageList($condition, $page, $page_size, 'a.created_time desc,a.id desc', $field, 'a', $join); + $list['data']['times'] = $times['data']; return $this->response($list); } @@ -164,12 +146,15 @@ class Futures extends BaseApi{ if(empty($date)){ return $this->response($this->error('','发布失败')); } - if($price <= $date['unit_price'] * (1 + 0 / 100) || $price >= $date['unit_price'] * (1 + 8 / 100)){//TODO 后台配置范围 + + $futuresModel = new FuturesModel; + $basicsConfig = $futuresModel->getBasicsConfig($this->site_id)['data']['value']; + if($price < $date['unit_price'] * (1 + $basicsConfig['price_range']['min'] / 100) || $price >= $date['unit_price'] * (1 + $basicsConfig['price_range']['max'] / 100)){// 后台配置范围 return $this->response($this->error('','发布失败,价格不在允许范围内')); } //TODO 拆单 + // $futuresInfo = $futuresModel->detail($date); - $futuresModel = new FuturesModel; return $this->response($futuresModel->release($id, $price)); } @@ -191,8 +176,26 @@ class Futures extends BaseApi{ $futuresModel = new FuturesModel; return $this->response($futuresModel->stock($id)); } + /** + * 提货 + * @return false|string + * @throws \Psr\SimpleCache\InvalidArgumentException + */ + public function pickUp(){ + $token = $this->checkToken(); + if ($token['code'] < 0) return $this->response($token); - //TODO 提货 + $id = input('id', 0); + + $date = model('futures')->getInfo([['id', '=', $id],['seller_uid', '=', $this->member_id]]); + if(empty($date)){ + return $this->response($this->error('','提货失败')); + } + //TODO 提货用来减少用户买入限制 + + $futuresModel = new FuturesModel; + return $this->response($futuresModel->pickUp($id)); + } /** * 详情 * @return false|string @@ -206,7 +209,23 @@ class Futures extends BaseApi{ $futuresModel = new FuturesModel; $detail = $futuresModel->detail($id, $this->site_id); + $times = $futuresModel->checkTimes($this->site_id, $this->member_id, $detail['data']['status']); + if($times['code'] != 0){ + return $this->response($times); + } + $detail['data']['times'] = $times; return $this->response($detail); } - + /** + * 获取设置 + * @return false|string + */ + public function getBasicsConfig(){ + $futuresModel = new FuturesModel; + $basicsConfig = $futuresModel->getBasicsConfig($this->site_id)['data']['value']; + $res = [ + 'price_range' => $basicsConfig['price_range'] + ]; + return $this->response($this->success($res)); + } } \ No newline at end of file diff --git a/addon/futures/config/menu_shop.php b/addon/futures/config/menu_shop.php index 0da44ae6..21b9492f 100644 --- a/addon/futures/config/menu_shop.php +++ b/addon/futures/config/menu_shop.php @@ -27,6 +27,13 @@ return [ 'is_show' => 1, 'sort' => 2, ], + [ + 'name' => 'TRADING_MARKET_CONFIG', + 'title' => '基础设置', + 'url' => 'futures://shop/config/basics', + 'is_show' => 1, + 'sort' => 2, + ], diff --git a/addon/futures/model/Futures.php b/addon/futures/model/Futures.php index 05f55817..506ef5b2 100644 --- a/addon/futures/model/Futures.php +++ b/addon/futures/model/Futures.php @@ -3,8 +3,8 @@ namespace addon\futures\model; use app\model\system\Config as ConfigModel; use app\model\BaseModel; +use Psr\SimpleCache\InvalidArgumentException; use think\facade\Cache; -use think\facade\Db; class Futures extends BaseModel{ @@ -63,16 +63,35 @@ class Futures extends BaseModel{ $detail = model('futures')->getInfo($condition, $field, 'a', $join); return $this->success($detail); } - + /** + * 增加库存 + * @param $data + * @return array + * @throws InvalidArgumentException + */ + public function add($data){ + $id = model('futures')->add($data); + if(!($id > 0)){ + return $this->error('','添加失败'); + } + Cache::store('redis_concurrent')->set('addon_futures_stock_'.$id,'1'); + return $this->success($id); + } /** * 发布 + * @param $id + * @param $price * @return array + * @throws InvalidArgumentException */ public function release($id, $price){ $date = model('futures')->getInfo([['id', '=', $id]]); if(empty($date) || $date['status'] !== 1){ return $this->error('','发布失败'); } + if(!Cache::store('redis_concurrent')->delete('addon_futures_stock_'.$id)){ + return $this->error('','发布失败'); + } model('futures')->update([ 'status' => 2, 'release_time' => time(), @@ -99,6 +118,25 @@ class Futures extends BaseModel{ ], [['id', '=', $id]]); return $this->success(); } + /** + * 提货 + * @param $id + * @return array + * @throws InvalidArgumentException + */ + public function pickUp($id){ + $date = model('futures')->getInfo([['id', '=', $id]]); + if(empty($date) || $date['status'] !== 1){ + return $this->error($date,'提货失败'); + } + if(!Cache::store('redis_concurrent')->delete('addon_futures_stock_'.$id)){ + return $this->error('','提货失败'); + } + model('futures')->update([ + 'status' => 5, + ], [['id', '=', $id]]); + return $this->success(); + } /** * 订单创建 @@ -139,7 +177,7 @@ class Futures extends BaseModel{ 'status' => 3, 'sell_time' => time() ], [['id', '=', $id]]); - model('futures')->add([ + $this->add([ 'site_id' => $date['site_id'], 'seller_uid' => $date['member_id'], 'old_futures_id' => $id, @@ -193,4 +231,51 @@ class Futures extends BaseModel{ $config = new ConfigModel(); return $config->getConfig([['site_id', '=', $site_id], ['app_module', '=', 'shop'], ['config_key', '=', 'MIAOSHA_BASICS_CONFIG']]); } + /** + * 判断当前时间是否在秒杀开启时间,并区分有无钻石用户 + * @param $site_id + * @param $member_id + * @param $status + * @return array + */ + public function checkTimes($site_id, $member_id, $status = 2){ + switch($status){ + case '2': + $startTimeKey = 'miaosha_start_time'; + $endTimeKey = 'miaosha_end_time'; + $fenxiao = model('fenxiao')->getInfo([['member_id', '=', $member_id]]); + if(!empty($fenxiao) && $fenxiao['diamond'] > 0){ + $startTimeKey = 'miaosha_early_start_time'; + } + break; + case '7': + $startTimeKey = 'jianlou_start_time'; + $endTimeKey = 'jianlou_end_time'; + break; + default: + return $this->error('','参数不合法'); + break; + } + + $nowTime = time() - strtotime("today"); + $is_read = FALSE; + $nowtimes = []; + + $basics = $this->getBasicsConfig($site_id)['data']['value']; + foreach($basics['times'] as $times){ + if($times[$startTimeKey] <= $nowTime && $times[$endTimeKey] >= $nowTime){ + $is_read = TRUE; + $nowtimes = [ + 'start_time' => $times[$startTimeKey], + 'end_time' => $times[$endTimeKey] + ]; + break; + } + } + + if(!$is_read){ + return $this->error('','未到秒杀时间'); + } + return $this->success($nowtimes); + } } \ No newline at end of file diff --git a/addon/futures/model/User.php b/addon/futures/model/User.php index 427b7dcf..0c939993 100644 --- a/addon/futures/model/User.php +++ b/addon/futures/model/User.php @@ -109,10 +109,11 @@ class User extends BaseModel{ if($memberId <= 0) return $this->error('','用户信息错误!'); // 赠送商品 try{ + $futuresModel = new Futures(); $time = time(); - $insert = []; + // $insert = []; for($i=0;$i<$info['order_num'];$i++){ - $insert[] = [ + $futuresModel->add([ 'site_id' => $this->site_id, 'seller_uid' => $memberId, 'goods_id' => (int)$info['goods_id'], @@ -120,20 +121,24 @@ class User extends BaseModel{ 'unit_price' => sprintf("%.2f",$info['unit_price'] * $info['goods_num']), 'status' => 1, 'created_time' => $time, - ]; + ]); + // $insert[] = [ + // 'site_id' => $this->site_id, + // 'seller_uid' => $memberId, + // 'goods_id' => (int)$info['goods_id'], + // 'total' => (int)$info['goods_num'], + // 'unit_price' => sprintf("%.2f",$info['unit_price'] * $info['goods_num']), + // 'status' => 1, + // 'created_time' => $time, + // ]; } - Db::name('futures')->insertAll($insert); + // Db::name('futures')->insertAll($insert); return $this->success(); }catch(Exception $e){ return $this->error('',$e->getMessage()); } } - - - - - - + //TODO 回收 } \ No newline at end of file diff --git a/addon/futures/model/order/FuturesOrderCreate.php b/addon/futures/model/order/FuturesOrderCreate.php index 493576b9..82a637b6 100644 --- a/addon/futures/model/order/FuturesOrderCreate.php +++ b/addon/futures/model/order/FuturesOrderCreate.php @@ -12,6 +12,7 @@ namespace addon\futures\model\order; use addon\cardservice\model\MemberCard; use addon\futures\model\Futures; +use addon\futures\model\Futures as FuturesModel; use app\model\order\Config; use app\model\order\OrderCreate; use app\model\goods\GoodsStock; @@ -271,6 +272,13 @@ class FuturesOrderCreate extends OrderCreate if(empty($futures_info)) return $this->error([], '该秒杀不存在!'); + $futuresModel = new FuturesModel; + $times = $futuresModel->checkTimes($site_id, $member_id, $futures_info['status']); + if($times['code'] != 0){ + return $times; + } + //TODO 判断用户是否有足够多的购买机会 + $futures_id = $futures_info['id']; $data['futures_id'] = $futures_id; $data['futures_info'] = $futures_info; diff --git a/addon/futures/shop/view/config/basics.html b/addon/futures/shop/view/config/basics.html index 1a614a3c..bf4917ff 100644 --- a/addon/futures/shop/view/config/basics.html +++ b/addon/futures/shop/view/config/basics.html @@ -104,6 +104,23 @@ +