| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- <?php
- use think\facade\Request;
- use think\facade\Log;
- // 应用公共文件
- /**
- * 打印调试函数
- * @param $content
- * @param $is_die
- */
- function p($content, $is_die = true)
- {
- header('Content-type: text/html; charset=utf-8');
- echo '<pre>' . print_r($content, true);
- $is_die && die();
- }
- /**
- * 隐藏敏感字符
- * @param $value
- * @return string
- */
- function substr_cut($value)
- {
- $strlen = mb_strlen($value, 'utf-8');
- if ($strlen <= 1) return $value;
- $firstStr = mb_substr($value, 0, 1, 'utf-8');
- $lastStr = mb_substr($value, -1, 1, 'utf-8');
- return $strlen == 2 ? $firstStr . str_repeat('*', $strlen - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
- }
- /**
- * 字符串进行加密。
- */
- function authcode($string,$operation)
- {
- $key ='887wesfds5fg56r';
- $key = md5($key ? $key : $GLOBALS['auth_key']);
- $key_length = strlen($key);
- $string = $operation == 'DECODE' ? base64_decode($string) : substr(md5($string . $key), 0, 8) . $string;
- $string_length = strlen($string);
- $rndkey = $box = array();
- $result = '';
- for ($i = 0; $i < 256; $i++) {
- $rndkey[$i] = ord($key[$i % $key_length]);
- $box[$i] = $i;
- }
- 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, 8) == substr(md5(substr($result, 8) . $key), 0, 8)) {
- return substr($result, 8);
- } else {
- return '';
- }
- } else {
- return str_replace('=', '', base64_encode($result));
- }
- }
- /**
- * 获取当前系统版本号
- * @return mixed|null
- * @throws Exception
- */
- function get_version()
- {
- try {
- $file = root_path() . '/version.json';
- $version = json_decode(file_get_contents($file), true);
- return $version['version'];
- } catch (\Exception $e) {
- return '';
- }
- }
- /**
- * 驼峰命名转下划线命名
- * @param $str
- * @return string
- */
- function toUnderScore($str)
- {
- $dstr = preg_replace_callback('/([A-Z]+)/', function ($matchs) {
- return '_' . strtolower($matchs[0]);
- }, $str);
- return trim(preg_replace('/_{2,}/', '_', $dstr), '_');
- }
- /**
- * 生成密码hash值
- * @param $password
- * @return string
- */
- function salt_hash($password)
- {
- return md5(md5($password) . 'jjjshop_salt_2020');
- }
- /**
- * curl请求指定url (post)
- * @param $url
- * @param array $data
- * @return mixed
- */
- function curlPost($url, $data = [])
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- $result = curl_exec($ch);
- curl_close($ch);
- return $result;
- }
- /**
- * 多维数组合并
- * @param $array1
- * @param $array2
- * @return array
- */
- function array_merge_multiple($array1, $array2)
- {
- $merge = $array1 + $array2;
- $data = [];
- foreach ($merge as $key => $val) {
- if (
- isset($array1[$key])
- && is_array($array1[$key])
- && isset($array2[$key])
- && is_array($array2[$key])
- ) {
- $data[$key] = array_merge_multiple($array1[$key], $array2[$key]);
- } else {
- $data[$key] = isset($array2[$key]) ? $array2[$key] : $array1[$key];
- }
- }
- return $data;
- }
- /**
- * 二维数组排序
- * @param $arr
- * @param $keys
- * @param bool $desc
- * @return mixed
- */
- function array_sort($arr, $keys, $desc = false)
- {
- $key_value = $new_array = array();
- foreach ($arr as $k => $v) {
- $key_value[$k] = $v[$keys];
- }
- if ($desc) {
- arsort($key_value);
- } else {
- asort($key_value);
- }
- reset($key_value);
- foreach ($key_value as $k => $v) {
- $new_array[$k] = $arr[$k];
- }
- return $new_array;
- }
- /**
- * 数据导出到excel(csv文件)
- * @param $fileName
- * @param array $tileArray
- * @param array $dataArray
- */
- function export_excel($fileName, $tileArray = [], $dataArray = [])
- {
- ini_set('memory_limit', '512M');
- ini_set('max_execution_time', 0);
- ob_end_clean();
- ob_start();
- header("Content-Type: text/csv");
- header("Content-Disposition:filename=" . $fileName);
- $fp = fopen('php://output', 'w');
- fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));// 转码 防止乱码(比如微信昵称)
- fputcsv($fp, $tileArray);
- $index = 0;
- foreach ($dataArray as $item) {
- if ($index == 1000) {
- $index = 0;
- ob_flush();
- flush();
- }
- $index++;
- fputcsv($fp, $item);
- }
- ob_flush();
- flush();
- ob_end_clean();
- }
- /**
- * 写入日志
- * @param $value
- * @param string $type
- */
- function log_write($value, $channel = '')
- {
- $msg = is_string($value) ? $value : var_export($value, true);
- if($channel != ''){
- Log::channel('task')->write($msg);
- }else{
- Log::channel($channel)->write($msg);
- }
- }
- /**
- * 获取当前域名及根路径
- * @return string
- */
- function base_url()
- {
- static $baseUrl = '';
- if (empty($baseUrl)) {
- $request = Request::instance();
- //$subDir = str_replace('\\', '/', dirname($request->server('PHP_SELF')));
- $baseUrl = $request->scheme() . '://' . $request->host() . '/';
- }
- return $baseUrl;
- // return replaceBaseUrl($baseUrl);
- }
- /**
- * 替换就域名
- * @param $baseUrl
- * @return mixed|string|string[]
- */
- function replaceBaseUrl($baseUrl)
- {
- $origin = 'cni-ekshop.cni.com.cn'; // 旧域名
- $modern = 'bs.cni-ekshop.com'; // 新域名
- $index = strpos($baseUrl, $origin);
- if ($index > 0) {
- $baseUrl = str_replace($origin, $modern, $baseUrl);
- }
- return $baseUrl;
- }
- /**
- * 左侧填充0
- * @param $value
- * @param int $padLength
- * @return string
- */
- function pad_left($value, $padLength = 2)
- {
- return \str_pad($value, $padLength, "0", STR_PAD_LEFT);
- }
- /**
- * 过滤emoji表情
- * @param $text
- * @return null|string|string[]
- */
- function filter_emoji($text)
- {
- // 此处的preg_replace用于过滤emoji表情
- // 如需支持emoji表情, 需将mysql的编码改为utf8mb4
- return preg_replace('/[\xf0-\xf7].{3}/', '', $text);
- }
- /**
- * 获取全局唯一标识符
- * @param bool $trim
- * @return string
- */
- function getGuidV4($trim = true)
- {
- // Windows
- if (function_exists('com_create_guid') === true) {
- $charid = com_create_guid();
- return $trim == true ? trim($charid, '{}') : $charid;
- }
- // OSX/Linux
- if (function_exists('openssl_random_pseudo_bytes') === true) {
- $data = openssl_random_pseudo_bytes(16);
- $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
- $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
- return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
- }
- // Fallback (PHP 4.2+)
- mt_srand((double)microtime() * 10000);
- $charid = strtolower(md5(uniqid(rand(), true)));
- $hyphen = chr(45); // "-"
- $lbrace = $trim ? "" : chr(123); // "{"
- $rbrace = $trim ? "" : chr(125); // "}"
- return $lbrace .
- substr($charid, 0, 8) . $hyphen .
- substr($charid, 8, 4) . $hyphen .
- substr($charid, 12, 4) . $hyphen .
- substr($charid, 16, 4) . $hyphen .
- substr($charid, 20, 12) .
- $rbrace;
- }
- function format_time($value)
- {
- return date('Y-m-d', $value);
- }
- /**
- * curl请求指定url (get)
- * @param $url
- * @param array $data
- * @return mixed
- */
- function curl($url, $data = [])
- {
- // 处理get数据
- if (!empty($data)) {
- $url = $url . '?' . http_build_query($data);
- }
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_HEADER, false);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
- $result = curl_exec($curl);
- curl_close($curl);
- return $result;
- }
- /**
- * json 转换true,false,数字转成vue可直接用的
- */
- function jsonRecursive(&$array)
- {
- foreach ($array as $key => $value) {
- if (is_array($value)) {
- jsonRecursive($array[$key]);
- } else {
- if($value === 'true'){
- $array[$key] = true;
- } else if($value === 'false'){
- $array[$key] = false;
- }
- }
- }
- }
- /**
- * 判断浏览器名称和版本
- */
- function get_client_browser()
- {
- if(empty($_SERVER['HTTP_USER_AGENT'])){
- return 'robot!';
- }
- if( (false == strpos($_SERVER['HTTP_USER_AGENT'],'MSIE')) && (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident')!==FALSE) ){
- return 'Internet Explorer 11.0';
- }
- if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 10.0')){
- return 'Internet Explorer 10.0';
- }
- if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 9.0')){
- return 'Internet Explorer 9.0';
- }
- if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 8.0')){
- return 'Internet Explorer 8.0';
- }
- if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 7.0')){
- return 'Internet Explorer 7.0';
- }
- if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 6.0')){
- return 'Internet Explorer 6.0';
- }
- if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Edge')){
- return 'Edge';
- }
- if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Firefox')){
- return 'Firefox';
- }
- if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Chrome')){
- return 'Chrome';
- }
- if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Safari')){
- return 'Safari';
- }
- if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Opera')){
- return 'Opera';
- }
- if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'360SE')){
- return '360SE';
- }
- //微信浏览器
- if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MicroMessage')){
- return 'MicroMessage';
- }
- return '';
- }
- /**
- * 数组转义为json
- */
- function jsonEncode($data)
- {
- return json_encode($data, JSON_UNESCAPED_UNICODE);
- }
- /**
- * json转义为数组
- */
- function jsonDecode($json)
- {
- return json_decode($json, true);
- }
- /**
- * 检查日期是否合法
- * @param string $date
- * @param array $formats
- * @return bool
- */
- function checkDateIsValid(string $date, array $formats = ["Y-m-d", "Y/m/d"]): bool
- {
- $unixTime = strtotime($date);
- if (!$unixTime) {
- return false;
- }
- foreach ($formats as $format) {
- if (date($format, $unixTime) == $date) {
- return true;
- }
- }
- return false;
- }
|