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