基于go-zero的微服务即时通讯项目day2——社交业务

业务设计

rpc

好友业务

  • 列出用户的所有好友

  • 申请好友

  • 列出收到的好友申请

  • 处理好友申请

群组业务

  • 创建群组

  • 列出群组

  • 列出群组内用户

  • 申请入群

  • 列出入群申请

  • 处理入群申请

  • 列出用户加入的群

.proto

syntax = "proto3";

package social;

option go_package = "./social";

// model
message Friends {
  int32  id = 1;
  string userId = 2;
  string remark = 3;
  int32  addSource = 4; // 添加来源
  string friendUid = 5;
}

message FriendRequests {
  int32  id = 1;
  string userId = 2;
  string reqUid = 3;
  string reqMsg = 4;
  int64  reqTime = 5;
  int32  handleResult = 6; // 处理结果
}

message Groups {
  string id = 1;
  string name = 2;
  string icon = 3;
  int32  status = 4;
  string creator_uid = 5;
  int32  groupType = 6;
  bool   isVerify = 7;
  string notification = 8; // 公告通知
  string notificationUid = 9;
}

message GroupMembers {
  int32  id = 1;
  string groupId = 2;
  string userId = 3;
  int32  roleLevel = 6;
  int64  joinTime = 7;
  int32  joinSource = 8;
  string inviterUid = 9;
  string operatorUid = 10;
}

message GroupRequests {
  int32  id = 1;
  string groupId = 2;
  string reqId = 3;
  string reqMsg = 4;
  int64  reqTime = 5;
  int32  joinSource = 6;
  string inviterUid = 7;
  string handleUid = 8;
  int32  handleResult = 9; // 处理结果
}

// req resp

message FriendPutInReq {
  string userId = 2;
  string reqUid = 3;
  string reqMsg = 4;
  int64  reqTime = 5;
}
message FriendPutInResp {}

message FriendPutInHandleReq {
  int32  friendReqId = 1;
  string userId = 2;
  int32 handleResult = 3; // 处理结果
}
message FriendPutInHandleResp {}

message FriendPutInListReq{
  string userId = 1;
}

message FriendPutInListResp{
  repeated FriendRequests list = 1;
}

message FriendListReq{
  string userId = 1;
}

message FriendListResp{
  repeated Friends list = 1;
}

// 群
message GroupCreateReq {
  string name = 2;
  string icon = 3;
  int32  status = 4;
  string creator_uid = 5;
}
message GroupCreateResp {
  string id = 1;
}

message GroupPutinReq {
  string groupId = 2;
  string reqId = 3;
  string reqMsg = 4;
  int64  reqTime = 5;
  int32  joinSource = 6;
  string inviterUid = 7;
}
message GroupPutinResp {
  string groupId = 1;
}

message GroupPutinListReq {
  string groupId = 1;
}
message GroupPutinListResp {
  repeated GroupRequests list = 1;
}

message GroupPutInHandleReq {
  int32  groupReqId = 1;
  string groupId = 2;
  string handleUid = 3;
  int32  handleResult = 4; // 处理结果

  string username = 6;
  string userAvatarUrl = 7;
}
message GroupPutInHandleResp {
  string groupId = 1;
}

message GroupListReq{
  string userId = 1;
}
message GroupListResp{
  repeated Groups list = 1;
}

message GroupUsersReq {
  string groupId = 1;
}
message GroupUsersResp {
  repeated GroupMembers List = 1;
}

// svc
service social {

  rpc FriendPutIn(FriendPutInReq) returns(FriendPutInResp);
  rpc FriendPutInHandle(FriendPutInHandleReq) returns(FriendPutInHandleResp);
  rpc FriendPutInList(FriendPutInListReq) returns(FriendPutInListResp);
  rpc FriendList(FriendListReq) returns (FriendListResp);

  rpc GroupCreate(GroupCreateReq) returns (GroupCreateResp);
  rpc GroupPutin(GroupPutinReq) returns (GroupPutinResp);
  rpc GroupPutinList(GroupPutinListReq) returns (GroupPutinListResp);
  rpc GroupPutInHandle(GroupPutInHandleReq) returns(GroupPutInHandleResp);
  rpc GroupList(GroupListReq) returns (GroupListResp);
  rpc GroupUsers(GroupUsersReq) returns (GroupUsersResp);

}

api

业务基本和rpc相同。

.api

syntax = "v1"

info (
    title:  "社交服务的实例对象"
    author: "FoyonaCZY"
)

type (
    Friends {
        Id        int32  `json:"id,omitempty"`
        FriendUid string `json:"friend_uid,omitempty"`
        Nickname  string `json:"nickname,omitempty"`
        Avatar    string `json:"avatar,omitempty"`
        Remark    string `json:"remark,omitempty"`
    }
    FriendRequests {
        Id           int64  `json:"id,omitempty"`
        UserId       string `json:"user_id,omitempty"`
        ReqUid       string `json:"req_uid,omitempty"`
        ReqMsg       string `json:"req_msg,omitempty"`
        ReqTime      int64  `json:"req_time,omitempty"`
        HandleResult int    `json:"handle_result,omitempty"`
        HandleMsg    string `json:"handle_msg,omitempty"`
    }
    Groups {
        Id              string `json:"id,omitempty"`
        Name            string `json:"name,omitempty"`
        Icon            string `json:"icon,omitempty"`
        Status          int64  `json:"status,omitempty"`
        GroupType       int64  `json:"group_type,omitempty"`
        IsVerify        bool   `json:"is_verify,omitempty"`
        Notification    string `json:"notification,omitempty"`
        NotificationUid string `json:"notification_uid,omitempty"`
    }
    GroupMembers {
        Id            int64  `json:"id,omitempty"`
        GroupId       string `json:"group_id,omitempty"`
        UserId        string `json:"user_id,omitempty"`
        Nickname      string `json:"nickname,omitempty"`
        UserAvatarUrl string `json:"user_avatar_url,omitempty"`
        RoleLevel     int    `json:"role_level,omitempty"`
        InviterUid    string `json:"inviter_uid,omitempty"`
        OperatorUid   string `json:"operator_uid,omitempty"`
    }
    GroupRequests {
        Id            int64  `json:"id,omitempty"`
        UserId        string `json:"user_id,omitempty"`
        GroupId       string `json:"group_id,omitempty"`
        ReqMsg        string `json:"req_msg,omitempty"`
        ReqTime       int64  `json:"req_time,omitempty"`
        JoinSource    int64  `json:"join_source,omitempty"`
        InviterUserId string `json:"inviter_user_id,omitempty"`
        HandleUserId  string `json:"handle_user_id,omitempty"`
        HandleTime    int64  `json:"handle_time,omitempty"`
        HandleResult  int64  `json:"handle_result,omitempty"`
    }
)

type (
    FriendPutInReq {
        ReqMsg  string `json:"req_msg,omitempty"`
        ReqTime int64  `json:"req_time,omitempty"`
        UserId  string `json:"user_uid"`
    }
    FriendPutInResp  {}
)

type (
    FriendPutInHandleReq {
        FriendReqId  int32 `json:"friend_req_id,omitempty"`
        HandleResult int32 `json:"handle_result,omitempty"` // 处理结果
    }
    FriendPutInHandleResp  {}
)

type (
    FriendPutInListReq  {}
    FriendPutInListResp {
        List []*FriendRequests `json:"list"`
    }
)

type (
    FriendListReq  {}
    FriendListResp {
        List []*Friends `json:"list"`
    }
)

type (
    GroupCreateReq {
        Name string `json:"name,omitempty"`
        Icon string `json:"icon,omitempty"`
    }
    GroupCreateResp  {}
)

type (
    GroupPutInRep {
        GroupId    string `json:"group_id,omitempty"`
        ReqMsg     string `json:"req_msg,omitempty"`
        ReqTime    int64  `json:"req_time,omitempty"`
        JoinSource int64  `json:"join_source,omitempty"`
    }
    GroupPutInResp  {}
)

type (
    GroupPutInHandleRep {
        GroupReqId   int32  `json:"group_req_id,omitempty"`
        GroupId      string `json:"group_id,omitempty"`
        HandleResult int32  `json:"handle_result,omitempty"` // 处理结果
    }
    GroupPutInHandleResp  {}
)

type (
    GroupPutInListRep {
        GroupId string `json:"group_id,omitempty"`
    }
    GroupPutInListResp {
        List []*GroupRequests `json:"list,omitempty"`
    }
)

type (
    GroupListRep  {}
    GroupListResp {
        List []*Groups `json:"list,omitempty"`
    }
)

type (
    GroupUserListReq {
        GroupId string `json:"group_id,omitempty"`
    }
    GroupUserListResp {
        List []*GroupMembers `json:"List,omitempty"`
    }
)

@server (
    prefix: v1/social
    group:  friend
    jwt:    JwtAuth
)
service social {
    @doc "好友申请"
    @handler friendPutIn
    post /friend/putIn (FriendPutInReq) returns (FriendPutInResp)

    @doc "好友申请处理"
    @handler friendPutInHandle
    put /friend/putIn (FriendPutInHandleReq) returns (FriendPutInHandleResp)

    @doc "好友申请列表"
    @handler friendPutInList
    get /friend/putIns (FriendPutInListReq) returns (FriendPutInListResp)

    @doc "好友列表"
    @handler friendList
    get /friends (FriendListReq) returns (FriendListResp)
}

@server (
    prefix: v1/social
    group:  group
    jwt:    JwtAuth
)
service social {
    @doc "创群"
    @handler createGroup
    post /group (GroupCreateReq) returns (GroupCreateResp)

    @doc "申请进群"
    @handler groupPutIn
    post /group/putIn (GroupPutInRep) returns (GroupPutInResp)

    @doc "申请进群处理"
    @handler groupPutInHandle
    put /group/putIn (GroupPutInHandleRep) returns (GroupPutInHandleResp)

    @doc "申请进群列表"
    @handler groupPutInList
    get /group/putIns (GroupPutInListRep) returns (GroupPutInListResp)

    @doc "用户申群列表"
    @handler groupList
    get /groups (GroupListRep) returns (GroupListResp)

    @doc "成员列表列表"
    @handler groupUserList
    get /group/users (GroupUserListReq) returns (GroupUserListResp)
}

models

包含群组表、申请表和一些关系表。

.sql

CREATE TABLE `friends` (
   `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
   `user_id` varchar(64) COLLATE utf8mb4_unicode_ci  NOT NULL ,
   `friend_uid` varchar(64) COLLATE utf8mb4_unicode_ci  NOT NULL ,
   `remark` varchar(255) DEFAULT NULL,
   `add_source`  tinyint COLLATE utf8mb4_unicode_ci DEFAULT NULL,
   `created_at` timestamp NULL DEFAULT NULL,
   PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `friend_requests` (
   `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
   `user_id` varchar(64) COLLATE utf8mb4_unicode_ci  NOT NULL ,
   `req_uid` varchar(64) COLLATE utf8mb4_unicode_ci  NOT NULL ,
   `req_msg` varchar(255) DEFAULT NULL,
   `req_time` timestamp  NOT NULL,
   `handle_result`  tinyint COLLATE utf8mb4_unicode_ci DEFAULT NULL,
   `handle_msg` varchar(255) DEFAULT NULL,
   `handled_at`timestamp NULL DEFAULT NULL,
   PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `groups` (
 `id` varchar(24) COLLATE utf8mb4_unicode_ci  NOT NULL ,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci  NOT NULL ,
  `icon` varchar(255) COLLATE utf8mb4_unicode_ci  NOT NULL ,
  `status`  tinyint COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `creator_uid` varchar(64) COLLATE utf8mb4_unicode_ci  NOT NULL ,
  `group_type` int(11) NOT NULL ,
  `is_verify` boolean NOT NULL ,
  `notification` varchar(255) DEFAULT NULL,
  `notification_uid` varchar(64) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `group_members` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `group_id` varchar(64) COLLATE utf8mb4_unicode_ci  NOT NULL ,
 `user_id` varchar(64) COLLATE utf8mb4_unicode_ci  NOT NULL ,
 `role_level`  tinyint COLLATE utf8mb4_unicode_ci NOT NULL ,
 `join_time` timestamp NULL DEFAULT NULL,
 `join_source`  tinyint COLLATE utf8mb4_unicode_ci DEFAULT NULL,
 `inviter_uid` varchar(64) DEFAULT NULL,
 `operator_uid` varchar(64) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `group_requests` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `req_id` varchar(64) COLLATE utf8mb4_unicode_ci  NOT NULL ,
  `group_id` varchar(64) COLLATE utf8mb4_unicode_ci  NOT NULL ,
  `req_msg` varchar(255) DEFAULT NULL,
  `req_time` timestamp NULL DEFAULT NULL,
  `join_source`  tinyint COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `inviter_user_id` varchar(64) DEFAULT NULL,
  `handle_user_id` varchar(64) DEFAULT NULL,
  `handle_time` timestamp NULL DEFAULT NULL,
  `handle_result`  tinyint COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

实现流程和用户业务相仿。

你刚刚浪费了人生中宝贵的几分钟。
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇