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 = count($columns); $rowCount = count($rows); //设置表头 for ($i = 0; $i < $colCount; $i++) { $objPHPExcel->setCellValue(getExcelColumnByIndex($i) . $titleStartLine, $columns[$i]); } //设置正文内容 for ($i = 0; $i < $rowCount; $i++) { for ($n = 0; $n < $colCount; $n++) { if (is_numeric($rows[$i][$n]) && strlen($rows[$i][$n]) > 17) { //超过17位的数字转成文本方以免被转化为科学数 $objPHPExcel->setCellValueExplicit(getExcelColumnByIndex($n) . ($rowStartLine + $i), $rows[$i][$n], $datatype::TYPE_STRING); } else { $objPHPExcel->setCellValue(getExcelColumnByIndex($n) . ($rowStartLine + $i), $rows[$i][$n]); } } } $objPHPExcel->getDefaultColumnDimension()->setWidth(16); //默认列宽 $objPHPExcel->getDefaultRowDimension()->setRowHeight(30); //默认列高 //样式设置 $settings = $settings ?: getCommonExcelSetting($colCount, $rowCount); foreach ($settings as $type => $cfg) { switch ($type) { case "width": for ($i = 0; $i < count($cfg); $i++) { $objPHPExcel->getColumnDimension($cfg[$i][0])->setWidth($cfg[$i][1]); } break; case "height": for ($i = 0; $i < count($cfg); $i++) { $objPHPExcel->getRowDimension($cfg[$i][0])->setRowHeight($cfg[$i][1]); } 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 "color": for ($i = 0; $i < count($cfg); $i++) { $objPHPExcel->getStyle($cfg[$i][0])->getFont()->getColor()->setARGB($cfg[$i][1]); } break; case "wrap": for ($i = 0; $i < count($cfg); $i++) { $objPHPExcel->getStyle($cfg[$i])->getAlignment()->setWrapText(true); //cellvalue包含\n设为自动换行 } 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 + 1), "wrap" => [sprintf("A2:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1)], "align" => [[sprintf("A1:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1), "hCenter"], [sprintf("A1:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1), "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://rc.jucai.gov.cn/ftp/{$path}"; } else { $path = getHostWithProtocol() . "/storage/{$path}"; } if (isImage($path) || $stillOriginalPath) return $path; return getFileView($path); } function getFileView($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 { $checkEnterpriseFullFields = ["bankCard", "bankNetwork", "bank", "imgurl", "bankImg", "beian"]; } 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); }