class InviteCodeService
{
// HxrhRD
public static $p_char_lists = [
['H', 'h', 'x', 'r', 'R'],
['D', 'd', 'Z', 'n', 'N'],
['G', 'g', 'X', 'q', 'Q'],
['C', 'c', 'o', 'm', 'M'],
['W', 'w', 'V', 's', 'S'],
['F', 'f', 'y', 'p', 'P'],
['A', 'a', 'Y', 'k', 'K'],
['E', 'e', 'z', 'u', 'U'],
['J', 'j', 'v', 't', 'T'],
['B', 'b', 'O', 'l', 'L'],
];
/**
* 生成邀请码
* @param $user_id
* @param int $type 1网红端 2 用户端
* @return string
*/
public function generateCode($user_id, $type = 1)
{
// 暂定用户量处于千万级
$code = '';
$nums = $this->getNumberChars($user_id);
$char_lists = $type === 1 ? self::$p_char_lists : self::$t_char_lists;
foreach ($nums as $num) {
$code .= $char_lists[$num][rand(0, 4)];
}
return $code;
}
/**
* 根据邀请码获取id
* @param $code
* @param $type 1网红端 2用户端
* @return int
*/
public function getUserId($code, $type = 1)
{
$id = '';
$chars = str_split($code);
$char_lists = $type === 1 ? self::$p_char_lists : self::$t_char_lists;
foreach ($chars as $char) {
foreach ($char_lists as $key => $char_list) {
if (in_array($char, $char_list)) {
$id .= strval($key);
}
}
}
return intval($id);
}
/**
* 固定6为邀请码
* 将用户ID 变成6位数字 不足左边补0, 然后变成数字数组
* @param $num
* @return array
*/
protected function getNumberChars($num)
{
$nums = str_split($num);
if ($num < 10) {
$nums = [0, 0, 0, 0, 0, $num];
} elseif ($num < 100) {
$nums = array_merge([0, 0, 0, 0], str_split(strval($num)));
} elseif ($num < 1000) {
$nums = array_merge([0, 0, 0], str_split(strval($num)));
} elseif ($num < 10000) {
$nums = array_merge([0, 0], str_split(strval($num)));
} elseif ($num < 100000) {
$nums = array_merge([0], str_split(strval($num)));
}
return $nums;
}
}