随着前端业务越来越复杂,自定义表单数据量比较大,每条数据的id生成则至关重要。想到前期IOS中实现的雪花算法ID,照着其实现JS版本,供大家学习参考。
一、库的建立引入
在你项目中创建一个snowFlake.js的文件:拷贝以下内容进去。
import bigInt from 'big-integer'
export default class SnowFlake {
constructor(_workerId=1, _dataCenterId=1, _sequence=0) {
// 开始时间截 (2012-01-01),这个可以设置开始使⽤该系统的时间,可往后使⽤69年
this.twepoch = 1325347200000;
this.workerIdBits = 5;
this.dataCenterIdBits = 5;
this.maxWorkerId = -1 ^ (-1 << this.workerIdBits) //值为31
this.maxDataCenterId = -1 ^ (-1 << this.dataCenterIdBits) //值为31
this.seq