添加请求返回状态码常量并将返回状态码修改为常量;添加数据库等配置常量文件并修改

This commit is contained in:
junleea 2024-06-25 09:38:21 +08:00
parent 554c70bec8
commit 823719051d
10 changed files with 147 additions and 81 deletions

View File

@ -4,12 +4,13 @@ import (
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"videoplayer/proto"
)
var DB *gorm.DB
func Init() {
dsn := "video_t2:2t2SKHmWEYj2xFKF@tcp(127.0.0.1:3306)/video_t2?charset=utf8mb4&parseTime=True&loc=Local"
dsn := proto.MYSQL_DSN
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {

2
go.mod
View File

@ -6,6 +6,7 @@ require (
github.com/gin-gonic/gin v1.10.0
github.com/go-redis/redis/v8 v8.11.5
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/google/uuid v1.6.0
gorm.io/driver/mysql v1.5.6
gorm.io/gorm v1.25.7
)
@ -24,7 +25,6 @@ require (
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect

View File

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"videoplayer/proto"
"videoplayer/service"
)
@ -60,12 +61,12 @@ func DeleteDevice(c *gin.Context) {
var req DeviceDelReq
if err := c.ShouldBindJSON(&req); err == nil {
if service.DeleteDevice(req.ID, int(id.(float64))) {
c.JSON(200, gin.H{"code": 0, "message": "success"})
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success"})
} else {
c.JSON(200, gin.H{"code": 17, "message": "failed"})
c.JSON(200, gin.H{"code": proto.OperationFailed, "message": "failed"})
}
} else {
c.JSON(200, gin.H{"code": 9, "message": "failed"})
c.JSON(200, gin.H{"code": proto.ParameterError, "message": "failed"})
}
}
@ -81,13 +82,13 @@ func UpdateDevice(c *gin.Context) {
})
} else {
c.JSON(200, gin.H{
"code": 16,
"code": proto.DeviceUpdateFailed,
"message": "failed",
})
}
} else {
c.JSON(200, gin.H{
"code": 9,
"code": proto.ParameterError,
"message": "failed",
})
}
@ -98,13 +99,13 @@ func SetDeviceStatus(c *gin.Context) {
if err := c.ShouldBindJSON(&req); err == nil {
if req.IP != "" {
if service.SetDeviceStatus(req.Status, req.ID, int(id.(float64))) {
c.JSON(200, gin.H{"code": 0, "message": "success"})
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success"})
} else {
c.JSON(200, gin.H{"code": 1, "message": "failed"})
}
}
} else {
c.JSON(200, gin.H{"code": 9, "message": "failed", "data": err.Error()})
c.JSON(200, gin.H{"code": proto.ParameterError, "message": "failed", "data": err.Error()})
}
}
@ -117,20 +118,20 @@ func AddDevice(c *gin.Context) {
device_id := service.AddDevice(req.DeviceName, req.DeviceIP, req.DeviceStatus, req.DeviceInfo, req.DeviceType, req.DeviceLocation, user_id)
if device_id != 0 {
c.JSON(200, gin.H{
"code": 0,
"code": proto.SuccessCode,
"message": "success",
"data": device_id,
})
} else {
c.JSON(200, gin.H{
"code": 15,
"code": proto.DeviceAddFailed,
"message": "failed",
"data": "device add failed",
})
}
} else {
c.JSON(200, gin.H{
"code": 9,
"code": proto.ParameterError,
"message": "failed",
"data": err.Error(),
})
@ -141,7 +142,7 @@ func GetDeviceList(c *gin.Context) {
id, _ := c.Get("id")
devices := service.GetDeviceList(int(id.(float64)))
c.JSON(200, gin.H{
"code": 0,
"code": proto.SuccessCode,
"message": "success",
"data": devices,
})
@ -151,7 +152,7 @@ func RestartDevice(c *gin.Context) {
user_id, _ := c.Get("id")
var req DeviceRestartReq
if err := c.ShouldBind(&req); err != nil {
c.JSON(200, gin.H{"code": 9, "message": "failed", "data": err.Error()})
c.JSON(200, gin.H{"code": proto.ParameterError, "message": "failed", "data": err.Error()})
return
}
device_id := req.ID
@ -160,13 +161,13 @@ func RestartDevice(c *gin.Context) {
if device.ID != 0 {
if device.DeviceIP != "" {
if Restart(device.DeviceIP) {
c.JSON(200, gin.H{"code": 0, "message": "success"})
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success"})
} else {
c.JSON(200, gin.H{"code": 13, "message": "failed"})
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": "failed"})
}
}
} else {
c.JSON(200, gin.H{"code": 14, "message": "failed", "data": string(device_id) + ": device not found"})
c.JSON(200, gin.H{"code": proto.DataNotFound, "message": "failed", "data": string(device_id) + ": device not found"})
}
} else if req.Option == "all" {
devices := service.GetDeviceList(int(user_id.(float64)))
@ -174,15 +175,15 @@ func RestartDevice(c *gin.Context) {
for _, device := range devices {
if device.DeviceIP != "" {
if !Restart(device.DeviceIP) {
c.JSON(200, gin.H{"code": 13, "message": "failed"})
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": "failed"})
return
}
}
}
} else {
c.JSON(200, gin.H{"code": 14, "message": "failed", "data": "device not found"})
c.JSON(200, gin.H{"code": proto.DataNotFound, "message": "failed", "data": "device not found"})
}
c.JSON(200, gin.H{"code": 0, "message": "success"})
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success"})
}
}

View File

@ -9,11 +9,12 @@ import (
"github.com/golang-jwt/jwt"
"github.com/google/uuid"
"time"
"videoplayer/proto"
"videoplayer/service"
"videoplayer/worker"
)
var signingKey = []byte("aadafcvretmoi9")
var signingKey = []byte(proto.TOKEN_SECRET)
func SetUpUserGroup(router *gin.Engine) {
userGroup := router.Group("/user")
@ -42,13 +43,13 @@ type QRReq struct {
func GetScanUUID(c *gin.Context) {
var ReqData QRReq
if err := c.ShouldBind(&ReqData); err != nil {
c.JSON(200, gin.H{"code": 9, "message": err, "data": "2"})
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": err, "data": "2"})
return
}
data := map[string]interface{}{"status": "0", "address": ReqData.Address, "ip": c.ClientIP()}
jsonData, err := json.Marshal(data)
if err != nil {
c.JSON(200, gin.H{"code": 9, "message": err, "data": "2"})
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": err, "data": "2"})
return
}
id := uuid.New()
@ -56,12 +57,12 @@ func GetScanUUID(c *gin.Context) {
if res {
var retrievedData map[string]interface{}
if err2 := json.Unmarshal([]byte(worker.GetRedis(id.String())), &retrievedData); err2 != nil {
c.JSON(200, gin.H{"code": 9, "message": err2, "data": "2"})
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": err2, "data": "2"})
return
}
c.JSON(200, gin.H{"code": 0, "message": retrievedData, "data": id.String()})
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": retrievedData, "data": id.String()})
} else {
c.JSON(200, gin.H{"code": 8, "message": "qr code invalid", "data": "1"})
c.JSON(200, gin.H{"code": proto.RedisSetError, "message": "qr code invalid", "data": "1"})
}
}
@ -69,28 +70,28 @@ func SetQRStatus(c *gin.Context) {
var qrsetReq QRReq
if err := c.ShouldBind(&qrsetReq); err == nil && qrsetReq.UUID != "" {
if worker.IsContainKey(qrsetReq.UUID) == false {
c.JSON(200, gin.H{"code": 18, "message": "uuid not found in server", "data": "0"})
c.JSON(200, gin.H{"code": proto.UUIDNotFound, "message": "uuid not found in server", "data": "0"})
return
}
var retrievedData map[string]interface{}
if err2 := json.Unmarshal([]byte(worker.GetRedis(qrsetReq.UUID)), &retrievedData); err2 != nil {
c.JSON(200, gin.H{"code": 9, "message": err2, "data": "2"})
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": err2, "data": "2"})
return
}
retrievedData["status"] = "1"
jsonData, err2 := json.Marshal(retrievedData)
if err2 != nil {
c.JSON(200, gin.H{"code": 9, "message": err2, "data": "2"})
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": err2, "data": "2"})
return
}
res := worker.SetRedisWithExpire(qrsetReq.UUID, string(jsonData), time.Minute*30)
if res {
c.JSON(200, gin.H{"code": 0, "message": "success", "data": retrievedData})
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": retrievedData})
} else {
c.JSON(200, gin.H{"code": 8, "message": "qr code invalid", "data": "1"})
c.JSON(200, gin.H{"code": proto.RedisSetError, "message": "qr code invalid", "data": "1"})
}
} else {
c.JSON(200, gin.H{"code": 9, "message": err, "data": "2"})
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": err, "data": "2"})
}
}
@ -104,33 +105,33 @@ func ConfirmQRLogin(c *gin.Context) {
key := "user_" + user_name.(string)
token := worker.GetRedis(key)
if token == "" {
c.JSON(200, gin.H{"code": 20, "message": "Token不存在", "data": "20"})
c.JSON(200, gin.H{"code": proto.RedisGetError, "message": "Token不存在", "data": "20"})
}
if worker.IsContainKey(qrsetReq.UUID) == false {
c.JSON(200, gin.H{"code": 18, "message": "uuid not found in server", "data": "0"})
c.JSON(200, gin.H{"code": proto.UUIDNotFound, "message": "uuid not found in server", "data": "0"})
return
}
var retrievedData map[string]interface{}
if err2 := json.Unmarshal([]byte(worker.GetRedis(qrsetReq.UUID)), &retrievedData); err2 != nil {
c.JSON(200, gin.H{"code": 9, "message": err2, "data": "2"})
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": err2, "data": "2"})
return
}
retrievedData["status"] = token
jsonData, err2 := json.Marshal(retrievedData)
if err2 != nil {
c.JSON(200, gin.H{"code": 9, "message": err2, "data": "2"})
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": err2, "data": "2"})
return
}
if worker.SetRedisWithExpire(qrsetReq.UUID, string(jsonData), time.Minute*10) {
c.JSON(200, gin.H{"code": 0, "message": "success", "data": "0"})
} else {
c.JSON(200, gin.H{"code": 8, "message": "设置Token失败", "data": "8"})
c.JSON(200, gin.H{"code": proto.RedisSetError, "message": "设置Token失败", "data": "8"})
}
} else {
c.JSON(200, gin.H{"code": 20, "message": "failed", "data": "20"})
c.JSON(200, gin.H{"code": proto.RedisGetError, "message": "failed", "data": "20"})
}
} else {
c.JSON(200, gin.H{"code": 9, "message": err, "data": "3"})
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": err, "data": "3"})
}
}
@ -139,24 +140,24 @@ func GetQRStatus(c *gin.Context) {
if err := c.ShouldBind(&qrReq); err == nil {
var retrievedData map[string]interface{}
if err2 := json.Unmarshal([]byte(worker.GetRedis(qrReq.UUID)), &retrievedData); err2 != nil {
c.JSON(200, gin.H{"code": 9, "message": err2, "data": "2"})
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": err2, "data": "2"})
return
}
str := retrievedData["status"].(string)
switch str {
case "":
c.JSON(200, gin.H{"code": 18, "message": "uuid not found", "data": "0"}) //空值
c.JSON(200, gin.H{"code": proto.UUIDNotFound, "message": "uuid not found", "data": "0"}) //空值
case "0":
c.JSON(200, gin.H{"code": 0, "message": "success", "data": "0"}) //空值
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "0"}) //空值
case "1":
c.JSON(200, gin.H{"code": 0, "message": "success", "data": "1"}) //已扫描待确认
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "1"}) //已扫描待确认
default:
// 解析 JWT 令牌
token, err := jwt.Parse(str, func(token *jwt.Token) (interface{}, error) {
return signingKey, nil
})
if err != nil {
c.JSON(200, gin.H{"error": err.Error(), "code": 19, "message": "error"})
c.JSON(200, gin.H{"error": err.Error(), "code": proto.TokenParseError, "message": "error"})
return
}
// 返回令牌
@ -165,10 +166,10 @@ func GetQRStatus(c *gin.Context) {
data["username"] = token.Claims.(jwt.MapClaims)["username"]
data["email"] = token.Claims.(jwt.MapClaims)["email"]
data["token"] = str
c.JSON(200, gin.H{"code": 0, "message": "success", "data": data}) //确认返回token数据
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": data}) //确认返回token数据
}
} else {
c.JSON(200, gin.H{"error": err.Error(), "code": 9, "message": "error"})
c.JSON(200, gin.H{"error": err.Error(), "code": proto.DeviceRestartFailed, "message": "error"})
}
}
@ -194,7 +195,7 @@ func loginHandler(c *gin.Context) {
})
tokenString, err = token.SignedString(signingKey)
if err != nil {
c.JSON(200, gin.H{"error": err.Error(), "code": 5, "message": "error"})
c.JSON(200, gin.H{"error": err.Error(), "code": proto.TokenGenerationError, "message": "error"})
return
}
@ -214,13 +215,13 @@ func loginHandler(c *gin.Context) {
data["username"] = user.Name
data["email"] = user.Email
data["token"] = tokenString
c.JSON(200, gin.H{"code": 0, "message": "success", "data": data})
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": data})
} else {
//用户名或密码错误
c.JSON(200, gin.H{"error": "用户名或密码错误", "code": 6, "message": "error"})
c.JSON(200, gin.H{"error": "用户名或密码错误", "code": proto.UsernameOrPasswordError, "message": "error"})
}
} else {
c.JSON(200, gin.H{"error": err.Error(), "code": 9, "message": "error"})
c.JSON(200, gin.H{"error": err.Error(), "code": proto.DeviceRestartFailed, "message": "error"})
}
}
@ -234,7 +235,7 @@ func registerHandler(c *gin.Context) {
req_data.Password = hex.EncodeToString(hasher.Sum(nil)) // 生成密码的 MD5 散列值
}
if service.ContainsUser(req_data.User, req_data.Email) == true {
c.JSON(200, gin.H{"error": "user already exists", "code": 7, "message": "error"})
c.JSON(200, gin.H{"error": "user already exists", "code": proto.UsernameExists, "message": "error"})
return
}
id := service.CreateUser(req_data.User, req_data.Password, req_data.Email)
@ -246,17 +247,17 @@ func registerHandler(c *gin.Context) {
})
tokenString, err = token.SignedString(signingKey)
if err != nil {
c.JSON(200, gin.H{"error": err.Error(), "code": 5, "message": "error"})
c.JSON(200, gin.H{"error": err.Error(), "code": proto.TokenGenerationError, "message": "error"})
}
} else {
c.JSON(200, gin.H{"error": err.Error(), "code": 9, "message": "error"})
c.JSON(200, gin.H{"error": err.Error(), "code": proto.DeviceRestartFailed, "message": "error"})
}
fmt.Println(req_data)
res := worker.SetRedisWithExpire(tokenString, tokenString, time.Hour*10) // 设置过期时间为10分钟
if !res {
c.JSON(200, gin.H{"error": "set token error", "code": 8, "message": "error"})
c.JSON(200, gin.H{"error": "set token error", "code": proto.RedisSetError, "message": "error"})
return
}
// 返回令牌
c.JSON(200, gin.H{"token": tokenString, "username": req_data.User, "code": 0, "message": "success"})
c.JSON(200, gin.H{"token": tokenString, "username": req_data.User, "code": proto.SuccessCode, "message": "success"})
}

View File

@ -8,6 +8,7 @@ import (
"os"
"strconv"
"time"
"videoplayer/proto"
"videoplayer/service"
"videoplayer/worker"
)
@ -95,12 +96,12 @@ func UpdateVideo(c *gin.Context) {
if err := c.ShouldBind(&video_req); err == nil {
res := service.UpdateVideo(video_req.VideoPath, video_req.VideoName, video_req.CameraID, video_req.ID, int(user_id.(float64)), video_req.Human, video_req.IsDelete, video_req.CreateTime, video_req.EndTime, video_req.FileSize)
if !res {
c.JSON(http.StatusOK, gin.H{"error": "update video failed", "code": 17, "message": "failed"})
c.JSON(http.StatusOK, gin.H{"error": "update video failed", "code": proto.OperationFailed, "message": "failed"})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success", "data": "update success"})
} else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": 9, "message": "failed"})
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
@ -142,12 +143,12 @@ func CreateVideo(c *gin.Context) {
fmt.Println(video_req)
id := service.CreateVideo(video_req.VideoPath, video_req.VideoName, video_req.CameraID, int(user_id.(float64)), video_req.Human, video_req.IsDelete, video_req.CreateTime, video_req.EndTime, video_req.DeleteTime, video_req.FileSize)
if id == 0 {
c.JSON(http.StatusOK, gin.H{"error": "create video failed", "code": 14, "message": "failed"})
c.JSON(http.StatusOK, gin.H{"error": "create video failed", "code": proto.DataNotFound, "message": "failed"})
return
}
c.JSON(http.StatusOK, gin.H{"id": id, "code": 0, "message": "success"})
} else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": 9, "message": "failed"})
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
@ -159,7 +160,7 @@ func GetVideoList(c *gin.Context) {
videos := service.GetVideoList(int(id.(float64)), gvl_req.StartTime, gvl_req.EndTime, gvl_req.Hour)
c.JSON(http.StatusOK, gin.H{"data": videos, "code": 0, "message": "success"})
} else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": 9, "message": "failed"})
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
@ -170,7 +171,7 @@ func DelayVideo(c *gin.Context) {
cnt := 0
if err := c.ShouldBind(&delay_req); err == nil {
if delay_req.Day > 30 || delay_req.Day < 1 {
c.JSON(http.StatusOK, gin.H{"code": 11, "data": "延迟天数过大或过小", "message": "failed"})
c.JSON(http.StatusOK, gin.H{"code": proto.VideoDelayOperationFailed, "data": "延迟天数过大或过小", "message": "failed"})
return
}
if delay_req.Option == "all" {
@ -180,7 +181,7 @@ func DelayVideo(c *gin.Context) {
cnt = service.DelayVideo(delay_req.ID, int(id.(float64)), delay_req.Day)
}
if cnt == 0 {
c.JSON(http.StatusOK, gin.H{"code": 17, "data": "延迟失败影响数据", "message": "failed,cnt:" + strconv.Itoa(cnt)})
c.JSON(http.StatusOK, gin.H{"code": proto.OperationFailed, "data": "延迟失败影响数据", "message": "failed,cnt:" + strconv.Itoa(cnt)})
} else {
data := map[string]interface{}{"auth_id": int(id.(float64)), "delay_time": time.Now().Format("2006-01-02 15:04:05"), "delay_day": delay_req.Day, "option": delay_req.Option, "id": delay_req.ID}
str, _ := json.Marshal(data)
@ -188,7 +189,7 @@ func DelayVideo(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"code": 0, "data": "延迟成功,影响记录:" + strconv.Itoa(cnt), "message": "success cnt:" + strconv.Itoa(cnt)})
}
} else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": 9, "message": "failed"})
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
@ -204,10 +205,10 @@ func DeleteVideo(c *gin.Context) {
worker.PushRedisList(user.(string)+"-"+strconv.Itoa(video_req.ID), string(str))
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success"})
} else {
c.JSON(http.StatusOK, gin.H{"code": 9, "message": "failed"})
c.JSON(http.StatusOK, gin.H{"code": proto.ParameterError, "message": "failed"})
}
} else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": 9, "message": "failed"})
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
@ -224,13 +225,13 @@ func QuashOption(c *gin.Context) {
code, msg := service.QuashVideo(int(id.(float64)), retrievedData)
c.JSON(http.StatusOK, gin.H{"code": code, "message": msg, "data": msg})
} else {
c.JSON(http.StatusOK, gin.H{"code": 9, "message": err2, "data": "2"})
c.JSON(http.StatusOK, gin.H{"code": proto.ParameterError, "message": err2, "data": "2"})
return
}
} else {
c.JSON(http.StatusOK, gin.H{"code": 9, "message": "redis data not found", "data": "error"})
c.JSON(http.StatusOK, gin.H{"code": proto.ParameterError, "message": "redis data not found", "data": "error"})
}
} else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": 9, "message": "failed"})
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}

View File

@ -7,10 +7,11 @@ import (
"strings"
"videoplayer/dao"
"videoplayer/handler"
"videoplayer/proto"
"videoplayer/worker"
)
var signingKey = []byte("aadafcvretmoi9")
var signingKey = []byte(proto.TOKEN_SECRET)
func main() {
@ -68,7 +69,7 @@ func JWTAuthMiddleware() gin.HandlerFunc {
c.JSON(200, gin.H{
"message": "Unauthorized",
"error": "token is empty",
"code": "3",
"code": proto.TokenIsNull,
})
return
}
@ -79,7 +80,7 @@ func JWTAuthMiddleware() gin.HandlerFunc {
c.JSON(200, gin.H{
"message": "NOT_LOGIN",
"error": "server token is empty",
"code": "3",
"code": proto.TokenIsNull,
})
return
}
@ -95,7 +96,7 @@ func JWTAuthMiddleware() gin.HandlerFunc {
c.JSON(200, gin.H{
"message": "NOT_LOGIN",
"error": "Invalid token",
"code": "4",
"code": proto.TokenExpired,
})
return
}

15
proto/conf.go Normal file
View File

@ -0,0 +1,15 @@
package proto
const (
MYSQL_USER = "root"
MYSQL_PASSWORD
MYSQL_HOST
MYSQL_PORT
MYSQL_DSN = "video_t2:2t2SKHmWEYj2xFKF@tcp(127.0.0.1:3306)/video_t2?charset=utf8mb4&parseTime=True&loc=Local"
REDIS_ADDR = "127.0.0.1:6379"
REDIS_PASSWORD = "lj502138"
REIDS_DB = 2
TOKEN_SECRET = "mfjurnc_32ndj9dfhj"
)

44
proto/status.go Normal file
View File

@ -0,0 +1,44 @@
package proto
const (
SuccessCode = 0 // 成功
// 通用错误码
ErrorCode = 1 // 未知错误或服务器内部错误
ParameterError = 9 // 请求参数解析错误
OperationFailed = 17 // 数据库数据操作失败
DataNotFound = 14 // 查询数据失败
InternalServerError = 10 // 服务器内部错误
// Token相关错误码
TokenInvalid = 2 // Token失效未登录
TokenIsNull = 3 // Token为空
TokenExpired = 4 // Token已过期
TokenGenerationError = 5 // Token生成错误
TokenParseError = 19 // Token解析错误
// 用户名密码相关错误码
UsernameOrPasswordError = 6 // 用户名或密码错误
UsernameExists = 7 // 用户名已存在
// Redis相关错误码
RedisSetError = 8 // 设置redis错误
RedisGetError = 20 // 获取redis错误
// 视频操作相关错误码
VideoDelayOperationFailed = 11 // 视频延迟操作失败
VideoDeleteFailed = 12 // 视频删除失败
// 设备操作相关错误码
DeviceRestartFailed = 13 // 设备重启失败
DeviceAddFailed = 15 // 设备添加失败
DeviceUpdateFailed = 16 // 设备修改失败
// 撤销操作相关错误码
RevokeOperation = 30 // 撤销
RevokeDelayOperationFailed = 31 // 撤销延迟操作失败
RevokeOperationFailed = 32 // 撤销操作失败
// UUID相关错误码
UUIDNotFound = 18 // uuid不存在
)

View File

@ -3,6 +3,7 @@ package service
import (
"strings"
"videoplayer/dao"
"videoplayer/proto"
)
func GetVideo(id, auth_id int) dao.Video {
@ -73,27 +74,27 @@ func QuashVideo(user int, data map[string]interface{}) (int, string) {
case "delay":
if data["option"] == "all" {
if dao.QuashAllDelay(user, data["day"].(int)) == 0 {
res = 31
res = proto.RevokeDelayOperationFailed
msg = "quash all video delay error"
} else {
res = 0
res = proto.SuccessCode
msg = "success"
}
} else if data["option"] == "one" {
if dao.QuashOneDelay(user, data["id"].(int), data["day"].(int)) == 0 {
res = 31
res = proto.RevokeDelayOperationFailed
msg = "quash one video error"
} else {
res = 0
res = proto.SuccessCode
msg = "success"
}
} else {
res = 32
res = proto.RevokeOperationFailed
msg = "option error,no such delay option"
}
default:
res = 32
res = proto.RevokeOperationFailed
msg = "method error,no such method"
}
return res, msg

View File

@ -4,6 +4,7 @@ import (
"fmt"
"strconv"
"time"
"videoplayer/proto"
)
import (
"context"
@ -16,9 +17,9 @@ func InitRedis() {
ctx := context.Background()
// 连接redis
redisClient = redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379", // Redis 服务器地址
Password: "lj502138", // 如果 Redis 设置了密码
DB: 2, // 使用的数据库编号
Addr: proto.REDIS_ADDR, // Redis 服务器地址
Password: proto.REDIS_PASSWORD, // 如果 Redis 设置了密码
DB: proto.REIDS_DB, // 使用的数据库编号
})
// 验证 Redis 客户端是否可以正常工作