// +---------------------------------------------------------------------- // 应用公共文件 /** * 随机字符 * @param number $length 长度 * @param string $type 类型 * @param number $convert 转换大小写 * @return string */ function randCode($length=6, $type='string', $convert=0){ $config = array( 'number'=>'1234567890', 'letter'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'string'=>'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789', 'all'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' ); if(!isset($config[$type])) $type = 'string'; $string = $config[$type]; $code = ''; $strlen = strlen($string) -1; for($i = 0; $i < $length; $i++){ $code .= $string[mt_rand(0, $strlen)]; } if($convert){ $code = ($convert > 0) ? strtoupper($code) : strtolower($code); } return $code; } function build_shop_order_num(){ /* 给随机数播种 php4.2.0以后不需要 */ mt_srand((double) microtime() * 1000000); /* 返回订单号(13位) */ return date('Ymd').str_pad(mt_rand(1, 99999),6,'0',STR_PAD_LEFT); } function getQiniuImage($value,$width = 400,$height = 400){ $domain = \think\Config::get("qiniu.QINIU_DOMAIN"); return "{$domain}/{$value}?imageView2/1/w/{$width}/h/{$height}"; } /** * 生成16位子流水号 * @param number $length 长度 * @param string $type 类型 * @param number $convert 转换大小写 * @return string */ //输出函数 function p($var){ dump($var,true,'
'); } //生成13位号码 function build_order_num(){ /* 给随机数播种 php4.2.0以后不需要 */ mt_srand((double) microtime() * 1000000); /* 返回订单号(13位) */ return date('Ymd').str_pad(mt_rand(1, 99999),5,'0',STR_PAD_LEFT); } /** * @param $value * @return bool * 检测手机号是否合法 */ function isMobile($value){ if(strlen($value) == "11") { preg_match_all("/13[123569]{1}d{8}|15[1235689]d{8}|188d{8}/",$value,$array); if($array){ return true; } }else{ return false; } } /** * @param $value * @return bool * 判断是否为整数 */ function isPositiveInteger($value){ if(!is_numeric($value) || !is_int($value + 0) || ($value + 0) < 0){ return false; } return true; } /** * @param $url * @param $file * @return bool|string */ function curlFile($url,$file){ $data = array('media'=>new CURLFile($file)); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在 curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器 curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包 curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环 curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 $return = curl_exec($curl); curl_close($curl); return $return; } /** * @param $data * @param string $filename * @throws PHPExcel_Exception * @throws PHPExcel_Reader_Exception * @throws PHPExcel_Writer_Exception */ function create_xls($data,$filename='simple.xls'){ ini_set('max_execution_time', '0'); $filename=str_replace('.xls', '', $filename).'.xls'; $phpexcel = new \PHPExcel(); $phpexcel->getProperties() ->setCreator("Maarten Balliauw") ->setLastModifiedBy("Maarten Balliauw") ->setTitle("Office 2007 XLSX Test Document") ->setSubject("Office 2007 XLSX Test Document") ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") ->setKeywords("office 2007 openxml php") ->setCategory("Test result file"); $phpexcel->getActiveSheet()->fromArray($data); $phpexcel->getActiveSheet()->setTitle('Sheet1'); // $phpexcel->getActiveSheet()->getColumnDimension('D')->setWidth(30); // $phpexcel -> getActiveSheet() -> getColumnDimension() -> setAutoSize(true); // $objPHPExcel -> getActiveSheet() -> getColumnDimension(PHPExcel_Cell::stringFromColumnIndex(0)) -> setAutoSize(true); $phpexcel->setActiveSheetIndex(0); ob_end_clean();//清除缓冲区,避免乱码 header('Content-Type: application/vnd.ms-excel'); header("Content-Disposition: attachment;filename=$filename"); header('Cache-Control: max-age=0'); header('Cache-Control: max-age=1'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified header('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header('Pragma: public'); // HTTP/1.0 $objwriter = PHPExcel_IOFactory::createWriter($phpexcel, 'Excel5'); $objwriter->save('php://output'); exit; } /** * 生成16位子流水号 * @param number $length 长度 * @param string $type 类型 * @param number $convert 转换大小写 * @return string */ function buildSerialNumber(){ $number = "1234567890"; $captical = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $code = ""; for($i = 0; $i < 2; $i++){ $code .= $captical[mt_rand(0, strlen($captical)-1)]; } for($i = 0; $i < 14; $i++){ $code .= $number[mt_rand(0, strlen($number)-1)]; } return $code; }