标签:
/**
*生成激活码
*
*/
function showGenerationActivationCode(){
#渠道类型id
$channel_id=$_POST[‘channel‘];
#根据渠道id去查询渠道英文名称
$channelInfo = load_mysql ( "channelInfo" );
$_res=$channelInfo->getInfoById($channel_id);
$en_name=$_res[‘en_name‘];
#活动类型
$type=$_POST[‘active_type‘];
#生成数量
$nub=$_POST[‘nub‘];
#接受生成的激活码的数组
$array_ActivationCode=$this->showunique_rand(10000000,99999999,$nub);
#生成code数组
$code_array=$this->showAlgorithm_ActivationCode($type,$channel_id,$array_ActivationCode);
#连接redis
$redis=new Redis();
$redis->connect(‘192.168.1.131‘,8899);
//授权
$redis->auth("funova2014");
}
/**
*生成code算法
*type:活动类型[取前三位]
*channel_id:渠道id[取前三位,不足以0填充]
*array_ActivationCode:激活码数组
*code算法格式=活动类型+渠道id+array_ActivationCode;
*return code的数组
*/
function showAlgorithm_ActivationCode($type,$channel_id,$array_ActivationCode){
#截取活动类型前位字符串
$type=mb_substr($type,0,3,‘utf-8‘);
#渠道id[取前三位,不足左边以0填充]
$channel_id=str_pad($channel_id,3,‘0‘,STR_PAD_LEFT);
#定义一个code算法数组
$code_array=array();
for($i=0;$i<count($array_ActivationCode);$i++){
$code_array[$i]=$type.‘‘.$channel_id.‘‘.$array_ActivationCode[$i];
}
return $code_array;
}
/**
*生成8位数的激活码算法
*/
function showunique_rand($min, $max, $num) {
$count = 0;
$return = array();
while ($count < $num) {
$return[] = mt_rand($min, $max);
$return = array_flip(array_flip($return));
$count = count($return);
}
return $return;
}
标签:
原文地址:http://www.cnblogs.com/hgj123/p/4531799.html