Files
xk-api-yii/common/helpers/FuncHelper.php
2025-04-25 15:47:08 +08:00

382 lines
10 KiB
PHP

<?php
namespace common\helpers;
use common\services\DataEncryptor;
use Yii;
/**
* 自定义辅助函数,处理其他杂项
*/
class FuncHelper
{
/**
* ---------------------------------------
* ajax标准返回格式
* @param $code integer 错误码
* @param $msg string 提示信息
* @param $obj mixed 返回数据
* @return void
* ---------------------------------------
*/
public static function ajaxReturn($code = 0, $msg = 'success', $obj = '')
{
/* api标准返回格式 */
$json = array(
'code' => $code,
'msg' => $msg,
'obj' => $obj,
);
header('Content-Type:application/json; charset=utf-8');
exit(json_encode($json));
}
/**
* ---------------------------------------
* 分析枚举类型字段值 格式 a:名称1,b:名称2
* @param $string string 字符串
* @return mixed
* ---------------------------------------
*/
public static function parse_field_attr($string)
{
if (0 === strpos($string, ':')) {
// 采用函数定义
return eval(substr($string, 1) . ';');
}
$array = preg_split('/[,;\r\n]+/', trim($string, ",;\r\n"));
if (strpos($string, ':')) {
$value = array();
foreach ($array as $val) {
[$k, $v] = explode(':', $val);
$value[$k] = $v;
}
} else {
$value = $array;
}
return $value;
}
/**
* Create the directory by pathname
* @param string $pathname The directory path.
* @param int $mode
* @return bool
*/
public static function make_dir($pathname, $mode = 0777)
{
if (is_dir($pathname)) {
return true;
}
if (is_dir(dirname($pathname))) {
return mkdir($pathname, $mode);
}
self::make_dir(dirname($pathname));
return mkdir($pathname, $mode);
}
/**
* @param string $words
* @param string $separator
* @return string
* 下划线转驼峰或者字符串第一个字母大写
*/
function hump($words, $separator = '_')
{
if (strpos($words, $separator) !== false) {
$newWords = str_replace($separator, " ", strtolower($words));
return ltrim(str_replace(" ", "", ucwords($newWords)), $separator);
} else {
return ucfirst($words);
}
}
/**
* 生成 前缀+24位数字的订单号
* @param string $prefix 前缀
* @return string
*/
public static function generate_order_no($prefix = '', $length = 6)
{
$randLen = $length;
$id = base_convert(substr(uniqid(), 0 - $randLen), 16, $length);
if (strlen($id) > $length) {
$id = substr($id, -$length);
} elseif (strlen($id) < $length) {
$rLen = $length - strlen($id);
$id = $id . rand(pow($length, $rLen - 1), pow($length, $rLen) - 1);
}
$dateTimeStr = date('YmdHis');
return $prefix . $dateTimeStr . $id;
}
/**
* 根据身份证获取年龄
* @param string $idno
* @return false|int|string
*/
public static function getAgeFromIdNo($idno = '')
{
// 如果字符串中包含xk_ase_256
if (strpos($idno, 'xk_ase_256') !== false) {
$idno = (new DataEncryptor(Yii::$app->params['ase_key']))->decrypt($idno);
}
$btime = strtotime(substr($idno, 6, 8));//idno是身份证号 截取日期并转为时间戳
$byear = date('Y', $btime);
$bmonth = date('m', $btime);
$bday = date('d', $btime);
$curYear = date('Y');
$curMoth = date('m');
$curDay = date('d');
$age = $curYear - $byear;
if ($curMoth < $bmonth || ($curMoth == $bmonth && $curDay < $bday)) {
$age--;
}
return $age;
}
//获取工作年限
public static function getYears($btime)
{
$byear = date('Y', $btime);
$bmonth = date('m', $btime);
$bday = date('d', $btime);
$curYear = date('Y');
$curMoth = date('m');
$curDay = date('d');
$age = $curYear - $byear;
if ($curMoth < $bmonth || ($curMoth == $bmonth && $curDay < $bday)) {
$age--;
}
return $age;
}
//获取前
public static function time_tran($the_time)
{
$now_time = time();
$show_time = $the_time;
$dur = $now_time - $show_time;
if ($dur < 0) {
return $the_time;
} else {
if ($dur < 60) {
return $dur . '秒前';
} else {
if ($dur < 3600) {
return floor($dur / 60) . '分钟前';
} else {
if ($dur < 86400) {
return floor($dur / 3600) . '小时前';
} else {
if ($dur < 259200) {//3天内
return floor($dur / 86400) . '天前';
} else {
return $the_time;
}
}
}
}
}
}
//获取还剩多少天多少小时多少分多少秒
//$diff两个数之差
public static function time_left($diff)
{
$str = '';
$date = floor($diff / 86400);
if ($date) {
$str .= $date . '天';
}
$hour = floor($diff % 86400 / 3600);
if ($hour) {
$str .= $hour . '小时';
}
$minute = floor($diff % 86400 % 3600 / 60);
if ($minute) {
$str .= $minute . '分';
}
$second = floor($diff % 86400 % 3600 % 60);
if ($second) {
$str .= $second . '秒';
}
return $str;
}
/**
* 获取文件真实路径
*
* @param string $type 模块
* @param string $path 路径
* @return string
*/
public static function getRealFilepath(string $type, string $path): string
{
return __DIR__ . '/../../web/' . $type . '/' . trim($path, '/');
}
/**
* 生成随机字符串
*/
public static function uuid()
{
if (function_exists('com_create_guid')) {
return com_create_guid();
} else {
$charid = md5(uniqid(rand(), true));
$hyphen = chr(45);// "-"
$uuid = substr($charid, 0, 8) . $hyphen
. substr($charid, 8, 4) . $hyphen
. substr($charid, 12, 4) . $hyphen
. substr($charid, 16, 4) . $hyphen
. substr($charid, 20, 12);
return $uuid;
}
}
//生成推广码
public static function create_invite_code()
{
$num = rand(1111, 9999);
$str = range('A', 'Z');
unset($str[array_search('O', $str)]);
shuffle($str);
$inviteCode = '';
$arr_len = count($str);
for ($i = 0; $i < 4; $i++) {
$rand = mt_rand(0, $arr_len - 1);
$inviteCode .= $str[$rand];
}
$inviteCode=str_shuffle($num.$inviteCode);
return $inviteCode;
}
/**
* 获取当天时间戳
*/
public static function getDayTime(){
$year = date("Y");
$month = date("m");
$day = date("d");
$start_time = mktime(0,0,0,$month,$day,$year);//当天开始时间戳
$end_time= mktime(23,59,59,$month,$day,$year);//当天结束时间戳
return [
'start_time'=>$start_time,
'end_time'=>$end_time,
];
}
//返回今天的开始时间和结束时间
public static function day_now()
{
$arr = [
mktime(0, 0, 0, date('m'), date('d'), date('Y')),
mktime(23, 59, 59, date('m'), date('d'), date('Y')),
];
return $arr;
}
//返回昨天开始结束时间 改造上边的方法
public static function day_yesterday()
{
$yesterday = date('d') - 1;
$arr = [
mktime(0, 0, 0, date('m'), $yesterday, date('Y')),
mktime(23, 59, 59, date('m'), $yesterday, date('Y')),
];
return $arr;
}
//获取当前时间的本周开始结束时间
public static function week_now()
{
$arr = [
strtotime(date('Y-m-d', strtotime("-1 week Monday", time()))),
strtotime(date('Y-m-d', strtotime("+0 week Sunday", time()))) - 1
];
return $arr;
}
//返回上周开始和结束的时间戳
public static function last_week()
{
// 1520179200 1520783999
$arr = [
// date('Y-m-d',strtotime('last week Monday',time())),
// date('Y-m-d',strtotime('last week Sunday',time()))
strtotime('last week Monday', time()),
strtotime('last week Sunday +1 days -1 seconds', time())
];
return $arr;
}
// 返回本月开始和结束的时间戳
public static function now_month()
{
$arr = [
mktime(0, 0, 0, date('m'), 1, date('Y')),
mktime(23, 59, 59, date('m'), date('t'), date('Y'))
];
return $arr;
}
// 返回某一年某一月的开始和结束的时间戳
public static function month_year($year, $month)
{
return [
$begin = mktime(0, 0, 0, $month, 1, $year),
$end = mktime(23, 59, 59, $month, date('t', $begin), $year)
];
}
// 返回当前季度的开始时间和结束时间
public static function now_quarter($month = 0)
{
$month = $month != 0 ? $month : date('n');
$season = ceil($month / 3);
return [
mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')),
mktime(0, 0, 0, $season * 3, date('t'), date('Y')) - 1
];
}
// 返回上个月开始和结束的时间戳
public static function lastMonth()
{
$begin = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
$end = mktime(23, 59, 59, date('m') - 1, date('t', $begin), date('Y'));
return [$begin, $end];
}
// 返回某天的结束时间戳
public static function getDayBE($day) {
return array(strtotime($day), strtotime($day)+24*3600-1);
}
public static function is_not_null($str){
//json_encode返回的是字符串, 而json_decode返回的是对象.
return is_null(json_decode($str));
}
//是否药性相畏
public static function is_drug_wei(){
}
//是否药性相反
public static function is_drug_opposite(){
}
}