common.php 6.0 KB
<?php
use \think\Db;
if (!function_exists('generate_user_token')) {
    /**
     * 生成token
     * @param $userId
     * @return string
     */
    function generate_user_token($userId)
    {
        $userTokenQuery = Db::name("user_token")
            ->where('user_id', $userId);
        $findUserToken  = $userTokenQuery->find();
        $currentTime    = time();
        $expireTime     = $currentTime + 24 * 3600 * 180;
        $token          = md5(uniqid()) . md5(uniqid());
        if (empty($findUserToken)) {
            Db::name("user_token")->insert([
                'token'       => $token,
                'user_id'     => $userId,
                'expiretime' => $expireTime,
                'createtime' => $currentTime,
            ]);
        } else {
            Db::name("user_token")
                ->where('user_id', $userId)
                ->update([
                    'token'      => $token,
                    'expiretime' => $expireTime,
                    'createtime' => $currentTime,
                ]);
        }

        return $token;
    }
}
if (!function_exists('get_current_user_token')) {
    /**
     * 获取当前登录前台用户token
     * @return int
     */
    function get_current_user_token()
    {
        $sessionUserToken = session('token');
        if (empty($sessionUserToken)) {
            return 0;
        }

        return $sessionUserToken;
    }
}
if (!function_exists('distance')) {
    /**
     * 根据经纬度计算两地的距离
     * @param $work
     * @param $user
     * @return float|int
     */
    function distance($work, $user)
    {
        $lng1 = $user['lng'];//经度1
        $lat1 = $user['lat'];//纬度1
        $lng2 = $work['lng'];//经度2
        $lat2 = $work['lat'];//纬度2

        $EARTH_RADIUS = 6378137;   //地球半径
        $RAD = pi() / 180.0;

        $radLat1 = $lat1 * $RAD;
        $radLat2 = $lat2 * $RAD;
        $a = $radLat1 - $radLat2;    // 两点纬度差
        $b = ($lng1 - $lng2) * $RAD;  // 两点经度差
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
        $s = $s * $EARTH_RADIUS;
        $result = round($s * 10000) / 10000;
        return round($result/1000,2);
    }
}
if (!function_exists('getAge')) {
    /**
     * 根据出生日期计算出年龄
     * @param $birthday
     * @return false|string
     */
    function getAge($birthday)
    {
        $birthday = strtotime($birthday);
        //格式化出生时间年月日
        $byear = date('Y', $birthday);
        $bmonth = date('m', $birthday);
        $bday = date('d', $birthday);

        //格式化当前时间年月日
        $tyear = date('Y');
        $tmonth = date('m');
        $tday = date('d');

        //开始计算年龄
        $age = $tyear - $byear;
        if ($bmonth > $tmonth || $bmonth == $tmonth && $bday > $tday) {
            $age--;
        }
        return $age;
    }
}
if (!function_exists('generateCode')) {
    /**
     * 生成n位随机数
     * @param int $length
     * @return int
     */
    function generateCode($length = 6)
    {
        $min = pow(10, ($length - 1));
        $max = pow(10, $length) - 1;
        return rand($min, $max);
    }
}

if(!function_exists('send_sms')){
    /**
     * 发送手机短信
     * @param $data 数据内容
     * @return mixed
     */
    function send_sms($data){
        //todo 短信账号密码
        require_once EXTEND_PATH . 'nsms/nsms.php';
        $url 		= "http://www.ztsms.cn/sendNSms.do";
        $username 	= 'gongpinda';
        $password 	= 'Cxz307312';
        $sendAPI = new \sendAPI($url, $username, $password);
        $sendAPI->data = $data;/*初始化数据包*/
        $return = $sendAPI->sendSMS('GET');
        return $return;
    }
}

if(!function_exists('send_sms2')) {
    function send_sms2($data){
        //todo 短信账号密码
        require_once EXTEND_PATH . 'nsms/nsms2.php';
        $url 		= "https://api.mix2.zthysms.com/v2/sendSms";
        $username 	= 'gongpinda';
        $password 	= 'Cxz307312';
        $sendAPI = new \sendAPI($url, $username, $password);
        $sendAPI->data = $data;/*初始化数据包*/
        $return = $sendAPI->sendSMS('POST');
        return $return;
    }
}

if(!function_exists('get_order_sn')) {
    /**
     * 获取惟一订单号
     * @return string
     */
    function get_order_sn()
    {
        return date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }
}

if(!function_exists('time_tran')){
    /**
     * 获取时间差
     * @param $the_time 时间戳
     * @return string
     */
    function time_tran($timer) {
        $diff = $_SERVER['REQUEST_TIME'] - $timer;
        $day = floor($diff / 86400);
        $free = $diff % 86400;
        if($day > 0) {
            return $day."天前";
        }else{
            if($free>0){
                $hour = floor($free / 3600);
                $free = $free % 3600;
                if($hour>0){
                    return $hour."小时前";
                }else{
                    if($free>0){
                        $min = floor($free / 60);
                        $free = $free % 60;
                        if($min>0){
                            return $min."分钟前";
                        }else{
                            if($free>0){
                                return $free."秒前";
                            }else{
                                return '刚刚';
                            }
                        }
                    }else{
                        return '刚刚';
                    }
                }
            }else{
                return '刚刚';
            }
        }
    }
}

if(!function_exists('getLocation')){
    function getLocation(){
        $ip = "111.164.175.116";//request()->ip(0, false);
        $url = "http://api.map.baidu.com/location/ip?ak=lu7CH9ucr6STjUk1GQgkfzjhpHbb7hPd&ip=$ip&coor=bd09ll";
        $data = \fast\Http::get($url);
        $data = json_decode($data,true);
        return $data;
    }
}