#站内信API服务
[toc]
##一、运行方法
- 执行
composer install
- 复制
.env.example
到.env
并修改其中的数据库配置。 - 运行
php artisan migrate
执行数据库迁移。 - 运行
php -S 0.0.0.0:8080 -t public
开启服务(测试用)。
启动mysql数据库:
docker run --name message_server_mysql \
-e MYSQL_ROOT_PASSWORD=pass \
-d mysql --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
创建message_server数据库:
docker exec message_server_mysql bash -c 'echo "CREATE DATABASE message_server" | mysql -u root -ppass'
启动message_server:
./build_image.sh
./start-container.sh
##二、单元测试
phpunit
##三、接口文档
请求:GET /groups
响应:
{
"data": [
{
"id": 1,
"name": "major group",
"created_at": "2016-03-30 06:28:17",
"updated_at": "2016-03-30 06:28:17"
},
{
"id": 2,
"name": "second group",
"created_at": "2016-03-30 06:29:39",
"updated_at": "2016-03-30 06:29:39"
}
]
}
请求:POST /groups
参数:
- name(string): 用户组名称
请求示例
curl -H "Content-Type: application/json" --data '{"name": "major group"}' $host/groups
响应
{
"data": {
"name": "major group",
"updated_at": "2016-03-30 06:28:17",
"created_at": "2016-03-30 06:28:17",
"id": 1
}
}
请求: POST /groups/add_users
参数:
- group_id(int): 用户组id
- users(array of int): 要添加入的用户列表
{
group_id: 1,
users: [1, 2, 3]
}
请求示例
curl -H "Content-Type: application/json" --data '{"group_id": 1, "users": [1, 2, 3]}' $host/groups/add_users
请求:DELETE /groups/delete_users
参数:
- group_id(int): 用户组
- users(array of int): 要删除的用户
{
group_id: 1,
users: [1, 2, 3]
}
请求示例
curl -H "Content-Type: application/json" -X DELETE --data '{"group_id": 1, "users": [1, 2, 3]}' $host/groups/delete_users
请求:DELETE /groups/{group_id}
参数:
- group_id(int) 用户组ID
请求示例
curl -X DELETE $host/groups/2
请求:POST /messages/
参数:
- content(string): 消息内容
- target_type(string): 消息发送类型,可选项为:
user
、group
、global
,分别为发送给用户的消息,发送给群组的消息和发送给全体的消息。 - targets(array of int): 发送类型为
user
和group
时可用,标示要接收的群体。 - sender_id(int): 消息发送者ID
example:
{
'content': 'this is a message',
'target_type': 'user',
'targets': [1, 2, 3],
'sender_id': 1
}
请求示例
curl -H "Content-Type: application/json" --data '{"content":"this is a message","target_type":"user","targets":[1,2,3],"sender_id":1}' $host/messages
响应:
{"data":[]}
请求:GET /users/{user_id}/unread_messages_count
参数:
- user_id(int):用户ID
请求示例
curl $host/users/1/unread_messages_count
响应
{"data":1}
请求:GET /users/{user_id}/unread_messages
参数:
- user_id(int):用户ID
请求示例
curl $host/users/1/unread_messages
响应:
{
"data": [
{
"id": 1,
"content": "this is a message",
"created_at": "2016-03-30 06:42:59",
"sender_id": 1
}
]
}
请求:POST /messages/read
参数:
- user_id(int):用户ID
- message_id(int): 消息ID
请求示例:
curl -H "Content-Type: application/json" --data '{"user_id": 1,"message_id":1}' $host/messages/read
请求:GET /users/{user_id}/read_messages
参数:
- user_id(int):用户ID
请求示例
curl $host/users/1/unread_messages
响应:
{
"data": [
{
"id": 1,
"content": "this is a message",
"created_at": "2016-03-30 06:42:59",
"sender_id": 1
}
]
}