common.php
4.4 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
use \think\Db;
use app\nsms\nsms;
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 = '';
$password = '';
$sendAPI = new nsms($url, $username, $password);
$sendAPI->data = $data;/*初始化数据包*/
$return = $sendAPI->sendSMS('GET');
return $return;
}