Browse Source

fix:日志文件权限问题解决

zzf 1 year ago
parent
commit
45661cfdf7

+ 5 - 5
api/chat/answer.go

@@ -1,7 +1,7 @@
 package chat
 
 import (
-	errmsg "confrontation-training/err"
+	errors "confrontation-training/err"
 	"confrontation-training/models/chat"
 	"confrontation-training/response"
 	serviceChat "confrontation-training/service/chat"
@@ -37,9 +37,9 @@ func (a *AnswerService) CreateAnswer(c *gin.Context) {
 	}
 	answer, count := a.CreateNewAnswer(param)
 	if count == 0 {
-		response.Failed(errmsg.AnswerCreateFailed, c)
+		response.Failed(errors.AnswerCreateFailed, c)
 	} else {
-		response.Success(errmsg.CreateSuccess, answer, c)
+		response.Success(errors.CreateSuccess, answer, c)
 	}
 	return
 }
@@ -65,9 +65,9 @@ func (a *AnswerService) GetAnswer(c *gin.Context) {
 		panic(err)
 	}
 	if count <= 0 {
-		response.Failed(errmsg.FindAnswerFailed, c)
+		response.Failed(errors.FindAnswerFailed, c)
 	} else {
-		response.Success(errmsg.FindSuccess, questions, c)
+		response.Success(errors.FindSuccess, questions, c)
 	}
 	return
 }

+ 7 - 7
api/chat/question.go

@@ -1,7 +1,7 @@
 package chat
 
 import (
-	errmsg "confrontation-training/err"
+	errors "confrontation-training/err"
 	modelsChat "confrontation-training/models/chat"
 	"confrontation-training/response"
 	serviceChat "confrontation-training/service/chat"
@@ -32,7 +32,7 @@ func (q *QuestionService) GetQuestion(c *gin.Context) {
 	var id uint8
 	if err := c.ShouldBind(&id); err != nil {
 		fmt.Printf("参数绑定异常:%s", err.Error())
-		response.Failed(errmsg.ParamInvalid, c)
+		response.Failed(errors.ParamInvalid, c)
 		return
 	}
 	if id == 0 {
@@ -40,9 +40,9 @@ func (q *QuestionService) GetQuestion(c *gin.Context) {
 	}
 	question, count := q.FindQuestionById(id)
 	if count == 0 {
-		response.Failed(errmsg.QuestionNotFound, c)
+		response.Failed(errors.QuestionNotFound, c)
 	} else {
-		response.Success(errmsg.FindSuccess, question, c)
+		response.Success(errors.FindSuccess, question, c)
 	}
 	return
 }
@@ -62,15 +62,15 @@ func (q *QuestionService) CreateQuestion(c *gin.Context) {
 	var param modelsChat.CreateQuestion
 	if err := c.ShouldBindJSON(&param); err != nil {
 		fmt.Printf("参数绑定异常:%s", err.Error())
-		response.Failed(errmsg.ParamInvalid, c)
+		response.Failed(errors.ParamInvalid, c)
 		return
 	}
 
 	question, count := q.CreateNewQuestion(param)
 	if count == 0 {
-		response.Failed(errmsg.QuestionCreateFailed, c)
+		response.Failed(errors.QuestionCreateFailed, c)
 	} else {
-		response.Success(errmsg.FindSuccess, question, c)
+		response.Success(errors.FindSuccess, question, c)
 	}
 	return
 }

+ 41 - 5
api/gateway/device.go

@@ -1,7 +1,7 @@
 package gateway
 
 import (
-	errmsg "confrontation-training/err"
+	errors "confrontation-training/err"
 	deviceModel "confrontation-training/models/gateway"
 	"confrontation-training/response"
 	deviceService "confrontation-training/service/device"
@@ -33,18 +33,54 @@ func (d *DeviceService) DeviceAdd(c *gin.Context) {
 	err := c.ShouldBindJSON(&param)
 	if err != nil {
 		fmt.Printf("参数格式化异常:%s", err.Error())
-		response.Failed(errmsg.ParamInvalid, c)
+		response.Failed(errors.ParamInvalid, c)
 		return
 	}
 	if _, count := d.FindDeviceByMac(param.Mac); count > 0 {
-		response.Failed(errmsg.DeviceAddFailed, c)
+		response.Failed(errors.DeviceAddFailed, c)
 		return
 	}
 	if result := d.CreateDevice(param); result.Error == nil {
-		response.Success(errmsg.DeviceAddSuccess, result.RowsAffected, c)
+		response.Success(errors.DeviceAddSuccess, result.RowsAffected, c)
 		return
 	} else {
-		response.Failed(errmsg.UserRegisterFailed+"数据库错:"+result.Error.Error(), c)
+		response.Failed(errors.UserRegisterFailed+"数据库错:"+result.Error.Error(), c)
 		return
 	}
 }
+
+// DeviceRemove
+// PingExample confrontation-training
+// @Summary 移除设备
+// @Schemes
+// @Description 移除设备
+// @Tags 设备管理
+// @Accept json
+// @Produce json
+// @Success 200 {string} string "ok"
+// @Router /v1/device/:mac/remove [post]
+func (d *DeviceService) DeviceRemove(c *gin.Context) {
+	mac := c.Param("mac")
+	count := d.RemoveDevice(mac)
+	if count == 0 {
+		response.Failed(errors.DeviceRemoveFailed, c)
+	} else {
+		response.Success(errors.DeviceRemoveSuccess, count, c)
+	}
+	return
+}
+
+// DeviceList
+// PingExample confrontation-training
+// @Summary 设备列表
+// @Schemes
+// @Description 设备列表
+// @Tags 设备管理
+// @Accept json
+// @Produce json
+// @Success 200 {string} string "ok"
+// @Router /v1/device/list/:type [get]
+func (d *DeviceService) DeviceList(c *gin.Context) {
+	deviceInfos, _ := d.FindDeviceByType(c.Param("type"))
+	response.Success(errors.FindSuccess, deviceInfos, c)
+}

+ 2 - 2
api/gateway/sse.go

@@ -2,7 +2,7 @@ package gateway
 
 import (
 	"confrontation-training/constant"
-	errmsg "confrontation-training/err"
+	errors "confrontation-training/err"
 	"confrontation-training/global"
 	"confrontation-training/models/gateway"
 	"encoding/json"
@@ -123,7 +123,7 @@ func SseOpenNotify() {
 			messageMap["data"] = string(msg.Data)
 			err := ws.WriteMessage(websocket.TextMessage, msg.Data)
 			if err != nil {
-				fmt.Println(errmsg.SendMessageError + err.Error())
+				fmt.Println(errors.SendMessageError + err.Error())
 				return
 			}
 		}

+ 24 - 24
api/gateway/user.go

@@ -3,7 +3,7 @@ package gateway
 import (
 	"confrontation-training/common"
 	"confrontation-training/constant"
-	errmsg "confrontation-training/err"
+	errors "confrontation-training/err"
 	"confrontation-training/models"
 	"confrontation-training/response"
 	"confrontation-training/service"
@@ -35,18 +35,18 @@ func (u *UserService) UseRegister(c *gin.Context) {
 	err := c.ShouldBindJSON(&param)
 	if err != nil {
 		fmt.Printf("参数格式化异常:%s", err.Error())
-		response.Failed(errmsg.ParamInvalid, c)
+		response.Failed(errors.ParamInvalid, c)
 		return
 	}
 	if _, count := u.FindUserByPhone(param.Phone); count > 0 {
-		response.Failed(errmsg.UserAlreadyExists, c)
+		response.Failed(errors.UserAlreadyExists, c)
 		return
 	}
 	if result := u.CreateUser(param); result.Error == nil {
-		response.Success(errmsg.UserRegisterSuccess, result.RowsAffected, c)
+		response.Success(errors.UserRegisterSuccess, result.RowsAffected, c)
 		return
 	} else {
-		response.Failed(errmsg.UserRegisterFailed+"数据库错:"+result.Error.Error(), c)
+		response.Failed(errors.UserRegisterFailed+"数据库错:"+result.Error.Error(), c)
 		return
 	}
 
@@ -69,7 +69,7 @@ func (u *UserService) UserLogin(c *gin.Context) {
 
 	if err != nil {
 		fmt.Printf("参数格式化异常:%s", err.Error())
-		response.Failed(errmsg.ParamInvalid, c)
+		response.Failed(errors.ParamInvalid, c)
 		return
 	}
 	user := u.Login(param)
@@ -79,9 +79,9 @@ func (u *UserService) UserLogin(c *gin.Context) {
 		resultMap := map[string]string{
 			"token": toke,
 		}
-		response.Success(errmsg.UserLoginSuccess, resultMap, c)
+		response.Success(errors.UserLoginSuccess, resultMap, c)
 	} else {
-		response.Failed(errmsg.UserLoginFailed, c)
+		response.Failed(errors.UserLoginFailed, c)
 	}
 }
 
@@ -101,30 +101,30 @@ func (u *UserService) ResetPassword(c *gin.Context) {
 	err := c.ShouldBindJSON(&param)
 	if err != nil {
 		fmt.Printf("参数格式化异常:%s", err.Error())
-		response.Failed(errmsg.ParamInvalid, c)
+		response.Failed(errors.ParamInvalid, c)
 		return
 	}
 	if admin := u.FindUserByPhoneAndRole(param.Phone, constant.RoleAdmin); admin == nil {
-		fmt.Printf("管理员"+errmsg.UserNotExists+"%s", param.Phone)
-		response.Failed("管理员"+errmsg.UserNotExists+param.Phone, c)
+		fmt.Printf("管理员"+errors.UserNotExists+"%s", param.Phone)
+		response.Failed("管理员"+errors.UserNotExists+param.Phone, c)
 		return
 	}
 	if normalUser := u.FindUserByPhoneAndRole(param.PhoneInit, constant.RoleNormal); normalUser == nil {
-		fmt.Printf(errmsg.UserNotExists+"%s", param.PhoneInit)
-		response.Failed(errmsg.UserNotExists+param.PhoneInit, c)
+		fmt.Printf(errors.UserNotExists+"%s", param.PhoneInit)
+		response.Failed(errors.UserNotExists+param.PhoneInit, c)
 		return
 	}
 	result := u.RestPassword(param.PhoneInit)
 	if result == nil {
-		response.Failed(errmsg.UserPasswordResetError, c)
+		response.Failed(errors.UserPasswordResetError, c)
 		return
 	}
 	if result.Error != nil {
-		response.Failed(errmsg.UserPasswordResetError+result.Error.Error(), c)
+		response.Failed(errors.UserPasswordResetError+result.Error.Error(), c)
 		return
 	}
 	if result.RowsAffected >= 0 {
-		response.Success(errmsg.UserPasswordResetSuccess, result.RowsAffected, c)
+		response.Success(errors.UserPasswordResetSuccess, result.RowsAffected, c)
 		return
 	}
 }
@@ -144,12 +144,12 @@ func (u *UserService) UserList(c *gin.Context) {
 	var param models.UserListParam
 	err := c.ShouldBindJSON(&param)
 	if err != nil {
-		fmt.Printf(errmsg.ParamInvalid+"%s", err.Error())
-		response.Failed(errmsg.ParamInvalid, c)
+		fmt.Printf(errors.ParamInvalid+"%s", err.Error())
+		response.Failed(errors.ParamInvalid, c)
 		return
 	}
 	userList, rows := u.GetUserList(param)
-	response.SuccessPage(errmsg.FindSuccess, userList, rows, c)
+	response.SuccessPage(errors.FindSuccess, userList, rows, c)
 }
 
 // ModePass
@@ -167,22 +167,22 @@ func (u *UserService) ModePass(c *gin.Context) {
 	var param models.ChangePassword
 	err := c.ShouldBindJSON(&param)
 	if err != nil {
-		response.Failed(errmsg.ParamInvalid, c)
+		response.Failed(errors.ParamInvalid, c)
 		return
 	}
 	user, count := u.FindUserByPhone(param.Phone)
 	if count <= 0 {
-		response.Failed(errmsg.UserNotExists, c)
+		response.Failed(errors.UserNotExists, c)
 		return
 	}
 	if user.Password != param.Password {
-		response.Failed(errmsg.UserOldPasswordError, c)
+		response.Failed(errors.UserOldPasswordError, c)
 		return
 	}
 	count = u.ChangePassword(user.ID, param.NewPassword)
 	if count <= 0 {
-		response.Failed(errmsg.UserPasswordChangeError, c)
+		response.Failed(errors.UserPasswordChangeError, c)
 		return
 	}
-	response.Success(errmsg.UserPasswordChangeSuccess, count, c)
+	response.Success(errors.UserPasswordChangeSuccess, count, c)
 }

+ 2 - 2
config/application.yaml

@@ -34,8 +34,8 @@ gateway:
 #日志系统
 log2file:
   filePath: ./logs/
-#  fileName: confrontation_training
-#  fileSuffix: .log
+  fileName: confrontation_training
+  fileSuffix: .log
 
 
 

+ 2 - 2
config/config.go

@@ -27,7 +27,7 @@ type Server struct {
 	Port string `mapstructure:"port"`
 }
 
-// Mysql 数据源配置
+// SQLite Mysql 数据源配置
 type SQLite struct {
 	Url string `mapstructure:"url"`
 }
@@ -37,7 +37,7 @@ type Jwt struct {
 	SigningKey string `mapstructure:"signingKey"`
 }
 
-// 文件上传相关路径配置
+// Upload 文件上传相关路径配置
 type Upload struct {
 	SavePath  string `mapstructer:"savePath"`
 	AccessUrl string `mapstructure:"accessUrl"`

+ 1 - 1
constant/constant.go

@@ -13,7 +13,7 @@ const (
 	StartTransCmd         = "e82001363380"
 	StopTransCmd          = "0xe826"
 	StopCollectCmd        = "0xe822"
-	//ECG默认用户信息
+	// EcgDefaultAge ECG默认用户信息
 	EcgDefaultAge      = 20 //年龄默认值20
 	EcgDefaultHeight   = 170
 	EcgDefaultWeight   = 65

+ 46 - 0
docs/docs.go

@@ -171,6 +171,29 @@ const docTemplate = `{
                 }
             }
         },
+        "/v1/device/:mac/remove": {
+            "post": {
+                "description": "移除设备",
+                "consumes": [
+                    "application/json"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "设备管理"
+                ],
+                "summary": "移除设备",
+                "responses": {
+                    "200": {
+                        "description": "ok",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            }
+        },
         "/v1/device/:mac/stop/collect": {
             "get": {
                 "description": "停止采集",
@@ -285,6 +308,29 @@ const docTemplate = `{
                 }
             }
         },
+        "/v1/device/list/:type": {
+            "get": {
+                "description": "设备列表",
+                "consumes": [
+                    "application/json"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "设备管理"
+                ],
+                "summary": "设备列表",
+                "responses": {
+                    "200": {
+                        "description": "ok",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            }
+        },
         "/v1/device/open/notify/": {
             "get": {
                 "description": "开启数据通知",

+ 46 - 0
docs/swagger.json

@@ -162,6 +162,29 @@
                 }
             }
         },
+        "/v1/device/:mac/remove": {
+            "post": {
+                "description": "移除设备",
+                "consumes": [
+                    "application/json"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "设备管理"
+                ],
+                "summary": "移除设备",
+                "responses": {
+                    "200": {
+                        "description": "ok",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            }
+        },
         "/v1/device/:mac/stop/collect": {
             "get": {
                 "description": "停止采集",
@@ -276,6 +299,29 @@
                 }
             }
         },
+        "/v1/device/list/:type": {
+            "get": {
+                "description": "设备列表",
+                "consumes": [
+                    "application/json"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "设备管理"
+                ],
+                "summary": "设备列表",
+                "responses": {
+                    "200": {
+                        "description": "ok",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            }
+        },
         "/v1/device/open/notify/": {
             "get": {
                 "description": "开启数据通知",

+ 30 - 0
docs/swagger.yaml

@@ -104,6 +104,21 @@ paths:
       summary: 断开连接
       tags:
       - 设备管理
+  /v1/device/:mac/remove:
+    post:
+      consumes:
+      - application/json
+      description: 移除设备
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: ok
+          schema:
+            type: string
+      summary: 移除设备
+      tags:
+      - 设备管理
   /v1/device/:mac/stop/collect:
     get:
       consumes:
@@ -178,6 +193,21 @@ paths:
       summary: 连接设备
       tags:
       - 设备管理
+  /v1/device/list/:type:
+    get:
+      consumes:
+      - application/json
+      description: 设备列表
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: ok
+          schema:
+            type: string
+      summary: 设备列表
+      tags:
+      - 设备管理
   /v1/device/open/notify/:
     get:
       consumes:

+ 2 - 1
err/errMsg.go

@@ -29,7 +29,8 @@ const (
 	QuestionCreateFailed      = "创建问题失败"
 	AnswerCreateFailed        = "创建答案失败"
 	FindAnswerFailed          = "查询答案信息失败"
-	FindCategoryFailed        = "类别查询失败"
 	DeviceAddFailed           = "新增设备信息失败"
 	DeviceAddSuccess          = "新增设备信息成功"
+	DeviceRemoveFailed        = "设备移除失败"
+	DeviceRemoveSuccess       = "设备移除成功"
 )

+ 3 - 1
initialize/router.go

@@ -49,7 +49,7 @@ func Router() {
 	//user.Use(middleware.JwtAuth())
 	user.POST("/register", gateway.GetUser().UseRegister)
 	user.POST("/reset", gateway.GetUser().ResetPassword)
-	user.POST("/find", gateway.GetUser().UserList)
+	user.GET("/find", gateway.GetUser().UserList)
 	user.POST("/change/password", gateway.GetUser().ModePass)
 	device := v1.Group("/device")
 	device.GET("/scan", gateway.ScanDevice)
@@ -60,6 +60,8 @@ func Router() {
 	device.GET("/:mac/stop/collect/", gateway.StopCollect)
 	device.GET("/:mac/disconnect/", gateway.Disconnect)
 	device.POST("/add/", gateway.GetDeviceService().DeviceAdd)
+	device.DELETE("/:mac/remove", gateway.GetDeviceService().DeviceRemove)
+	device.GET("/list/:type", gateway.GetDeviceService().DeviceList)
 	chats := v1.Group("/chat")
 	chats.GET("/get/chat/", chat.GetQuestionService().GetQuestion)
 	chats.POST("/create/chat/", chat.GetQuestionService().CreateQuestion)

+ 9 - 5
middleware/log_2_file.go

@@ -3,7 +3,6 @@ package middleware
 import (
 	"confrontation-training/common"
 	"confrontation-training/global"
-	"fmt"
 	"github.com/gin-gonic/gin"
 	rotatelogs "github.com/lestrrat-go/file-rotatelogs"
 	"github.com/rifflock/lfshook"
@@ -22,12 +21,17 @@ func LoggerToFile() gin.HandlerFunc {
 	// 日志文件
 	fileName := path.Join(logFilePath, logFileName)
 	// 写入文件
-	//src, err := os.OpenFile(fileName, os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.ModeAppend)
-	src, err := os.OpenFile(fileName, os.O_CREATE|os.O_APPEND|os.O_RDWR, os.ModeAppend)
+	err := os.MkdirAll(logFilePath, os.ModePerm)
 	if err != nil {
-		fmt.Println("err", err)
+		panic(err)
+		return nil
 	}
-
+	//src, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR|os.O_APPEND, os.ModeAppend|os.ModePerm)
+	//if err != nil {
+	//	fmt.Println("err", err)
+	//	panic(err)
+	//}
+	src, _ := os.OpenFile(fileName+"a.log", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
 	// 实例化
 	logger := logrus.New()
 

+ 8 - 0
service/device/device.go

@@ -26,3 +26,11 @@ func (d *DeviceService) CreateDevice(param deviceModel.DeviceAddParam) *gorm.DB
 	}
 	return global.Db.Create(addParam)
 }
+
+func (d *DeviceService) RemoveDevice(mac string) int64 {
+	_, count := d.FindDeviceByMac(mac)
+	if count > 0 {
+		count = global.Db.Delete(" mac = ?", mac).RowsAffected
+	}
+	return count
+}