CodeExtend.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\extra\tools;
  3. class CodeExtend
  4. {
  5. /**
  6. * 生成随机编码
  7. * @param integer $size 编码长度
  8. * @param integer $type 编码类型(1纯数字,2纯字母,3数字字母)
  9. * @param string $prefix 编码前缀
  10. * @return string
  11. */
  12. public static function random(int $size = 10, int $type = 1, string $prefix = ''): string
  13. {
  14. $numbs = '0123456789';
  15. $chars = 'abcdefghijklmnopqrstuvwxyz';
  16. if ($type === 1) $chars = $numbs;
  17. if ($type === 3) $chars = "{$numbs}{$chars}";
  18. $code = $prefix . $chars[rand(1, strlen($chars) - 1)];
  19. while (strlen($code) < $size) $code .= $chars[rand(0, strlen($chars) - 1)];
  20. return $code;
  21. }
  22. /**
  23. * 生成日期编码
  24. * @param integer $size 编码长度
  25. * @param string $prefix 编码前缀
  26. * @return string
  27. */
  28. public static function uniqidDate(int $size = 16, string $prefix = ''): string
  29. {
  30. if ($size < 14) $size = 14;
  31. $code = $prefix . date('Ymd') . (date('H') + date('i')) . date('s');
  32. while (strlen($code) < $size) $code .= rand(0, 9);
  33. return $code;
  34. }
  35. /**
  36. * 生成数字编码
  37. * @param integer $size 编码长度
  38. * @param string $prefix 编码前缀
  39. * @return string
  40. */
  41. public static function uniqidNumber(int $size = 12, string $prefix = ''): string
  42. {
  43. $time = strval(time());
  44. if ($size < 10) $size = 10;
  45. $code = $prefix . (intval($time[0]) + intval($time[1])) . substr($time, 2) . rand(0, 9);
  46. while (strlen($code) < $size) $code .= rand(0, 9);
  47. return $code;
  48. }
  49. /**
  50. * 文本转码
  51. * @param string $text 文本内容
  52. * @param string $target 目标编码
  53. * @return string
  54. */
  55. public static function text2utf8(string $text, string $target = 'UTF-8'): string
  56. {
  57. [$first2, $first4] = [substr($text, 0, 2), substr($text, 0, 4)];
  58. if ($first4 === chr(0x00) . chr(0x00) . chr(0xFE) . chr(0xFF)) $ft = 'UTF-32BE';
  59. elseif ($first4 === chr(0xFF) . chr(0xFE) . chr(0x00) . chr(0x00)) $ft = 'UTF-32LE';
  60. elseif ($first2 === chr(0xFE) . chr(0xFF)) $ft = 'UTF-16BE';
  61. elseif ($first2 === chr(0xFF) . chr(0xFE)) $ft = 'UTF-16LE';
  62. return mb_convert_encoding($text, $target, $ft ?? mb_detect_encoding($text));
  63. }
  64. /**
  65. * 数据解密处理
  66. * @param mixed $data 加密数据
  67. * @param string $skey 安全密钥
  68. * @return string
  69. */
  70. public static function encrypt($data, string $skey): string
  71. {
  72. $iv = static::random(16, 3);
  73. $value = openssl_encrypt(serialize($data), 'AES-256-CBC', $skey, 0, $iv);
  74. return static::enSafe64(json_encode(['iv' => $iv, 'value' => $value]));
  75. }
  76. /**
  77. * 数据加密处理
  78. * @param string $data 解密数据
  79. * @param string $skey 安全密钥
  80. * @return mixed
  81. */
  82. public static function decrypt(string $data, string $skey)
  83. {
  84. $attr = json_decode(static::deSafe64($data), true);
  85. return unserialize(openssl_decrypt($attr['value'], 'AES-256-CBC', $skey, 0, $attr['iv']));
  86. }
  87. /**
  88. * Base64Url 安全编码
  89. * @param string $text 待加密文本
  90. * @return string
  91. */
  92. public static function enSafe64(string $text): string
  93. {
  94. return rtrim(strtr(base64_encode($text), '+/', '-_'), '=');
  95. }
  96. /**
  97. * Base64Url 安全解码
  98. * @param string $text 待解密文本
  99. * @return string
  100. */
  101. public static function deSafe64(string $text): string
  102. {
  103. return base64_decode(str_pad(strtr($text, '-_', '+/'), strlen($text) % 4, '='));
  104. }
  105. /**
  106. * 压缩数据对象
  107. * @param mixed $data
  108. * @return string
  109. */
  110. public static function enzip($data): string
  111. {
  112. return static::enSafe64(gzcompress(serialize($data)));
  113. }
  114. /**
  115. * 解压数据对象
  116. * @param string $string
  117. * @return mixed
  118. */
  119. public static function dezip(string $string)
  120. {
  121. return unserialize(gzuncompress(static::deSafe64($string)));
  122. }
  123. }