functions.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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, $status = 0, $message = 'success', $exceptionData = false)
  13. {
  14. $out = ['status' => $status, '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, $status = 0, $message = 'success', $exceptionData = false)
  24. {
  25. $out = ['status' => $status, '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. }
  66. //获取distance的sql字段
  67. if (!function_exists('get_distance_field')) {
  68. function get_distance_field($latitude, $longitude)
  69. {
  70. if (empty($latitude) || empty($longitude)) {
  71. return '999999999 distance';
  72. }
  73. return 'if(longitude=0 and latitude=0,999999999,round(6378.138*2*asin(sqrt(pow(sin( (' . $latitude . '*pi()/180-latitude*pi()/180)/2),2)+cos(' . $latitude . '*pi()/180)*cos(latitude*pi()/180)* pow(sin((' . $longitude . '*pi()/180-longitude*pi()/180)/2),2)))*1000)) distance';
  74. }
  75. }
  76. //构建单号
  77. if (!function_exists('build_sn')) {
  78. function build_sn($id, $len = 2, $prefix = '')
  79. {
  80. $idx = 0 - $len;
  81. $suffix = substr($id, $idx);
  82. $suffix = str_pad($suffix, $len, '0', STR_PAD_LEFT);
  83. $sn = $prefix.substr(date('YmdHis'), 2).$suffix;
  84. return $sn;
  85. }
  86. }