basePath() . '/config' . ($path ? '/' . $path : $path); } } /** * 强转UTF-8 * @param $str * @return mixed|string */ function mbEncodingUtf8($str) { if (isUTF8($str)) return $str; else { return mb_convert_encoding($str, 'UTF-8', 'ascii,GB2312,UTF-8,gbk'); } } /** * http请求 * @param $method * @param $url * @param array $params * @param array $headers * @param int $timeout * @param int $error 是否处理错误 * @return string * @throws \App\Base\Exceptions\ApiException */ function httpClient($method, $url, $params = [], $headers = [], $timeout = 0, $error = 1) { $curl = new \Curl\Curl(); $curl->setOpt(CURLOPT_FOLLOWLOCATION, 5); $curl->setOpt(CURLOPT_ENCODING, "gzip"); $curl->setUserAgent('Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'); //https 不验证ssl证书 $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false); if ($timeout > 0) { $curl->setOpt(CURLOPT_CONNECTTIMEOUT, 60); $curl->setOpt(CURLOPT_TIMEOUT, $timeout); } if (is_array($headers) && !empty($headers)) { foreach ($headers as $key => $value) { $curl->setHeader($key, $value); } } $method = strtolower($method); $curl->$method($url, $params); $code = $curl->http_status_code; $curl->close(); if ($error && $code != 200) { \Illuminate\Support\Facades\Log::alert('code:' . $curl->error_code . ', error: ' . $curl->error, ['method' => $method, 'url' => $url, 'params' => $params]); throw new \App\Base\Exceptions\ApiException($curl->error_code); } return $curl->response; } /** * 对象重组 * @param array $map * @param $key * @return array */ function mapByKey(array $map, $key) { $data = []; foreach ($map as $item) { $data[$item[$key]] = $item; } return $data; } /** * 当前时间 * @param bool|int $ts * @return string */ function nowTime($ts = false) { return $ts ? date('Y-m-d H:i:s', $ts) : date('Y-m-d H:i:s'); } /* * 日期间隔天数 * */ function dayDiff($date1, $date2) { $datetime1 = new \DateTime($date1); $datetime2 = new \DateTime($date2); $interval = $datetime1->diff($datetime2); return $interval->format('%R%a'); } /** * 验证email格式 * @param $email * @return bool */ function validEmail($email) { $email = trim($email); return filter_var($email, FILTER_VALIDATE_EMAIL) !== false; $pattern = "/^((\w[-\w.+]*[-\w])|[-\w])@([A-Za-z0-9][-_A-Za-z0-9]*\.)+[A-Za-z]{2,20}$/"; if (preg_match($pattern, $email)) { return true; } return false; } /** * 加密解密函数 * @param $string * @param string $operation * @param string $key * @param int $expiry * @return bool|string */ function authCode($string, $operation = 'DECODE', $key = '', $expiry = 0) { $ckey_length = 4; $key = md5($key ? $key : 'matchexpo'); $keya = md5(substr($key, 0, 16)); $keyb = md5(substr($key, 16, 16)); $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : ''; $cryptkey = $keya . md5($keya . $keyc); $key_length = strlen($cryptkey); $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string; $string_length = strlen($string); $result = ''; $box = range(0, 255); $rndkey = array(); for ($i = 0; $i <= 255; $i++) { $rndkey[$i] = ord($cryptkey[$i % $key_length]); } for ($j = $i = 0; $i < 256; $i++) { $j = ($j + $box[$i] + $rndkey[$i]) % 256; $tmp = $box[$i]; $box[$i] = $box[$j]; $box[$j] = $tmp; } for ($a = $j = $i = 0; $i < $string_length; $i++) { $a = ($a + 1) % 256; $j = ($j + $box[$a]) % 256; $tmp = $box[$a]; $box[$a] = $box[$j]; $box[$j] = $tmp; $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256])); } if ($operation == 'DECODE') { if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) { return substr($result, 26); } else { return ''; } } else { return $keyc . str_replace('=', '', base64_encode($result)); } } /** * html转为text * @param $str * @param int $formatText * @return mixed */ function html2text($str, $formatText = 1) { $str = preg_replace("/