登录
This commit is contained in:
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\utils;
|
||||
|
||||
// UID旋转器(依次循环获取)
|
||||
class UidRotator
|
||||
{
|
||||
private static $uids = [];
|
||||
private static $index = 0;
|
||||
private static $initialized = false;
|
||||
|
||||
/**
|
||||
* 初始化或重置分配器
|
||||
*
|
||||
* @param array $uids UID数组
|
||||
* @param int $startIndex 起始索引位置
|
||||
*/
|
||||
public static function initialize(array $uids = [], int $startIndex = 0)
|
||||
{
|
||||
if (!empty($uids)) {
|
||||
self::$uids = $uids;
|
||||
}
|
||||
|
||||
self::$index = $startIndex % max(1, count(self::$uids));
|
||||
self::$initialized = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 动态设置新的UID数组
|
||||
*
|
||||
* @param array $newUids 新的UID数组
|
||||
* @param bool $resetIndex 是否重置索引
|
||||
*/
|
||||
public static function setUids(array $newUids, bool $resetIndex = true)
|
||||
{
|
||||
self::$uids = $newUids;
|
||||
if ($resetIndex) {
|
||||
self::$index = 0;
|
||||
} else {
|
||||
// 确保索引不越界
|
||||
self::$index = min(self::$index, max(0, count(self::$uids) - 1));
|
||||
}
|
||||
|
||||
self::$initialized = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加新的UID到数组
|
||||
*
|
||||
* @param int|array $newUids 单个UID或UID数组
|
||||
*/
|
||||
public static function addUids($newUids)
|
||||
{
|
||||
if (!self::$initialized) {
|
||||
self::initialize();
|
||||
}
|
||||
|
||||
if (is_array($newUids)) {
|
||||
self::$uids = array_merge(self::$uids, $newUids);
|
||||
} else {
|
||||
self::$uids[] = $newUids;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下一个分配的UID
|
||||
*
|
||||
* @return int|null 返回UID,没有可用UID时返回null
|
||||
*/
|
||||
public static function next()
|
||||
{
|
||||
if (!self::$initialized) {
|
||||
self::initialize();
|
||||
}
|
||||
|
||||
if (empty(self::$uids)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$uid = self::$uids[self::$index];
|
||||
|
||||
// 移动到下一个位置(循环)
|
||||
self::$index = (self::$index + 1) % count(self::$uids);
|
||||
|
||||
return $uid;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前指针位置
|
||||
*/
|
||||
public static function getPosition(): int
|
||||
{
|
||||
return self::$index;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置当前指针位置
|
||||
*
|
||||
* @param int $position 新的位置索引
|
||||
*/
|
||||
public static function setPosition(int $position)
|
||||
{
|
||||
if (!self::$initialized) {
|
||||
self::initialize();
|
||||
}
|
||||
|
||||
if (!empty(self::$uids)) {
|
||||
self::$index = $position % count(self::$uids);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前UID数组
|
||||
*/
|
||||
public static function getUids(): array
|
||||
{
|
||||
return self::$uids;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置指针到起始位置
|
||||
*/
|
||||
public static function reset()
|
||||
{
|
||||
self::$index = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机打乱当前UID数组
|
||||
*/
|
||||
public static function shuffle()
|
||||
{
|
||||
shuffle(self::$uids);
|
||||
self::$index = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// // 使用方法:
|
||||
|
||||
// // 初始化分配器
|
||||
// UidRotator::initialize([64, 63, 83, 93]);
|
||||
|
||||
// echo UidRotator::next(); // 64
|
||||
// echo UidRotator::next(); // 63
|
||||
|
||||
// // 动态添加新UID
|
||||
// UidRotator::addUids(108);
|
||||
// UidRotator::addUids([118, 131]);
|
||||
|
||||
// echo UidRotator::next(); // 83
|
||||
|
||||
// // 设置新数组并重置索引
|
||||
// UidRotator::setUids([150, 143, 156]);
|
||||
|
||||
// echo UidRotator::next(); // 150
|
||||
// echo UidRotator::next(); // 143
|
||||
|
||||
// // 设置新数组但保持当前索引位置
|
||||
// UidRotator::setUids([157, 158, 159, 160], false);
|
||||
// echo UidRotator::next(); // 159 (因为之前索引是2)
|
||||
|
||||
|
||||
// // 从特定位置开始
|
||||
// UidRotator::initialize([64, 63, 83, 93], 2);
|
||||
// echo UidRotator::next(); // 83
|
||||
|
||||
// // 获取当前状态
|
||||
// $currentPosition = UidRotator::getPosition();
|
||||
// $allUids = UidRotator::getUids();
|
||||
|
||||
// // 设置新位置
|
||||
// UidRotator::setPosition(1);
|
||||
// echo UidRotator::next(); // 63
|
||||
|
||||
// // 随机打乱数组
|
||||
// UidRotator::shuffle();
|
||||
// print_r(UidRotator::getUids()); // 随机顺序的数组
|
||||
Reference in New Issue
Block a user