0) { for ($i = 0; $i < $iteration; $i++) { $encoded = hash($algo, $encoded, true); } } $tmp = unpack('H*', $encoded); if (!empty($tmp) && !empty($tmp[1])) { $res = $tmp[1]; } return $res; } /** * 检查权限 * @param type $url * @param type $old_url * @return type */ function chkCommission($url, $old_url) { return app\common\api\MenuApi::chkPermission($url, $old_url); } /** * 随机字符ID * @return type */ function getStringId() { $day = random_int(10, 30); $time = strtotime("-4 years -6 months -" . $day . " days"); $randnum = random_int(100000000, 999999999); $randnum = str_shuffle($randnum); return $time . $randnum; } function isNullOrEmpty($obj) { if (!$obj || $obj == "" || !isset($obj)) return ""; return $obj; } function getTreeList($array, $id_field = "id", $pid_field = "pid", $value = "0") { static $result = []; foreach ($array as $key => $item) { if ($value == $item[$pid_field]) { $result[] = $item; unset($array[$key]); getTreeList($array, $id_field, $pid_field, $item[$id_field]); } } return $result; } /** * 读取excel * @param type $filepath * @param type $sheetIndex * @return type */ function getExcelDatas($filepath, $sheetIndex = 0) { $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($filepath); $sheet = $spreadsheet->getSheet($sheetIndex); /* $reader_type = \PHPExcel_IOFactory::identify($filepath); $reader = \PHPExcel_IOFactory::createReader($reader_type); $phpexcel = $reader->load($filepath); $sheet = $phpexcel->getSheet($sheetIndex); */ return $sheet->toArray(); } /** * 导出excel * @param type $columns 列标题 * @param type $rows 内容 * @param string $filename 文件名 * @param string $settings 样式批设置 * @param type $sheetname sheet标题 * @param type $saveurl 保存位置 * @param type $author 作者 */ function export($columns, $rows, $filename = "jjrcw", $settings = [], $sheetname = "sheet1", $saveurl = "php://output", $author = "晋江人才网") { $datatype = new \PhpOffice\PhpSpreadsheet\Cell\DataType; $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); $spreadsheet->getProperties()->setCreator($author) ->setLastModifiedBy($author) ->setTitle($filename) ->setSubject($filename); $spreadsheet->setActiveSheetIndex(0); $objPHPExcel = $spreadsheet->getActiveSheet(); $objPHPExcel->setTitle($sheetname); $filename .= "_" . time(); $titleStartLine = 1; $rowStartLine = $titleStartLine + 1; $colCount = 0; $rowCount = count($rows); //设置表头 for ($i = 0; $i < count($columns); $i++) { if (is_array($columns[$i])) { if (is_int($columns[$i][1][0])) { $merge_x = $columns[$i][1][0]; $merge_y = $columns[$i][1][1]; $rowStartLine = $rowStartLine > $merge_y ? $rowStartLine : $merge_y; $objPHPExcel->mergeCells(sprintf("%s:%s", getExcelColumnByIndex($colCount) . $titleStartLine, getExcelColumnByIndex($colCount + $merge_x - 1) . ($titleStartLine + $merge_y - 1))); $objPHPExcel->setCellValue(getExcelColumnByIndex($colCount) . ($titleStartLine), $columns[$i][0]); $colCount += $columns[$i][1][0]; } else { $rowStartLine = $rowStartLine > 3 ? $rowStartLine : 3; $child_titles = $columns[$i][1]; $child_count = count($child_titles); $objPHPExcel->mergeCells(sprintf("%s:%s", getExcelColumnByIndex($colCount) . $titleStartLine, getExcelColumnByIndex($colCount + $child_count - 1) . $titleStartLine)); $objPHPExcel->setCellValue(getExcelColumnByIndex($colCount) . $titleStartLine, $columns[$i][0]); for ($n = 0; $n < $child_count; $n++) { $objPHPExcel->setCellValue(getExcelColumnByIndex($colCount + $n) . ($titleStartLine + 1), $child_titles[$n]); } $colCount += $child_count; } } else { $objPHPExcel->setCellValue(getExcelColumnByIndex($i) . $titleStartLine, $columns[$i]); $colCount = count($columns); } } $formatString = []; //需要文本处理的列 //设置正文内容 for ($i = 0; $i < $rowCount; $i++) { for ($n = 0; $n < $colCount; $n++) { if (is_numeric($rows[$i][$n]) && strlen($rows[$i][$n]) > 11) { //超过11位的数字转成文本方以免被转化为科学数 $objPHPExcel->setCellValueExplicit(getExcelColumnByIndex($n) . ($rowStartLine + $i), $rows[$i][$n], $datatype::TYPE_STRING); $formatString[] = $n; } else { $objPHPExcel->setCellValue(getExcelColumnByIndex($n) . ($rowStartLine + $i), $rows[$i][$n]); } } } $formatString = array_unique($formatString); //去掉重复项 $objPHPExcel->getDefaultColumnDimension()->setWidth(16); //默认列宽 $objPHPExcel->getDefaultRowDimension()->setRowHeight(30); //默认列高 //样式设置 $defaultSetting = getCommonExcelSetting($colCount, $rowStartLine + $rowCount - 1); foreach ($formatString as $fs) { $columnName = getExcelColumnByIndex($fs); $defaultSetting["format"][] = ["string", sprintf("%s%d:%s%d", $columnName, 2, $columnName, count($rows) + 1)]; } $mergeSettings = []; //合并默认设置与个性设置的集合 foreach ($defaultSetting as $key => $set) { $mergeSettings[$key] = is_array($set) ? array_merge((array) $set, (array) $settings[$key]) : ($settings[$key] ?: $set); unset($settings[$key]); } $mergeSettings = $settings ? array_merge($mergeSettings, $settings) : $mergeSettings; foreach ($mergeSettings as $type => $cfg) { switch ($type) { case "width": for ($i = 0; $i < count($cfg); $i++) { if (is_array($cfg[$i])) { $objPHPExcel->getColumnDimension($cfg[$i][0])->setWidth($cfg[$i][1]); } else if (is_numeric($cfg[$i])) { for ($n = 0; $n < $colCount; $n++) { $column = getExcelColumnByIndex($n); $objPHPExcel->getColumnDimension($column)->setWidth($cfg[$i]); } } } break; case "height": for ($i = 0; $i < count($cfg); $i++) { if (is_array($cfg[$i])) { $objPHPExcel->getRowDimension($cfg[$i][0])->setRowHeight($cfg[$i][1]); } else if (is_numeric($cfg[$i])) { for ($n = 1; $n <= ($rowStartLine + $rowCount - 1); $n++) { $objPHPExcel->getRowDimension($n)->setRowHeight($cfg[$i]); } } } break; case "background-color": for ($i = 0; $i < count($cfg); $i++) { $objPHPExcel->getStyle($cfg[$i][0])->getFill()->setFillType(PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor($cfg[$i][1])->setARGB($cfg[$i][1]); } break; case "border": $objPHPExcel->getStyle($cfg)->applyFromArray(array( "borders" => array( "allBorders" => array( 'borderStyle' => PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN ) ) )); break; case "align": for ($i = 0; $i < count($cfg); $i++) { switch ($cfg[$i][1]) { case "left": $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT; break; case "right": $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT; break; case "hCenter": $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER; break; case "hJustify": $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY; break; case "top": $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_TOP; break; case "bottom": $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_BOTTOM; break; case "vCenter": $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER; break; case "vJustify": $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_JUSTIFY; break; } $align = []; if ($hAlign) { $align["horizontal"] = $hAlign; } if ($vAlign) { $align["vertical"] = $vAlign; } $objPHPExcel->getStyle($cfg[$i][0])->applyFromArray([ "alignment" => $align ]); } break; case "font": for ($i = 0; $i < count($cfg); $i++) { $ft = $cfg[$i][1]; $fm = $cfg[$i][2] ?: "宋体"; $fb = $cfg[$i][3] ?: false; $fc = $cfg[$i][4] ?: "000000"; switch ($cfg[$i][0]) { default: $objPHPExcel->getStyle($cfg[$i][0])->getFont()->setName($fm)->setSize($ft)->setBold($fb)->getColor()->setRGB($fc); break; } } break; case "color": for ($i = 0; $i < count($cfg); $i++) { $objPHPExcel->getStyle($cfg[$i][0])->getFont()->getColor()->setARGB($cfg[$i][1]); } break; case "format": for ($i = 0; $i < count($cfg); $i++) { switch ($cfg[$i][0]) { case "string": $objPHPExcel->getStyle($cfg[$i][1])->getNumberFormat()->setFormatCode(PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT); break; case "usd": $objPHPExcel->getStyle($cfg[$i][1])->getNumberFormat()->setFormatCode(PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_CURRENCY_USD_SIMPLE); break; default: $objPHPExcel->getStyle($cfg[$i][1])->getNumberFormat()->setFormatCode(PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED2); break; } } break; case "wrap": for ($i = 0; $i < count($cfg); $i++) { $objPHPExcel->getStyle($cfg[$i])->getAlignment()->setWrapText(true); //cellvalue包含\n设为自动换行 } break; case "scale": $objPHPExcel->getSheetView()->setZoomScale($cfg ?: 100); break; case "freeze": $objPHPExcel->freezePane($cfg); break; case "filter": $objPHPExcel->setAutoFilter($cfg); break; } } header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="' . $filename . '.xls"'); $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xls($spreadsheet); $writer->save($saveurl); //删除临时的sheet $spreadsheet->disconnectWorksheets(); unset($spreadsheet); exit; } /** * 根据传入的数值(游标),从26个英文字母的数组中查询,返回excel列标 * @param int $index 游标从0开始 * @return string 返回列标 */ function getExcelColumnByIndex(int $index) { $letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); if ($letters[$index]) return $letters[$index]; $rowIndex = floor($index / 26) - 1; $colIndex = $index % 26; return $letters[$rowIndex] . $letters[$colIndex]; } /** * 公共的excel设置 * @return type */ function getCommonExcelSetting($columns, $rows) { return $settings = [ "background-color" => [[sprintf("A1:%s1", getExcelColumnByIndex($columns - 1)), "0066CC"]], "color" => [[sprintf("A1:%s1", getExcelColumnByIndex($columns - 1)), "FFFFFF"]], "border" => sprintf("A1:%s%d", getExcelColumnByIndex($columns - 1), $rows), "wrap" => [sprintf("A2:%s%d", getExcelColumnByIndex($columns - 1), $rows)], "align" => [[sprintf("A1:%s%d", getExcelColumnByIndex($columns - 1), $rows), "hCenter"], [sprintf("A1:%s%d", getExcelColumnByIndex($columns - 1), $rows), "vCenter"]] ]; } /** * 检查是不是excel格式,不确定是否都是可用文件,主要还是第一和最后一个比较常见 * @param type $mime * @return type */ function isExcelFile($mime) { return in_array($mime, [ "application/vnd.ms-excel", "application/msexcel", "application/x-msexcel", "application/x-ms-excel", "application/x-excel", "application/x-dos_ms_excel", "application/xls", "application/x-xls", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ]); } /** * 获取主机名带协议 * @return http[s]://xxxx */ function getHostWithProtocol() { $protocol = (strpos(strtolower($_SERVER['SERVER_PROTOCOL']), 'http/2.0') !== false || $_SERVER["HTTPS"] == "on" || $_SERVER["REQUEST_SCHEME"] == "https" ? 'https' : 'http') . "://"; return $protocol . $_SERVER["HTTP_HOST"]; } /** * 获得上传文件的路径 * @param $path * @param $stillOriginalPath 如果是pdf这类文件会生成fileview格式的url,此参数为真是则生成原始url * @return string */ function getStoragePath($path, $stillOriginalPath = false) { if (!$path) return ""; if (strpos($path, "jjrcw") === 0) { $path = "https://static.jucai.gov.cn/ftp/{$path}"; } else { $path = getHostWithProtocol() . "/storage/{$path}"; } if (isImage($path) || $stillOriginalPath) return $path; return getFileView($path); } function getFileView($path) { return $path; //$complete_path = "https://report.jinjianghc.com/" . getStoragePath($path); return "https://fileview.jinjianghc.com/onlinePreview?url=" . base64_encode($path) . "&officePreviewType=pdf"; } function isImage($filename) { $types = '.gif|.jpeg|.png|.bmp'; //定义检查的图片类型 try { $info = getimagesize($filename); if ($info && stripos($types, image_type_to_extension($info['2'])) !== false) { return true; } return false; } catch (\think\exception $e) { //文件不存在,根据拓展名返回 $types = "gif|jpg|jpeg|png|bmp"; $pathinfo = pathinfo($filename); $ext = $pathinfo["extension"]; if ($pathinfo && $ext && stripos($types, $ext) !== false) { return true; } return false; } } function chkEnterpriseFull($ep) { switch ($ep->special) { case 0: if ($ep["type"] == 1) { $checkEnterpriseFullFields = ["agencyType", "enterpriseTag", "enterpriseType", "bankCard", "bankNetwork", "bank", "imgurl", "bankImg", "beian"]; if ($ep["agencyType"] == 1) { $checkEnterpriseFullFields[] = "industryFieldNew"; $checkEnterpriseFullFields[] = "industryFieldOld"; $checkEnterpriseFullFields[] = "domainImg"; } if (in_array($ep["enterpriseType"], ['guishang', 'gaoxinjishu', 'zhuanjingtexin'])) { $checkEnterpriseFullFields[] = "typeImg"; } } else { if (!in_array($ep["type"], [app\common\state\CommonConst::ENTERPRISE_WJ, app\common\state\CommonConst::ENTERPRISE_GJ])) { $checkEnterpriseFullFields = ["bankCard", "bankNetwork", "bank", "imgurl", "bankImg", "beian"]; } else { $checkEnterpriseFullFields = []; } } break; case 1: $checkEnterpriseFullFields = ["institutionTag"]; break; case 3: $checkEnterpriseFullFields = ["organizationTag"]; break; } $errorCounts = 0; while ($chk = array_shift($checkEnterpriseFullFields)) { if ($ep[$chk] == null) $errorCounts++; } if ($errorCounts > 0) { echo sprintf(""); return false; } return true; } function generate_password($length = 8) { $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*?'; $password = ''; for ($i = 0; $i < $length; $i++) { // 这里提供两种字符获取方式 // 第一种是使用 substr 截取$chars中的任意一位字符; // 第二种是取字符数组 $chars 的任意元素 // $password .= substr($chars, mt_rand(0, strlen($chars) – 1), 1); $password .= $chars[mt_rand(0, strlen($chars) - 1)]; } return $password; } /** * 判断是否为合法的身份证号码 * @param $mobile * @return int */ function isCreditNo($vStr) { $vCity = array( '11', '12', '13', '14', '15', '21', '22', '23', '31', '32', '33', '34', '35', '36', '37', '41', '42', '43', '44', '45', '46', '50', '51', '52', '53', '54', '61', '62', '63', '64', '65', '71', '81', '82', '91' ); if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $vStr)) return false; if (!in_array(substr($vStr, 0, 2), $vCity)) return false; $vStr = preg_replace('/[xX]$/i', 'a', $vStr); $vLength = strlen($vStr); if ($vLength == 18) { $vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2); } else { $vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2); } if (date('Y-m-d', strtotime($vBirthday)) != $vBirthday) return false; if ($vLength == 18) { $vSum = 0; for ($i = 17; $i >= 0; $i--) { $vSubStr = substr($vStr, 17 - $i, 1); $vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr, 11)); } if ($vSum % 11 != 1) return false; } return true; } function get_client_ip() { $forwarded = request()->header("x-forwarded-for"); if ($forwarded) { $ip = explode(',', $forwarded)[0]; } else { $ip = request()->ip(); } return $ip; } /** * 通过CURL发送HTTP请求 * @param string $url //请求URL * @param array $postFields //请求参数 * @return mixed * */ function curlPost($url, $postFields) { $postFields = json_encode($postFields); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json; ) ); curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); $ret = curl_exec($ch); if (false == $ret) { $result = curl_error($ch); } else { $rsp = curl_getinfo($ch, CURLINFO_HTTP_CODE); if (200 != $rsp) { $result = "请求状态 " . $rsp . " " . curl_error($ch); } else { $result = $ret; } } curl_close($ch); return $result; } function getCacheById($key, $field, $fieldKey = null) { $redis = \app\common\Redis::instance(think\facade\Config::get("cache.stores.redis.select")); $info = $redis->hGet($key, $field); $result = json_decode($info, true); if ($fieldKey) return $result[$fieldKey]; return $result; } function getJsonConfig($filepath, $field) { if (file_exists($filepath)) { return json_decode(file_get_contents($filepath), true)[$field]; } return null; } //加密前补齐 function mystr_pad($data, $len = 16) { $n = $len - strlen($data) % $len; $data = $data . str_repeat(chr($n), $n); return $data; } // 解密后去掉补齐 function mystr_unpad($data) { $n = ord(substr($data, -1)); return substr($data, 0, -$n); } //计算两个日期的时间差 function diffDate($date1, $date2) { if (strtotime($date1) > strtotime($date2)) { $ymd = $date2; $date2 = $date1; $date1 = $ymd; } $date1 = date('Y-m-d', strtotime($date1)); $date2 = date('Y-m-d', strtotime($date2)); list($y1, $m1, $d1) = explode('-', $date1); list($y2, $m2, $d2) = explode('-', $date2); $y = $m = $d = $_m = 0; $math = ($y2 - $y1) * 12 + $m2 - $m1; $y = intval(floor($math / 12)); $m = intval($math % 12); $d = (mktime(0, 0, 0, $m2, $d2, $y2) - mktime(0, 0, 0, $m2, $d1, $y2)) / 86400; if ($d < 0) { $m -= 1; $d += date('j', mktime(0, 0, 0, $m2, 0, $y2)); } return array($y, $m, $d); } function formatDateByMonth($date1, $date2, $data = []) { if (strtotime($date1) > strtotime($date2)) { $ymd = $date2; $date2 = $date1; $date1 = $ymd; } $sTime = strtotime(date('Y-m-01', strtotime($date1))); $eTime = strtotime(date('Y-m-01', strtotime($date2))); $month_arr = []; for ($sTime; $sTime <= $eTime; $sTime = strtotime('+1 month', $sTime)) { $month_arr[date('Ym', $sTime)] = date('Y-m', $sTime); // 取得递增月; } if (is_array($data) && count($data) > 0) { foreach ($data as $item) { if (array_key_exists($item['aae003'], $month_arr)) { $month_arr[$item['aae003']] .= "已缴费"; } } } return $month_arr; } function proSearch($str, $arr) { $match_res = []; array_filter($arr, function($arr) use ($str, &$match_res) { if (stripos($arr['value'], $str) !== false) { $match_res[] = $arr['value']; return true; } else { return false; } }); } //冒泡排序 function bubbleSort($array, $sortkey, $order = "asc") { sort($array); //重新分配索引 $length = count($array); for ($i = 0; $i < $length - 1; $i++) { for ($j = 0; $j < $length - $i - 1; $j++) { $v1 = intval($array[$j][$sortkey]) ?: 0; $v2 = intval($array[$j + 1][$sortkey]) ?: 0; if ($v1 > $v2) { $temp = $array[$j]; $array[$j] = $array[$j + 1]; $array[$j + 1] = $temp; } } } if ($order != "asc") { $array = array_reverse($array); } return $array; }