Skip to content

Commit

Permalink
Merge pull request #17 from dbhuo/master
Browse files Browse the repository at this point in the history
修改feed部分逻辑
  • Loading branch information
dbhuo authored Jun 7, 2022
2 parents 15f8dee + e71681e commit 70fac34
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 142 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## 4399 菜鸟驿站队

### 项目启动方式
+ 登录方式 : 81.68.239.206:8080 导入到apk中

```shell
go run main.go
```

底层服务:mysql、Redis<br>
ORM框架:Gorm、Gorm拓展soft-delete<br>
HTTP框架:Gin、Gin拓展Gin-Jwt<br>
Expand All @@ -17,13 +24,12 @@ HTTP框架:Gin、Gin拓展Gin-Jwt<br>
>logic 存放实际业务逻辑<br>
>models 存放数据库结构体<br>
>pkg 项目相关模块包<br>
>
>>middleware 存放拦截器等中间件<br>
>>jwt 鉴权模块<br>
>>seting 全局信息相关<br>
>
>public 暂时存放视频,随后视频会自动传送到aliyun-oss<br>
>router 存放路由器<br>
>storage 项目生成的临时文件<br>
### 项目文档地址

https://bytedancecampus1.feishu.cn/docs/doccndqOddWdeOdGb1sTqcDQxLf#TO4ZzD
9 changes: 3 additions & 6 deletions configs/config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#mysql连接配置
mysql:
DBType: mysql
Username: douyin # 填写你的数据库账号
Password: abc123456+ # 填写你的数据库密码
Host: 81.68.239.206:3306
#Username: root
#Password: 123
#Host: 127.0.0.1:3306
Username: root
Password: 123
Host: 127.0.0.1:3306
DBName: douyin
TablePrefix: dy_
Charset: utf8
Expand Down
32 changes: 0 additions & 32 deletions controller/demo_data.go

This file was deleted.

15 changes: 4 additions & 11 deletions controller/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,18 @@ type FeedResponse struct {

// Feed same demo video list for every request
func Feed(c *gin.Context) {
param := new(models.ParamAuth)
c.ShouldBind(param)
token := param.Token
//处理业务逻辑
videoList, err := logic.GetVideoList()
videoList, err := logic.GetVideoList(token)
if err != nil {
global.Logger.Error("videoList not exit", zap.Error(err))
c.JSON(http.StatusBadRequest, FeedResponse{
Response: Response{StatusCode: 1, StatusMsg: "获取视频列表失败"},
NextTime: time.Now().Unix(),
})
}
//TODO:如果用户存在Token,那么需要设置是否喜欢状态。也就是需要设置 is_favorite字段
//token := c.Query("token")
//if len(token) != 0 {
// //存在token且未过期,用户处在登陆状态
// if logic.ExistsKey(global.TokenPrefix + token){
//
// }
//
//}
//返回响应
c.JSON(http.StatusOK, FeedResponse{
Response: Response{StatusCode: 0},
VideoList: videoList,
Expand Down
67 changes: 3 additions & 64 deletions controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@ import (
"github.com/go-playground/validator/v10"
)

// usersLoginInfo use map to store user info, and key is username+password for demo
// user data will be cleared every time the server starts
// test data: username=zhanglei, password=douyin
var usersLoginInfo = map[string]models.User{
"zhangleidouyin": {
Id: 1,
Name: "zhanglei",
FollowCount: 10,
FollowerCount: 5,
IsFollow: true,
},
}

type UserLoginResponse struct {
Response
UserId int64 `json:"user_id,omitempty"`
Expand All @@ -38,30 +25,8 @@ type UserResponse struct {
User *models.User `json:"user"`
}

// Register register a new user
func Register(c *gin.Context) {
// username := c.Query("username")
// password := c.Query("password")

// token := username + password

// if _, exist := usersLoginInfo[token]; exist {
// c.JSON(http.StatusOK, UserLoginResponse{
// Response: Response{StatusCode: 1, StatusMsg: "User already exist"},
// })
// } else {
// atomic.AddInt64(&userIdSequence, 1)
// newUser := User{
// Id: userIdSequence,
// Name: username,
// }
// usersLoginInfo[token] = newUser
// c.JSON(http.StatusOK, UserLoginResponse{
// Response: Response{StatusCode: 0},
// UserId: userIdSequence,
// Token: username + password,
// })
// }

//1、获取参数以及参数校验
p := new(models.ParamRegister)
if err := c.ShouldBind(p); err != nil {
Expand Down Expand Up @@ -100,25 +65,8 @@ func Register(c *gin.Context) {
})
}

// Login
func Login(c *gin.Context) {
// username := c.Query("username")
// password := c.Query("password")

// token := username + password

// if user, exist := usersLoginInfo[token]; exist {
// c.JSON(http.StatusOK, UserLoginResponse{
// Response: Response{StatusCode: 0},
// UserId: user.Id,
// Token: token,
// })
// } else {
// c.JSON(http.StatusOK, UserLoginResponse{
// Response: Response{StatusCode: 1, StatusMsg: "User doesn't exist"},
// })
// }

//1、获取请求参数以及参数校验
p := new(models.ParamLogin)
if err := c.ShouldBind(p); err != nil {
//请求参数有误
Expand Down Expand Up @@ -161,17 +109,8 @@ func Login(c *gin.Context) {
})
}

// UserInfo get user info
func UserInfo(c *gin.Context) {
// if user, exist := usersLoginInfo[token]; exist {
// c.JSON(http.StatusOK, UserResponse{
// Response: Response{StatusCode: 0},
// User: user,
// })
// } else {
// c.JSON(http.StatusOK, UserResponse{
// Response: Response{StatusCode: 1, StatusMsg: "User doesn't exist"},
// })
// }
//1、参数校验
p := new(models.ParamInfo)
if err := c.ShouldBindQuery(p); err != nil {
Expand Down
44 changes: 24 additions & 20 deletions dao/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package dao
import (
"douyin/global"
"douyin/models"
"encoding/json"
"time"

"go.uber.org/zap"
)
Expand Down Expand Up @@ -48,26 +46,32 @@ func Login(p *models.User) error {

// GetUserByID 根据作者的ID号得到作者信息
func GetUserByID(uid int64) (*models.User, error) {

var user *models.User
var err error
// 从缓存查找
userJson, _ := global.RedisEngine.Get(global.Ctx, global.UserPrefix+string(uid)).Result()
if len(userJson) == 0 {
//不存在,从数据库里面查找
err := global.MysqlEngine.Where("user_id = ?", uid).Find(user).Error
if err != nil {
global.Logger.Warn("GetUserByID Failed!", zap.Int64("uid:", uid), zap.Error(err))
err = ErrorInvalidID
}
userJson, _ := json.Marshal(*user)
global.RedisEngine.Set(global.Ctx, global.UserPrefix+string(uid), userJson, 24*time.Hour)
} else {
//存在
user = &models.User{}
json.Unmarshal([]byte(userJson), user)
user := new(models.User)
err := global.MysqlEngine.Where("user_id = ?", uid).Find(user).Error
if err != nil {
global.Logger.Warn("GetUserByID Failed!", zap.Int64("uid:", uid), zap.Error(err))
err = ErrorInvalidID
}
return user, err
// var user *models.User
// var err error
// // 从缓存查找
// userJson, _ := global.RedisEngine.Get(global.Ctx, global.UserPrefix+strconv.FormatInt(uid, 10)).Result()
// if len(userJson) == 0 {
// //不存在,从数据库里面查找
// err := global.MysqlEngine.Where("user_id = ?", uid).Find(user).Error
// if err != nil {
// global.Logger.Error("GetUserByID Failed!", zap.Int64("uid:", uid), zap.Error(err))
// err = ErrorInvalidID
// }
// userJson, _ := json.Marshal(*user)
// global.RedisEngine.Set(global.Ctx, global.UserPrefix+strconv.FormatInt(uid, 10), userJson, 24*time.Hour)
// } else {
// //存在
// user = &models.User{}
// json.Unmarshal([]byte(userJson), user)
// }
// return user, err
}

// CheckUserExistById 根据id判断用户是否存在
Expand Down
21 changes: 20 additions & 1 deletion logic/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,31 @@ package logic

import (
"douyin/dao"
"douyin/global"
"douyin/models"
)

// GetVideoList获取前30条视频,直接用于播放
func GetVideoList() ([]models.Video, error) {
func GetVideoList(token string) ([]models.Video, error) {
//获取播放视频列表
videoList, err := dao.GetVideoList()
if err != nil {
return nil, err
}
//存储用户喜欢的视频列表
mp := map[int64]bool{}
if len(token) != 0 {
if ExistsKey(global.TokenPrefix + token) {
user_id := GetUserByToken(token).Id
favoriteList, err := dao.GetFavoriteListByUserId(user_id)
if err != nil {
return nil, err
}
for i := 0; i < len(favoriteList); i++ {
mp[favoriteList[i].Id] = true
}
}
}
//根据不同的视频获取不同作者信息
list_len := len(videoList)
for i := 0; i < list_len; i++ {
Expand All @@ -21,6 +36,10 @@ func GetVideoList() ([]models.Video, error) {
continue
}
videoList[i].Author = *authorInfo
if mp[videoList[i].Id]{
videoList[i].IsFavorite = true
}
}
//如果用户已经登录,获取用户对应的视频信息
return videoList, nil
}
7 changes: 4 additions & 3 deletions logic/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"douyin/global"
"douyin/models"
"fmt"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"io/ioutil"
"net/http"
"path/filepath"

"github.com/gin-gonic/gin"
"gorm.io/gorm"
)

func DoPublish(param *models.ParamPublishAction, c *gin.Context) error {
Expand Down Expand Up @@ -73,6 +74,6 @@ func SaveVideoToOSS(videoName string, data []byte) (videoUrl string, coverUrl st
//回传视频和封面地址
videoUrl = fmt.Sprintf("%s/%s%s", global.OSSetting.TargetURL, global.OSSetting.TargetPath, videoName)
coverUrl = fmt.Sprintf("https://kauizhaotan.oss-cn-shanghai.aliyuncs.com/%s%s"+
"?x-oss-process=video/snapshot,t_5000,m_fast", global.OSSetting.TargetPath, videoName)
"?x-oss-process=video/snapshot,t_500,m_fast", global.OSSetting.TargetPath, videoName)
return videoUrl, coverUrl, nil
}
2 changes: 1 addition & 1 deletion models/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type ParamRelationAction struct {

// ParamAuth 用户Token获取
type ParamAuth struct {
Token string `form:"token" json:"token" binding:"required"`
Token string `form:"token" json:"token"`
}

// ParamPublishAction 用户上传视频请求
Expand Down
1 change: 0 additions & 1 deletion readme.txt

This file was deleted.

0 comments on commit 70fac34

Please sign in to comment.