webman_duanju/app/common/model/Common.php

125 lines
3.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\common\model;
use app\api\model\CommonInfo;
use app\common\library\DatabaseRoute;
use app\czg\model\SysCaptcha;
use think\cache\driver\Redis;
use think\facade\Db;
use think\Model;
use support\think\Cache;
class Common extends Model
{
// 统一处理分表表新增编辑查询(主表中没有的表)
public static function saveDbData($table, $sale, $data, $operate = 'insertGetId', $where = [])
{
if(in_array($operate, ['insertGetId', 'insert']) || $operate == 'update') {
$connect_name = DatabaseRoute::getConnection($table, $sale, true);
}else {
$connect_name = DatabaseRoute::getConnection($table, $sale);
}
$db = Db::connect($connect_name);
if($operate == 'insert' || $operate == 'insertGetId') {
return $db->name($table)->insertGetId($data);
}elseif($operate == 'update') {
return $db->name($table)->where($where)->update($data);
}elseif ($operate == 'select') {
return $db->name($table)->where($where)->select();
}elseif ($operate == 'find') {
return $db->name($table)->where($where)->find();
}
}
public static function db_count($table, $sale, $where)
{
$connect_name = DatabaseRoute::getConnection($table, $sale);
$db = Db::connect($connect_name);
return $db->name($table)->where($where)->count();
}
public static function getAppUseKv()
{
$info = Db::connect(config('think-orm.search_library'))->name('common_info')->where(['is_app_use' => 1])->field('id, value')->select();
$data = [];
foreach ($info as $k => $v) {
$data[$v['id']] = $v['value'];
}
return returnSuccessData($data);
}
/**
* 检查是否允许访问基于Redis的频率限制
* @param string $id 唯一标识如用户ID、IP等
* @param string $key 操作类型(如"updateWeekCourseView"
* @param int $count 允许的访问次数
* @param int $seconds 时间窗口(秒)
* @return bool 是否允许访问
*/
public static function isAccessAllowed($id, $key, $count, $seconds, $sys_data = false)
{
if($sys_data) {
$redisKey = 'sys:data:' . $key . ':' . $id;
}else {
$redisKey = generateRedisKey($key, $id);
}
// 获取当前访问次数
$currentCount = Cache::get($redisKey);
if ($currentCount === null) {
// 首次访问:初始化计数器并设置过期时间
Cache::set($redisKey, $seconds, 1);
return true;
}
if ((int)$currentCount < $count) {
// 未超过限制:增加计数
Cache::set($redisKey,$currentCount + 1);
return true;
}
// 已超过限制
return false;
}
/**
* @param int $courseId 课程ID
* @return int 周播放量
*/
public static function getCourseWeekViewCount($courseId)
{
$key = "course:viewCount:{$courseId}";
// 从Redis获取周播放量
$viewCount = Cache::get($key);
if (empty($viewCount)) {
// 计算下周一的时间戳
$now = time();
$dayOfWeek = date('N', $now); // 1-71表示周一
$daysToMonday = $dayOfWeek === 1 ? 7 : (1 - $dayOfWeek + 7) % 7;
$nextMonday = $now + $daysToMonday * 86400;
// 计算剩余秒数并设置缓存
$seconds = $nextMonday - $now;
Cache::set($key, 1, $seconds);
return 1;
}
// 播放量递增并返回
$newCount = Cache::set($key, $viewCount + 1, -1);
return $newCount;
}
}