12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- // 应用公共函数库文件
- /**
- * 获取当前访问的渠道(微信小程序、H5、APP等)
- * @return string|null
- */
- function getPlatform()
- {
- static $value = 'H5';
- // 从header中获取 channel
- empty($value) && $value = request()->header('platform');
- // 调试模式下可通过param中获取
- if (is_debug() && empty($value)) {
- $value = request()->param('platform');
- }
- return $value;
- }
- // 加密函数
- function encrypt0(string $data, $key = 'vp-256-bit-secret-key')
- {
- $method = 'AES-256-CBC';
- //$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($method));
- $iv = random_bytes(openssl_cipher_iv_length($method));
- //$iv = mb_convert_encoding($iv,'utf8');
- $encrypted = openssl_encrypt($data, $method, $key, 0, $iv);
- // 将iv和加密数据拼接在一起,然后进行base64编码
- return base64_encode($iv . $encrypted);
- }
- function encrypt(string $data)
- {
- $timestamp = time();
- $pre0 = str_repeat('0', 10 - strlen($data));
- return strrev(strval($timestamp)) . $pre0 . $data;
- }
- // 解密函数
- function decrypt0($data, $key = 'vp-256-bit-secret-key')
- {
- $method = 'AES-256-CBC';
- // 解码加密字符串,并分离iv
- $decoded = base64_decode($data);
- $iv = substr($decoded, 0, openssl_cipher_iv_length($method));
- $decrypted = openssl_decrypt(substr($decoded, openssl_cipher_iv_length($method)), $method, $key, 0, $iv);
- return $decrypted;
- }
- function decrypt($data)
- {
- if (empty($data) || strlen($data) < 10){
- return 0;
- }
- return intval(substr($data, 10));
- }
|