ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
**一些内置函数** 命令行中,防止重复执行 ~~~ if(!is_cli()){ exit('RUN IN CLI'); } set_time_limit(-1); ini_set('memory_limit','1024M'); cli_prevent_duplication($argv, $cmd = 'php io.php'); ~~~ 判断平台管理员是否登录,未登录自动跳到登录页 ~~~ check_admin_login(); ~~~ 写日志 ~~~ write_log($msg,$is_error = false) ~~~ 生成条形码 ~~~ $bar = new lib\Barcode; echo $bar->display(date("YmdHis"));exit; ~~~ 发送邮件,配置在 `config.ini.php`中。 ~~~ \lib\Mail::send([ 'from' => 'ddd@163.com', 'to' => $data['email'], 'subject'=> "标题, 'html' => "<p>内容</p>" ]); ~~~ 取位置lat,lng ~~~ https://lbs.qq.com/dev/console/key/setting lib\Map::tx('地址') 腾讯地图 lib\Map::gaode($address, $show_full = false)高德地图 ~~~ 最近几天 ~~~ lib\Time::neerDays($num = 5,$separate = "") ~~~ 返回最近几月 ~~~ lib\Time::neerMonths($num = 5,$separate = "") ~~~ 返回最近几年 ~~~ lib\Time::neerYears($num = 5,$separate = "") ~~~ 取今日、本周、本月、本年、昨日、上周、上月、上年 ~~~ lib\Time::get($key, $date_format = false) ~~~ 返回多少岁 ~~~ lib\Time::age($bornUnix) ~~~ 计算时间剩余  ~~~ lib\Time::less_time($a, $b = null) ~~~ 返回图片信息 ~~~ $file = PATH.'../demos/doc/1.jpg'; $pdf = new \lib\Img($file); //取长宽 print_r($pdf->getWh()); //2是横版,1是竖版 print_r($pdf->getDimensionsType()); ~~~ 多少时间之前 ~~~ timeago($time) ~~~ 设置配置 ~~~ set_config($title,$body) ~~~ 优先取数据库,未找到后取配置文件 ~~~ get_config($title) ~~~ XML 转成 数组 ~~~ xml_to_array($xml) ~~~ 数组转xml ~~~ array_to_xml($arr) ~~~ AES加密 ~~~ $config['aes_key'] = "123456"; $config['aes_iv'] = md5('app_sun'); $token = urlencode(aes_encode($d)); ~~~ 返回两个时间点间的日期数组 ~~~ get_dates($start, $end) ~~~ 当前时间是周几 ~~~ get_current_week($date) ~~~ 多语言翻译 ~~~ lang('welcome',['name'=>'test'] ) ~~~ 配置文件在 `lang/zh-cn/app.php`中。 搜索替换\n , ,空格。 转成数组 ~~~ string_to_array($str) ~~~ AES解密 ~~~ $token = $_GET['token']; $token = aes_decode($token); pr($token); ~~~ 根据用户id查用户信息 ~~~ get_user($user_id) ~~~ 查用户信息,where条件 ~~~ get_user_where($where = []) ~~~ 取用户扩展字段值 ~~~ get_user_meta($user_id) ~~~ 更新用户的meta信息 ,参数$meta如 ['nickname'=>''] ~~~ set_user_meta($user_id,$meta) ~~~ 获取HTTP_REFERER ~~~ referer($show_full = false) ~~~ 当前域名 ,结尾带/ ~~~ host() ~~~ 取用户扩展字段值 ~~~ get_user_meta_where($where = [],$return_row = false) ~~~ 所有会员 ~~~ get_user_all($where = []) ~~~ 取单个用户组信息 ~~~ user_group_get($group_id) ~~~ 跳转 ~~~ jump($url) ~~~ CDN地址,需要在`config.ini.php`配置` $config['cdn_url'] = [];` ~~~ static_url() ~~~ 判断是命令行下 ~~~ is_cli() ~~~ 判断是否为json ~~~ is_json($str) ~~~ 数组转对象 ~~~ array_to_object($arr) ~~~ 对象转数组 ~~~ object_to_array($obj) ~~~ 取目录名 ~~~ get_dir($name) ~~~ 取后缀,不包含`.` ~~~ get_ext($name) ~~~ 取文件名 ~~~ get_name($name) ~~~ 创建目录 ~~~ create_dir($arr = []) ~~~ 取IP ~~~ get_ip() ~~~ 计算两点地理坐标之间的距离,返回公里数 ~~~ get_distance($longitude1, $latitude1, $longitude2, $latitude2) ~~~ 设置、获取cookie ~~~ cookie($name, $value = null, $expire = 0, $path = '/') ~~~ 删除COOKIE ~~~ remove_cookie($name) ~~~ 返回 min="1900-01-01" max="2099-12-31" ~~~ date_limit() ~~~ 路径列表,支持文件夹下的子所有文件夹 ~~~ get_deep_dir($path) ~~~ 返回成功信息,JSON格式 ~~~ json_success($arr = []) ~~~ 返回错误信息,JSON格式 ~~~ json_error($arr = []) ~~~ yaml转数组,数组转yaml格式 ~~~ yaml($str) ~~~ 解析xlsx文件 ~~~ $lists = lib\Xls::load($file, [ '产品编号' => 'product_num', '产品规格' => 'name', '注册证号' => 'cert_num', '单位' => 'unit', ],$column_use_date = []); ~~~ `$column_use_date ` 指定哪几列是时间格式 xls生成 ~~~ 生成多个worksheet事例: $where = [ 'status' => 1, 'date[>=]' => $start, 'date[<=]' => $end, ]; $title = [ 'invoice_number' => '发票号码', 'customer_name' => '供应商', 'product_num' => '规格型号', 'num' => '数量', 'price' => '单价', 'total_price' => '金额', ]; //专票 $where['invoice_type'] = 1; $values = invoice_set_xls_data($where,$title); //普票 $where['invoice_type'] = 2; $new_data = invoice_set_xls_data($where,$title); //第一个worksheet Xls::$label = '专票'; Xls::$sheet_width = [ 'A' => "15", 'B' => "36", 'C' => "30", 'D' => "10", 'E' => "10", 'F' => "10", ]; //更多worksheet,如果只是一个,可不用下面代码,直接跳到Xls::create处 Xls::$works = [ [ 'title' => $title, 'label' => '普票', 'data' => $new_data, 'width' => Xls::$sheet_width, ] ]; Xls::create($title, $values, $name, true); ~~~ 验证数据 ~~~ $data = g(); $vali = validate([ 'company_title' => '客户名', 'email' => '邮件地址', 'active_plugins' => '系统', 'exp_time' => '过期时间', ],$data,[ 'required' => [ ['company_title'], ['email'], ['active_plugins'], ['exp_time'], ], 'email'=>[ ['email'] ] ]); if($vali){ json($vali); } ~~~ 规则 ~~~ required - Field is required requiredWith - Field is required if any other fields are present requiredWithout - Field is required if any other fields are NOT present equals - Field must match another field (email/password confirmation) different - Field must be different than another field accepted - Checkbox or Radio must be accepted (yes, on, 1, true) numeric - Must be numeric integer - Must be integer number boolean - Must be boolean array - Must be array length - String must be certain length lengthBetween - String must be between given lengths lengthMin - String must be greater than given length lengthMax - String must be less than given length min - Minimum max - Maximum listContains - Performs in_array check on given array values (the other way round than in) in - Performs in_array check on given array values notIn - Negation of in rule (not in array of values) ip - Valid IP address ipv4 - Valid IP v4 address ipv6 - Valid IP v6 address email - Valid email address emailDNS - Valid email address with active DNS record url - Valid URL urlActive - Valid URL with active DNS record alpha - Alphabetic characters only alphaNum - Alphabetic and numeric characters only ascii - ASCII characters only slug - URL slug characters (a-z, 0-9, -, _) regex - Field matches given regex pattern date - Field is a valid date dateFormat - Field is a valid date in the given format dateBefore - Field is a valid date and is before the given date dateAfter - Field is a valid date and is after the given date contains - Field is a string and contains the given string subset - Field is an array or a scalar and all elements are contained in the given array containsUnique - Field is an array and contains unique values creditCard - Field is a valid credit card number instanceOf - Field contains an instance of the given class optional - Value does not need to be included in data array. If it is however, it must pass validation. arrayHasKeys - Field is an array and contains all specified keys. ~~~ 数组转tree ~~~ /** * 数组转tree * * 输入$list * [ * {id:1,pid:0,其他字段}, * {id:2,pid:1,其他字段}, * {id:3,pid:1,其他字段}, * ] * 输出 * [ * [ * id:1, * pid:0, * 其他字段, * children:[ * {id:2,pid:1,其他字段}, * {id:3,pid:1,其他字段}, * ] * ] * ] * */ array_to_tree($list, $pk = 'id', $pid = 'pid', $child = 'children', $root = 0, $my_id = '') ~~~