RandomUtil.java 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package jnpf.util;
  2. import com.github.yitter.contract.IdGeneratorOptions;
  3. import com.github.yitter.idgen.YitIdHelper;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.stereotype.Component;
  6. import java.util.Random;
  7. /**
  8. * @author JNPF开发平台组
  9. * @version V3.1.0
  10. * @copyright 引迈信息技术有限公司
  11. * @date 2021/3/16 8:54
  12. */
  13. @Slf4j
  14. @Component
  15. public class RandomUtil {
  16. static {
  17. if(YitIdHelper.getIdGenInstance() == null){
  18. IdGeneratorOptions options = new IdGeneratorOptions((short) 1);
  19. // options.WorkerIdBitLength = 8;
  20. // options.SeqBitLength = 4;
  21. YitIdHelper.setIdGenerator(options);
  22. }
  23. }
  24. public static void main(String[] args) {
  25. System.out.println(uuId());
  26. }
  27. /**
  28. * 生成主键id
  29. *
  30. * @return
  31. */
  32. public static String uuId() {
  33. long newId = YitIdHelper.nextId();
  34. return newId + "";
  35. }
  36. /**
  37. * 生成6位数随机英文
  38. *
  39. * @return
  40. */
  41. public static String enUuId() {
  42. String str = "";
  43. for (int i = 0; i < 6; i++) {
  44. //你想生bai成几个字符的,du就把3改成zhi几,dao如果改成1,那就生成一个1653随机字母.
  45. str = str + (char) (Math.random() * 26 + 'a');
  46. }
  47. return str;
  48. }
  49. /**
  50. * 生成排序编码
  51. *
  52. * @return
  53. */
  54. public static Long parses() {
  55. Long time = 0L;
  56. return time;
  57. }
  58. /**
  59. * 生成短信验证码
  60. *
  61. * @return
  62. */
  63. public static String getRandomCode() {
  64. String code = "";
  65. Random rand = new Random();
  66. for (int i = 0; i < 6; i++) {
  67. int ran = rand.nextInt(10);
  68. code = code + ran;
  69. }
  70. return code;
  71. }
  72. }