// +---------------------------------------------------------------------- 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)); }