common.php 1.9 KB

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