diff --git a/dbsql/db.go b/dbsql/db.go index 224f602..59d1614 100644 --- a/dbsql/db.go +++ b/dbsql/db.go @@ -2,6 +2,7 @@ package dbsql import ( "fmt" + "strings" "gorm.io/driver/mysql" "gorm.io/gorm" @@ -35,5 +36,11 @@ func SetPageQuery(db *gorm.DB, pageIndex int, pageSize int) *gorm.DB { } func SetDSN(pwd string) { - DSN_local = "root:" + pwd + "@tcp(mysql.flatincbr.work:3306)/qa?charset=utf8&parseTime=true" + DSN_local = "root:" + pwd + "@tcp(qa.flatincbr.work:3306)/qa?charset=utf8&parseTime=true" +} + +func DSN_qaoms() string { + str1 := strings.Split(DSN, ")/")[0] + ")/" + str2 := "?" + strings.Split(DSN, "?")[1] + return str1 + "qaoms" + str2 } diff --git a/device/devices.go b/device/device.go similarity index 96% rename from device/devices.go rename to device/device.go index c5feabf..e6812ab 100644 --- a/device/devices.go +++ b/device/device.go @@ -1,293 +1,293 @@ -package device - -import ( - "autogo/controllers" - "autogo/dbsql" - "autogo/models" - "net/http" - "strconv" - - "github.com/gin-gonic/gin" - "github.com/spf13/cast" -) - -// @Tags 设备相关 /api/device/v1/ -// @Summary 查询设备 -// @Description 根据udid查询设备 -// @accept x-www-form-urlencoded -// @Param udid query string true "设备udid" -// @Success 200 {object} models.Response "返回设备信息" -// @Router /api/device/v1/get_device [get] -func GetDeviceByUdid(c *gin.Context) { - rsp := controllers.NewResponse() - - udid := c.DefaultQuery("udid", "") - if udid == "" { - c.JSON(http.StatusOK, rsp.Error("udid error")) - return - } - db, err := dbsql.GetConn(dbsql.DSN) - if err != nil { - c.JSON(http.StatusOK, rsp.Error(err.Error())) - return - } - defer dbsql.Close(db) - - var device models.Device - - db.Model(&models.Device{}).Where("udid = ?", udid).Last(&device) - - if device.ID < 1 { - c.JSON(http.StatusOK, rsp.Error("没有找到该设备,udid-"+udid)) - return - } - - rsp.Success() - rsp.Data = device - c.JSON(http.StatusOK, rsp) -} - -// @Tags 设备相关 /api/device/v1/ -// @Summary 获取设备列表 -// @Description 获取设备列表 -// @accept x-www-form-urlencoded -// @Param page_size query int false "每页大小,默认为10" -// @Param page_index query int false "第几页,默认为第一页" -// @Success 200 {object} models.Response "返回设备列表信息" -// @Router /api/device/v1/list [get] -func GetDevices(c *gin.Context) { - rsp := controllers.NewResponse() - var p models.Page - - pageIndex, err := strconv.Atoi(c.DefaultQuery("page_index", "1")) - if err != nil { - c.JSON(http.StatusOK, rsp.Error(err.Error())) - return - } - p.Index = pageIndex - pageSize, err := strconv.Atoi(c.DefaultQuery("page_size", "10")) - if err != nil { - c.JSON(http.StatusOK, rsp.Error(err.Error())) - return - } - p.Size = pageSize - - db, err := dbsql.GetConn(dbsql.DSN) - if err != nil { - c.JSON(http.StatusOK, rsp.Error(err.Error())) - return - } - defer dbsql.Close(db) - - var list []models.Device - - db = db.Model(&models.Device{}) - var count int64 - db = db.Count(&count) - db = dbsql.SetPageQuery(db, pageIndex, pageSize) - db.Where("is_del = 0").Order("id desc").Find(&list) - dbsql.Close(db) - - db, _ = dbsql.GetConn(dbsql.DSN) - - dbsql.Close(db) - - p.MaxPage = count/(int64(p.Size)+1) + 1 - p.List = list - p.Total = int(count) - - rsp.Success() - rsp.Data = p - c.JSON(http.StatusOK, rsp) -} - -// @Tags 设备相关 /api/device/v1/ -// @Summary 新建设备 -// @Description 新建设备,UDID唯一 -// @accept x-www-form-urlencoded -// @Param project formData string true "项目" -// @Param product_name formData string true "产品" -// @Param device_name formData string true "存放设备名字型号" -// @Param os formData string false "操作系统的版本" -// @Param udid formData string true "设备唯一udid" -// @Param platform formData string true "操作系统平台adr/ios" -// @Success 200 {object} models.Response "返回创建后的设备信息" -// @Router /api/device/v1/create [post] -func CreateDevice(c *gin.Context) { - rsp := controllers.NewResponse() - - db, err := dbsql.GetConn(dbsql.DSN) - if err != nil { - c.JSON(http.StatusOK, rsp.Error(err.Error())) - return - } - defer dbsql.Close(db) - - var obj models.Device - obj.Project = c.PostForm("project") - obj.ProductName = c.PostForm("product_name") - obj.DeviceName = c.PostForm("device_name") - obj.OS = c.PostForm("os") - obj.Udid = c.PostForm("udid") - obj.Platform = c.PostForm("platform") - - // || obj.Creator == "" - if obj.Project == "" || obj.ProductName == "" || - obj.DeviceName == "" || obj.Platform == "" || - obj.Udid == "" { - rsp.Error("get formdata error") - rsp.Data = obj - c.JSON(http.StatusOK, rsp) - return - } - - var _device models.Device - db.Model(models.Device{}).Where("udid = ?", obj.Udid).Find(&_device) - if _device.ID > 0 { - c.JSON(http.StatusOK, rsp.Error("添加失败,该设备已存在")) - return - } - - db.Model(models.Device{}).Create(&obj) - if db.Error != nil { - c.JSON(http.StatusOK, rsp.Error(db.Error.Error())) - return - } - - rsp.Success() - rsp.Data = obj - c.JSON(http.StatusOK, rsp) -} - -// @Tags 设备相关 /api/device/v1/ -// @Summary 更新设备 -// @Description 根据主键更新设备 -// @accept x-www-form-urlencoded -// @Param id formData string true "设备id,必传" -// @Param project formData string false "项目" -// @Param product_name formData string false "产品" -// @Param device_name formData string false "存放设备名字型号" -// @Param os formData string false "操作系统的版本" -// @Param udid formData string false "设备唯一udid" -// @Param platform formData string false "操作系统平台adr/ios" -// @Success 200 {object} models.Response "返回创建后的设备信息" -// @Router /api/device/v1/update [post] -func UpdateDevice(c *gin.Context) { - rsp := controllers.NewResponse() - - id := cast.ToInt(c.PostForm("id")) - if id == 0 { - c.JSON(http.StatusOK, rsp.Error("device.id error - "+c.PostForm("id"))) - return - } - - db, err := dbsql.GetConn(dbsql.DSN) - if err != nil { - c.JSON(http.StatusOK, rsp.Error(err.Error())) - return - } - defer dbsql.Close(db) - - var obj models.Device - db.Model(models.Device{}).Where("id = ?", obj.ID).Find(&obj) - if obj.ID < 1 { - c.JSON(http.StatusOK, rsp.Error("修改失败,该设备不存在")) - return - } - if c.PostForm("project") != "" { - obj.Project = c.PostForm("project") - } - if c.PostForm("product_name") != "" { - obj.ProductName = c.PostForm("product_name") - } - if c.PostForm("device_name") != "" { - obj.DeviceName = c.PostForm("device_name") - } - if c.PostForm("os") != "" { - obj.OS = c.PostForm("os") - } - if c.PostForm("udid") != "" { - obj.Udid = c.PostForm("udid") - } - if c.PostForm("platform") != "" { - obj.Platform = c.PostForm("platform") - } - - db.Model(models.Device{}).Where("id = ?", obj.ID).Updates(&obj) - if db.Error != nil { - c.JSON(http.StatusOK, rsp.Error(db.Error.Error())) - return - } - - rsp.Success() - rsp.Data = obj - c.JSON(http.StatusOK, rsp) -} - -// @Tags 设备相关 /api/device/v1/ -// @Summary 删除设备 -// @Description 根据主键软删除设备,is_del设为1 -// @accept x-www-form-urlencoded -// @Param id path string true "设备id,必传" -// @Success 200 {object} models.Response "返回创建后的设备信息" -// @Router /api/device/v1/delete/{id} [delete] -func DeleteDevice(c *gin.Context) { - rsp := controllers.NewResponse() - - id := cast.ToInt(c.Param("id")) - if id == 0 { - rsp.Error("device.id error - " + c.Param("id")) - c.JSON(http.StatusOK, rsp) - return - } - - db, err := dbsql.GetConn(dbsql.DSN) - if err != nil { - rsp.Error(err.Error()) - c.JSON(http.StatusOK, rsp) - return - } - defer dbsql.Close(db) - - db.Model(models.Device{}).Where("id = ?", id).Update("is_del", 1) - - rsp.Success() - c.JSON(http.StatusOK, rsp) -} - -// @Tags 设备相关 /api/device/v1/ -// @Summary 更新设备状态 -// @Description 更新设备状态,错误的状态会被拒绝 -// @accept x-www-form-urlencoded -// @Param udid formData string true "设备udid" -// @Param status formData string true "要更新的设备状态,online-在线空闲,busy-占用中,offline-离线" -// @Success 200 {object} models.Response "返回更新的状态" -// @Router /api/device/v1/update_status [post] -func UpdateDeviceStatus(c *gin.Context) { - rsp := controllers.NewResponse() - - if c.PostForm("udid") == "" { - rsp.Error("参数udid错误:" + c.PostForm("udid")) - c.JSON(http.StatusOK, rsp) - return - } - if c.PostForm("status") != "offline" && c.PostForm("status") != "online" && c.PostForm("status") != "busy" { - rsp.Error("参数status错误:" + c.PostForm("status")) - c.JSON(http.StatusOK, rsp) - return - } - - db, err := dbsql.GetConn(dbsql.DSN) - if err != nil { - rsp.Error(err.Error()) - c.JSON(http.StatusOK, rsp) - return - } - defer dbsql.Close(db) - - db.Model(models.Device{}).Where("udid = ?", c.PostForm("udid")).Update("status", c.PostForm("status")) - - rsp.Success() - rsp.Data = c.PostForm("udid") - c.JSON(http.StatusOK, rsp) -} +package device + +import ( + "autogo/controllers" + "autogo/dbsql" + "autogo/models" + "net/http" + "strconv" + + "github.com/gin-gonic/gin" + "github.com/spf13/cast" +) + +// @Tags 设备相关 /api/device/v1/ +// @Summary 查询设备 +// @Description 根据udid查询设备 +// @accept x-www-form-urlencoded +// @Param udid query string true "设备udid" +// @Success 200 {object} models.Response "返回设备信息" +// @Router /api/device/v1/get_device [get] +func GetDeviceByUdid(c *gin.Context) { + rsp := controllers.NewResponse() + + udid := c.DefaultQuery("udid", "") + if udid == "" { + c.JSON(http.StatusOK, rsp.Error("udid error")) + return + } + db, err := dbsql.GetConn(dbsql.DSN) + if err != nil { + c.JSON(http.StatusOK, rsp.Error(err.Error())) + return + } + defer dbsql.Close(db) + + var device models.Device + + db.Model(&models.Device{}).Where("udid = ?", udid).Last(&device) + + if device.ID < 1 { + c.JSON(http.StatusOK, rsp.Error("没有找到该设备,udid-"+udid)) + return + } + + rsp.Success() + rsp.Data = device + c.JSON(http.StatusOK, rsp) +} + +// @Tags 设备相关 /api/device/v1/ +// @Summary 获取设备列表 +// @Description 获取设备列表 +// @accept x-www-form-urlencoded +// @Param page_size query int false "每页大小,默认为10" +// @Param page_index query int false "第几页,默认为第一页" +// @Success 200 {object} models.Response "返回设备列表信息" +// @Router /api/device/v1/list [get] +func GetDevices(c *gin.Context) { + rsp := controllers.NewResponse() + var p models.Page + + pageIndex, err := strconv.Atoi(c.DefaultQuery("page_index", "1")) + if err != nil { + c.JSON(http.StatusOK, rsp.Error(err.Error())) + return + } + p.Index = pageIndex + pageSize, err := strconv.Atoi(c.DefaultQuery("page_size", "10")) + if err != nil { + c.JSON(http.StatusOK, rsp.Error(err.Error())) + return + } + p.Size = pageSize + + db, err := dbsql.GetConn(dbsql.DSN) + if err != nil { + c.JSON(http.StatusOK, rsp.Error(err.Error())) + return + } + defer dbsql.Close(db) + + var list []models.Device + + db = db.Model(&models.Device{}) + var count int64 + db = db.Count(&count) + db = dbsql.SetPageQuery(db, pageIndex, pageSize) + db.Where("is_del = 0").Order("id desc").Find(&list) + dbsql.Close(db) + + db, _ = dbsql.GetConn(dbsql.DSN) + + dbsql.Close(db) + + p.MaxPage = count/(int64(p.Size)+1) + 1 + p.List = list + p.Total = int(count) + + rsp.Success() + rsp.Data = p + c.JSON(http.StatusOK, rsp) +} + +// @Tags 设备相关 /api/device/v1/ +// @Summary 新建设备 +// @Description 新建设备,UDID唯一 +// @accept x-www-form-urlencoded +// @Param project formData string true "项目" +// @Param product_name formData string true "产品" +// @Param device_name formData string true "存放设备名字型号" +// @Param os formData string false "操作系统的版本" +// @Param udid formData string true "设备唯一udid" +// @Param platform formData string true "操作系统平台adr/ios" +// @Success 200 {object} models.Response "返回创建后的设备信息" +// @Router /api/device/v1/create [post] +func CreateDevice(c *gin.Context) { + rsp := controllers.NewResponse() + + db, err := dbsql.GetConn(dbsql.DSN) + if err != nil { + c.JSON(http.StatusOK, rsp.Error(err.Error())) + return + } + defer dbsql.Close(db) + + var obj models.Device + obj.Project = c.PostForm("project") + obj.ProductName = c.PostForm("product_name") + obj.DeviceName = c.PostForm("device_name") + obj.OS = c.PostForm("os") + obj.Udid = c.PostForm("udid") + obj.Platform = c.PostForm("platform") + + // || obj.Creator == "" + if obj.Project == "" || obj.ProductName == "" || + obj.DeviceName == "" || obj.Platform == "" || + obj.Udid == "" { + rsp.Error("get formdata error") + rsp.Data = obj + c.JSON(http.StatusOK, rsp) + return + } + + var _device models.Device + db.Model(models.Device{}).Where("udid = ?", obj.Udid).Find(&_device) + if _device.ID > 0 { + c.JSON(http.StatusOK, rsp.Error("添加失败,该设备已存在")) + return + } + + db.Model(models.Device{}).Create(&obj) + if db.Error != nil { + c.JSON(http.StatusOK, rsp.Error(db.Error.Error())) + return + } + + rsp.Success() + rsp.Data = obj + c.JSON(http.StatusOK, rsp) +} + +// @Tags 设备相关 /api/device/v1/ +// @Summary 更新设备 +// @Description 根据主键更新设备 +// @accept x-www-form-urlencoded +// @Param id formData string true "设备id,必传" +// @Param project formData string false "项目" +// @Param product_name formData string false "产品" +// @Param device_name formData string false "存放设备名字型号" +// @Param os formData string false "操作系统的版本" +// @Param udid formData string false "设备唯一udid" +// @Param platform formData string false "操作系统平台adr/ios" +// @Success 200 {object} models.Response "返回创建后的设备信息" +// @Router /api/device/v1/update [post] +func UpdateDevice(c *gin.Context) { + rsp := controllers.NewResponse() + + id := cast.ToInt(c.PostForm("id")) + if id == 0 { + c.JSON(http.StatusOK, rsp.Error("device.id error - "+c.PostForm("id"))) + return + } + + db, err := dbsql.GetConn(dbsql.DSN) + if err != nil { + c.JSON(http.StatusOK, rsp.Error(err.Error())) + return + } + defer dbsql.Close(db) + + var obj models.Device + db.Model(models.Device{}).Where("id = ?", obj.ID).Find(&obj) + if obj.ID < 1 { + c.JSON(http.StatusOK, rsp.Error("修改失败,该设备不存在")) + return + } + if c.PostForm("project") != "" { + obj.Project = c.PostForm("project") + } + if c.PostForm("product_name") != "" { + obj.ProductName = c.PostForm("product_name") + } + if c.PostForm("device_name") != "" { + obj.DeviceName = c.PostForm("device_name") + } + if c.PostForm("os") != "" { + obj.OS = c.PostForm("os") + } + if c.PostForm("udid") != "" { + obj.Udid = c.PostForm("udid") + } + if c.PostForm("platform") != "" { + obj.Platform = c.PostForm("platform") + } + + db.Model(models.Device{}).Where("id = ?", obj.ID).Updates(&obj) + if db.Error != nil { + c.JSON(http.StatusOK, rsp.Error(db.Error.Error())) + return + } + + rsp.Success() + rsp.Data = obj + c.JSON(http.StatusOK, rsp) +} + +// @Tags 设备相关 /api/device/v1/ +// @Summary 删除设备 +// @Description 根据主键软删除设备,is_del设为1 +// @accept x-www-form-urlencoded +// @Param id path string true "设备id,必传" +// @Success 200 {object} models.Response "返回创建后的设备信息" +// @Router /api/device/v1/delete/{id} [delete] +func DeleteDevice(c *gin.Context) { + rsp := controllers.NewResponse() + + id := cast.ToInt(c.Param("id")) + if id == 0 { + rsp.Error("device.id error - " + c.Param("id")) + c.JSON(http.StatusOK, rsp) + return + } + + db, err := dbsql.GetConn(dbsql.DSN) + if err != nil { + rsp.Error(err.Error()) + c.JSON(http.StatusOK, rsp) + return + } + defer dbsql.Close(db) + + db.Model(models.Device{}).Where("id = ?", id).Update("is_del", 1) + + rsp.Success() + c.JSON(http.StatusOK, rsp) +} + +// @Tags 设备相关 /api/device/v1/ +// @Summary 更新设备状态 +// @Description 更新设备状态,错误的状态会被拒绝 +// @accept x-www-form-urlencoded +// @Param udid formData string true "设备udid" +// @Param status formData string true "要更新的设备状态,online-在线空闲,busy-占用中,offline-离线" +// @Success 200 {object} models.Response "返回更新的状态" +// @Router /api/device/v1/update_status [post] +func UpdateDeviceStatus(c *gin.Context) { + rsp := controllers.NewResponse() + + if c.PostForm("udid") == "" { + rsp.Error("参数udid错误:" + c.PostForm("udid")) + c.JSON(http.StatusOK, rsp) + return + } + if c.PostForm("status") != "offline" && c.PostForm("status") != "online" && c.PostForm("status") != "busy" { + rsp.Error("参数status错误:" + c.PostForm("status")) + c.JSON(http.StatusOK, rsp) + return + } + + db, err := dbsql.GetConn(dbsql.DSN) + if err != nil { + rsp.Error(err.Error()) + c.JSON(http.StatusOK, rsp) + return + } + defer dbsql.Close(db) + + db.Model(models.Device{}).Where("udid = ?", c.PostForm("udid")).Update("status", c.PostForm("status")) + + rsp.Success() + rsp.Data = c.PostForm("udid") + c.JSON(http.StatusOK, rsp) +} diff --git a/device/device_v2.go b/device/device_v2.go new file mode 100644 index 0000000..90d6e8e --- /dev/null +++ b/device/device_v2.go @@ -0,0 +1,108 @@ +package device + +import ( + "autogo/controllers" + "autogo/dbsql" + "autogo/models" + "net/http" + + "github.com/gin-gonic/gin" +) + +// @Tags 设备相关 /api/device/v2/ +// @Summary 新建设备 +// @Description 新建设备,UDID唯一 +// @accept application/json +// @Param body body models.DeviceV2 true "设备信息" +// @Success 200 {object} models.Response "返回结果" +// @Router /api/device/v2/create [post] +func CreateDeviceV2(c *gin.Context) { + rsp := controllers.NewResponse() + + var dv models.DeviceV2 + c.ShouldBind(&dv) + + if dv.Udid == "" { + c.JSON(http.StatusOK, rsp.Error("udid不能为空")) + return + } + if dv.Platform == "" { + c.JSON(http.StatusOK, rsp.Error("platform不能为空")) + return + } + if dv.Platform != "adr" && dv.Platform != "ios" { + c.JSON(http.StatusOK, rsp.Error("参数platform异常, 只允许值=adr/ios: "+dv.Platform)) + return + } + + db, err := dbsql.GetConn(dbsql.DSN_qaoms()) + if err != nil { + c.JSON(http.StatusOK, rsp.Error(err.Error())) + return + } + defer dbsql.Close(db) + + db.Model(models.DeviceV2{}).Create(&dv) + if db.Error != nil { + c.JSON(http.StatusOK, rsp.Error(db.Error.Error())) + return + } + + if dv.ID < 1 { + c.JSON(http.StatusOK, rsp.Error("新建失败")) + return + } + + rsp.Success() + rsp.Data = dv + c.JSON(http.StatusOK, rsp) +} + +// @Tags 设备相关 /api/device/v2/ +// @Summary 更新设备状态 +// @Description 更新设备状态,错误的状态会被拒绝 +// @accept x-www-form-urlencoded +// @Param udid formData string true "设备udid" +// @Param status formData string true "要更新的设备状态,online-在线空闲,busy-占用中,offline-离线" +// @Param status_details formData string false "状态详细,接收文本" +// @Success 200 {object} models.Response "返回更新的状态" +// @Router /api/device/v2/update_status [post] +func UpdateDeviceStatusV2(c *gin.Context) { + rsp := controllers.NewResponse() + + udid := c.PostForm("udid") + status := c.PostForm("status") + status_details := c.PostForm("status_details") + + if udid == "" { + c.JSON(http.StatusOK, rsp.Error("参数udid错误:"+udid)) + return + } + if c.PostForm("status") != "offline" && c.PostForm("status") != "online" && c.PostForm("status") != "busy" { + c.JSON(http.StatusOK, rsp.Error("参数status错误: "+status)) + return + } + + db, err := dbsql.GetConn(dbsql.DSN_qaoms()) + if err != nil { + rsp.Error(err.Error()) + c.JSON(http.StatusOK, rsp) + return + } + defer dbsql.Close(db) + + var dv models.DeviceV2 + db.Model(models.DeviceV2{}).Where("udid = ?", udid).Last(&dv) + if dv.ID < 1 { + c.JSON(http.StatusOK, rsp.Error("设备不存在: "+udid)) + return + } + dv.Status = status + dv.StatusDetails = status_details + + db.Save(&dv) + + rsp.Success() + rsp.Data = dv + c.JSON(http.StatusOK, rsp) +} diff --git a/docs/docs.go b/docs/docs.go index 242ef1e..72bc958 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -315,6 +315,79 @@ const docTemplate = `{ } } }, + "/api/device/v2/create": { + "post": { + "description": "新建设备,UDID唯一", + "consumes": [ + "application/json" + ], + "tags": [ + "设备相关 /api/device/v2/" + ], + "summary": "新建设备", + "parameters": [ + { + "description": "设备信息", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/models.DeviceV2" + } + } + ], + "responses": { + "200": { + "description": "返回结果", + "schema": { + "$ref": "#/definitions/models.Response" + } + } + } + } + }, + "/api/device/v2/update_status": { + "post": { + "description": "更新设备状态,错误的状态会被拒绝", + "consumes": [ + "application/x-www-form-urlencoded" + ], + "tags": [ + "设备相关 /api/device/v2/" + ], + "summary": "更新设备状态", + "parameters": [ + { + "type": "string", + "description": "设备udid", + "name": "udid", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "要更新的设备状态,online-在线空闲,busy-占用中,offline-离线", + "name": "status", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "状态详细,接收文本", + "name": "status_details", + "in": "formData" + } + ], + "responses": { + "200": { + "description": "返回更新的状态", + "schema": { + "$ref": "#/definitions/models.Response" + } + } + } + } + }, "/api/monkey/v1/create_task": { "post": { "description": "新建Monkey任务,创建成功后将会发送指令到monkey服务,初始status为WAITTING-等待中", @@ -1213,6 +1286,56 @@ const docTemplate = `{ } }, "definitions": { + "models.DeviceV2": { + "type": "object", + "properties": { + "app_did": { + "type": "string" + }, + "brand": { + "type": "string" + }, + "create_time": { + "description": "创建时间", + "type": "string" + }, + "device_name": { + "type": "string" + }, + "id": { + "description": "主键ID", + "type": "integer" + }, + "is_enabled": { + "type": "integer" + }, + "lan_ip": { + "type": "string" + }, + "os_version": { + "type": "string" + }, + "platform": { + "type": "string" + }, + "product_id": { + "type": "integer" + }, + "status": { + "type": "string" + }, + "status_details": { + "type": "string" + }, + "udid": { + "type": "string" + }, + "update_time": { + "description": "更新时间", + "type": "string" + } + } + }, "models.Response": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index c184ce0..3759b06 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -303,6 +303,79 @@ } } }, + "/api/device/v2/create": { + "post": { + "description": "新建设备,UDID唯一", + "consumes": [ + "application/json" + ], + "tags": [ + "设备相关 /api/device/v2/" + ], + "summary": "新建设备", + "parameters": [ + { + "description": "设备信息", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/models.DeviceV2" + } + } + ], + "responses": { + "200": { + "description": "返回结果", + "schema": { + "$ref": "#/definitions/models.Response" + } + } + } + } + }, + "/api/device/v2/update_status": { + "post": { + "description": "更新设备状态,错误的状态会被拒绝", + "consumes": [ + "application/x-www-form-urlencoded" + ], + "tags": [ + "设备相关 /api/device/v2/" + ], + "summary": "更新设备状态", + "parameters": [ + { + "type": "string", + "description": "设备udid", + "name": "udid", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "要更新的设备状态,online-在线空闲,busy-占用中,offline-离线", + "name": "status", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "状态详细,接收文本", + "name": "status_details", + "in": "formData" + } + ], + "responses": { + "200": { + "description": "返回更新的状态", + "schema": { + "$ref": "#/definitions/models.Response" + } + } + } + } + }, "/api/monkey/v1/create_task": { "post": { "description": "新建Monkey任务,创建成功后将会发送指令到monkey服务,初始status为WAITTING-等待中", @@ -1201,6 +1274,56 @@ } }, "definitions": { + "models.DeviceV2": { + "type": "object", + "properties": { + "app_did": { + "type": "string" + }, + "brand": { + "type": "string" + }, + "create_time": { + "description": "创建时间", + "type": "string" + }, + "device_name": { + "type": "string" + }, + "id": { + "description": "主键ID", + "type": "integer" + }, + "is_enabled": { + "type": "integer" + }, + "lan_ip": { + "type": "string" + }, + "os_version": { + "type": "string" + }, + "platform": { + "type": "string" + }, + "product_id": { + "type": "integer" + }, + "status": { + "type": "string" + }, + "status_details": { + "type": "string" + }, + "udid": { + "type": "string" + }, + "update_time": { + "description": "更新时间", + "type": "string" + } + } + }, "models.Response": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 68857aa..9136c13 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1,4 +1,38 @@ definitions: + models.DeviceV2: + properties: + app_did: + type: string + brand: + type: string + create_time: + description: 创建时间 + type: string + device_name: + type: string + id: + description: 主键ID + type: integer + is_enabled: + type: integer + lan_ip: + type: string + os_version: + type: string + platform: + type: string + product_id: + type: integer + status: + type: string + status_details: + type: string + udid: + type: string + update_time: + description: 更新时间 + type: string + type: object models.Response: properties: code: @@ -209,6 +243,54 @@ paths: summary: 更新设备状态 tags: - 设备相关 /api/device/v1/ + /api/device/v2/create: + post: + consumes: + - application/json + description: 新建设备,UDID唯一 + parameters: + - description: 设备信息 + in: body + name: body + required: true + schema: + $ref: '#/definitions/models.DeviceV2' + responses: + "200": + description: 返回结果 + schema: + $ref: '#/definitions/models.Response' + summary: 新建设备 + tags: + - 设备相关 /api/device/v2/ + /api/device/v2/update_status: + post: + consumes: + - application/x-www-form-urlencoded + description: 更新设备状态,错误的状态会被拒绝 + parameters: + - description: 设备udid + in: formData + name: udid + required: true + type: string + - description: 要更新的设备状态,online-在线空闲,busy-占用中,offline-离线 + in: formData + name: status + required: true + type: string + - description: 状态详细,接收文本 + in: formData + name: status_details + type: string + responses: + "200": + description: 返回更新的状态 + schema: + $ref: '#/definitions/models.Response' + summary: 更新设备状态 + tags: + - 设备相关 /api/device/v2/ /api/monkey/v1/create_task: post: consumes: diff --git a/models/oms_devices.go b/models/oms_devices.go new file mode 100644 index 0000000..c705d73 --- /dev/null +++ b/models/oms_devices.go @@ -0,0 +1,31 @@ +package models + +import ( + "time" + + "gorm.io/gorm" +) + +// Device 结构体 +type DeviceV2 struct { + ID uint `gorm:"primarykey"` // 主键ID + ProductId *int `json:"product_id" form:"product_id" gorm:"column:product_id;comment:产品表主键id;"` + DeviceName string `json:"device_name" form:"device_name" gorm:"column:device_name;comment:;"` + Brand string `json:"brand" form:"brand" gorm:"column:brand;comment:设备品牌;"` + Udid string `json:"udid" form:"udid" gorm:"column:udid;comment:;"` + AppDid string `json:"app_did" form:"app_did" gorm:"column:app_did;comment:;"` + Platform string `json:"platform" form:"platform" gorm:"column:platform;comment:支持两项ios/adr;"` + OsVersion string `json:"os_version" form:"os_version" gorm:"column:os_version;comment:;"` + LanIp string `json:"lan_ip" form:"lan_ip" gorm:"column:lan_ip;comment:局域网ip地址;"` + Status string `json:"status" form:"status" gorm:"column:status;comment:描述设备的状态;"` + StatusDetails string `json:"status_details" form:"status_details" gorm:"column:status_details;comment:设备详细状态;"` + IsEnabled *int `json:"is_enabled" form:"is_enabled" gorm:"column:is_enabled;comment:;"` + CreatedAt time.Time `json:"create_time" gorm:"column:created_at;autoCreate"` // 创建时间 + UpdatedAt time.Time `json:"update_time" gorm:"column:updated_at;autoUpdate"` // 更新时间 + DeletedAt gorm.DeletedAt `gorm:"index" json:"-"` // 删除时间 +} + +// TableName Device 表名 +func (DeviceV2) TableName() string { + return "qa_devices" +} diff --git a/router/router.go b/router/router.go index 401d048..eb6b77b 100644 --- a/router/router.go +++ b/router/router.go @@ -5,6 +5,8 @@ import ( "net/http" "github.com/gin-gonic/gin" + swaggerFiles "github.com/swaggo/files" + ginSwagger "github.com/swaggo/gin-swagger" ) func InitRoute() *gin.Engine { @@ -14,6 +16,9 @@ func InitRoute() *gin.Engine { c.String(http.StatusOK, "hello World!") }) + r.GET("/swagger", ginSwagger.WrapHandler(swaggerFiles.Handler)) + r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) + setRoute(r) setRouteV2(r) @@ -29,7 +34,7 @@ func Cors() gin.HandlerFunc { //接收客户端发送的origin (重要!) c.Writer.Header().Set("Access-Control-Allow-Origin", origin) //服务器支持的所有跨域请求的方法 - c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE,UPDATE") + c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE") //允许跨域设置可以返回其他子段,可以自定义字段 c.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, Token,session, Origin, X-Requested-With, Content-Type, Accept") //允许浏览器(客户端)可以解析的头部 (重要) diff --git a/router/v2.go b/router/v2.go index 5c0fc8e..dde3daa 100644 --- a/router/v2.go +++ b/router/v2.go @@ -2,6 +2,7 @@ package router import ( "autogo/controllers" + "autogo/device" "autogo/monkey" "github.com/gin-gonic/gin" @@ -16,4 +17,7 @@ func setRouteV2(r *gin.Engine) { r.GET("/api/setting/v2/get_bugly_token", controllers.GetBuglyToken) r.POST("/api/setting/v2/update_bugly_token", controllers.UpdateBuglyToken) + + r.POST("/api/device/v2/create", device.CreateDeviceV2) + r.POST("/api/device/v2/update_status", device.UpdateDeviceStatusV2) }