proto3文件定义Demo-用户表单条、多条、所有、编辑

一、用户表SQL

  • 用户表sql如下:
CREATE TABLE `user` (
  `id` int(11) NOT NULL COMMENT '主键ID',
  `name` varchar(100) DEFAULT NULL COMMENT '用户名',
  `password` varchar(100) DEFAULT NULL COMMENT '用户密码',
  `balance` decimal(10,2) DEFAULT NULL COMMENT '用户余额',
  `status` tinyint(1) DEFAULT NULL COMMENT '用户状态:0-待审核;1-已审核',
  `create_time` int(10) DEFAULT NULL COMMENT '创建时间',
  `update_time` int(10) DEFAULT NULL COMMENT '更新时间',
  `is_deleted` tinyint(1) DEFAULT NULL COMMENT '是否删除:1-是;0-否',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

二、proto3文件定义如下:

syntax = "proto3";

package user;
option go_package = "shop/proto/user;user";

service UserService {
    rpc Info(InfoRequest) returns (InfoResponse) {} //单条数据获取
    rpc List(ListRequest) returns (ListResponse) {} //分页数据获取
    rpc ListAll(ListRequest) returns (ListAllResponse) {} //所有数据获取
    rpc Edit(EditRequest) returns (EditResponse) {} //编辑用户数据
}

//定义单条数据请求、响应数据
message InfoRequest {
    int64 id = 1;
}

message InfoResponse {
    int64 id = 1;
    string name = 2;
    string password = 3;
    double balance = 4;
    int64 status = 5;
    int64 create_time = 6;
    int64 update_time = 7;
    int64 is_deleted = 8;
}

//定义分页数据请求、响应数据
message ListRequest {
    int64 id = 1;
    repeated int64 ids = 2;
    string name = 3;
    int64 page = 4;
    int64 page_size = 5;
    repeated string fields = 6;
    map<string, string> sorter = 7;
}

message ListResponse {
    int64 count = 1;
    repeated InfoResponse data = 2;
}

//定义所有数据请求、响应数据
message ListAllResponse {
    repeated InfoResponse list = 1;
}

//用户编辑数据请求、响应数据
message EditRequest {
    InfoResponse data = 1;
    repeated string fields = 2;
}

message EditResponse {
    bool res = 1;
}

三、proto文件字段对应的Json数据格式

1、InfoRequest

{
    
    
	"id": 1
}

2、InfoResponse

{
    
    
	"id": 1,
	"name": "姓名",
	"password": "$2y$10$G\/oxMamAVl.CPpE4MIeppujopq51TjM0hH0VV4oInrAKTti7nblAy",
	"balance": 0.01,
	"status": 1,
	"create_time": 1612669219,
	"update_time": 1612669219,
	"is_delete": 0
}

3、ListRequest

{
    
    
	"id": 1,
	"ids": [1, 2, 3],
	"name": "姓名",
	"page": 1,
	"page_size": 10,
	"fields": ["id", "name", "balance", "status"],
	"sorter": {
    
    
		"create_time": "DESC",
		"id": "ASC"
	}
}

4、ListResponse

{
    
    
	"count": 2,
	"data": [{
    
    
		"id": 1,
		"name": "姓名",
		"password": "$2y$10$G\/oxMamAVl.CPpE4MIeppujopq51TjM0hH0VV4oInrAKTti7nblAy",
		"balance": 0.01,
		"status": 1,
		"create_time": 1612669219,
		"update_time": 1612669219,
		"is_delete": 0
	}, {
    
    
		"id": 1,
		"name": "姓名",
		"password": "$2y$10$G\/oxMamAVl.CPpE4MIeppujopq51TjM0hH0VV4oInrAKTti7nblAy",
		"balance": 0.01,
		"status": 1,
		"create_time": 1612669219,
		"update_time": 1612669219,
		"is_delete": 0
	}]
}

5、ListAllResponse

{
    
    
	"list": [{
    
    
		"id": 1,
		"name": "姓名",
		"password": "$2y$10$G\/oxMamAVl.CPpE4MIeppujopq51TjM0hH0VV4oInrAKTti7nblAy",
		"balance": 0.01,
		"status": 1,
		"create_time": 1612669219,
		"update_time": 1612669219,
		"is_delete": 0
	}, {
    
    
		"id": 1,
		"name": "姓名",
		"password": "$2y$10$G\/oxMamAVl.CPpE4MIeppujopq51TjM0hH0VV4oInrAKTti7nblAy",
		"balance": 0.01,
		"status": 1,
		"create_time": 1612669219,
		"update_time": 1612669219,
		"is_delete": 0
	}]
}

6、EditRequest

{
    
    
	"data": {
    
    
		"id": 1,
		"name": "姓名",
		"password": "$2y$10$G\/oxMamAVl.CPpE4MIeppujopq51TjM0hH0VV4oInrAKTti7nblAy",
		"balance": 0.01,
		"status": 1,
		"create_time": 1612669219,
		"update_time": 1612669219,
		"is_delete": 0
	},
	"fields": ["id", "name", "balance", "status"]
}

7、EditResponse

{
    
    
	"res": true
}

猜你喜欢

转载自blog.csdn.net/qq_36025814/article/details/113738267