|
|
|
@ -18,7 +18,7 @@ use Yansongda\Pay\Exceptions\GatewayException; |
|
|
|
|
use app\admin\model\transaction\Record; |
|
|
|
|
use think\Log; |
|
|
|
|
use addons\shopro\traits\CouponSend; |
|
|
|
|
|
|
|
|
|
use think\Cache; |
|
|
|
|
/** |
|
|
|
|
* 首页接口 |
|
|
|
|
*/ |
|
|
|
@ -27,7 +27,7 @@ class Order extends Api |
|
|
|
|
use CouponSend; |
|
|
|
|
protected $noNeedLogin = ['callback']; |
|
|
|
|
protected $noNeedRight = ['*']; |
|
|
|
|
|
|
|
|
|
const CACHE_TIME = 5; |
|
|
|
|
/** |
|
|
|
|
* 首页 |
|
|
|
|
* |
|
|
|
@ -237,19 +237,23 @@ class Order extends Api |
|
|
|
|
* @return [type] [description] |
|
|
|
|
*/ |
|
|
|
|
public function createOrder(){ |
|
|
|
|
|
|
|
|
|
$address_id = $this->request->post("address_id", 0); |
|
|
|
|
$goods_id = $this->request->post("goods_id", 0); |
|
|
|
|
$num = $this->request->post("num", 1); |
|
|
|
|
$coupon_id = $this->request->post("coupon_id", 0); |
|
|
|
|
$user_id = $this->auth->id; |
|
|
|
|
$user = $this->auth->getUserinfo(); |
|
|
|
|
//var_dump($user_id); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$address = Address::where('id', $address_id)->where('user_id', $user_id)->find(); |
|
|
|
|
if (!$address) { |
|
|
|
|
$this->error("收货地址不存在"); |
|
|
|
|
} |
|
|
|
|
$goods = Goods::where('id', $goods_id)->where('status','normal')->find(); |
|
|
|
|
if (!$goods) { |
|
|
|
|
$this->error("商品不存在"); |
|
|
|
|
$this->error("商品已被抢完"); |
|
|
|
|
} |
|
|
|
|
$warehouse = Warehouse::where('id', $goods['warehouse_id'])->find(); |
|
|
|
|
$this->getBuyTime($warehouse, $user); |
|
|
|
@ -259,11 +263,17 @@ class Order extends Api |
|
|
|
|
if (time() > strtotime($warehouse['end'])) { |
|
|
|
|
$this->error("抢购时间已结束!"); |
|
|
|
|
} |
|
|
|
|
//使用redis锁,限制下单 |
|
|
|
|
$lock_key = "createOrder_".$goods_id; |
|
|
|
|
$res = Cache::store('redis')->setnx($lock_key, 1); |
|
|
|
|
|
|
|
|
|
if (!$res) { |
|
|
|
|
$this->error("您的手速太慢了,商品已被抢!"); |
|
|
|
|
} |
|
|
|
|
Cache::store('redis')->expire($lock_key, self::CACHE_TIME); |
|
|
|
|
//exit(); |
|
|
|
|
$order_amount = $goods['price'] * $num; |
|
|
|
|
$coupon_price = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$pay_amount = $order_amount - $coupon_price; |
|
|
|
|
$order_sn = "ZH".date("YmdHis").rand(1000,9999); |
|
|
|
|
//创建订单 |
|
|
|
@ -301,21 +311,17 @@ class Order extends Api |
|
|
|
|
if ($ret === false) { |
|
|
|
|
$this->error("下单失败"); |
|
|
|
|
} |
|
|
|
|
//使用优惠卷 |
|
|
|
|
$ret = UserCouponModel::where("id", $coupon_id)->update(['use_order_id' => $order->id, 'use_time' => time()]); |
|
|
|
|
if ($ret === false) { |
|
|
|
|
$this->error("下单失败"); |
|
|
|
|
} |
|
|
|
|
//更新商品状态为下架 |
|
|
|
|
Goods::where('id', $goods_id)->update(['status' => 'hidden', "updatetime" => time()]); |
|
|
|
|
|
|
|
|
|
//分佣给买家上级 |
|
|
|
|
$site = Config::get("site"); |
|
|
|
|
$commission = bcmul($order_amount, $site['primary_distribution'] * 0.01, 2); |
|
|
|
|
$obj = new UserService(); |
|
|
|
|
$obj->userCommission(0, $this->auth->getUserinfo(), $order_sn, $commission); |
|
|
|
|
//更新商品状态为下架 |
|
|
|
|
Goods::where('id', $goods_id)->update(['status' => 'hidden', "updatetime" => time()]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Cache::store('redis')->rm($lock_key); |
|
|
|
|
$this->success("succ"); |
|
|
|
|
} |
|
|
|
|
/** |
|
|
|
|