diff --git a/.env b/.env index a15c551..d30629e 100644 --- a/.env +++ b/.env @@ -6,9 +6,9 @@ DEFAULT_TIMEZONE = Asia/Shanghai [DATABASE] TYPE = mysql HOSTNAME = 127.0.0.1 -DATABASE = youda -USERNAME = youda -PASSWORD = youda +DATABASE = youda_test +USERNAME = youda_test +PASSWORD = youda_test HOSTPORT = 3306 CHARSET = utf8 DEBUG = false diff --git a/app/api/controller/Base.php b/app/api/controller/Base.php index 945e967..f0737fe 100755 --- a/app/api/controller/Base.php +++ b/app/api/controller/Base.php @@ -23,7 +23,26 @@ class Base extends MyController protected function getUser() { $token = request()->header('token', ''); - $user_account = UserAccount::getInfo(['account_token' => $token]); + + // 先从Redis中获取用户账户信息 + $redis = (new \app\common\server\RedisHelper())->getRedis(); + $redis_key = 'user_account_token:' . $token; + $user_account = $redis->get($redis_key); + + if ($user_account) { + // Redis中存在数据,直接使用 + $user_account = json_decode($user_account, true); + } else { + // Redis中不存在,从数据库获取 + $user_account = UserAccount::getInfo(['account_token' => $token]); + + // 如果数据库中存在该用户账户信息,则存入Redis + if ($user_account) { + $redis->set($redis_key, json_encode($user_account)); + $redis->expire($redis_key, 600); // 设置过期时间为10分钟(600秒) + } + } + if (!$user_account) { $data['status'] = '-1'; $data['msg'] = "没有找到用户信息1"; @@ -67,6 +86,62 @@ class Base extends MyController return $user; } + + /** + * 获取当前登录用户ID + * + * @return int 用户ID,未登录或状态异常时返回0 + */ + protected function getUserId() + { + $token = request()->header('token', ''); + // 尝试从请求参数中获取token + if (empty($token)) { + $token = $this->request->param('token', ''); + } + + if (empty($token)) { + return 0; + } + + // 先从Redis中获取用户账户信息 + $redis = (new \app\common\server\RedisHelper())->getRedis(); + $redis_key = 'user_account_token:' . $token; + $user_account = $redis->get($redis_key); + + if ($user_account) { + // Redis中存在数据,直接使用 + $user_account = json_decode($user_account, true); + } else { + // Redis中不存在,从数据库获取 + $user_account = UserAccount::getInfo(['account_token' => $token]); + + // 如果数据库中存在该用户账户信息,则存入Redis + if ($user_account) { + $redis->set($redis_key, json_encode($user_account)); + $redis->expire($redis_key, 600); // 设置过期时间为10分钟(600秒) + } + } + + if (!$user_account) { + return 0; + } + + // 验证token有效性 + $user_account_is = user_md5($user_account['user_id'] . $user_account['token_num'] . $user_account['token_time']); + if ($token !== $user_account_is) { + return 0; + } + + // 检查用户状态 + $user = User::where(['id' => $user_account['user_id']])->find(); + if (!$user || $user['status'] != 1) { + return 0; + } + + return $user['id']; + } + /** * 为用户生成UID * @@ -211,42 +286,6 @@ class Base extends MyController return false; } - /** - * 获取当前登录用户ID - * - * @return int 用户ID,未登录或状态异常时返回0 - */ - protected function getUserId() - { - $token = request()->header('token', ''); - // 尝试从请求参数中获取token - if (empty($token)) { - $token = $this->request->param('token', ''); - } - - if (empty($token)) { - return 0; - } - - $user_account = UserAccount::getInfo(['account_token' => $token]); - if (!$user_account) { - return 0; - } - - // 验证token有效性 - $user_account_is = user_md5($user_account['user_id'] . $user_account['token_num'] . $user_account['token_time']); - if ($token !== $user_account_is) { - return 0; - } - - // 检查用户状态 - $user = User::where(['id' => $user_account['user_id']])->find(); - if (!$user || $user['status'] != 1) { - return 0; - } - - return $user['id']; - } /* * 判断优惠券是否领过 diff --git a/app/api/controller/Infinite.php b/app/api/controller/Infinite.php index f97aa28..df29f0c 100755 --- a/app/api/controller/Infinite.php +++ b/app/api/controller/Infinite.php @@ -306,13 +306,24 @@ class Infinite extends Base } /** - * 中赏记录 + * 中奖记录 */ - public function infinite_shang_log(Request $request) + public function infinite_shang_log() { $shang_id = request()->param('shang_id/d', 0); $goods_id = request()->param('goods_id/d', 0); $is_mibao = request()->param('is_mibao/d', 0); + + // 生成缓存键 + $redis = (new \app\common\server\RedisHelper())->getRedis(); + $cache_key = "infinite_shang_log:{$goods_id}:{$shang_id}:{$is_mibao}"; + + // 尝试从缓存获取数据 + $cached_data = $redis->get($cache_key); + if ($cached_data) { + return $this->renderSuccess("请求成功", json_decode($cached_data, true)); + } + #盒子信息 $goods = Goodsmodel::field('id,stock,status,type') ->where(['id' => $goods_id]) @@ -362,6 +373,10 @@ class Infinite extends Base 'data' => $data->items(), 'last_page' => $data->lastPage(), ]; + + // 将数据存入缓存,设置1分钟过期时间 + $redis->set($cache_key, json_encode($new_data), 60); + return $this->renderSuccess("请求成功", $new_data); } @@ -512,12 +527,12 @@ class Infinite extends Base return $this->renderError('请求错误'); } - $is_goodslist = GoodsList::field('id') - ->where($where) - ->find(); - if (!$is_goodslist) { - return $this->renderError('暂无奖品信息'); - } + // $is_goodslist = GoodsList::field('id') + // ->where($where) + // ->find(); + // if (!$is_goodslist) { + // return $this->renderError('暂无奖品信息'); + // } if ($goods['type'] == 9) { $prize_num = intval($prize_num); @@ -558,7 +573,7 @@ class Infinite extends Base if ($redis_key_info) { return $this->renderError("当前操作太快了,请等待"); } else { - $redis->set($redis_key, 1, 3); + $redis->set($redis_key, 1, 10); } Db::startTrans(); diff --git a/app/api/controller/Notify.php b/app/api/controller/Notify.php index 6b44aef..087743d 100755 --- a/app/api/controller/Notify.php +++ b/app/api/controller/Notify.php @@ -1268,7 +1268,7 @@ class Notify extends Base $order_type = $order['order_type'];#订单类型 $whe = []; $whe[] = ['id', '=', $order['goods_id']]; - $infinite_goods = Goods::getInfo($whe, 'type'); + $infinite_goods = Goods::getInfo($whe, 'type,lingzhu_is,lingzhu_fan,lingzhu_shang_id,king_user_id'); $where = []; $where[] = ['goods_id', '=', $goods_id]; @@ -1288,7 +1288,7 @@ class Notify extends Base ->select()->toArray(); if ($goodslist) { - $res[] = $this->infinite_drawprize_box($goodslist, $prize_num, $order_id, $user_id, $goods_id, $order_type); + $res[] = $this->infinite_drawprize_box($goodslist, $prize_num, $order_id, $user_id, $goods_id, $order_type, $infinite_goods); //去除秘宝池次数 if ($infinite_goods['type'] == 9 && $order['is_mibao'] == 1) { @@ -1317,7 +1317,7 @@ class Notify extends Base * 抽出奖品 * @param $order 订单信息 */ - protected function infinite_drawprize_box($goodslist, $prize_num, $order_id, $user_id, $goods_id, $order_type) + protected function infinite_drawprize_box($goodslist, $prize_num, $order_id, $user_id, $goods_id, $order_type, $infinite_goods) { // 计算总概率 @@ -1337,7 +1337,9 @@ class Notify extends Base } $multiple = ConfigHelper::getInfiniteMultiple(); $lingzhu_shang_id = 0; - $infinite_goods = Goods::getInfo(['id' => $goods_id], 'type,lingzhu_is,lingzhu_fan,lingzhu_shang_id,king_user_id'); + if (!$infinite_goods) { + $infinite_goods = Goods::getInfo(['id' => $goods_id], 'type,lingzhu_is,lingzhu_fan,lingzhu_shang_id,king_user_id'); + } if ($infinite_goods != null && $infinite_goods['type'] == 8 && $infinite_goods['lingzhu_is'] == 1) { // $infinite_goods['lingzhu_shang_id'] $lingzhu_shang_id = $infinite_goods['lingzhu_shang_id']; @@ -1427,7 +1429,7 @@ class Notify extends Base ->where('goods_list_id', '=', $prize_info['id']) ->select()->toArray(); // $res[] = $this->infinite_drawprize_box($order); - $res[] = $this->infinite_drawprize_box($goodslist_1, 1, $order_id, $user_id, $goods_id, $order_type); + $res[] = $this->infinite_drawprize_box($goodslist_1, 1, $order_id, $user_id, $goods_id, $order_type, $infinite_goods); } } return $res; diff --git a/app/api/middleware.php b/app/api/middleware.php index 4c7ffbd..e6c794b 100755 --- a/app/api/middleware.php +++ b/app/api/middleware.php @@ -1,14 +1,17 @@ pathMatch($whitePath, $path)) { // 白名单路径,跳过域名检查 - \think\facade\Log::info('白名单路径访问: ' . $path . ', 域名: ' . $domain); + // \think\facade\Log::info('白名单路径访问: ' . $path . ', 域名: ' . $domain); return $next($request); } } @@ -42,7 +42,7 @@ class DomainBind // 检查域名是否允许 if (!$this->checkDomain($domain)) { // 记录被拒绝的请求 - \think\facade\Log::warning('域名访问被拒绝: ' . $domain . ', 路径: ' . $path); + // \think\facade\Log::warning('域名访问被拒绝: ' . $domain . ', 路径: ' . $path); return $this->error('未授权,无法访问', 403); } diff --git a/app/api/middleware/ResponseTimeMiddleware.php b/app/api/middleware/ResponseTimeMiddleware.php new file mode 100644 index 0000000..7c9e9e7 --- /dev/null +++ b/app/api/middleware/ResponseTimeMiddleware.php @@ -0,0 +1,42 @@ +header(['X-Response-Time' => $executionTime . 'ms']); + } + + return $response; + } +} \ No newline at end of file diff --git a/app/common.php b/app/common.php index eeb6e89..8b160ea 100755 --- a/app/common.php +++ b/app/common.php @@ -150,8 +150,24 @@ if (!function_exists('getConfig')) { */ function getConfig($type) { + // 生成缓存键 + $redis = (new \app\common\server\RedisHelper())->getRedis(); + $cache_key = "config:{$type}"; + + // 尝试从缓存获取数据 + $cached_data = $redis->get($cache_key); + if ($cached_data) { + return json_decode($cached_data, true); + } + $content = Db::name('config')->where(['key' => $type])->value('value'); $config = json_decode($content, true); + + // 将数据存入缓存,设置10分钟过期时间 + if ($config) { + $redis->set($cache_key, json_encode($config), 600); + } + return $config; } } diff --git a/app/common/model/GoodsExtend.php b/app/common/model/GoodsExtend.php index 813fb0c..b992fe6 100644 --- a/app/common/model/GoodsExtend.php +++ b/app/common/model/GoodsExtend.php @@ -42,6 +42,16 @@ class GoodsExtend extends Base */ public static function getGoodsExtendByGoodsId($goodsId, $goods_type) { + // 生成缓存键 + $redis = (new \app\common\server\RedisHelper())->getRedis(); + $cache_key = "goods_extend:{$goodsId}:{$goods_type}"; + + // 尝试从缓存获取数据 + $cached_data = $redis->get($cache_key); + if ($cached_data) { + return json_decode($cached_data, true); + } + $goods_extend = self::where('goods_id', $goodsId)->find(); if (!$goods_extend) { // 从goods_type表获取默认值 @@ -63,6 +73,12 @@ class GoodsExtend extends Base ]; } } + + // 将数据存入缓存,设置5分钟过期时间 + if ($goods_extend) { + $redis->set($cache_key, json_encode($goods_extend), 300); + } + return $goods_extend; } } \ No newline at end of file diff --git a/app/common/server/RedisHelper.php b/app/common/server/RedisHelper.php index 1f08971..7c6dda4 100755 --- a/app/common/server/RedisHelper.php +++ b/app/common/server/RedisHelper.php @@ -10,38 +10,9 @@ namespace app\common\server; class RedisHelper { /** - * @var \Redis Redis 实例 + * @var \Redis Redis 实例(静态属性) */ - private $redis; - - /** - * 构造函数,初始化 Redis 连接 - */ - public function __construct() - { - // 从环境变量中获取 Redis 主机和端口,默认为 127.0.0.1 和 6379 - $host = env('redis.host', '127.0.0.1'); - $port = env('redis.port', '6379'); - // 从环境变量中获取 Redis 数据库编号,默认为 0 - $db = env('redis.db', '0'); - // 创建 Redis 实例 - $this->redis = new \Redis(); - // 连接到 Redis 服务器 - $this->connect($host, $port); - // 选择数据库 - $this->redis->select((int)$db); - } - - /** - * 连接到 Redis 服务器 - * - * @param string $host Redis 主机地址 - * @param int $port Redis 端口 - */ - private function connect($host, $port) - { - $this->redis->connect($host, $port); - } + private static $redis; /** * 获取 Redis 实例 @@ -50,7 +21,33 @@ class RedisHelper */ public function getRedis() { - return $this->redis; + if (self::$redis === null) { + // 从环境变量中获取 Redis 主机和端口,默认为 127.0.0.1 和 6379 + $host = env('redis.host', '127.0.0.1'); + $port = env('redis.port', '6379'); + // 从环境变量中获取 Redis 数据库编号,默认为 0 + $db = env('redis.db', '0'); + // 创建 Redis 实例 + self::$redis = new \Redis(); + // 连接到 Redis 服务器 + $this->connect($host, $port); + // 选择数据库 + self::$redis->select((int)$db); + } + + return self::$redis; + } + + /** + * 连接到 Redis 服务器 + * + * @param string $host Redis 主机 + * @param int $port Redis 端口 + * @return bool + */ + private function connect($host, $port) + { + return self::$redis->connect($host, (int)$port); } /** @@ -61,7 +58,7 @@ class RedisHelper */ public function get($key) { - return $this->redis->get($key); + return $this->getRedis()->get($key); } /** @@ -75,9 +72,9 @@ class RedisHelper public function set($key, $value, $timeout = 600) { if ($timeout > 0) { - return $this->redis->setex($key, $timeout, $value); + return $this->getRedis()->setex($key, $timeout, $value); } - return $this->redis->set($key, $value); + return $this->getRedis()->set($key, $value); } /** @@ -88,7 +85,7 @@ class RedisHelper */ public function delete($key) { - return $this->redis->del($key); + return $this->getRedis()->del($key); } /** @@ -99,7 +96,7 @@ class RedisHelper */ public function exists($key) { - return $this->redis->exists($key); + return $this->getRedis()->exists($key); } /** @@ -111,7 +108,7 @@ class RedisHelper */ public function expire($key, $timeout) { - return $this->redis->expire($key, $timeout); + return $this->getRedis()->expire($key, $timeout); } /** @@ -122,6 +119,6 @@ class RedisHelper */ public function select($db) { - return $this->redis->select($db); + return $this->getRedis()->select($db); } } \ No newline at end of file diff --git a/app/common/service/PaymentCalculator.php b/app/common/service/PaymentCalculator.php index 42157d9..32d7c71 100644 --- a/app/common/service/PaymentCalculator.php +++ b/app/common/service/PaymentCalculator.php @@ -65,8 +65,8 @@ class PaymentCalculator $shou_zhe_price = 0; if ($goods['type'] != 5 && $goods_type != 10 && $goods_type != 15) { $is_chou = Order::field('id')->where([['user_id', '=', $user['id']], ['status', '=', 1]])->find(); - $is_chou2 = Order::field('id')->where([['is_shou_zhe', '=', 1], ['status', '=', 1], ['user_id', '=', $user['id']]])->find(); - if (!$is_chou && !$is_chou2 && $goods['is_shou_zhe'] == 1) { + // $is_chou2 = Order::field('id')->where([['is_shou_zhe', '=', 1], ['status', '=', 1], ['user_id', '=', $user['id']]])->find(); + if (!$is_chou && $goods['is_shou_zhe'] == 1) { $shou_zhe_price = bcmul("$box_price", "0.5", 2); } }