/**
* 信息提示及跳转函数
* @param string $string [description]
* @param string $url [description]
* @return [type] [description]
*/
function alertMsg($string = "", $url = "") {
if (!$url) {
if ($_SERVER['HTTP_REFERER']) {
echo "<script> alert('$string');location.href='{$_SERVER['HTTP_REFERER']}'; </script>";die;
} else {
echo "<script> alert('$string');location.href=history.back(); </script>";die;
}
} else {
echo "<script> alert('$string');location.href='$url'; </script>";die;
}
}
/**
* 分页功能
* @param [type] $count [description]
* @param integer $num [description]
* @return [type] [description]
*/
function paging($count,$num = 10) {
$Page = new \Think\Page($count, $num);
$show = $Page -> show();
$limit = $Page->firstRow . ',' . $Page->listRows;
return array(
'show' => $show,
'limit' => $limit,
);
}
/**
* 删除服务器上一条的文件
* @param [type] $filePosition [description]
* @return [type] [description]
*/
function deleteTheFile($file) {
$file = $_SERVER['DOCUMENT_ROOT'].$file;
unlink($file);
}
/*
* $img_path 被压缩的图片的路径
* $thumb_w 压缩的宽
* $save_path 压缩后图片的存储路径
* $is_del 是否删除原文件,默认删除
*/
function thumb_img($img_path, $thumb_w, $save_path, $is_del = true){
$image = new \Think\Image();
$image->open($img_path);
$width = $image->width(); // 返回图片的宽度
if($width > $thumb_w){
$width = $width/$thumb_w; //取得图片的长宽比
$height = $image->height();
$thumb_h = ceil($height/$width);
}
//如果文件路径不存在则创建
$save_path_info = pathinfo($save_path);
if(!is_dir($save_path_info['dirname'])) mkdir ($save_path_info['dirname'], 0777);
$image->thumb($thumb_w, $thumb_h)->save($save_path);
if($is_del) @unlink($img_path); //删除源文件
}
/**
* [unsetParam 从链接中去掉参数]
* @param string $param [要去除的参数]
* @param string $url [链接]
*/
function unsetParam($param = '', $url = '') {
return preg_replace(
array("/{$param}=[^&]*/i", '/[&]+/', '/\?[&]+/', '/[?&]+$/',),
array('','&','?','',),
$url
);
}
function https_request($url = ''){
if (empty($url)) { return false; }
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
function getAccessToken(){
vendor('WeChat.Jssdk');
$jssdk = new \Jssdk();
$access_token = $jssdk->getAccessToken();
return $access_token;
}
function getJssdk(){
vendor('WeChat.Jssdk');
$jssdk = new \Jssdk();
$sign = $jssdk->getSignPackage();
return "appId:'{$sign["appid"]}',timestamp:{$sign["timestamp"]},
nonceStr:'{$sign["nonceStr"]}',signature:'{$sign["signature"]}'";
}
function deldir($dir) {
$dh=opendir($dir);
while ($file=readdir($dh)) {
if($file!="." && $file!="..") {
$fullpath=$dir."/" .$file;
if(!is_dir($fullpath)) {
unlink($fullpath);
} else {
deldir($fullpath);
}
}
}
closedir($dh);
if(rmdir($dir)) {
return true;
} else {
return false;
}
}
/**
* 发送post请求
* @param string $url [description]
* @param string $param [description]
* @return [type] [description]
*/
function request_post($url = '', $param = '') {
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $postUrl); //抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch); //运行curl
curl_close($ch);
return $data;
}
/**
* 获取本机网络信息
*/
function getLatLong(){
$getIp = get_client_ip();
$content = file_get_contents("http://api.map.baidu.com/location/ip?ak=PKRTHOLpfzhfhvWZ0r6lGqr0&ip={$getIp}&coor=bd09ll");
$json = json_decode($content);
if($json){
$address = json_decode(json_encode($json),true);
return $address['content'];
}else{
return false;
}
}
/**
* [randCode 生成随机数]
* @param integer $length [生成随机数长度]
* @param integer $type [类型,-1,0,1,2,3,4]
*/
function randCode($length = 5, $type = 0) {
$arr = array(1 => "0123456789", 2 => "abcdefghijklmnopqrstuvwxyz", 3 => "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4 => "~@#$%^&*(){}[]|");
if ($type == 0) {
array_pop($arr);
$string = implode("", $arr);
} elseif ($type == "-1") {
$string = implode("", $arr);
} else {
$string = $arr[$type];
}
$count = strlen($string) - 1;
$code = '';
for ($i = 0; $i < $length; $i++) {
$code .= $string[rand(0, $count)];
}
return $code;
}
/**
* 邮件发送函数
*/
function sendMail($to, $subject, $content) {
Vendor('PHPMailer.PHPMailerAutoload');
$mail = new PHPMailer(); //实例化
$mail->IsSMTP(); // 启用SMTP
$mail->Host = C('MailConf.HOST'); //smtp服务器的名称(这里以126邮箱为例)
$mail->SMTPAuth = C('MailConf.SMTPAUTH'); //启用smtp认证
$mail->Username = C('MailConf.USERNAME'); //你的邮箱名
$mail->Password = C('MailConf.PASSWORD') ; //邮箱密码
$mail->From = C('MailConf.FROM'); //发件人地址(也就是你的邮箱地址)
$mail->FromName = C('MailConf.FROMNAME'); //发件人姓名
$mail->AddAddress($to,"name");
$mail->WordWrap = 50; //设置每行字符长度
$mail->IsHTML(C('MailConf.ISHTML')); // 是否HTML格式邮件
$mail->CharSet = C('MailConf.CHARSET'); //设置邮件编码
$mail->Subject = $subject; //邮件主题
$mail->Body = $content; //邮件内容
$mail->AltBody = "邮件正文不支持HTML"; //邮件正文不支持HTML的备用显示
if($mail->Send()){
return true;
}else{
echo $mail->ErrorInfo;
}
}
/**
* @desc 将log存储到文件
* @param string $fileName 已存在的目标文件名
* @param string $data 记录内容
*/
function edtHtml($fileName,$data='记录内容为空') {
$fileName = "./Record/".$fileName.".html";
$html = "<p style='display:flex;'>".date('Y-m-d H:i:s').":".$data."</p><br>";
if(file_get_contents($fileName) == false) {
$tpl = '<title>Log</title><br>';
$tpl .= '<style type="text/css">body{padding-bottom:100vh;}</style>';
$tpl .= '<script type="text/javascript">';
$tpl .= 'window.onload = function() {';
$tpl .= 'var pp = document.getElementsByTagName("p");';
$tpl .= 'setTimeout(function() {';
$tpl .= 'document.getElementsByTagName("body")[0].scrollTop = pp[pp.length-1].offsetTop;';
$tpl .= '},10);';
$tpl .= '}';
$tpl .= '</script><br>';
$tpl .= $html;
}else {
}
file_put_contents($fileName,$html,FILE_APPEND);
}