function.php 6.4 KB
<?php

use Model\emailSql;


/**
 * 进程pid
 * @return int
 * @author:dc
 * @time 2023/2/18 14:00
 */
function posix_pid(){
    $pid = getmypid();
    return $pid ? $pid : 0;
}

/**
 * redis 驱动
 * @return \Lib\RedisPool
 * @author:dc
 * @time 2023/2/13 9:44
 */
function redis():\Lib\RedisPool {

    return \Lib\RedisPool::instance(posix_pid().co::getCid());

}


/**
 * 操作db
 * @return \Lib\DbPool
 * @author:dc
 * @time 2023/2/13 14:15
 */
function db():\Lib\DbPool{

    return \Lib\DbPool::instance(posix_pid().co::getCid());
}





/**
 * 记录日志
 * @param $message
 * @author:dc
 * @time 2023/2/10 14:58
 */
function logs($message,$filename=null){
    if(is_array($message)){
        $message = print_r($message,true);
    }
    \Lib\Log::append($message, $filename);

}


/**
 * 消息输出
 * @param $message
 * @author:dc
 * @time 2023/2/10 15:42
 */
function _echo($message){
    echo date('Y-m-d H:i:s').' '.$message."\n";
}



/**
 * 文本消息,多语言
 * @param $key
 * @return mixed
 * @author:dc
 * @time 2023/2/13 10:51
 */
function __($key):mixed{
    return $key ? \Lib\Lang::msg($key) : '';
}


/**
 * @return \Lib\App
 * @author:dc
 * @time 2023/2/13 11:48
 */
function app():\Lib\App{
    return \Lib\App::instance();
}


/**
 * 过滤函数
 * @param $value
 * @param null $filter
 * @return array|false|float|int|mixed
 * @author:dc
 * @time 2023/2/13 11:54
 */
function my_filter($value,$filter=null){
    if(is_array($value)){
        foreach ($value as $key=>$val){
            $value[$key]    =   my_filter($val,$filter);
        }
    } else {
        // 过滤函数
        if(!is_array($filter)){
            $filter =   [$filter];
        }
        // 合并默认过滤
        $filter =   array_merge(['trim'], $filter);

        // 循环过滤
        foreach ($filter as $fil){
            $fil && $value  =   call_user_func($fil,$value);
        }
    }
    // 强制转类型
    if(is_numeric($value)&&strlen($value)<10&&intval(substr($value,0,1))!==0){
        if(strpos($value,'.')!==false){
//            $value = doubleval($value); 这个要报毒,华为电脑
            $value = floatval($value);
        }else{
            $value = intval($value);
        }
    }

    return $value;
}

/**
 *  db 组合条件
 * @param array $where
 * @param string $ar
 * @return string
 * @author:dc
 * @time 2023/2/17 10:41
 */
function dbWhere(array $where, string $ar = 'and'):string{
    $sql = [];
    foreach ($where as $f=>$v){
        if(is_array($v)){
            $v = array_map(function ($n){
                return "'".addslashes($n)."'";
            },$v);
            if(count($v)===1){
                // 只有一个值时就是 =
                $sql[] = "`{$f}` = ".$v[0];
            }elseif (count($v) > 1){
                $sql[] = "`{$f}` in (".implode(',',$v).")";
            }

        }else{
            $sql[] = "`{$f}` = '".addslashes($v)."'";
        }

    }
    return implode(' '.$ar.' ',$sql);
}

/**
 * db 更新sql
 * @param array $data
 * @return string
 * @author:dc
 * @time 2023/2/17 10:43
 */
function dbUpdate(array $data):string {
    $sql = [];
    foreach ($data as $f=>$v){
        $sql[] = "`{$f}` = :{$f}";
    }
    return implode(' , ',$sql);
}


/**
 * 获取前端提交过来的邮箱
 * @return array
 * @throws \Lib\Err
 * @author:dc
 * @time 2023/3/10 14:57
 */
function web_request_emails():array {
    $emails = app()->request('emails');
    $emails = is_array($emails) ? $emails : [$emails];
    foreach ($emails as $k=>$email){
        if(!$email || !\Lib\Verify::sEmail($email)){
            unset($emails[$k]);
        }
    }

    if(empty($emails)){
        app()->e('email_request_required');
    }

    return array_values($emails);
}

/**
 * 前端获取邮箱一个
 * @return string
 * @throws \Lib\Err
 * @author:dc
 * @time 2023/3/10 16:05
 */
function web_request_email():string {
    $email = app()->request('email');

    if(!$email || !\Lib\Verify::sEmail($email)){
        app()->e('email_request_required');
    }

    return $email;
}




/**
 * 分页数据
 * @param $item
 * @param $total
 * @param $page
 * @param $limit
 * @return array
 * @author:dc
 * @time 2023/2/17 15:05
 */
function listsPage($item,$total,$page,$limit){
    return [
        'item'  =>  $item,
        'page'  =>  $page,
        'total'  =>  $total,
        'limit'  =>  $limit,
    ];
}




/**
 * 把返回的数据集转换成Tree
 * @param $list array 数据列表
 * @param string|int $pk 主键|root
 * @param string $pid 父id
 * @param string $child 子键
 * @param int $root 获取哪个id下面
 * @param bool $empty_child 当子数据不存在,是否要返回空子数据
 * @return array
 */
function list_to_tree($list, $pk='id',$pid = 'pid',$child = '_child',$root=0,$empty_child=true) {
    // 创建Tree
    $tree = array();
    if(is_array($list)) {
        // 创建基于主键的数组引用
        $refer = array();
        foreach ($list as $key => $data) {
            if($empty_child){
                $list[$key][$child] =   [];
            }
            $refer[$data[$pk]] =& $list[$key];
        }
        foreach ($list as $key => $data) {
            // 判断是否存在parent
            $parentId = $data[$pid];
            if ($root == $parentId) {
                $tree[] =& $list[$key];
            }else{
                if (isset($refer[$parentId])) {
                    $refer[$parentId][$child][] =   & $list[$key];
                }
            }
        }
    }
    return $tree;
}


/**
 * todo:: 立即开始同步邮件,非必要请不要手动调用,,系统有定时调用,
 * @author:dc
 * @time 2023/2/18 11:04
 */
function start_now_mail(){
    $id = 0;

    while (true){
        $ids = db()->all('select `id` from `'.\Model\emailSql::$table.'` where `id` > '.$id.' order by `id` asc limit 1000 offset 0');
        if(!$ids){
            break;
        }
        foreach ($ids as $v){
            $id = $v['id'];
            redis()->rPush('sync_email_lists', $v['id']);
        }
    }

}


/**
 * 固定文件夹的名称,统一
 * @param $folder
 * @return string
 * @author:dc
 * @time 2023/3/21 16:00
 */
function folderAlias($folder){
    $folder_map = [
        'INBOX' =>  '收件箱',
        // qq的
        'Sent Messages' =>  '发件箱',
        'Drafts' =>  '草稿箱',
        'Junk' =>  '垃圾箱',
        'Deleted Messages' =>  '回收站',
    ];

    return $folder_map[$folder]??$folder;

}