123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- /**
- * Created by PhpStorm.
- * User: zilongs
- * Date: 20-9-23
- * Time: 上午10:56
- */
- use Illuminate\Support\Facades\Log;
- use App\Exceptions\ExitOutException;
- //统一输出格式话的json数据
- if (!function_exists('out')) {
- function out($data = null, $status = 0, $message = 'success', $exceptionData = false)
- {
- $out = ['status' => $status, 'message' => $message, 'data' => $data];
- if ($exceptionData !== false) {
- trace([$message => $exceptionData], 'error');
- }
- return response()->json($out);
- }
- }
- //统一异常输出格式话的json数据
- if (!function_exists('exit_out')) {
- function exit_out($data = null, $status = 0, $message = 'success', $exceptionData = false)
- {
- $out = ['status' => $status, 'message' => $message, 'data' => $data];
- if ($exceptionData !== false) {
- trace([$message => $exceptionData], 'error');
- }
- $json = json_encode($out, JSON_UNESCAPED_UNICODE);
- throw new ExitOutException($json);
- }
- }
- //日志记录
- if (!function_exists('trace')) {
- function trace($log = '', $level = 'info')
- {
- Log::log($level, $log);
- }
- }
- //AES加密
- if (!function_exists('aes_encrypt')) {
- function aes_encrypt($data)
- {
- if (is_array($data)) {
- $data = json_encode($data, JSON_UNESCAPED_UNICODE);
- }
- $key = config('config.aes_key');
- $iv = config('config.aes_iv');
- $cipher_text = openssl_encrypt($data, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
- $cipher_text = base64_encode($cipher_text);
- return urlencode($cipher_text);
- }
- }
- //AES解密
- if (!function_exists('aes_decrypt')) {
- function aes_decrypt($encryptData)
- {
- $encryptData = urldecode($encryptData);
- $encryptData = base64_decode($encryptData);
- $key = config('config.aes_key');
- $iv = config('config.aes_iv');
- $original_plaintext = openssl_decrypt($encryptData, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
- return json_decode($original_plaintext, true);
- }
- }
- //获取distance的sql字段
- if (!function_exists('get_distance_field')) {
- function get_distance_field($latitude, $longitude)
- {
- if (empty($latitude) || empty($longitude)) {
- return '999999999 distance';
- }
- 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';
- }
- }
- //构建单号
- if (!function_exists('build_sn')) {
- function build_sn($id, $len = 2, $prefix = '')
- {
- $idx = 0 - $len;
- $suffix = substr($id, $idx);
- $suffix = str_pad($suffix, $len, '0', STR_PAD_LEFT);
- $sn = $prefix.substr(date('YmdHis'), 2).$suffix;
- return $sn;
- }
- }
|