123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- /**
- * Created by PhpStorm.
- * User: zilongs
- * Date: 20-9-23
- * Time: 上午10:56
- */
- use Illuminate\Support\Facades\Log;
- use App\Exceptions\ExitOutException;
- use App\Models\User;
- //统一输出格式话的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';
- }
- }
- //获取用户的distance的sql字段
- if (!function_exists('get_user_distance_field')) {
- function get_user_distance_field($user)
- {
- $coordinate = get_user_coordinate($user);
- $latitude = $coordinate['latitude'];
- $longitude = $coordinate['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;
- }
- }
- //生日转年龄
- if (!function_exists('birthday_to_age')){
- function birthday_to_age($birthday)
- {
- list($year, $month, $day) = explode("-", $birthday);
- $year_diff = (date("Y") - $year) > 0 ? date("Y") - $year.'岁':'';
- $month_diff = (date("m") - $month) > 0 ? date("m") - $month.'个月':'';
- $day_diff = (date("d") - $day) > 0 ? date("d") - $day.'天':'';
- if ($day_diff < 0 || $month_diff < 0) {
- $year_diff--;
- }
- return $year_diff.$month_diff.$day_diff ;
- }
- }
- //计算经纬度两点之间距离(返回为米)
- if (!function_exists('get_distance')) {
- function get_distance($lat1, $lng1, $lat2, $lng2)
- {
- if (empty($lat1) || empty($lng1) || empty($lat2) || empty($lng2)) {
- return '999999999';
- }
- $earthRadius = 6378138;
- $lat1 = ($lat1 * pi()) / 180;
- $lng1 = ($lng1 * pi()) / 180;
- $lat2 = ($lat2 * pi()) / 180;
- $lng2 = ($lng2 * pi()) / 180;
- $calcLongitude = $lng2 - $lng1;
- $calcLatitude = $lat2 - $lat1;
- $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
- $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
- $calculatedDistance = $earthRadius * $stepTwo;
- return number_format($calculatedDistance, 2, '.', '');
- }
- }
- //获取用户坐标
- if (!function_exists('get_user_coordinate')) {
- function get_user_coordinate($user)
- {
- $req = request()->post();
- if (empty($req['latitude']) || empty($req['longitude'])) {
- $latitude = $user['latitude'];
- $longitude = $user['longitude'];
- }
- else {
- $latitude = $req['latitude'];
- $longitude = $req['longitude'];
- }
- return ['latitude' => $latitude, 'longitude' => $longitude];
- }
- }
- //获取用户距离
- if (!function_exists('get_user_distance')) {
- function get_user_distance($user, $lat, $lng)
- {
- $coordinate = get_user_coordinate($user);
- $data = get_distance($coordinate['latitude'], $coordinate['longitude'], $lat, $lng);
- return $data;
- }
- }
|