init
This commit is contained in:
341
app/common.php
Normal file
341
app/common.php
Normal file
@@ -0,0 +1,341 @@
|
||||
<?php
|
||||
// 应用公共文件
|
||||
|
||||
namespace app;
|
||||
|
||||
use app\controller\Aes;
|
||||
use app\controller\HMACMD5;
|
||||
use Firebase\JWT\JWT;
|
||||
use Firebase\JWT\Key;
|
||||
|
||||
class common
|
||||
{
|
||||
function generate_order($length)
|
||||
{
|
||||
$chars = '0123456789';
|
||||
$time = time();
|
||||
$password = 'DZZS' . $time;
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$password .= $chars[mt_rand(0, strlen($chars) - 1)];
|
||||
}
|
||||
return $password;
|
||||
}
|
||||
|
||||
function generate_EquipmentAuth($length)
|
||||
{
|
||||
$chars = '0123456789';
|
||||
$time = time();
|
||||
$password = config('hard.OperatorID') . $time . '1';//1代表是生成的设备认证流水号
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$password .= $chars[mt_rand(0, strlen($chars) - 1)];
|
||||
}
|
||||
return $password;
|
||||
}
|
||||
|
||||
function generate_StartChargeSeq($length)
|
||||
{
|
||||
$chars = '0123456789';
|
||||
$time = time();
|
||||
$password = config('hard.OperatorID') . $time . '2';//2代表是生成的充电订单号
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$password .= $chars[mt_rand(0, strlen($chars) - 1)];
|
||||
}
|
||||
return $password;
|
||||
}
|
||||
|
||||
function returnFree($dian, $fu)
|
||||
{
|
||||
$time = date('H:i', time());
|
||||
// 计算当前电费
|
||||
$dian_1 = '0.00';
|
||||
$ElectricityFee = str_replace('电费:', '', $dian);
|
||||
$Elect = explode(',', $ElectricityFee);
|
||||
|
||||
$dian_arr = [];
|
||||
for ($index = 0; $index < count($Elect); $index++) {
|
||||
$dian_arr[$index]['time_interval'] = substr($Elect[$index], 0, 11);
|
||||
|
||||
$start_time = substr($Elect[$index], 0, 5);
|
||||
$end_time = substr($Elect[$index], 6, 5);
|
||||
|
||||
$dian_arr[$index]['price'] = substr($Elect[$index], 12, 4);
|
||||
|
||||
$dian_arr[$index]['dian_1'] = '';
|
||||
if ($start_time < $time && $end_time > $time) {
|
||||
$dian_arr[$index]['dian_1'] = explode(':', $Elect[$index])[3];
|
||||
$dian_1 = explode(':', $Elect[$index])[3];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 计算服务费
|
||||
$fu_1 = '0.00';
|
||||
$ServiceFee = str_replace('服务费:', '', $fu);
|
||||
|
||||
$Elect = explode(',', $ServiceFee);
|
||||
$fu_arr = [];
|
||||
for ($index = 0; $index < count($Elect); $index++) {
|
||||
$fu_arr[$index]['time_interval'] = substr($Elect[$index], 0, 11);
|
||||
|
||||
$start_time = substr($Elect[$index], 0, 5);
|
||||
$end_time = substr($Elect[$index], 6, 5);
|
||||
|
||||
|
||||
$fu_arr[$index]['price'] = substr($Elect[$index], 12, 4);
|
||||
|
||||
$fu_arr[$index]['fu_1'] = '';
|
||||
if ($start_time < $time && $end_time > $time) {
|
||||
$fu_arr[$index]['dian_1'] = explode(':', $Elect[$index])[3];
|
||||
$fu_1 = explode(':', $Elect[$index])[3];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($dian_arr as $key => &$value) {
|
||||
$value['dian_1'] = false;
|
||||
$start_time = substr($value['time_interval'], 0, 5);
|
||||
$end_time = substr($value['time_interval'], 6, 5);
|
||||
|
||||
if ($start_time < $time && $end_time > $time) {
|
||||
$value['dian_1'] = true;
|
||||
}
|
||||
|
||||
foreach ($fu_arr as $kk => $vv) {
|
||||
|
||||
if ($value['time_interval'] == $vv['time_interval']) {
|
||||
|
||||
$value['price'] = number_format((double)$value['price'], 4);
|
||||
$value['fu_price'] = number_format((double)$vv['price'], 4);
|
||||
$value['total_price'] = number_format($vv['price'] + $value['price'], 4);
|
||||
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
return ['time_price' => number_format($dian_1 + $fu_1, 4), 'price_list' => $dian_arr];
|
||||
|
||||
}
|
||||
|
||||
function getMonthDate($date)
|
||||
{
|
||||
$date_t = array();
|
||||
$date_t[] = $date;
|
||||
for ($i = 0; $i < 4; $i++) {
|
||||
$this_date = strtotime($date);
|
||||
$next_date = date("Y-m-d", strtotime('next monday', $this_date));
|
||||
$date_t[] = $next_date;
|
||||
$date = $next_date;
|
||||
}
|
||||
return $date_t;
|
||||
|
||||
}
|
||||
|
||||
function getSeasonDate($date)
|
||||
{
|
||||
$date_t = array();
|
||||
$date_t[] = $date;
|
||||
for ($i = 0; $i < 2; $i++) {
|
||||
$this_date = strtotime($date);
|
||||
$next_date = date("Y-m-d", strtotime('next month', $this_date));
|
||||
$date_t[] = $next_date;
|
||||
$date = $next_date;
|
||||
}
|
||||
return $date_t;
|
||||
|
||||
}
|
||||
|
||||
function getYearDate($date)
|
||||
{
|
||||
$date_t = array();
|
||||
$date_t[] = $date;
|
||||
for ($i = 0; $i < 11; $i++) {
|
||||
$this_date = strtotime($date);
|
||||
$next_date = date("Y-m-d", strtotime('next month', $this_date));
|
||||
$date_t[] = $next_date;
|
||||
$date = $next_date;
|
||||
}
|
||||
return $date_t;
|
||||
|
||||
}
|
||||
|
||||
function getDayDate($date)
|
||||
{
|
||||
$date_t = array();
|
||||
$date_t[] = $date;
|
||||
$this_date = strtotime($date);
|
||||
$end_date = date("Y-m-d H:i:s", strtotime('+4 hour -1 second', $this_date));
|
||||
$date_end[] = $end_date;
|
||||
for ($i = 0; $i < 5; $i++) {
|
||||
$this_date = strtotime($date);
|
||||
$next_date = date("Y-m-d H:i:s", strtotime('+4 hour', $this_date));
|
||||
$this_date_end = strtotime($end_date);
|
||||
$next_date_end = date("Y-m-d H:i:s", strtotime('+4 hour', $this_date_end));
|
||||
$date_end[] = $next_date_end;
|
||||
$date_t[] = $next_date;
|
||||
$date = $next_date;
|
||||
$next_date_end = date("Y-m-d H:59:59", strtotime($next_date_end));
|
||||
$end_date = $next_date_end;
|
||||
}
|
||||
$kk['start_time'] = $date_t;
|
||||
$kk['end_time'] = $date_end;
|
||||
return $kk;
|
||||
|
||||
}
|
||||
|
||||
function getDistance($lat1, $lng1, $lat2, $lng2)
|
||||
{
|
||||
$earthRadius = 6367000;
|
||||
$lat1 = ($lat1 * pi()) / 180;
|
||||
$lng1 = ($lng1 * pi()) / 180;
|
||||
$lat2 = ($lat2 * pi()) / 180;
|
||||
$lng2 = ($lng2 * pi()) / 180;
|
||||
$calcLongitude = $lng2 - $lng1;
|
||||
$calcLatitude = $lat2 - $lat1;
|
||||
$stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
|
||||
$stepTwo = 2 * asin(min(1, sqrt($stepOne)));
|
||||
$calculatedDistance = $earthRadius * $stepTwo;
|
||||
return round($calculatedDistance / 1000, 2);
|
||||
}
|
||||
|
||||
|
||||
function Encrypt($data)
|
||||
{
|
||||
$aes = new Aes();
|
||||
return $aes->encrypt($data);
|
||||
}
|
||||
|
||||
function Decrypt($data)
|
||||
{
|
||||
$aes = new Aes();
|
||||
return $aes->decrypt($data);
|
||||
}
|
||||
|
||||
function HmacMD5($data)
|
||||
{
|
||||
$md5 = new HMACMD5();
|
||||
return $md5->HMAC($data);
|
||||
}
|
||||
|
||||
function CurlSend($url, $data = '', $token = '')
|
||||
{
|
||||
$data = json_encode($data);
|
||||
// echo $data;
|
||||
|
||||
$ch = curl_init();
|
||||
$headers = array();
|
||||
if ($token) {
|
||||
$headers[] = 'Authorization: Bearer ' . $token;
|
||||
}
|
||||
$headers[] = 'Content-Type: application/json;charset=utf-8';
|
||||
$headers[] = 'Content-Length: ' . strlen($data);
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
|
||||
curl_setopt($ch, CURLOPT_HEADER, 0);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
||||
$curl_return = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
return json_decode(trim($curl_return), true);
|
||||
}
|
||||
|
||||
function checkParams($params)
|
||||
{
|
||||
$arr = ['OperatorID', 'Data', 'TimeStamp', 'Seq', 'Sig'];
|
||||
// 参数检查
|
||||
foreach ($arr as $v) {
|
||||
if (empty($params[$v])) {
|
||||
return $v;
|
||||
}
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
|
||||
function checkSig($OperatorID, $params)
|
||||
{
|
||||
$sig = $OperatorID . $params['Data'] . $params['TimeStamp'] . $params['Seq'];
|
||||
$ecry_sig = $this->HmacMD5($sig);
|
||||
if ($params['Sig'] != $ecry_sig) {
|
||||
return 1;
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
function signToken()
|
||||
{
|
||||
$key = config('hard.DataSecretIV'); //这里是自定义的一个随机字串,应该写在config文件中的,解密时也会用,相当 于加密中常用的 盐 salt
|
||||
$token = array(
|
||||
"iss" => $key, //签发者 可以为空
|
||||
"aud" => '', //面象的用户,可以为空
|
||||
"iat" => time(), //签发时间
|
||||
"nbf" => time() + 1, //在什么时候jwt开始生效 (这里表示生成100秒后才生效)
|
||||
"exp" => time() + 7200, //token 过期时间
|
||||
"data" => [ //记录的userid的信息,这里是自已添加上去的,如果有其它信息,可以再添加数组的键值对
|
||||
'name' => 'zuxing'
|
||||
]
|
||||
);
|
||||
// print_r($token);
|
||||
$jwt = JWT::encode($token, $key, "HS256"); //根据参数生成了 token
|
||||
return $jwt;
|
||||
}
|
||||
|
||||
|
||||
//验证token
|
||||
function checkToken_assess($token)
|
||||
{
|
||||
$key = config('hard.DataSecretIV');
|
||||
$key = new Key($key, 'HS256');
|
||||
$status = array("code" => 2);
|
||||
try {
|
||||
JWT::$leeway = 60;//当前时间减去60,把时间留点余地
|
||||
$decoded = JWT::decode($token, $key, array('HS256')); //HS256方式,这里要和签发的时候对应
|
||||
$arr = (array)$decoded;
|
||||
$res['code'] = 1;
|
||||
$res['data'] = $arr['data'];
|
||||
return 2;
|
||||
|
||||
} catch (\Firebase\JWT\SignatureInvalidException $e) { //签名不正确
|
||||
$status['msg'] = "签名不正确";
|
||||
return 1;
|
||||
} catch (\Firebase\JWT\BeforeValidException $e) { // 签名在某个时间点之后才能用
|
||||
$status['msg'] = "token失效";
|
||||
return 1;
|
||||
} catch (\Firebase\JWT\ExpiredException $e) { // token过期
|
||||
$status['msg'] = "token失效";
|
||||
return 1;
|
||||
} catch (\Exception $e) { //其他错误
|
||||
$status['msg'] = "未知错误";
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
function encodeData($data, $params, $ret, $msg = '')
|
||||
{
|
||||
$data = json_encode($data);
|
||||
$data = $this->Encrypt($data);
|
||||
$seq = empty($Seq) ? '0001' : $params['Seq'];
|
||||
$datetime = empty($TimeStamp) ? date('YmdHis') : $params['TimeStamp'];
|
||||
$sig = $params['OperatorID'] . $data . $datetime . $seq;
|
||||
$s = $this->HmacMD5($sig);
|
||||
return json(['Msg' => $msg, 'Ret' => $ret, 'Data' => $data, 'Sig' => $s]);
|
||||
}
|
||||
|
||||
function generate_password($length)
|
||||
{
|
||||
$chars = '0123456789';
|
||||
$time = time();
|
||||
$password = config('hard.OperatorID') . $time;
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$password .= $chars[mt_rand(0, strlen($chars) - 1)];
|
||||
}
|
||||
return $password;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user