# bytedance **Repository Path**: w1ndowsZzz/bytedance ## Basic Information - **Project Name**: bytedance - **Description**: bytedance后端极简版抖音项目 - **Primary Language**: Unknown - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-05-14 - **Last Updated**: 2022-06-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # simple-demo ## 抖音项目服务端简单示例 具体功能内容参考飞书说明文档 工程无其他依赖,直接编译运行即可 ```shell go build && ./simple-demo ``` ### 功能说明 完善了所有基础接口,并且额外实现了点赞列表以及点赞操作,保存到v1.5版本中 * 用户登录数据保存在内存中,单次运行过程中有效 * 视频上传后会保存到本地 public 目录中,访问时用 127.0.0.1:8080/static/video_name 即可 # 接口完善 ## mysql说明 本地mysql创建douyin ` create database douyin; ` 在common/database中的初始化函数InitDB(), 修改账户密码为你自己的mysql账号密码 ```go username := "root" password := "123456" ``` ## User注册登录获取信息说明 ### model/user.go 结构体User对应douyin中的user表,成员对应字段 结构体Video对应douyin中的video表,成员对应字段 密码传输过程中使用哈希函数对密码进行加密保证数据传输过程中的安全性 注册的思路为首先在用户信息表中查询,若已有与当前username相同的用户则注册失败,若不存在则往user表中插入一条新用户信息。 登录的思路为首先在用户信息表中查询,若已有与当前username相同的用户则继续验证密码是否正确,两者皆正确则返回登录token以及用户信息。 _**扩展说明:**_ 该项目设计的表结构设计如下图所示: ```go type User struct {//用户信息表 gorm.Model UserId int64 `gorm:"int(64)"`//没啥用 Username string `gorm:"varchar(32);not null"` Password string `gorm:"size:255;not null"` FollowCount int64 `json:"follow_count,omitempty"` FollowerCount int64 `json:"follower_count,omitempty"` } type User_like_Video struct {//用户收藏的短视频表 UserId int64 `gorm:"int(64);not null"` VideoId int64 `gorm:"int(64);not null"` } type Video struct {//视频信息表 gorm.Model CreateAt time.Time VideoId int64 `gorm:"int(64);not null"`//没啥用 Username string `gorm:"varchar(32);not null"` PlayUrl string `json:"play_url" json:"play_url,omitempty"` CoverUrl string `json:"cover_url,omitempty"` FavoriteCount int64 `json:"favorite_count,omitempty"` CommentCount int64 `json:"comment_count,omitempty"` } type FavoriteVideo struct { //用户点赞的短视频表 gorm.Model UserId int64 `gorm:"int(64)"` VideoId int64 `gorm:"int(64)"` } ``` ## 视频流接口功能声明 直接返回video表中所有的视频,调用gorm框架中的scan方法 ## 用户信息接口功能声明 直接返回user表中用户id与请求中user_id相等的用户,调用gorm框架中的first方法 ## 上传视频接口功能声明 使用Select方法将传入的值与字段对应起来,上传VideoId, Username, PlayUrl, CoverUrl, FavoriteCount, CommentCount等必要信息,注意url需要使用http而非https ## 发布列表接口功能声明 直接返回video表中视频上传者id与请求中user_id相同的短视频,调用gorm框架中的scan方法。 ## 点赞接口功能声明 首先根据请求判断action_type的情况,若其等于1则为点赞操作,反之则为取消点赞操作 对于第一种操作,在数据库Favorite_Video表中增加一条点赞记录(点赞人,点赞视频),并将video表中对应视频的FavoriteCount++ 对于第二种操作,在数据库Favorite_Video表中删除一条点赞记录(点赞人,点赞视频),并将video表中对应视频的FavoriteCount-- ## 点赞列表功能声明 将Favorite_Video表和Video表联合使用进行查询,令Favorite_Video中的video_id=Video表中的video_id且user_id=请求中的user_id,返回视频列表。 ### Token 写成中间件在middleware,并且在调用上述接口之前做判断,来确认登录信息是否过期 在路由router.go中做以下修改 ```go apiRouter.GET("/user/", middleware.AuthMiddleware(), controller.UserInfo) ``` 自动验证token