Redis 缓存

PhpRedis 操作类,使用前请确实是否已安装 Redis 扩展,你需要在主配置或对应的模块下创建配置 config/redis.php,例如:

  1. return [
  2. 'connect' => 'localhost',
  3. 'port' => 6379,
  4. 'auth' => '12345678',
  5. 'select' => 0
  6. ];
  • connect string 连接地址
  • port int 端口
  • auth string 验证密码
  • select int 库号

定义 Redis 操作模型

  • model ($index)
    • index int|null 库号,默认 null
    • return Redis

设置一个字符串缓存

  1. use think\bit\facade\Redis;
  2. Redis::model()->set('hello', 'world');

定义 Redis 事务处理

  • transaction(Closure $closure)
    • closure Closure
    • return boolean

执行一段缓存事务设置

  1. use think\bit\facade\Redis;
  2. Redis::transaction(function (\Redis $redis) {
  3. return (
  4. $redis->set('name1', 'js') &&
  5. $redis->set('name2', 'php')
  6. );
  7. });
  8. // true or false

Mongo 数据库

MongoDB 数据库的操作类,使用前请确实是否已安装 MongoDB 扩展,你需要在主配置或对应的模块下创建配置 config/mongo.php,例如:

  1. return [
  2. 'uri' => 'mongodb://127.0.0.1:27017',
  3. 'uriOptions' => [],
  4. 'driverOptions' => [],
  5. 'database' => 'test'
  6. ];
  • uri string 连接地址
  1. mongodb://[username:password@]host1[:port1][,...hostN[:portN]]][/[database][?options]]
  • uriOptions array 等于 [?options]
  • driverOptions array 驱动参数
  • database string 默认数据库

指向数据库

  • Db($database = ‘’)
    • database string 数据库名称,默认值为配置默认数据库
    • return \MongoDB\Database

查询数据

  1. $result = Mgo::Db()
  2. ->selectCollection('api')
  3. ->find();
  4. return $result->toArray();

写入数据

  1. $result = Mgo::Db('center')->selectCollection('admin')->insertOne([
  2. 'name' => 'kain',
  3. 'status' => 1,
  4. 'create_time' => new \MongoDB\BSON\UTCDateTime(time() * 1000),
  5. 'update_time' => new \MongoDB\BSON\UTCDateTime(time() * 1000)
  6. ])->isAcknowledged();
  7. return $result;

更多操作可参考 MongoDB PHP Library Reference.

Rabbit 消息队列

RabbitMQ 消息队列 AMQP 操作类,使用前请确实是否已安装 php-amqplib/php-amqplib,如未安装请手动执行

  1. composer require php-amqplib/php-amqplib

当前 window 系统下需要使用 "php-amqplib/php-amqplib": "^2.8.2-rc3" 才可正常运行

连接参数 :id=args

默认下 rabbitmq 连接参数为:

配置名称 默认值 说明
hostname localhost AMQP 连接地址
port 5672 AMQP 连接端口
username guest 连接用户
password guest 连接用户口令
virualhost / 虚拟主机
insist false 不允许代理重定向
login_method AMQPLAIN 登录方法
login_response null 登录响应
locale en_US 国际化
connection_timeout 3.0 连接超时
read_write_timeout 3.0 读写超时
context null 内容
keepalive false 保持连接
heartbeat 0 连接心跳
channel_rpc_timeout 0.0 信道 RPC 超时

你需要在主配置或对应的模块下创建配置 config/rabbitmq.php,例如:

  1. return [
  2. 'hostname' => 'localhost',
  3. 'port' => 5672,
  4. 'username' => 'guest',
  5. 'password' => 'guest',
  6. ];

也可以配合 Env 实现开发、生产分离配置:

  1. return [
  2. 'hostname' => env('rabbitmq.hostname', 'localhost'),
  3. 'port' => env('rabbitmq.port', 5672),
  4. 'username' => env('rabbitmq.username', 'guest'),
  5. 'password' => env('rabbitmq.password', 'guest'),
  6. ];

创建默认信道

  • start($closure, $args = [], $config = [])
    • closure Closure 信道处理
    • args array 连接参数
    • config array 操作配置 | 操作配置名称 | 类型 | 默认值 | 说明 | | —- | —- | —- | —- | | transaction | boolean | false | 开启事务 | | channel_id | string | null | 定义信道 ID | | reply_code | int | 0 | 回复码 | | reply_text | string | ‘’ | 回复文本 | | method_sig | array | [0,0] | - |
  1. Rabbit::start(function () {
  2. Rabbit::queue('hello')->create();
  3. });

创建自定义信道

  • connect($closure, $args = [], $config = [])
    • closure Closure 信道处理
    • args array 连接参数
    • config array 操作配置 | 操作配置名称 | 类型 | 默认值 | 说明 | | —- | —- | —- | —- | | transaction | boolean | false | 开启事务 | | channel_id | string | null | 定义信道 ID | | reply_code | int | 0 | 回复码 | | reply_text | string | ‘’ | 回复文本 | | method_sig | array | [0,0] | - |
  1. Rabbit::connect(function () {
  2. Rabbit::queue('hello')->create();
  3. }, [
  4. 'hostname' => 'developer.com',
  5. 'port' => 5672,
  6. 'username' => 'kain',
  7. 'password' => '******'
  8. ]);

获取连接对象

  • native()
    • return AMQPStreamConnection
  1. Rabbit::start(function () {
  2. dump(Rabbit::native());
  3. dump(Rabbit::native()->getChannelId());
  4. });

获取信道

  • channel()
    • return AMQPChannel
  1. Rabbit::start(function () {
  2. dump(Rabbit::native());
  3. dump(Rabbit::native()->getChannelId());
  4. });

创建消息对象

  • message($text = ‘’, $config = [])
    • text string|array 消息
    • config array 操作配置
    • return AMQPMessage
  1. Rabbit::start(function () {
  2. Rabbit::message('test');
  3. });

发布消息

  • publish($text = ‘’, $config = [])
    • text string|array 消息
    • config array 操作配置
  1. Rabbit::start(function () {
  2. Rabbit::exchange('extest')->create('direct');
  3. Rabbit::queue('hello')->create();
  4. Rabbit::queue('hello')->bind('extest', [
  5. 'routing_key' => 'rtest'
  6. ]);
  7. Rabbit::publish('test', [
  8. 'exchange' => 'extest',
  9. 'routing_key' => 'rtest'
  10. ]);
  11. });

交换器操作类

  • exchange($exchange)
    • exchange string 交换器名称
    • return Exchange 交换器类
  1. Rabbit::start(function () {
  2. $exchange = Rabbit::exchange('extest');
  3. });

声明交换器

  • ->create($type, $config = [])
    • type string 交换器类型 (direct、headers、fanout、topic)
    • config array 操作配置
    • return mixed|null | 操作配置名称 | 类型 | 默认值 | 说明 | | —- | —- | —- | —- | | passive | boolean | false | 检验队列是否存在 | | durable | boolean | false | 是否持久化 | | auto_delete | boolean | true | 自动删除 | | internal | boolean | false | 仅交换绑定有效 | | nowait | boolean | false | 客户端不等待回复 | | arguments | array | [] | 扩展参数 | | ticket | string | null | - |
  1. Rabbit::start(function () {
  2. Rabbit::exchange('extest')->create('direct');
  3. });

起源交换器绑定交换器

  • ->bind($destination, $config = [])
    • destination string 绑定交换器
    • config array 操作配置
    • return mixed|null | 操作配置名称 | 类型 | 默认值 | 说明 | | —- | —- | —- | —- | | routing_key | string | ‘’ | 路由键 | | nowait | boolean | false | 客户端不等待回复 | | arguments | array | [] | 扩展参数 | | ticket | string | null | - |
  1. Rabbit::start(function () {
  2. Rabbit::exchange('extest')->create('direct');
  3. Rabbit::exchange('newtest')->create('direct');
  4. Rabbit::exchange('newtest')->bind('extest');
  5. });

起源交换器解除绑定的交换器

  • ->unbind($destination, $config = [])
    • destination string 绑定交换器
    • config array 操作配置
    • return mixed | 操作配置名称 | 类型 | 默认值 | 说明 | | —- | —- | —- | —- | | routing_key | string | ‘’ | 路由键 | | nowait | boolean | false | 客户端不等待回复 | | arguments | array | [] | 扩展参数 | | ticket | string | null | - |
  1. Rabbit::start(function () {
  2. Rabbit::exchange('extest')->create('direct');
  3. Rabbit::exchange('newtest')->create('direct');
  4. Rabbit::exchange('newtest')->bind('extest');
  5. Rabbit::exchange('newtest')->unbind('extest');
  6. });

删除交换器

  • ->delete($config = [])
    • config array 操作配置
    • return mixed|null | 操作配置名称 | 类型 | 默认值 | 说明 | | —- | —- | —- | —- | | if_unused | boolean | false | 仅删除没有队列绑定的交换器 | | nowait | boolean | false | 客户端不等待回复 | | ticket | string | null | - |
  1. Rabbit::start(function () {
  2. Rabbit::exchange('extest')->delete();
  3. });

队列操作类

  • queue($queue)
    • queue string 队列名称
    • return Queue
  1. Rabbit::start(function () {
  2. $queue = Rabbit::queue('hello');
  3. $queue->create();
  4. });

声明队列

  • ->create($config = [])
    • config array 操作配置
    • return mixed|null | 操作配置名称 | 类型 | 默认值 | 说明 | | —- | —- | —- | —- | | passive | boolean | false | 检验队列是否存在 | | durable | boolean | false | 是否持久化 | | exclusive | boolean | false | 排除队列 | | auto_delete | boolean | true | 自动删除 | | nowait | boolean | false | 客户端不等待回复 | | arguments | array | [] | 扩展参数 | | ticket | string | null | - |
  1. Rabbit::start(function () {
  2. Rabbit::queue('hello')->create();
  3. });

绑定队列

  • ->bind($exchange, $config = [])
    • exchange string 交换器名称
    • config array 操作配置
    • return mixed|null | 操作配置名称 | 类型 | 默认值 | 说明 | | —- | —- | —- | —- | | routing_key | string | ‘’ | 路由键 | | nowait | boolean | false | 客户端不等待回复 | | arguments | array | [] | 扩展参数 | | ticket | string | null | - |
  1. Rabbit::start(function () {
  2. Rabbit::exchange('extest')->create('direct');
  3. $queue = Rabbit::queue('hello');
  4. $queue->create();
  5. $queue->bind('extest');
  6. });

解除绑定

  • ->unbind($exchange, $config = [])
    • exchange string
    • config array 操作配置
    • return mixed | 操作配置名称 | 类型 | 默认值 | 说明 | | —- | —- | —- | —- | | routing_key | string | ‘’ | 路由键 | | arguments | array | [] | 扩展参数 | | ticket | string | null | - |
  1. Rabbit::start(function () {
  2. Rabbit::exchange('extest')->create('direct');
  3. $queue = Rabbit::queue('hello');
  4. $queue->create();
  5. $queue->bind('extest');
  6. $queue->unbind('extest');
  7. });

清除队列

  • ->purge($config = [])
    • config array 操作配置
    • return mixed|null | 操作配置名称 | 类型 | 默认值 | 说明 | | —- | —- | —- | —- | | arguments | array | [] | 扩展参数 | | ticket | string | null | - |
  1. Rabbit::start(function () {
  2. Rabbit::exchange('extest')->create('fanout');
  3. $queue = Rabbit::queue('hello');
  4. $queue->create();
  5. $queue->bind('extest');
  6. Rabbit::publish('message', [
  7. 'exchange' => 'extest',
  8. ]);
  9. $queue->purge();
  10. });

删除队列

  • ->delete($config = [])
    • config array 操作配置
    • return mixed|null | 操作配置名称 | 类型 | 默认值 | 说明 | | —- | —- | —- | —- | | if_unused | boolean | false | 仅删除没有队列绑定的交换器 | | if_empty | boolean | false | 完全清空队列 | | arguments | array | [] | 扩展参数 | | ticket | string | null | - |

if_empty 删除队列时,如果在服务器配置中定义了任何挂起的消息,则会将任何挂起的消息发送到死信队列,并且队列中的所有使用者都将被取消

  1. Rabbit::start(function () {
  2. $queue = Rabbit::queue('hello');
  3. $queue->create();
  4. $queue->delete();
  5. });

获取队列信息

  • ->get($config = [])
    • config array 操作配置
    • return mixed | 操作配置名称 | 类型 | 默认值 | 说明 | | —- | —- | —- | —- | | no_ack | boolean | false | 手动确认消息 | | ticket | string | null | - |
  1. Rabbit::start(function () {
  2. Rabbit::exchange('extest')->create('fanout');
  3. $queue = Rabbit::queue('hello');
  4. $queue->create();
  5. $queue->bind('extest');
  6. Rabbit::publish('message', [
  7. 'exchange' => 'extest',
  8. ]);
  9. dump($queue->get()->body);
  10. });
  11. // message

消费者操作类

  • consumer($consumer)
    • consumer string 消费者名称
    • return Consumer

启用消费者

  • ->start($queue, $config = [])
    • queue string 队列名称
    • config array 操作配置
    • return mixed|string | 操作配置名称 | 类型 | 默认值 | 说明 | | —- | —- | —- | —- | | no_local | boolean | false | 独占消费 | | no_ack | boolean | false | 手动确认消息 | | exclusive | boolean | false | 排除队列 | | nowait | boolean | false | 客户端不等待回复 | | callback | Closure | null | 回调函数 | | arguments | array | [] | 扩展参数 | | ticket | string | null | - |

no_local 请求独占消费者访问权限,这意味着只有此消费者才能访问队列

结束消费者

  • ->cancel($config = [])
    • config array 操作配置
    • return mixed | 操作配置名称 | 类型 | 默认值 | 说明 | | —- | —- | —- | —- | | nowait | boolean | false | 客户端不等待回复 | | noreturn | boolean | false | - |

确认消息

  • ack($delivery_tag, $multiple = false)
    • delivery_tag string 标识
    • multiple boolean 批量

拒绝传入的消息

  • reject($delivery_tag, $requeue = false)
    • delivery_tag string 标识
    • requeue boolean 重新发送

拒绝一个或多个收到的消息

  • nack($delivery_tag, $multiple = false, $requeue = false)
    • delivery_tag string 标识
    • multiple boolean 批量
    • requeue boolean 重新发送

重新发送未确认的消息

  • revover($requeue = false)
    • requeue boolean 重新发送
    • return mixed

Cipher 密码

Cipher 是用于加密的工具函数,首先要定义配置 config/cipher.php

  1. return [
  2. 'key' => env('cipher.key'),
  3. 'iv' => env('cipher.iv')
  4. ];
  • key string 加密密钥
  • iv string 偏移量

加密明文

  • encrypt($context, $key, $iv)
    • context string 明文
    • key string 自定义密钥
    • iv string 自定义偏移量
    • Return string 密文
  1. dump(Cipher::encrypt('123'));
  2. // s7Tkeof7utaDU4tVsTSbyA==

解密密文

  • decrypt($secret, $key, $iv)
    • secret string 密文
    • key string 自定义密钥
    • iv string 自定义偏移量
    • Return string 明文
  1. $secret = Cipher::encrypt('123');
  2. dump($secret);
  3. // s7Tkeof7utaDU4tVsTSbyA==
  4. dump(Cipher::decrypt($secret));
  5. // 123

加密数组为密文

  • encryptArray($data, $key, $iv)
    • data array 数组
    • key string 自定义密钥
    • iv string 自定义偏移量
    • Return string 密文
  1. dump(Cipher::encryptArray([1, 2, 3]));
  2. // eFIs2OR2/IXC3vv3febOVA==

解密密文为数组

  • decryptArray($secret, $key, $iv)
    • secret string 密文
    • key string 自定义密钥
    • iv string 自定义偏移量
    • Return array
  1. $secret = Cipher::encryptArray([1, 2, 3]);
  2. dump($secret);
  3. // eFIs2OR2/IXC3vv3febOVA==
  4. dump(Cipher::decryptArray($secret));
  5. // array (size=3)
  6. // 0 => int 1
  7. // 1 => int 2
  8. // 2 => int 3

Tools 工具

生成 uuid

  • uuid($version, $namespace, $name)
    • version string 为 uuid 型号,其中包含 v1v3v4v5,默认 v4
    • namespace string 命名空间,使用在 v3v5
    • name string 名称,使用在 v3v5
    • return string
  1. dump(Tools::uuid());
  2. // '4f38cd10-3518-4656-95a3-9cbb4d5a8f25'
  3. dump(Tools::uuid('v1'));
  4. // '3fe018b6-1f89-11e9-863d-aa151017e551'
  5. dump(Tools::uuid('v3', Uuid::NAMESPACE_DNS, 'van'));
  6. // '88124da6-a376-3c77-8fb1-456250a33254'
  7. dump(Tools::uuid('v5', Uuid::NAMESPACE_DNS, 'van'));
  8. // '72ca19ff-6897-5a8e-80c4-ed5d3b753115'
UUID Version 说明
v1 基于时间的 UUID
v3 基于名字的 UUID(MD5)
v4 随机 UUID
v5 基于名字的 UUID(SHA1)

生产订单号

  • orderNumber($service_code, $product_code, $user_code)
    • service_code string 业务码
    • product_code string 产品码
    • user_code string 用户码
    • return string
  1. dump(Tools::orderNumber('2', '100', '555'));
  2. // 28100154830173082555

随机数 16 位

  • random()
  1. dump(Tools::random());
  2. // 3nnoIk3XbVphym4k

随机数 8 位

  • randomShort()
  1. dump(Tools::randomShort());
  2. // 2maJYwas

Lists 列表数组

ArrayLists 列表数组操作类

列表数组初始化

  • data($lists)
    • lists array 传入初始化的数组
    • return BitLists
  1. $lists = Lists::data([1, 2, 3, 4, 5, 6]);
  2. dump($lists->toArray());
  3. // array (size=6)
  4. // 0 => int 1
  5. // 1 => int 2
  6. // 2 => int 3
  7. // 3 => int 4
  8. // 4 => int 5
  9. // 5 => int 6

获取数组大小

  • size()
    • return int
  1. $lists = Lists::data([1, 2, 3, 4, 5, 6]);
  2. $size = $lists->size();
  3. dump($size);
  4. // 6

设置键值

  • set($key, $value)
    • key string 键名
    • value string 键值
  1. $lists = Lists::data([1, 2, 3, 4, 5, 6]);
  2. $lists->set('name', 'test');
  3. dump($lists->toArray());
  4. // array (size=7)
  5. // 0 => int 1
  6. // 1 => int 2
  7. // 2 => int 3
  8. // 3 => int 4
  9. // 4 => int 5
  10. // 5 => int 6
  11. // 'name' => string 'test' (length=4)

数组加入元素

  • add(…$data)
    • data mixed 加入的元素
  1. $lists = Lists::data([1, 2, 3, 4, 5, 6]);
  2. $lists->add(7, 8, 9);
  3. dump($lists->toArray());
  4. // array (size=9)
  5. // 0 => int 1
  6. // 1 => int 2
  7. // 2 => int 3
  8. // 3 => int 4
  9. // 4 => int 5
  10. // 5 => int 6
  11. // 6 => int 7
  12. // 7 => int 8
  13. // 8 => int 9

向前数组加入元素

  • unshift(…$data)
    • data mixed 加入的元素
  1. $lists = Lists::data([1, 2, 3, 4, 5, 6]);
  2. $lists->unshift(-1, 0);
  3. dump($lists->toArray());
  4. // array (size=8)
  5. // 0 => int -1
  6. // 1 => int 0
  7. // 2 => int 1
  8. // 3 => int 2
  9. // 4 => int 3
  10. // 5 => int 4
  11. // 6 => int 5
  12. // 7 => int 6

数组是否为空

  • isEmpty()
    • return boolean
  1. $lists = Lists::data([]);
  2. dump($lists->isEmpty());
  3. // true

判断是否存在键名

  • has($key)
    • key string 键名
    • return boolean
  1. $lists = Lists::data([
  2. 'name' => 'test'
  3. ]);
  4. dump($lists->has('name'));
  5. // true

判断是否存在键值

  • contains($value)
    • value mixed 键值
    • return boolean
  1. $lists = Lists::data([
  2. 'name' => 'test'
  3. ]);
  4. dump($lists->contains('test'));
  5. // true

获取键值

  • get($key)
    • key mixed 键名
    • return mixed
  1. $lists = Lists::data([
  2. 'name' => 'test'
  3. ]);
  4. dump($lists->get('name'));
  5. // test

移除键值

  • delete($key)
    • key mixed 键名
  1. $lists = Lists::data([
  2. 'name' => 'test'
  3. ]);
  4. $lists->delete('name');
  5. dump($lists->toArray());
  6. // array (size=0)

数组开头的单元移出元素

  • shift()
    • return mixed 移出的元素
  1. $lists = Lists::data([1, 2, 3]);
  2. $lists->shift();
  3. dump($lists->toArray());
  4. // array (size=2)
  5. // 0 => int 2
  6. // 1 => int 3

数组出栈

  • pop()
    • return mixed 出栈的元素
  1. $lists = Lists::data([1, 2, 3]);
  2. $lists->pop();
  3. dump($lists->toArray());
  4. // array (size=2)
  5. // 0 => int 1
  6. // 1 => int 2

去除重复

  • unique()
  1. $lists = Lists::data([1, 1, 2, 2, 3]);
  2. $lists->unique();
  3. dump($lists->toArray());
  4. // array (size=3)
  5. // 0 => int 1
  6. // 2 => int 2
  7. // 4 => int 3

清除数据

  • clear()
  1. $lists = Lists::data([1, 1, 2, 2, 3]);
  2. $lists->clear();
  3. dump($lists->toArray());
  4. // array (size=0)

返回键名

  • keys()
    • return array 所有键名
  1. $lists = Lists::data([
  2. 'name' => 'van',
  3. 'age' => 100,
  4. 'sex' => 0
  5. ]);
  6. dump($lists->keys());
  7. // array (size=3)
  8. // 0 => string 'name' (length=4)
  9. // 1 => string 'age' (length=3)
  10. // 2 => string 'sex' (length=3)

返回键值

  • values()
    • return array 所有键值
  1. $lists = Lists::data([
  2. 'name' => 'van',
  3. 'age' => 100,
  4. 'sex' => 0
  5. ]);
  6. dump($lists->values());
  7. // array (size=3)
  8. // 0 => string 'van' (length=3)
  9. // 1 => int 100
  10. // 2 => int 0

搜索给定的值,返回键名

  • indexOf($value)
    • value mixed 键值
    • return string 键名
  1. $lists = Lists::data([
  2. 'name' => 'van',
  3. 'age' => 100,
  4. 'sex' => 0
  5. ]);
  6. dump($lists->indexOf('van'));
  7. // name

数组遍历返回

  • map(Closure $closure)
    • closure Closure 闭包函数
    • return array
  1. $lists = Lists::data([
  2. [
  3. 'product' => 'test1',
  4. 'price' => 10
  5. ],
  6. [
  7. 'product' => 'test2',
  8. 'price' => 20
  9. ]
  10. ]);
  11. $other_lists = $lists->map(function ($v) {
  12. $v['price'] += 10;
  13. return $v;
  14. });
  15. dump($other_lists);
  16. // array (size=2)
  17. // 0 =>
  18. // array (size=2)
  19. // 'product' => string 'test1' (length=5)
  20. // 'price' => int 20
  21. // 1 =>
  22. // array (size=2)
  23. // 'product' => string 'test2' (length=5)
  24. // 'price' => int 30

数组过滤

  • filter(Closure $closure)
    • closure Closure 闭包函数
    • return array
  1. $lists = Lists::data([
  2. [
  3. 'product' => 'test1',
  4. 'price' => 10
  5. ],
  6. [
  7. 'product' => 'test2',
  8. 'price' => 20
  9. ],
  10. [
  11. 'product' => 'test3',
  12. 'price' => 30
  13. ]
  14. ]);
  15. $other_lists = $lists->filter(function ($v) {
  16. return $v['price'] > 10;
  17. });
  18. dump($other_lists);
  19. // array (size=2)
  20. // 1 =>
  21. // array (size=2)
  22. // 'product' => string 'test2' (length=5)
  23. // 'price' => int 20
  24. // 2 =>
  25. // array (size=2)
  26. // 'product' => string 'test3' (length=5)
  27. // 'price' => int 30

数组切片

  • slice($offset, $length)
    • offset int 起始
    • length int 长度
    • return array
  1. $lists = Lists::data([1, 2, 3, 4, 5]);
  2. dump($lists->slice(1, 3));
  3. // array (size=3)
  4. // 0 => int 2
  5. // 1 => int 3
  6. // 2 => int 4

获取数组

  • toArray()
    • return array
  1. $lists = Lists::data([
  2. [
  3. 'product' => 'test1',
  4. 'price' => 10
  5. ],
  6. [
  7. 'product' => 'test2',
  8. 'price' => 20
  9. ],
  10. [
  11. 'product' => 'test3',
  12. 'price' => 30
  13. ]
  14. ]);
  15. dump($lists->toArray());
  16. // array (size=3)
  17. // 0 =>
  18. // array (size=2)
  19. // 'product' => string 'test1' (length=5)
  20. // 'price' => int 10
  21. // 1 =>
  22. // array (size=2)
  23. // 'product' => string 'test2' (length=5)
  24. // 'price' => int 20
  25. // 2 =>
  26. // array (size=2)
  27. // 'product' => string 'test3' (length=5)
  28. // 'price' => int 30

转为 Json

  • toJson()
    • return string
  1. $lists = Lists::data([
  2. [
  3. 'product' => 'test1',
  4. 'price' => 10
  5. ],
  6. [
  7. 'product' => 'test2',
  8. 'price' => 20
  9. ],
  10. [
  11. 'product' => 'test3',
  12. 'price' => 30
  13. ]
  14. ]);
  15. dump($lists->toJson());
  16. // [{"product":"test1","price":10},{"product":"test2","price":20},{"product":"test3","price":30}]

转为二进制

  • toBinary()
    • return string
$lists = Lists::data([
    [
        'product' => 'test1',
        'price' => 10
    ],
    [
        'product' => 'test2',
        'price' => 20
    ],
    [
        'product' => 'test3',
        'price' => 30
    ]
]);

dump($lists->toBinary());
// ���product�test1�price
// ��product�test2�price��product�test3�price

转为树形结构

  • toTree($id_name = ‘id’, $parent_name = ‘parent’, $child_name = ‘children’, $top_parent = 0) :id=to_tree
    • id_name string 数组主键名称
    • parent_name string 数组父级关联名称
    • child_name string 树形子集名称定义
    • top_parent int|string 最高级父级
    • return array
$lists = Lists::data([
    [
        'id' => 1,
        'name' => 'node1',
        'parent' => 0
    ],
    [
        'id' => 2,
        'name' => 'node2',
        'parent' => 0
    ],
    [
        'id' => 3,
        'name' => 'node3',
        'parent' => 1
    ],
    [
        'id' => 4,
        'name' => 'node4',
        'parent' => 1
    ],
    [
        'id' => 5,
        'name' => 'node5',
        'parent' => 4
    ],
    [
        'id' => 6,
        'name' => 'node6',
        'parent' => 2
    ],
]);

$tree = $lists->toTree();

Collect 数据收集

Collect 是用于简化数据收集消息队列写入的函数, 首先需要 Rabbitmq 配置 config/rabbitmq.php, 然后在主配置或对应的模块下设置配置 config/collect.php

return [
    'authorization' => [
        'appid' => 'xxx',
        'secret' => 'xxx'
    ],
    'exchange' => 'collect',
    'queue' => 'collect'
];
  • authorization 执行授权
    • appid 自定义应用 ID
    • secret 应用密钥
  • exchange 交换器
  • queue 队列

数据收集队列写入

  • push($motivation, $data = [], $time_field = [])
    • motivation string 行为命名
    • data array 数据
    • time_field array 时间字段

使用如下

Collect::push('pay_order', [
    'order' => Tools::orderNumber('L1', 'A1', '1100'),
    'product' => Tools::uuid(),
    'user' => Tools::uuid(),
    'create_time' => time(),
    'update_time' => time()
], ['create_time', 'update_time']);

使用前对应配置队列写入服务 https://github.com/kainonly/collection-service