function.php 3.6 KB
<?php

declare(strict_types=1);

use Helper\Response;

/**
 * 数据返回
 * @return Response
 * @author:dc
 * @time 2023/2/5 9:37
 */
function res():Response {
    return new Response();
}

/**
 * @return string
 * @author:dc
 * @time 2023/2/5 14:01
 */
function token_get():string {
    $token = request()->header('__TOKEN__');
    if($token){
        return token_de($token);
    }
    return '';
}


/**
 * @param string $str
 * @return string
 * @author:dc
 * @time 2023/2/5 13:56
 */
function token_de(string $str):string {

    $str = decrypt($str);

    list($base,$sign) = explode('+',$str);

    $base = @base64_decode($base);

    if(md5($base.'.1') != $sign){
        return '';
    }

    return $base;
}

/**
 * @param string $str
 * @return string
 * @author:dc
 * @time 2023/2/5 13:57
 */
function token_en(string $str):string {
    $str = base64_encode($str) . '+' . md5($str.'.1');

    return encrypt($str);
}


/**
 * 数组转树组
 * @param $list
 * @param string $pk
 * @param string $pid
 * @param string $child
 * @param int $root
 * @param bool $empty_child
 * @return array
 * @author:dc
 * @time 2023/2/6 9:14
 */
function list_to_tree($list, $pk='id',$pid = 'pid',$child = '_child',$root=0, $empty_child=true):array {
    // 创建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;
}


/**
 * swoole 携程使用,在协程里面不要使用 laravel 的 cache 缓存 的redis驱动
 * @return \Swoole\Coroutine\Redis
 * @author:dc
 * @time 2023/2/7 15:07
 */
function swoole_redis():\Swoole\Coroutine\Redis {

    // 不能定义全局变量接搜。
    $redis  =   new \Swoole\Coroutine\Redis();
    $redis->connect(
        env('REDIS_HOST','127.0.0.1'),
        intval(env('REDIS_PORT',6379)),
        true
    );
    // 密码
    $password = env('REDIS_PASSWORD',null);
    $password && $redis->auth($password);
    // 用库4
    $redis->select(4);

    return $redis;

}

/**
 * 返回 lua 脚本 存在key就无法添加成功
 * @param $key
 * @param $val
 * @param int $ttl
 * @return array
 * @author:dc
 * @time 2023/2/7 16:24
 */
function swoole_redis_add($key,$val,$ttl=-1):array {
    return [
        "return redis.call('exists',KEYS[1])<1 and redis.call('setex',KEYS[1],ARGV[2],ARGV[1])",
        [$key, $val, $ttl],
        1
    ];
}

/**
 * swoole 操作db
 * @return \Swoole\Coroutine\MySQL
 * @author:dc
 * @time 2023/2/8 16:48
 */
function swoole_db():\Swoole\Coroutine\MySQL{

    $swoole_mysql = new \Swoole\Coroutine\MySQL();

    $swoole_mysql->connect([
        'host'     => env('DB_HOST','127.0.0.1'),
        'port'     => env('DB_PORT',3306),
        'user'     => env('DB_USERNAME'),
        'password' => env('DB_PASSWORD'),
        'database' => env('DB_DATABASE'),
    ]);

    return $swoole_mysql;
}

/**
 * 查询一条
 * @param $db \Swoole\Coroutine\MySQL
 * @param $query
 * @return mixed
 * @author:dc
 * @time 2023/2/8 17:15
 */
function swoole_db_first(&$db,$query){
    $row = $db->query($query);
    return $row[0]??null;
}