common.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. // 应用公共函数库文件
  13. /**
  14. * 获取当前访问的渠道(微信小程序、H5、APP等)
  15. * @return string|null
  16. */
  17. function getPlatform()
  18. {
  19. static $value = 'H5';
  20. // 从header中获取 channel
  21. empty($value) && $value = request()->header('platform');
  22. // 调试模式下可通过param中获取
  23. if (is_debug() && empty($value)) {
  24. $value = request()->param('platform');
  25. }
  26. return $value;
  27. }
  28. // 加密函数
  29. function encrypt0(string $data, $key = 'vp-256-bit-secret-key')
  30. {
  31. $method = 'AES-256-CBC';
  32. //$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($method));
  33. $iv = random_bytes(openssl_cipher_iv_length($method));
  34. //$iv = mb_convert_encoding($iv,'utf8');
  35. $encrypted = openssl_encrypt($data, $method, $key, 0, $iv);
  36. // 将iv和加密数据拼接在一起,然后进行base64编码
  37. return base64_encode($iv . $encrypted);
  38. }
  39. function encrypt(string $data)
  40. {
  41. $timestamp = time();
  42. $pre0 = str_repeat('0', 10 - strlen($data));
  43. return strrev(strval($timestamp)) . $pre0 . $data;
  44. }
  45. // 解密函数
  46. function decrypt0($data, $key = 'vp-256-bit-secret-key')
  47. {
  48. $method = 'AES-256-CBC';
  49. // 解码加密字符串,并分离iv
  50. $decoded = base64_decode($data);
  51. $iv = substr($decoded, 0, openssl_cipher_iv_length($method));
  52. $decrypted = openssl_decrypt(substr($decoded, openssl_cipher_iv_length($method)), $method, $key, 0, $iv);
  53. return $decrypted;
  54. }
  55. function decrypt($data)
  56. {
  57. if (empty($data) || strlen($data) < 10){
  58. return 0;
  59. }
  60. return intval(substr($data, 10));
  61. }