123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- <?php
- if (!function_exists('get_client_code_version')) {
- /**
- * 读取客户端代码版本号,用于客户端自动加载新提交的代码
- * @return string
- */
- function get_client_code_version()
- {
- // 前端版本号文件路径
- $versionFile = public_path('version.md');
- // 文件存在,且有读权限
- if (file_exists($versionFile) && is_readable($versionFile)) {
- $version = @file_get_contents($versionFile);
- if ($version) {
- return trim($version);
- }
- }
- return false;
- }
- }
- if (!function_exists('getInfoFromXml')) {
- /**
- * 读取xml字符串,获取对应数据
- * @return string
- */
- function getInfoFromXml($xmlStr, $field)
- {
- $xml_tree = new \DOMDocument();
- $xml_tree->loadXML($xmlStr);
- $array_e = $xml_tree->getElementsByTagName($field);
- $info = $array_e->item(0)->nodeValue;
- return $info;
- }
- }
- if(!function_exists('getDaysByMonth')) {
- /**
- * 根据年月获取所属月份的日期
- * @param $year int 年份
- * @param $month int 月份
- * @return array
- * */
- function getDaysByMonth($year, $month)
- {
- $days = date('t', strtotime($year. '-' . $month));
- $start_time = strtotime(date($year. '-' . $month. '-01'));
- $array = array();
- for($i=0; $i<$days;$i++) {
- $array[] = date('Y-m-d', $start_time + $i*86400);
- }
- return $array;
- }
- }
- /*
- * 获取时差
- * */
- if(!function_exists('date_diff_for_hour')) {
- /**
- * 获取时差
- * @param $dateOne datetime
- * @param $dateTwo datetime
- * @return int
- * */
- function date_diff_for_hour($dateOne, $dateTwo=null)
- {
- if(is_null($dateTwo)) $dateTwo = date('Y-m-d H:i:s');
- return abs(floor((strtotime($dateOne)-strtotime($dateTwo))%86400 / 3600));
- }
- }
- /*
- * 获取近七日日期
- * */
- if(!function_exists('get_recent_day')) {
- /**
- * 获取最近日期
- * @param $time
- * @param $format string
- * @return array
- * */
- function get_recent_day($time = '', $format='Y-m-d'){
- $time = empty($time) ? time() : $time;
- $date = [];
- for ($i=0; $i<=6; $i++){
- $timestamp = strtotime( '+' . ($i-7) .' days', $time);
- $yearInfo = date('Y', $timestamp);
- $date[$yearInfo][] = date($format, $timestamp);
- }
- return $date;
- }
- }
- /*
- * 将近N小时和日期转化为*年*月*日 *:*:*格式
- * */
- if(!function_exists('to_datetime')) {
- function to_datetime($param) {
- if(empty($param))
- return null;
- if(strtotime($param) === false) {
- $result = date('Y-m-d H:i:s', strtotime('-' . (int)$param. ' hour'));
- } else {
- $result = date('Y-m-d H:i:s', strtotime($param));
- }
- return $result;
- }
- }
- /*
- * 获取字符串对应的hash值
- * */
- if(!function_exists('get_hash_code')) {
- function get_hash_code($str, $remainder=0)
- {
- if(empty($str)) return false;
- $encrypt = md5($str);
- $number = base_convert($encrypt, 16, 10);
- $label = substr($number, -4);
- return $remainder > 0 ? abs($label % $remainder) : $number;
- }
- }
- /*
- * 获取日期对应的数字(0:星期日 1:星期一 2:星期二以此类推)
- * */
- if(!function_exists('get_week')) {
- function get_week($timestamp){
- $date_str=date('Y-m-d',$timestamp);
- $arr=explode("-", $date_str);
- $year=$arr[0];
- //月,输出2位整型,不够2位右对齐
- $month=sprintf('%02d',$arr[1]);
- //日,输出2位整型,不够2位右对齐
- $day=sprintf('%02d',$arr[2]);
- //时分秒默认赋值为0;
- $hour = $minute = $second = 0;
- //转换成时间戳
- $strap = mktime($hour,$minute,$second,$month,$day,$year);
- //获取数字型星期几
- return date("w",$strap);
- }
- }
- /*
- * 获取字符串对应的hash值
- * */
- if(!function_exists('get_token')) {
- function get_token($corpid, $userId, $timestamps, $extra='')
- {
- $str = 'wAAVfvfgR8uMIGIoRJquy66uw';
- return md5($corpid . $userId . $str . $timestamps . $extra);
- }
- }
- if(!function_exists('get_random_str')) {
- function get_random_str($length)
- {
- $nonceStr = "";
- $codeAlphabet = "ABCDEFGHIJKLMnopQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789";
- for ($i = 0; $i < $length; $i++) {
- $nonceStr .= $codeAlphabet[mt_rand(0, strlen($codeAlphabet) - 1)];
- }
- return $nonceStr;
- }
- }
- function h5_set_sign($params)
- {
- if(empty($params)){
- return false;
- }
- $str = '';
- ksort($params);
- foreach($params as $k=>$v){
- if(is_array($v)){
- $v = json_encode($v);
- }
- $str .= $k.'='.$v;
- }
- $solt = 'ur904wtr5pfctaw9';
- $sign = md5(md5($str) . $solt);
- return $sign;
- }
- function h5_verify_sign($params)
- {
- if( empty($params) || !isset($params['sign']) ){
- return false;
- }
- $sign = $params['sign'];
- unset($params['sign']);
- $_sign = h5_set_sign($params);
- if($sign == $_sign){
- return true;
- }
- return false;
- }
- /*
- * 将分钟转换为 x天y小时z分钟
- * */
- if(!function_exists('turn_minute')) {
- function turn_minute($minute)
- {
- $days = floor($minute/1440);
- $hours = floor(($minute - $days*1440)/60);
- $minutes = ($minute - $days*60*24 - $hours*60);
- $days = $days > 0 ? $days . '天' : '';
- $hours = $hours > 0 ? $hours . '小时' : '';
- $minutes = $minutes > 0 ? $minutes . '分钟' : '';
- return $days . $hours . $minutes;
- }
- }
- /**
- * 获取url参数
- * */
- if(!function_exists('getUrlParams')) {
- function getUrlParams($url, $param)
- {
- $pathData = parse_url($url);
- if(isset($pathData['fragment']) && $pathData['fragment']) {
- $pathData = parse_url($pathData['fragment']);
- }
- $value = '';
- if(isset($pathData['query']) && $pathData['query']) {
- parse_str($pathData['query'], $query);
- $value = $query[$param] ?? '';
- }
- return $value;
- }
- }
- /**
- * 获取url参数
- * */
- if(!function_exists('shouldMiss')) {
- /**
- * @param $currentValue integer 当前订单数
- * @param $missSetValue integer 间隔的订单数
- * @param $missValue integer 需要漏掉的订单数
- * @return boolean 应该漏单返回true,否则false
- **/
- function shouldMiss($currentValue, $missSetValue, $missValue)
- {
- for($i=0; $i<$missValue; $i++) {
- $remainder = ($missSetValue + $missValue - $i) % ($missSetValue + $missValue);
- if( $currentValue >1 && ($currentValue % ($missSetValue + $missValue)) == $remainder) {
- return true;
- }
- }
- return false;
- }
- }
- /**
- * 获取毫秒级时间戳
- */
- if(!function_exists('getMillisecond')) {
- function getMillisecond()
- {
- list($microsecond, $time) = explode(' ', microtime()); //' '中间是一个空格
- return (float)sprintf('%.0f', (floatval($microsecond) + floatval($time)) * 1000);
- }
- }
|