common.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 encrypt($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. $encrypted = openssl_encrypt($data, $method, $key, 0, $iv);
  34. // 将iv和加密数据拼接在一起,然后进行base64编码
  35. return base64_encode($iv . $encrypted);
  36. }
  37. // 解密函数
  38. function decrypt($data, $key = 'vp-256-bit-secret-key')
  39. {
  40. $method = 'AES-256-CBC';
  41. // 解码加密字符串,并分离iv
  42. $decoded = base64_decode($data);
  43. $iv = substr($decoded, 0, openssl_cipher_iv_length($method));
  44. $decrypted = openssl_decrypt(substr($decoded, openssl_cipher_iv_length($method)), $method, $key, 0, $iv);
  45. return $decrypted;
  46. }