# 我觉得有点用的公共函数(I think it's a little bit useful) --- ## 格式化文件大小 ``` /** * [format_size 格式化文件大小] * @param [int] $size [文件大小] * @return [float/double] [格式化后的文件大小] */ function format_size($size) { $arr = ['B', 'KB', 'M', 'G', 'T', 'P']; $i = 0; while ($size >= 1024) { $size = $size / 1024; $i++; } // 四舍五入进行返回 return round($size, 4).$arr[$i]; } ``` ## 可逆加密算法 encrypt ``` /** * [encrypt 可逆加密算法] * @param [type] $data [description] * @param [type] $key [description] * @return [type] [description] */ function encrypt($data, $key) { $key = md5($key); $x = 0; $len = strlen($data); $l = strlen($data); $char = ''; $str = ''; for ($i=0; $i < $len; $i++) { if ($x == $l) { $x = 0; } $char .= $key{$x}; $x++; } for ($i = 0; $i < $len; $i++) { $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256); } return base64_encode($str); } ``` ## 解密算法 decrypt ``` /** * [decrypt 解密算法] * @param [type] $data [description] * @param [type] $key [description] * @return [type] [description] */ function decrypt($data, $key) { $key = md5($key); $x = 0; $data = base64_decode($data); $len = strlen($data); $l = strlen($key); $char = ""; $str = ""; for ($i = 0; $i < $len; $i++) { if ($x == $l) { $x = 0; } $char .= substr($key, $x, 1); $x++; } for ($i = 0; $i < $len; $i++) { if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) { $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1))); } else { $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1))); } } return $str; } ``` ## 导出Excel ``` // 导出excle function export_to($data,$name=false,$type = 0){ if(!$name){$name=date("Y-m-d-H-i-s",time());} $PHPExcel = new PHPExcel(); //实例化PHPExcel类,类似于在桌面上新建一个Excel表格 $PHPExcel->getActiveSheet()->fromArray($data); $PHPExcel->getActiveSheet()->setTitle('Sheet1'); //给当前活动sheet设置名称 $PHPExcel->setActiveSheetIndex(0); $fileName = './public/'.date('Y-m-d_', time()).time().'.xlsx'; $saveName = $name.date('Y-m-d', time()).'.xlsx'; $PHPWriter = PHPExcel_IOFactory::createWriter($PHPExcel,'Excel5');//按照指定格式生成Excel文件,‘Excel2007’表示生成2007版本的xlsx,‘Excel5’表示生成2003版本Excel文件 if ($type == 0) { header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告诉浏览器输出07Excel文件 header('Content-Type:application/vnd.ms-excel');//告诉浏览器将要输出Excel03版本文件 header('Content-Disposition: attachment;filename="'.$saveName.'"');//告诉浏览器输出浏览器名称 // header('Content-Disposition: attachment;filename="01simple.xlsx"');//告诉浏览器输出浏览器名称 header('Cache-Control: max-age=0');//禁止缓存 $PHPWriter->save("php://output"); }else{ $PHPWriter->save($fileName); //表示在$path路径下面生成demo.xlsx文件 } } ``` ## 无限极分类树 ``` function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $keys = 'id', $sort = 'asc', $root = 0) { // 创建Tree $tree = array(); if (is_array($list)) { // 创建基于主键的数组引用 $refer = array(); foreach ($list as $key => $data) { $list[$key][$child] = []; $refer [$data[$pk]] = &$list[$key]; } foreach ($list as $key => $data) { // 判断是否存在parent $parentId = $data[$pid]; if ($root == $parentId) { $tree [] = &$list[$key]; $tree = my_sort($tree,$keys,$sort,SORT_NUMERIC); } else { if (isset ($refer [$parentId])) { $parent = &$refer[$parentId]; $parent[$child] [] = &$list[$key]; $parent[$child] = my_sort($parent[$child],$keys,$sort,SORT_NUMERIC); } } } } return $tree; } ``` ## 系统邮件发送函数 ``` /** * 系统邮件发送函数 * @param string $tomail 接收邮件者邮箱 * @param string $name 接收邮件者名称 * @param string $subject 邮件主题 * @param string $body 邮件内容 * @param string $attachment 附件列表 * @return boolean * @author static7 <static7@qq.com> 使用方法: $toemail='111111@qq.com'; $name = 'dqwdqqwdq';//收件人昵称 $subject='注册验证码'; $mail_tpl = model('MailTpl')->where('type', 'register')->find()->toArray(); $content = str_replace("\$code","123456",$mail_tpl['content']); $result = send_mail($toemail,$name,$subject,$content); dump($result); */ function send_mail($tomail, $name, $subject = '', $body = '', $attachment = null) { $config_mail = model('Mail')->find()->toArray(); $shop = model('Config')->find(); $mail = new \PHPMailer(); //实例化PHPMailer对象 $mail->CharSet = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码 $mail->IsSMTP(); // 设定使用SMTP服务 $mail->SMTPDebug = 0; // SMTP调试功能 0=关闭 1 = 错误和消息 2 = 消息 $mail->SMTPAuth = true; // 启用 SMTP 验证功能 if($config_mail['secure']){ $mail->SMTPSecure = 'ssl'; // 使用安全协议Enable TLS encryption, `ssl` also accepted }else{ $mail->SMTPSecure = 'tls'; // 使用安全协议Enable TLS encryption, `ssl` also accepted } $mail->Host = $config_mail['host']; // SMTP 服务器 $mail->Port = $config_mail['port']; // SMTP服务器的端口号 $mail->Username = $config_mail['user']; // SMTP服务器用户名 $mail->Password = $config_mail['pass']; // SMTP服务器密码 $mail->SetFrom($config_mail['user'], $shop['title']); $replyEmail = $config_mail['replyTo']; //留空则为发件人EMAIL $replyName = '发送回复'; //回复名称(留空则为发件人名称) $mail->AddReplyTo($replyEmail, $replyName); $mail->Subject = $subject; $mail->MsgHTML($body); $mail->AddAddress($tomail, $name); if (is_array($attachment)) { // 添加附件 foreach ($attachment as $file) { is_file($file) && $mail->AddAttachment($file); } } return $mail->Send() ? true : $mail->ErrorInfo; } ``` ## 获取当前url地址 ``` /** * [getActionUrl description]获取当前url * @return [type] [description] */ function getActionUrl(){ $module = strtolower(request()->module()); $controller = strtolower(request()->controller()); $action = strtolower(request()->action()); return $module.'/'.$controller.'/'.$action; } ```