//@todo 身份证号码检测
1 /** 2 * 同样适用大数据大并发 生成ID 3 */ 4 function getNewID($type) 5 { 6 $command = Yii::app()->db->createCommand('call P_GET_ORDER_ID_NEW(' . $type . ',@orderId)'); 7 $rs = $command->execute(); 8 $command = Yii::app()->db->createCommand('select @orderId'); 9 10 $rs = $command->queryRow(); 11 return $rs['@orderId']; 12 } 13 14 //结合存储过程 15 每天从0开始 16 CREATE DEFINER=`dwww`@`%` PROCEDURE `GET_ORDER_ID`(type int,OUT orderId bigint) 17 COMMENT 'ID生成器存储过程' 18 BEGIN 19 20 DECLARE _orderId bigint(20);#新的ID 21 DECLARE _idLen int;# 序列的长度 22 23 DECLARE _upRows int default 0; 24 25 set _idLen = 6;#默认6位 26 IF(type=900) THEN 27 REPLACE INTO id_90(stub) VALUES(1); 28 set orderId = LAST_INSERT_ID(); 29 set _idLen = 8;##根据业务的数据量适当扩充 30 31 ELSEIF(type=905) THEN 32 REPLACE INTO id_905(stub) VALUES(1); 33 set orderId = LAST_INSERT_ID(); 34 ELSE 35 set orderId = 0; 36 END IF; 37 # 组装订单号 38 IF orderId > 0 THEN 39 set orderId = CONCAT(CONCAT(type,''),DATE_FORMAT(now(), "%y%m%d"),RIGHT(CONCAT("000000000",orderId),_idLen)); 40 END IF; 41 END
1 //大并发获取ID 2 /** 3 * 乐观锁获取ID 4 */ 5 function getTableID($type, $step = 1) 6 { 7 $type = (int) $type; 8 $step = (int) $step; 9 $newID = 0; 10 $time = 0; 11 //未获取到ID且最多尝试32次 12 while (!$newID && $time < 32) { 13 $command = Yii::app()->db->createCommand('SELECT `VALUE` FROM ID_SEQ WHERE `NAME`=' . $type); 14 $first = $command->queryRow(); 15 $command = Yii::app()->db->createCommand('UPDATE T_ID_SEQUENCE SET `VALUE`=`VALUE` + ' . $step . ' WHERE `NAME`=' . $type . ' AND `VALUE`=' . $first['VALUE']); 16 $sec = $command->execute(); 17 $time++; 18 if(!$sec) { 19 //Yii::log("result: {$sec}, type:{$type}, curvalue:{$first['VALUE']}, errtimes:{$time}", 'error', 'getTableID'); 20 continue; 21 } 22 23 $data['baseID'] = ($first['VALUE'] + 1) % 1000000; 24 $newID = $type . date('ymd') . sprintf('%06s', $data['baseID']); 25 $data['newID'] = $newID; 26 $data['step'] = $step; 27 if($step > 1) 28 return $data; 29 return $newID; 30 } 31 }
//银行卡算法校验
1 /** 2 * 银行卡号格式检测 3 */ 4 function chkCard($card) 5 { 6 if(empty($card)) return false; 7 8 $cardNo = RSADecode($card); 9 10 //Luhn 算法 11 $cardNo .= ''; 12 $len = strlen($cardNo); 13 if($len != 16 && $len != 19) 14 return false; 15 if($len < 15 && $len > 19) return false; 16 if(!is_numeric($cardNo)) 17 return false; 18 19 $a1 = 0; //偶数和 20 $a2 = 0; //奇数和 21 if($len == 19) { //19位卡 22 for($i = $len - 2; $i >= 0; $i--) { 23 if($i%2 != 0) { 24 $t = $cardNo[$i] * 2; 25 $t .= ''; 26 $a1 += $t[0]; 27 if(isset($t[1])) { 28 $a1 += $t[1]; 29 } 30 } else { 31 $a2 += $cardNo[$i]; 32 } 33 } 34 } 35 if($len == 16) { //16位信用卡 36 for($i = $len - 2; $i >= 0; $i--) { 37 if($i%2 == 0) { 38 $t = $cardNo[$i] * 2; 39 $t .= ''; 40 $a1 += $t[0]; 41 if(isset($t[1])) { 42 $a1 += $t[1]; 43 } 44 } else { 45 $a2 += $cardNo[$i]; 46 } 47 } 48 } 49 50 $mod = ($a2 + $a1) % 10; 51 $last = $mod; 52 if($mod > 0) 53 $last = 10 - $mod; 54 55 if($last != $cardNo[$len - 1]) 56 return false; 57 58 return true; 59 }
/**
* 检测姓名中文格式
*/
1 function chkChinese($name) 2 { 3 //$pattern = '/^[\u4E00-\u9FFF]+$/'; 4 if(preg_match("/^[\x{4e00}-\x{9fa5}]+$/u", $name)) 5 return true; 6 return false; 7 }
1 /** 2 *根据身份证号计算性别 3 **/ 4 function getGender($idNo) 5 { 6 //15位数的身份证最后一位是双数就是女的,单数就是男的 7 //18位数的身份证倒数第二位(第17位)是双数就是女的,单数就是男的 8 $len = strlen($idNo); 9 $gender = '先生'; 10 11 $last = $len == 15 ? substr($idNo, 0, -1) : ($len == 18 ? substr($idNo, -2, 1) : 1); 12 if($last % 2 == 0) 13 $gender = '女士'; 14 return $gender; 15 }
1 /** 2 * 生成指定长度的随机字符串 3 */ 4 function getRandChar($amount) 5 { 6 $str = '23456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ'; 7 $len = strlen($str); 8 $charStr = ''; 9 for($i = 0; $i < $amount; $i++) { 10 $rnd = rand(0, $len - 1); 11 $charStr .= $str[$rnd]; 12 } 13 14 return $charStr; 15 }
//自定义非对称加密和解密
1 /** 2 * RSA解密 3 */ 4 function RSADecode($argEncoded) 5 { 6 $source = base64_decode($argEncoded); 7 //rsa 配置加载 8 $rsa = configParam('RSA'); 9 $res = openssl_pkey_get_private($rsa['privateKey'], $rsa['passPhrase']); 10 openssl_private_decrypt($source,$newSource,$res); 11 12 return $newSource; 13 14 }
1 /** 2 * RSA 加密 3 */ 4 function rsaEncode($mob) 5 { 6 $pub = '-----BEGIN PUBLIC KEY----- 7 B 8 -----END PUBLIC KEY-----'; 9 10 $res = openssl_pkey_get_public($pub); 11 openssl_public_encrypt($mob, $newStr, $res); 12 $en = base64_encode($newStr); 13 14 return $en; 15 }
//自定义对称 加密和解密
1 function selfEncode($str, $k) { 2 $encoded = ''; 3 $len = strlen($str); 4 $lk = strlen($k); 5 for($i = 0; $i < $len; $i++) 6 { 7 $mod = fmod($i, $lk); 8 $encoded .= $str[$i. ''] ^ $k[$mod.'']; 9 } 10 $encoded = base64_encode($encoded); 11 12 return $encoded; 13 }
14 function selfDecode($str, $k) { 15 $str = base64_decode($str); 16 $decoded = ''; 17 $len = strlen($str); 18 $lk = strlen($k); 19 for($i = 0; $i < $len; $i++) 20 { 21 $mod = fmod($i, $lk); 22 $decoded .= $str[$i. ''] ^ $k[$mod.'']; 23 } 24 25 return $decoded; 26 }
//身份证 校验规则
function chkIdNo($idNo) { $len = strlen($idNo); //十五位身份证1999 10 01不再办理 if($len == 15) { //13 04 03 991001 002 //$pattern = '/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{2}[Xx0-9]$/'; //修复 月份 天 不能同时为00 $pattern = '/^[1-9]\d{7}((0[1-9])|(1[0-2]))((0[1-9])|([1-2]\d)|3[0-1])\d{2}[Xx0-9]$/'; $res = preg_match($pattern, $idNo); if($res) return true; return false; } if($len == 18){ //前17位对应的权重数字算法 (2<<($len - 2 - n)%11 $bits = str_split($idNo); //每位对应的权重数字 $weights = array(); //求和 $sum = 0; for($i = 0; $i < 17; $i++) { //计算权重 $weights[$i] = (2 << ($len - 2 - $i)) % 11; $sum += $bits[$i] * $weights[$i]; } $mod = $sum % 11; //校验码array(1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2); $chkCodes = array(1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2); $code = isset($chkCodes[$mod]) ? $chkCodes[$mod].'' : ''; if($code !== $bits[$len - 1]) { return false; } return true; } return false; }
curl post请求
function curl_post($url, $data) { global $debug; $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); ob_start(); curl_exec($ch); $result = ob_get_contents() ; ob_end_clean(); if($debug){ var_dump($result); exit; } $res = json_decode($result, true); var_dump($res); exit; }
function getSign($args)
{
$sk = '123abc5lfs123';
ksort($args);
$s = md5( md5(implode('', $args)) . $sk );
return $s;
}