常用生成订单号的规则
常用天生订单号的端正
订单号就是您在购物网站购物后取得的订单号,纪录的是购物订单信息。在您必要与购物网站举行订单查询等利用时,必要给购物网站提供商家订单号。【来自百度百科】
软件开发中我们常常会碰到天生订单号的业务逻辑来记任命户下单、物流、发票、付出、结算等信息。天生订单号的办法多种多样。大局部会参考业务需求来计划天生办法,但订单号的天生端正双管齐下相差不大。底下给各位总结先容几种订单号的天生端正以供参考。
1、随机数办法。
function gen_order_rand() {
$strTime = str_pad(str_replace(".", "", microtime(true)), 14, "0", STR_PAD_RIGHT);
$strRand = strval(mt_rand(10000, 99999));
return $strTime . $strRand;
}
var_dump(gen_order_rand());
运转后果:
string(19) "1648171258258353703"
2、随机数+uid办法
function gen_order_uid_rand($uid) {
$strUid = str_pad(substr(strval($uid), -5), 5, "0", STR_PAD_RIGHT);
$strTime = date('ymdHis');
$strRand = strval(mt_rand(1000, 9999));
return $strUid . $strTime . $strRand;
}
$intUid = 1234567890;
var_dump(gen_order_uid_rand($intUid));
运转后果:
string(21) "678902203250120587473"
3、uniqid+crc64办法
function crc64Table()
{
$crc64tab = [];
// ECMA polynomial
$poly64rev = (0xC96C5795 << 32) | 0xD7870F42;
// ISO polynomial
// $poly64rev = (0xD8 << 56);
for ($i = 0; $i < 256; $i++)
{
for ($part = $i, $bit = 0; $bit < 8; $bit++) {
if ($part & 1) {
$part = (($part >> 1) & ~(0x8 << 60)) ^ $poly64rev;
} else {
$part = ($part >> 1) & ~(0x8 << 60);
}
}
$crc64tab[$i] = $part;
}
return $crc64tab;
}
/**
* @param string $string
* @param string $format
* @return mixed
*
* Formats:
* crc64('php'); // afe4e823e7cef190
* crc64('php', '0x%x'); // 0xafe4e823e7cef190
* crc64('php', '0x%X'); // 0xAFE4E823E7CEF190
* crc64('php', '%d'); // -5772233581471534704 signed int
* crc64('php', '%u'); // 12674510492238016912 unsigned int
*/
function crc64($string, $format = '%x')
{
static $crc64tab;
if ($crc64tab === null) {
$crc64tab = crc64Table();
}
$crc = 0;
for ($i = 0; $i < strlen($string); $i++) {
$crc = $crc64tab[($crc ^ ord($string[$i])) & 0xff] ^ (($crc >> 8) & ~(0xff << 56));
}
return sprintf($format, $crc);
}
//uniqid办法
function gen_order_uuid() {
$strUuid = crc64(uniqid(), '%u');
return $strUuid;
}
var_dump(gen_order_uuid());
运转后果:
string(19) "2559207576599622011"
4、随机字符串+crc64办法
function crc64Table()
{
$crc64tab = [];
// ECMA polynomial
$poly64rev = (0xC96C5795 << 32) | 0xD7870F42;
// ISO polynomial
// $poly64rev = (0xD8 << 56);
for ($i = 0; $i < 256; $i++)
{
for ($part = $i, $bit = 0; $bit < 8; $bit++) {
if ($part & 1) {
$part = (($part >> 1) & ~(0x8 << 60)) ^ $poly64rev;
} else {
$part = ($part >> 1) & ~(0x8 << 60);
}
}
$crc64tab[$i] = $part;
}
return $crc64tab;
}
/**
* @param string $string
* @param string $format
* @return mixed
*
* Formats:
* crc64('php'); // afe4e823e7cef190
* crc64('php', '0x%x'); // 0xafe4e823e7cef190
* crc64('php', '0x%X'); // 0xAFE4E823E7CEF190
* crc64('php', '%d'); // -5772233581471534704 signed int
* crc64('php', '%u'); // 12674510492238016912 unsigned int
*/
function crc64($string, $format = '%x')
{
static $crc64tab;
if ($crc64tab === null) {
$crc64tab = crc64Table();
}
$crc = 0;
for ($i = 0; $i < strlen($string); $i++) {
$crc = $crc64tab[($crc ^ ord($string[$i])) & 0xff] ^ (($crc >> 8) & ~(0xff << 56));
}
return sprintf($format, $crc);
}
//随机字符串+crc64办法
function gen_order_randchar() {
$strChar = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
$strRandChar = substr(str_shuffle($strChar), mt_rand(0, strlen($strChar) - 11), 16);
$strRandChar = crc64($strRandChar, '%u');
return $strRandChar;
}
var_dump(gen_order_randchar());
运转后果:
string(20) "15531636943001169826"
5、Twitter SnowFlake算法办法
composer require youthage/snowflake
require_once 'vendor/autoload.php';
use Ysnowflake\Snowflake;
function gen_order_snowflake() {
$objSnowflake = new Snowflake();
return $objSnowflake->nextId();
}
var_dump(gen_order_snowflake());
运转后果:
int(434087018926964736)
感激各位批评、点赞、分享。。。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。