common.php 4.8 KB
<?php
use \think\Db;
use app\nsms\nsms;


/**
 * 传json字符串
 * @param $code
 * @param array $data
 * @param string $msg
 * @param int $errorCode
 * @return \think\response\Json
 */
function writeJson($data = [], $code = 0, $msg = 'ok', $errorCode = 200)
{
    $data = [
        'code' => $code,
        'data' => $data,
        'msg' => $msg,
        'time' => time()
    ];
    return json($data, $errorCode);
}

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('update_current_user')){
    /**
     * 更新当前登录前台用户的信息
     * @param array $user 前台用户的信息
     */
    function update_current_user($user)
    {
        session('user', $user);
    }
}
if (!function_exists('get_current_user_id')) {
    /**
     * 获取当前登录前台用户id
     * @return int
     */
    function get_current_user_id()
    {
        $sessionUserId = session('user.id');
        if (empty($sessionUserId)) {
            return 0;
        }

        return $sessionUserId;
    }
}
if (!function_exists('distance')) {
    /**
     * 根据经纬度计算两地的距离
     * @param $work
     * @param $user
     * @return float|int
     */
    function distance($work, $user)
    {
        $lng1 = $user['longitude'];//经度1
        $lat1 = $user['latitude'];//纬度1
        $lng2 = $work['longitude'];//经度2
        $lat2 = $work['latitude'];//纬度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 $result;
    }
}
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('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('get_client_ip')){
    /**
     * 获取客户端IP地址
     * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
     * @param boolean $adv 是否进行高级模式获取(有可能被伪装)
     * @return string
     */
    function get_client_ip($type = 0, $adv = false)
    {
        return request()->ip($type, $adv);
    }
}

/**
 * 生成n位随机数
 * @param int $length
 * @return int
 */
function generateCode($length = 6) {
    $min = pow(10 , ($length - 1));
    $max = pow(10, $length) - 1;
    return rand($min, $max);
}

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