Functions.php
来源:原创
时间:2016-07-23
作者:脚本小站
分类:代码笔记
<?php /** * 收录规则 1.自己写的 2.常用的 */ //------------------------------------------------------------ /** * 将权限菜单分组。将不同的菜单归类分组,这个函数就是分组用的, * 这样在角色管理的前台中,添加权限时权限列表较为清晰好遍历 * 前台遍历方法: * <volist name="list" id="o"> * <volist name="o['group']" id="vo"> * {$vo.menu}<br/> * </volist> * </volist> * * @param array $list 菜单列表,这个列表必须要使用 auth asc 排序 * @return array 一个分组后的大数组 */ function group_auth($list){ if(is_array($list)){ $count = count($list); $ret = array(); for ($i = 0,$start = 0,$length = 0; $i < $count; $i++){ if($list[$i]['level'] == 0){ $start = $i - $length; if($length != 0){ $ret[]['group'] = array_slice($list,$start,$length); } $length = 0; } // 最后一组 if($i == $count-1){ $start = $i - $length; $ret[]['group'] = array_splice($list,$start); } $length++; } return $ret; }else{ return false; } } //------------------------------------------------------------ /** * 返回分类表的path和level字段 * * @param $table String 数据表名称或数据表对象 * @param $pid int 此分类的父级id * @param $category_id int 添加此条数据还回的id * @return array( path=>'',level=>'') */ protected function return_path_level_pid($table,$pid,$category_id){ if(!($table instanceof \Think\Model)){ $table = M($table); } if($pid == 0){ $data['path'] = $category_id; $data['level'] = 0; }else{ $parent_path = $table->where("id = {$pid}")->getField('path');//父级id $data['path'] = $parent_path.'-'.$category_id; $data['level'] = substr_count($data['path'],'-'); } $data['pid'] = $pid; return $data; } //------------------------------------------------------------ /* * 传入方法名,判断当前用户是否有权限访问此方法,返回布尔值 * 复制右边即可调用: have_authority(CONTROLLER_NAME,ACTION_NAME) * * param $controller String 控制器 可用常量 CONTROLLER_NAME 代替 * param $method String 方法 可用常量 ACTION_NAME 代替 */ function have_authority($controller,$method){ if($_SESSION[md5($_SERVER['SERVER_NAME'])]['supermanager'] == '1'){return true;} $role_id = $_SESSION[md5($_SERVER['SERVER_NAME'])]['role_id']; $role = M('role'); $user_role_data = $role->where("id = $role_id")->find(); $str = strtolower($controller.'-'.$method); $scan_arr = explode(',',strtolower($user_role_data['controller_method'])); if(in_array($str,$scan_arr)){ return true; } return false; } //------------------------------------------------------------ /** * 返回经过等比缩放的图像最小宽度 * * @param $pic_width int 图像宽度 * @param $pic_height int 图像高度 * @param $min_width int 要保持的最小宽度 * @return array 返回数组 array('width'=>$width,'height'=>$height) */ function image_min_width($pic_width = '200',$pic_height = '300',$min_width = '200'){ if($pic_width > $pic_height){ $height = $min_width; $width = $height*($pic_width/$pic_height); }else if($pic_height > $pic_width){ $width = $min_width; $height = $width*($pic_height/$pic_width); }else if($pic_width == $pic_height){ $width = $min_width; $height = $min_width; } return array('width'=>$width,'height'=>$height); } //------------------------------------------------------------ /** * 剪切后三位中文字符串 * @param $str String 要剪切的字符串 */ function cut_utf8($str){ return mb_substr($str,mb_strlen($str,'utf-8')-3,3,'utf-8'); } //------------------------------------------------------------