functions.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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', $exceptionData = false)
  13. {
  14. $out = ['code' => $code, 'message' => $message, 'data' => $data];
  15. if ($exceptionData !== false) {
  16. trace([$message => $exceptionData], 'error');
  17. }
  18. return response()->json($out);
  19. }
  20. }
  21. //统一异常输出格式话的json数据
  22. if (!function_exists('exit_out')) {
  23. function exit_out($data = null, $code = 200, $message = 'success', $exceptionData = false)
  24. {
  25. $out = ['code' => $code, 'message' => $message, 'data' => $data];
  26. if ($exceptionData !== false) {
  27. trace([$message => $exceptionData], 'error');
  28. }
  29. $json = json_encode($out, JSON_UNESCAPED_UNICODE);
  30. throw new ExitOutException($json);
  31. }
  32. }
  33. //日志记录
  34. if (!function_exists('trace')) {
  35. function trace($log = '', $level = 'info')
  36. {
  37. Log::log($level, $log);
  38. }
  39. }
  40. //AES加密
  41. if (!function_exists('aes_encrypt')) {
  42. function aes_encrypt($data)
  43. {
  44. if (is_array($data)) {
  45. $data = json_encode($data, JSON_UNESCAPED_UNICODE);
  46. }
  47. $key = config('config.aes_key');
  48. $iv = config('config.aes_iv');
  49. $cipher_text = openssl_encrypt($data, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
  50. $cipher_text = base64_encode($cipher_text);
  51. return urlencode($cipher_text);
  52. }
  53. }
  54. //AES解密
  55. if (!function_exists('aes_decrypt')) {
  56. function aes_decrypt($encryptData)
  57. {
  58. $encryptData = urldecode($encryptData);
  59. $encryptData = base64_decode($encryptData);
  60. $key = config('config.aes_key');
  61. $iv = config('config.aes_iv');
  62. $original_plaintext = openssl_decrypt($encryptData, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
  63. return json_decode($original_plaintext, true);
  64. }
  65. }