functions.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zilongs
  5. * Date: 20-9-23
  6. * Time: 上午10:56
  7. */
  8. use Illuminate\Support\Facades\Log;
  9. use App\Exceptions\ExitOutException;
  10. //统一输出格式话的json数据
  11. if (!function_exists('out')) {
  12. function out($data = null, $code = 200, $message = 'success')
  13. {
  14. $out = ['code' => $code, 'message' => $message, 'data' => $data];
  15. return response()->json($out);
  16. }
  17. }
  18. //统一异常输出格式话的json数据
  19. if (!function_exists('exit_out')) {
  20. function exit_out($data = null, $code = 500, $message = 'fail')
  21. {
  22. $out = ['code' => $code, 'message' => $message, 'data' => $data];
  23. $message = json_encode($out, JSON_UNESCAPED_UNICODE);
  24. throw new ExitOutException($message);
  25. }
  26. }
  27. //日志记录
  28. if (!function_exists('trace')) {
  29. function trace($log = '', $level = 'info')
  30. {
  31. Log::log($level, $log);
  32. }
  33. }
  34. //AES加密
  35. if (!function_exists('aes_encrypt')) {
  36. function aes_encrypt($data)
  37. {
  38. if (is_array($data)) {
  39. $data = json_encode($data, JSON_UNESCAPED_UNICODE);
  40. }
  41. $key = config('config.aes_key');
  42. $iv = config('config.aes_iv');
  43. $cipher_text = openssl_encrypt($data, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
  44. $cipher_text = base64_encode($cipher_text);
  45. return urlencode($cipher_text);
  46. }
  47. }
  48. //AES解密
  49. if (!function_exists('aes_decrypt')) {
  50. function aes_decrypt($encryptData)
  51. {
  52. $encryptData = urldecode($encryptData);
  53. $encryptData = base64_decode($encryptData);
  54. $key = config('config.aes_key');
  55. $iv = config('config.aes_iv');
  56. $original_plaintext = openssl_decrypt($encryptData, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
  57. return json_decode($original_plaintext, true);
  58. }
  59. }