审查视图

application/index/common.php 6.0 KB
郭盛 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
<?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([
王晓刚 authored
28
                    'token'      => $token,
郭盛 authored
29 30 31 32 33 34 35 36
                    'expiretime' => $expireTime,
                    'createtime' => $currentTime,
                ]);
        }

        return $token;
    }
}
王晓刚 authored
37
if (!function_exists('get_current_user_token')) {
郭盛 authored
38
    /**
王晓刚 authored
39
     * 获取当前登录前台用户token
郭盛 authored
40 41
     * @return int
     */
王晓刚 authored
42
    function get_current_user_token()
郭盛 authored
43
    {
王晓刚 authored
44 45
        $sessionUserToken = session('token');
        if (empty($sessionUserToken)) {
郭盛 authored
46 47 48
            return 0;
        }
王晓刚 authored
49
        return $sessionUserToken;
郭盛 authored
50 51 52 53 54 55 56 57 58 59 60
    }
}
if (!function_exists('distance')) {
    /**
     * 根据经纬度计算两地的距离
     * @param $work
     * @param $user
     * @return float|int
     */
    function distance($work, $user)
    {
王晓刚 authored
61 62 63 64
        $lng1 = $user['lng'];//经度1
        $lat1 = $user['lat'];//纬度1
        $lng2 = $work['lng'];//经度2
        $lat2 = $work['lat'];//纬度2
郭盛 authored
65 66 67 68 69 70 71 72 73 74 75

        $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;
王晓刚 authored
76
        return round($result/1000,2);
郭盛 authored
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    }
}
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;
    }
}
王晓刚 authored
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
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";
王晓刚 authored
130
        $username 	= 'gongpinda';
王晓刚 authored
131 132 133 134 135 136 137 138
        $password 	= 'Cxz307312';
        $sendAPI = new \sendAPI($url, $username, $password);
        $sendAPI->data = $data;/*初始化数据包*/
        $return = $sendAPI->sendSMS('GET');
        return $return;
    }
}
王晓刚 authored
139 140 141 142 143 144 145 146 147 148 149 150 151 152
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;
    }
}
王晓刚 authored
153
if(!function_exists('get_order_sn')) {
郭盛 authored
154 155 156 157 158 159 160 161 162
    /**
     * 获取惟一订单号
     * @return string
     */
    function get_order_sn()
    {
        return date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }
}
王晓刚 authored
163 164

if(!function_exists('time_tran')){
郭盛 authored
165
    /**
王晓刚 authored
166 167
     * 获取时间差
     * @param $the_time 时间戳
郭盛 authored
168 169
     * @return string
     */
王晓刚 authored
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    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 '刚刚';
            }
        }
郭盛 authored
203
    }
王晓刚 authored
204 205 206 207 208 209 210 211 212 213
}

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;
    }
郭盛 authored
214
}