PHP根据id生成邀请码

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/sqlquan/article/details/88872105

id就是一个唯一标识,只是不够长度,我对id进行36进制转换就可以了

    /**
     * @description: 10进制转36进制
     * @param {type} 
     * @return: 
     */
    protected function createCode($userId)
    {
        static $sourceString = [
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
            'a', 'b', 'c', 'd', 'e', 'f',
            'g', 'h', 'i', 'j', 'k', 'l',
            'm', 'n', 'o', 'p', 'q', 'r',
            's', 't', 'u', 'v', 'w', 'x',
            'y', 'z'
        ];

        $num = $userId;
        $code = '';
        while ($num) {
            $mod = bcmod($num, 36);
            $num = bcdiv($num, 36);
            $code = "{$sourceString[$mod]}{$code}"; //邀请码拼接
        }
        //判断code的长度
        if (empty($code[4]))
            $code=str_pad($code, 5, '0', STR_PAD_LEFT); //长度不够拼接'0'

        return $code;
    }

参考资料:https://blog.csdn.net/xuefei0619/article/details/79862287

猜你喜欢

转载自blog.csdn.net/sqlquan/article/details/88872105