From 29a858757da7cede13031660e8844cc7c7283b53 Mon Sep 17 00:00:00 2001 From: lqmac Date: Wed, 25 Oct 2023 14:12:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0redis=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/api/controller/Index.php | 2 ++ application/api/controller/Order.php | 34 +++++++++++-------- application/config.php | 18 ++++++++-- thinkphp/library/think/cache/driver/Redis.php | 21 ++++++++++++ 4 files changed, 58 insertions(+), 17 deletions(-) diff --git a/application/api/controller/Index.php b/application/api/controller/Index.php index deeded8..a4ae771 100755 --- a/application/api/controller/Index.php +++ b/application/api/controller/Index.php @@ -18,6 +18,8 @@ class Index extends Api */ public function index() { + echo "111"; + exit(); $list = Db::name('user')->where('status','normal')->select(); foreach ($list as $key => $value) { $amount = Db::name('transaction_record')->where('type',2)->where('user_id', $value['id'])->sum('amount'); diff --git a/application/api/controller/Order.php b/application/api/controller/Order.php index 388e3fa..f2ae72c 100755 --- a/application/api/controller/Order.php +++ b/application/api/controller/Order.php @@ -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"); } /** diff --git a/application/config.php b/application/config.php index aa9281c..d21b0ff 100755 --- a/application/config.php +++ b/application/config.php @@ -183,11 +183,23 @@ return [ // +---------------------------------------------------------------------- // | 缓存设置 // +---------------------------------------------------------------------- + // 'cache' => [ + // // 驱动方式 + // 'type' => 'File', + // // 缓存保存目录 + // 'path' => CACHE_PATH, + // // 缓存前缀 + // 'prefix' => '', + // // 缓存有效期 0表示永久缓存 + // 'expire' => 0, + // ], + 'cache' => [ // 驱动方式 - 'type' => 'File', - // 缓存保存目录 - 'path' => CACHE_PATH, + 'type' => 'redis', + 'host' => '127.0.0.1', + 'port' => '6379', + 'password' => '', // 缓存前缀 'prefix' => '', // 缓存有效期 0表示永久缓存 diff --git a/thinkphp/library/think/cache/driver/Redis.php b/thinkphp/library/think/cache/driver/Redis.php index d7bf6d1..b403de0 100755 --- a/thinkphp/library/think/cache/driver/Redis.php +++ b/thinkphp/library/think/cache/driver/Redis.php @@ -185,4 +185,25 @@ class Redis extends Driver return $this->handler->flushDB(); } + /** + * 设置一个key,如果key存在,不做任何操作. + * @param unknown $key + * @param unknown $value + */ + public function setnx($name, $value) + { + $key = $this->getCacheKey($name); + return $this->handler->setnx($key, $value); + } + /** + * 设置一个key,如果key存在,不做任何操作. + * @param unknown $key + * @param unknown $value + */ + public function expire($name, $expire) + { + $key = $this->getCacheKey($name); + return $this->handler->expire($key, $expire); + } + }