//树
function tree(&$data,$pid = 0,$count = 1) {
static $treeList = array();
foreach ($data as $key => $value){
if($value['pid']==$pid){
$value['count'] = $count;
$treeList []=$value;
// unset($data[$key]);
tree($data,$value['id'],$count+1);
}
}
return $treeList;
}
//树
function generateTree($list, $pk = 'id', $pid = 'pid', $child = 'child', $root = 0)
{
$tree = array();
$packData = array();
foreach ($list as $data) {
$packData[$data[$pk]] = $data;
}
foreach ($packData as $key => $val) {
if ($val[$pid] == $root) {
//代表跟节点, 重点一
$tree[] = &$packData[$key];
} else {
//找到其父类,重点二
$packData[$val[$pid]][$child][] = &$packData[$key];
}
}
return $tree;
}
//API签名验证token
function api_sign($param, $origin=false)
{
$sign = concat($param);
if($origin){
return $sign;
}
return hash('md5', $sign);
}
function concat(array $param)
{
ksort($param);
$first = '';
foreach ($param as $key => $val) {
if(is_array($val)) {
$first .= concat($val);
continue;
}
$first .= $val;
if(!empty($val)) {
$first .= $val;
}
}
return $first;
}
//API验签
// 获取sign
public function getSign($secret, $data) {
// 对数组的值按key排序
ksort($data);
// 生成url的形式
$params = http_build_query($data);
// 生成sign
$sign = md5($params . $secret);
return $sign;
}
/**
* 后台验证sign是否合法
* @param [type] $secret [description]
* @param [type] $data [description]
* @return [type] [description]
*/
function verifySign($secret, $data) {
// 验证参数中是否有签名
if (!isset($data['sign']) || !$data['sign']) {
echo '发送的数据签名不存在';
die();
}
if (!isset($data['stamp']) || !$data['stamp']) {
echo '发送的数据参数不合法';
die();
}
// 验证请求, 10分钟失效
if (time() - $data['stamp'] > 600) {
echo '验证失效, 请重新发送请求';
die();
}
$sign = $data['sign'];
unset($data['sign']);
ksort($data);
$params = http_build_query($data);
// $secret是通过key在api的数据库中查询得到
$sign2 = md5($params . $secret);
if ($sign == $sign2) {
die('验证通过');
} else {
die('请求不合法');
}
}
//订单号
function getOrderSn(){
$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
$orderSn = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
return $orderSn;
}
//银行卡号校验
function luhm($s) {
$n = 0;
for ($i = strlen($s); $i >= 1; $i--) {
$index=$i-1;
//偶数位
if ($i % 2==0) {
$n += $s{$index};
} else {//奇数位
$t = $s{$index} * 2;
if ($t > 9) {
$t = (int)($t/10)+ $t%10;
}
$n += $t;
}
}
return ($n % 10) == 0;
}
//判断请求http或https
function get_http(){
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
return $http_type;
}
//随机红包
function sendHB($money_total, $num) {
if($money_total < $num * 0.01) {
exit('钱太少');
}
if($money_total > 999) {
exit('钱太多');
}
$rand_arr = array();
for($i=0; $i<$num; $i++) {
$rand = rand(1, 100);
$rand_arr[] = $rand;
}
$rand_sum = array_sum($rand_arr);
$rand_money_arr = array();
$rand_money_arr = array_pad($rand_money_arr, $num, 0.01); //保证每个红包至少0.01
foreach ($rand_arr as $key => $r) {
$rand_money = number_format($money_total*$r/$rand_sum, 2);
if($rand_money <= 0.01 || number_format(array_sum($rand_money_arr), 2) >= number_format($money_total, 2)) {
$rand_money_arr[$key] = 0.01;
} else {
$rand_money_arr[$key] = $rand_money;
}
}
$max_index = $max_rand = 0;
foreach ($rand_money_arr as $key => $rm) {
if($rm > $max_rand) {
$max_rand = $rm;
$max_index = $key;
}
}
unset($rand_money_arr[$max_index]);
//这里的array_sum($rand_money_arr)一定是小于$money_total的
$rand_money_arr[$max_index] = number_format($money_total - array_sum($rand_money_arr), 2);
ksort($rand_money_arr);
return $rand_money_arr;
}
//二位数组根据一个健值大小排序排序
function arr_sort($arr){
$flag = array();
foreach($arr as $arr2){
$flag[] = $arr2["money"];
}
array_multisort($flag, SORT_DESC, $arr);
return $arr;
}
/**
* @param $latitude1 纬度
* @param $longitude1 经度
* @param $latitude2
* @param $longitude2
* @return array
* 获取两点之间的距离
*/
function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2) {
$theta = $longitude1 - $longitude2;
$miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
$miles = acos($miles);
$miles = rad2deg($miles);
$miles = $miles * 60 * 1.1515;
$feet = $miles * 5280;
$yards = $feet / 3;
$kilometers = $miles * 1.609344;
$meters = $kilometers * 1000;
return compact('miles','feet','yards','kilometers','meters');
}
//随机32位字符串
function nonce_str(){
$str = '';
$num = 'QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm';
for ($i = 0; $i <32; $i ++){
$str .= $num[rand(0,48)];
}
return $str;
}
/**
* 封装base64位图片上传
*/
function base64_upload($base64) {
$base64_image = str_replace(' ', '+', $base64);
//post的数据里面,加号会被替换为空格,需要重新替换回来,如果不是post的数据,则注释掉这一行
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image, $result)){
//匹配成功
if($result[2] == 'jpeg'){
$image_name = uniqid().'.jpg';
//纯粹是看jpeg不爽才替换的
}else{
$image_name = uniqid().'.'.$result[2];
}
$image_file = "./Uploads/{$image_name}";
//服务器文件存储路径
if (file_put_contents($image_file, base64_decode(str_replace($result[1], '', $base64_image)))){
return './Uploads/'.$image_name;
}else{
return false;
}
}else{
return false;
}
}