common.php
6.0 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
167
168
169
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
203
204
205
206
207
208
209
210
211
212
213
214
<?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;
}
}