diff --git a/controller/api/v1/basic/vendor.go b/controller/api/v1/basic/vendor.go index 94390da..555e4aa 100644 --- a/controller/api/v1/basic/vendor.go +++ b/controller/api/v1/basic/vendor.go @@ -10,23 +10,32 @@ import ( "go.uber.org/zap" ) +type CreateVendorRequest struct { + Name string `json:"name" binding:"required"` + Uuid int `json:"uuid"` +} + +type UpdateVendorRequest struct { + Name string `json:"name"` + Uuid int `json:"uuid"` +} + // @Summary 系统厂商列表 // @Tags vendors // @Description // @Accept json // @Produce json // @Param Authorization header string true "auth by /auth" -// @Param data body models.VendorSearch true "页码, 每页大小, 搜索条件" +// @Param name query string false "系统厂商名称" +// @Param page query int false "页码" +// @Param page_size query int false "每页数量" // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}" // @Router /v1/vendors [get] func GetVendors(c *gin.Context) { var search basic.VendorSearch - if err := c.ShouldBindJSON(&search); err != nil { - c.JSON(http.StatusBadRequest, gin.H{ - "error": err.Error(), - }) - } + search.Name = c.Query("name") + search.Page, search.PageSize = utils.GetPageInfo(c) if result, err := basic.GetVendors(&search); err != nil { global.GO_LOG.Error("获取失败!", zap.Any("err", err)) @@ -42,17 +51,29 @@ func GetVendors(c *gin.Context) { // @Accept mpfd // @Produce json // @Param Authorization header string true "auth by /auth" -// @Param data body basic.Vendor true "Vendor模型" +// @Param data body CreateVendorRequest true "Vendor模型" // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}" // @Router /v1/vendors [post] -func AddVendor(c *gin.Context) { - var vendor basic.Vendor +func CreateVendor(c *gin.Context) { + var vendor CreateVendorRequest if err := c.ShouldBindJSON(&vendor); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "error": err.Error(), }) + return } - if err := basic.CreateVendor(vendor); err != nil { + if exist := basic.ExistVendorByName(vendor.Name); exist { + global.GO_LOG.Error("该系统厂商名称已存在!") + utils.FailWithMessage("该系统厂商名称已存在", c) + return + } + if exist := basic.ExistVendorByUuid(vendor.Uuid); exist { + global.GO_LOG.Error("该系统厂商uuid已存在!") + utils.FailWithMessage("该系统厂商uuid已存在", c) + return + } + + if err := basic.CreateVendor(basic.Vendor{Name: vendor.Name, Uuid: vendor.Uuid}); err != nil { global.GO_LOG.Error("创建失败!", zap.Any("err", err)) utils.FailWithMessage("创建失败", c) } else { @@ -67,16 +88,35 @@ func AddVendor(c *gin.Context) { // @Produce json // @Param Authorization header string true "auth by /auth" // @Param id path int true "系统厂商 ID" -// @Param data body basic.Vendor true "Vendor模型" +// @Param data body UpdateVendorRequest true "Vendor模型" // @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}" // @Router /v1/vendors/{id} [patch] func UpdateVendor(c *gin.Context) { - var vendor basic.Vendor + vendor, gErr := basic.GetVendorById(c.Param("id")) + if gErr != nil { + global.GO_LOG.Error("系统厂商不存在!", zap.Any("err", gErr)) + utils.FailWithMessage("系统厂商不存在!", c) + return + } + if err := c.ShouldBindJSON(&vendor); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "error": err.Error(), }) + return } + + if exist := basic.ExistVendorByName(vendor.Name); exist { + global.GO_LOG.Error("该系统厂商名称已存在!") + utils.FailWithMessage("该系统厂商名称已存在", c) + return + } + if exist := basic.ExistVendorByUuid(vendor.Uuid); exist { + global.GO_LOG.Error("该系统厂商uuid已存在!") + utils.FailWithMessage("该系统厂商uuid已存在", c) + return + } + if err := basic.UpdateVendor(&vendor); err != nil { global.GO_LOG.Error("更新失败!", zap.Any("err", err)) utils.FailWithMessage("更新失败", c) @@ -95,11 +135,11 @@ func UpdateVendor(c *gin.Context) { // @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}" // @Router /v1/vendors/{id} [delete] func DeleteVendor(c *gin.Context) { - var vendor basic.Vendor - if err := c.ShouldBindJSON(&vendor); err != nil { - c.JSON(http.StatusBadRequest, gin.H{ - "error": err.Error(), - }) + vendor, gErr := basic.GetVendorById(c.Param("id")) + if gErr != nil { + global.GO_LOG.Error("系统厂商不存在!", zap.Any("err", gErr)) + utils.FailWithMessage("系统厂商不存在!", c) + return } if err := basic.DeleteVendor(&vendor); err != nil { global.GO_LOG.Error("删除失败!", zap.Any("err", err)) diff --git a/controller/api/v1/user.go b/controller/api/v1/user.go index 7af48f2..35d3e27 100644 --- a/controller/api/v1/user.go +++ b/controller/api/v1/user.go @@ -1,7 +1,7 @@ /* * @Date: 2021-03-21 19:54:57 * @LastEditors: viletyy - * @LastEditTime: 2021-03-23 00:44:10 + * @LastEditTime: 2021-03-24 10:08:27 * @FilePath: /potato/controller/api/v1/user.go */ package v1 @@ -13,69 +13,92 @@ import ( "github.com/viletyy/potato/global" "github.com/viletyy/potato/models" "github.com/viletyy/potato/utils" + "github.com/viletyy/potato/utils/crypt" "go.uber.org/zap" ) -type LoginResponse struct { +type AuthResponse struct { User models.User `json:"user"` Token string `json:"token"` } +type AuthRequest struct { + Username string `json:"username" binding:"required"` + Password string `json:"password" binding:"required,gte=6` +} + +type RegisterRequest struct { + Username string `json:"username" binding:"required"` + Password string `json:"password" binding:"required,gte=6` + Nickname string `json:"nickname"` +} + // @Summary 用户验证 // @Description // @Accept json // @Produce json -// @Param data body models.User true "User模型" +// @Param data body AuthRequest true "用户名,密码" // @Success 200 {string} json "{"code" : 200, "data" : {"token" : ""}, "msg" : "ok"}" -// @Router /v1/auth [get] -func GetUserAuth(c *gin.Context) { - var user models.User +// @Router /v1/auth [post] +func Auth(c *gin.Context) { + var user AuthRequest if err := c.ShouldBindJSON(&user); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "error": err.Error(), }) + return } mUser, gErr := models.GetUserByUsername(user.Username) if gErr != nil { global.GO_LOG.Error("查找用户失败", zap.Any("err", gErr)) utils.FailWithMessage("查找用户失败", c) + return } isTrue := mUser.CheckPassword(user.Password) if !isTrue { global.GO_LOG.Error("用户密码不正确") utils.FailWithMessage("用户密码不正确", c) + return } token, tokenErr := utils.GenerateToken(mUser.ID) if tokenErr != nil { global.GO_LOG.Error("获取token失败", zap.Any("err", tokenErr)) utils.FailWithMessage("获取token失败", c) + return } - utils.OkWithDetailed(LoginResponse{ + utils.OkWithDetailed(AuthResponse{ User: mUser, Token: token, }, "登录成功", c) } -// @Summary 新增用户 -// @Tags users +// @Summary 注册用户 // @Description -// @Accept mpfd +// @Accept json // @Produce json -// @Param data body basic.User true "User模型" +// @Param data body RegisterRequest true "用户名" // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}" -// @Router /v1/users [post] -func AddUser(c *gin.Context) { - var user models.User +// @Router /v1/register [post] +func Register(c *gin.Context) { + var user RegisterRequest if err := c.ShouldBindJSON(&user); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "error": err.Error(), }) + return } - if err := models.CreateUser(user); err != nil { + + if isExsit := models.ExistUserByUsername(user.Username); isExsit { + global.GO_LOG.Error("用户已存在") + utils.FailWithMessage("用户已存在", c) + return + } + + if err := models.CreateUser(models.User{Username: user.Username, Password: crypt.Md5Encode(user.Password), Nickname: user.Nickname}); err != nil { global.GO_LOG.Error("创建失败!", zap.Any("err", err)) utils.FailWithMessage("创建失败", c) } else { diff --git a/docs/docs.go b/docs/docs.go index 4ead646..cfc7922 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -25,7 +25,7 @@ var doc = `{ "basePath": "{{.BasePath}}", "paths": { "/v1/auth": { - "get": { + "post": { "consumes": [ "application/json" ], @@ -35,18 +35,13 @@ var doc = `{ "summary": "用户验证", "parameters": [ { - "type": "string", - "description": "用户 用户名", - "name": "username", - "in": "query", - "required": true - }, - { - "type": "string", - "description": "用户 密码", - "name": "password", - "in": "query", - "required": true + "description": "用户名,密码", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/v1.AuthRequest" + } } ], "responses": { @@ -59,523 +54,29 @@ var doc = `{ } } }, - "/v1/businesses": { - "get": { - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "businesses" - ], - "summary": "业务系统列表", - "parameters": [ - { - "type": "string", - "description": "auth by /auth", - "name": "Authorization", - "in": "header", - "required": true - } - ], - "responses": { - "200": { - "description": "{\"code\" : 200, \"data\" : {}, \"msg\" : \"ok\"}", - "schema": { - "type": "string" - } - } - } - }, + "/v1/register": { "post": { "consumes": [ - "multipart/form-data" + "application/json" ], "produces": [ "application/json" ], - "tags": [ - "businesses" - ], - "summary": "新增业务系统", + "summary": "注册用户", "parameters": [ { - "type": "string", - "description": "auth by /auth", - "name": "Authorization", - "in": "header", - "required": true - }, - { - "type": "string", - "description": "业务系统 名称", - "name": "name", - "in": "formData", - "required": true - }, - { - "type": "string", - "description": "业务系统 描述", - "name": "desc", - "in": "formData" - }, - { - "type": "integer", - "description": "业务系统 云端id", - "name": "c_id", - "in": "formData" - } - ], - "responses": { - "200": { - "description": "{\"code\" : 200, \"msg\" : \"ok\"}", + "description": "用户名", + "name": "data", + "in": "body", + "required": true, "schema": { - "type": "string" + "$ref": "#/definitions/v1.RegisterRequest" } } - } - } - }, - "/v1/businesses/{id}": { - "delete": { - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "businesses" - ], - "summary": "删除业务系统", - "parameters": [ - { - "type": "string", - "description": "auth by /auth", - "name": "Authorization", - "in": "header", - "required": true - }, - { - "type": "integer", - "description": "业务系统 ID", - "name": "id", - "in": "path", - "required": true - } ], "responses": { "200": { - "description": "{\"code\" : 200, \"msg\" : \"ok\"}", - "schema": { - "type": "string" - } - } - } - }, - "patch": { - "consumes": [ - "multipart/form-data" - ], - "produces": [ - "application/json" - ], - "tags": [ - "businesses" - ], - "summary": "修改业务系统", - "parameters": [ - { - "type": "string", - "description": "auth by /auth", - "name": "Authorization", - "in": "header", - "required": true - }, - { - "type": "integer", - "description": "业务系统 ID", - "name": "id", - "in": "path", - "required": true - }, - { - "type": "string", - "description": "业务系统 名称", - "name": "name", - "in": "formData" - }, - { - "type": "string", - "description": "业务系统 描述", - "name": "desc", - "in": "formData" - }, - { - "type": "string", - "description": "业务系统 云端id", - "name": "c_id", - "in": "formData" - } - ], - "responses": { - "200": { - "description": "{\"code\" : 200, \"msg\" : \"ok\"}", - "schema": { - "type": "string" - } - } - } - } - }, - "/v1/meta_databases": { - "get": { - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "meta_databases" - ], - "summary": "数据源列表", - "parameters": [ - { - "type": "string", - "description": "auth by /auth", - "name": "Authorization", - "in": "header", - "required": true - } - ], - "responses": { - "200": { - "description": "{\"code\" : 200, \"basic\" : {}, \"msg\": \"ok\" }", - "schema": { - "type": "string" - } - } - } - }, - "post": { - "consumes": [ - "multipart/form-data" - ], - "produces": [ - "application/json" - ], - "tags": [ - "meta_databases" - ], - "summary": "新增数据源", - "parameters": [ - { - "type": "string", - "description": "auth by /auth", - "name": "Authorization", - "in": "header", - "required": true - }, - { - "type": "string", - "description": "数据源 名称", - "name": "name", - "in": "formData", - "required": true - }, - { - "type": "string", - "description": "数据源 地址", - "name": "host", - "in": "formData", - "required": true - }, - { - "type": "integer", - "description": "数据源 端口号", - "name": "port", - "in": "formData", - "required": true - }, - { - "type": "string", - "description": "数据源 数据库名称", - "name": "db_name", - "in": "formData", - "required": true - }, - { - "type": "string", - "description": "数据源 用户名", - "name": "username", - "in": "formData", - "required": true - }, - { - "type": "string", - "description": "数据源 密码", - "name": "password", - "in": "formData", - "required": true - }, - { - "type": "string", - "description": "数据源 备注", - "name": "comment", - "in": "formData" - }, - { - "type": "integer", - "description": "系统厂商 id", - "name": "vendor_id", - "in": "formData", - "required": true - }, - { - "type": "integer", - "description": "业务系统 id", - "name": "business_id", - "in": "formData", - "required": true - } - ], - "responses": { - "200": { - "description": "{\"code\": 200, data: {}, \"msg\" : \"ok\"}", - "schema": { - "type": "string" - } - } - } - } - }, - "/v1/meta_databases/{id}": { - "delete": { - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "meta_databases" - ], - "summary": "删除数据源", - "parameters": [ - { - "type": "string", - "description": "auth by /auth", - "name": "Authorization", - "in": "header", - "required": true - }, - { - "type": "integer", - "description": "数据源 ID", - "name": "id", - "in": "path", - "required": true - } - ], - "responses": { - "200": { - "description": "{\"code\" : 200, \"msg\" : \"ok\"}", - "schema": { - "type": "string" - } - } - } - }, - "patch": { - "consumes": [ - "multipart/form-data" - ], - "produces": [ - "application/json" - ], - "tags": [ - "meta_databases" - ], - "summary": "修改数据源", - "parameters": [ - { - "type": "string", - "description": "auth by /auth", - "name": "Authorization", - "in": "header", - "required": true - }, - { - "type": "integer", - "description": "数据源 ID", - "name": "id", - "in": "path", - "required": true - }, - { - "type": "string", - "description": "数据源 名称", - "name": "name", - "in": "formData" - }, - { - "type": "string", - "description": "数据源 地址", - "name": "host", - "in": "formData" - }, - { - "type": "integer", - "description": "数据源 端口号", - "name": "port", - "in": "formData" - }, - { - "type": "string", - "description": "数据源 数据库名称", - "name": "db_name", - "in": "formData" - }, - { - "type": "string", - "description": "数据源 用户名", - "name": "username", - "in": "formData" - }, - { - "type": "string", - "description": "数据源 密码", - "name": "password", - "in": "formData" - }, - { - "type": "string", - "description": "数据源 备注", - "name": "comment", - "in": "formData" - }, - { - "type": "integer", - "description": "系统厂商 id", - "name": "vendor_id", - "in": "formData" - }, - { - "type": "integer", - "description": "业务系统 id", - "name": "business_id", - "in": "formData" - } - ], - "responses": { - "200": { - "description": "{\"code\" : 200, \"msg\" : \"ok\"}", - "schema": { - "type": "string" - } - } - } - } - }, - "/v1/meta_databases/{id}/meta_tables": { - "get": { - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "meta_tables" - ], - "summary": "元数据列表", - "parameters": [ - { - "type": "string", - "description": "auth by /auth", - "name": "Authorization", - "in": "header", - "required": true - }, - { - "type": "integer", - "description": "数据源 ID", - "name": "id", - "in": "path", - "required": true - } - ], - "responses": { - "200": { - "description": "{\"code\" : 200, \"basic\" : {}, \"msg\": \"ok\" }", - "schema": { - "type": "string" - } - } - } - } - }, - "/v1/users": { - "get": { - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "users" - ], - "summary": "用户列表", - "responses": { - "200": { - "description": "{\"code\" : 200, \"data\" : {}, \"msg\" : \"ok\"}", - "schema": { - "type": "string" - } - } - } - }, - "post": { - "consumes": [ - "multipart/form-data" - ], - "produces": [ - "application/json" - ], - "tags": [ - "users" - ], - "summary": "新增用户", - "parameters": [ - { - "type": "string", - "description": "用户 用户名", - "name": "username", - "in": "formData", - "required": true - }, - { - "type": "string", - "description": "用户 密码", - "name": "password", - "in": "formData", - "required": true - }, - { - "type": "string", - "description": "用户 真实姓名", - "name": "nickname", - "in": "formData", - "required": true - } - ], - "responses": { - "200": { - "description": "{\"code\" : 200, data: {}, \"msg\" : \"ok\"}", + "description": "{\"success\":true,\"data\":{},\"msg\":\"创建成功\"}", "schema": { "type": "string" } @@ -602,11 +103,29 @@ var doc = `{ "name": "Authorization", "in": "header", "required": true + }, + { + "type": "string", + "description": "系统厂商名称", + "name": "name", + "in": "query" + }, + { + "type": "integer", + "description": "页码", + "name": "page", + "in": "query" + }, + { + "type": "integer", + "description": "每页数量", + "name": "page_size", + "in": "query" } ], "responses": { "200": { - "description": "{\"code\" : 200, \"data\" : {}, \"msg\" : \"ok\"}", + "description": "{\"success\":true,\"data\":{},\"msg\":\"获取成功\"}", "schema": { "type": "string" } @@ -633,22 +152,18 @@ var doc = `{ "required": true }, { - "type": "string", - "description": "系统厂商 名称", - "name": "name", - "in": "formData", - "required": true - }, - { - "type": "integer", - "description": "系统厂商 云端id", - "name": "c_id", - "in": "formData" + "description": "Vendor模型", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/basic.CreateVendorRequest" + } } ], "responses": { "200": { - "description": "{\"code\" : 200, \"msg\" : \"ok\"}", + "description": "{\"success\":true,\"data\":{},\"msg\":\"创建成功\"}", "schema": { "type": "string" } @@ -686,7 +201,7 @@ var doc = `{ ], "responses": { "200": { - "description": "{\"code\" : 200, \"msg\" : \"ok\"}", + "description": "{\"success\":true,\"data\":{},\"msg\":\"删除成功\"}", "schema": { "type": "string" } @@ -720,21 +235,18 @@ var doc = `{ "required": true }, { - "type": "string", - "description": "系统厂商 名称", - "name": "name", - "in": "formData" - }, - { - "type": "integer", - "description": "系统厂商 云端id", - "name": "c_id", - "in": "formData" + "description": "Vendor模型", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/basic.UpdateVendorRequest" + } } ], "responses": { "200": { - "description": "{\"code\" : 200, \"msg\" : \"ok\"}", + "description": "{\"success\":true,\"data\":{},\"msg\":\"更新成功\"}", "schema": { "type": "string" } @@ -742,6 +254,64 @@ var doc = `{ } } } + }, + "definitions": { + "basic.CreateVendorRequest": { + "type": "object", + "required": [ + "name" + ], + "properties": { + "name": { + "type": "string" + }, + "uuid": { + "type": "integer" + } + } + }, + "basic.UpdateVendorRequest": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "uuid": { + "type": "integer" + } + } + }, + "v1.AuthRequest": { + "type": "object", + "required": [ + "username" + ], + "properties": { + "password": { + "type": "string" + }, + "username": { + "type": "string" + } + } + }, + "v1.RegisterRequest": { + "type": "object", + "required": [ + "username" + ], + "properties": { + "nickname": { + "type": "string" + }, + "password": { + "type": "string" + }, + "username": { + "type": "string" + } + } + } } }` diff --git a/docs/swagger.json b/docs/swagger.json index 08554be..d63ac98 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -9,7 +9,7 @@ "basePath": "/api", "paths": { "/v1/auth": { - "get": { + "post": { "consumes": [ "application/json" ], @@ -19,18 +19,13 @@ "summary": "用户验证", "parameters": [ { - "type": "string", - "description": "用户 用户名", - "name": "username", - "in": "query", - "required": true - }, - { - "type": "string", - "description": "用户 密码", - "name": "password", - "in": "query", - "required": true + "description": "用户名,密码", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/v1.AuthRequest" + } } ], "responses": { @@ -43,523 +38,29 @@ } } }, - "/v1/businesses": { - "get": { - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "businesses" - ], - "summary": "业务系统列表", - "parameters": [ - { - "type": "string", - "description": "auth by /auth", - "name": "Authorization", - "in": "header", - "required": true - } - ], - "responses": { - "200": { - "description": "{\"code\" : 200, \"data\" : {}, \"msg\" : \"ok\"}", - "schema": { - "type": "string" - } - } - } - }, + "/v1/register": { "post": { "consumes": [ - "multipart/form-data" + "application/json" ], "produces": [ "application/json" ], - "tags": [ - "businesses" - ], - "summary": "新增业务系统", + "summary": "注册用户", "parameters": [ { - "type": "string", - "description": "auth by /auth", - "name": "Authorization", - "in": "header", - "required": true - }, - { - "type": "string", - "description": "业务系统 名称", - "name": "name", - "in": "formData", - "required": true - }, - { - "type": "string", - "description": "业务系统 描述", - "name": "desc", - "in": "formData" - }, - { - "type": "integer", - "description": "业务系统 云端id", - "name": "c_id", - "in": "formData" - } - ], - "responses": { - "200": { - "description": "{\"code\" : 200, \"msg\" : \"ok\"}", + "description": "用户名", + "name": "data", + "in": "body", + "required": true, "schema": { - "type": "string" + "$ref": "#/definitions/v1.RegisterRequest" } } - } - } - }, - "/v1/businesses/{id}": { - "delete": { - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "businesses" - ], - "summary": "删除业务系统", - "parameters": [ - { - "type": "string", - "description": "auth by /auth", - "name": "Authorization", - "in": "header", - "required": true - }, - { - "type": "integer", - "description": "业务系统 ID", - "name": "id", - "in": "path", - "required": true - } ], "responses": { "200": { - "description": "{\"code\" : 200, \"msg\" : \"ok\"}", - "schema": { - "type": "string" - } - } - } - }, - "patch": { - "consumes": [ - "multipart/form-data" - ], - "produces": [ - "application/json" - ], - "tags": [ - "businesses" - ], - "summary": "修改业务系统", - "parameters": [ - { - "type": "string", - "description": "auth by /auth", - "name": "Authorization", - "in": "header", - "required": true - }, - { - "type": "integer", - "description": "业务系统 ID", - "name": "id", - "in": "path", - "required": true - }, - { - "type": "string", - "description": "业务系统 名称", - "name": "name", - "in": "formData" - }, - { - "type": "string", - "description": "业务系统 描述", - "name": "desc", - "in": "formData" - }, - { - "type": "string", - "description": "业务系统 云端id", - "name": "c_id", - "in": "formData" - } - ], - "responses": { - "200": { - "description": "{\"code\" : 200, \"msg\" : \"ok\"}", - "schema": { - "type": "string" - } - } - } - } - }, - "/v1/meta_databases": { - "get": { - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "meta_databases" - ], - "summary": "数据源列表", - "parameters": [ - { - "type": "string", - "description": "auth by /auth", - "name": "Authorization", - "in": "header", - "required": true - } - ], - "responses": { - "200": { - "description": "{\"code\" : 200, \"basic\" : {}, \"msg\": \"ok\" }", - "schema": { - "type": "string" - } - } - } - }, - "post": { - "consumes": [ - "multipart/form-data" - ], - "produces": [ - "application/json" - ], - "tags": [ - "meta_databases" - ], - "summary": "新增数据源", - "parameters": [ - { - "type": "string", - "description": "auth by /auth", - "name": "Authorization", - "in": "header", - "required": true - }, - { - "type": "string", - "description": "数据源 名称", - "name": "name", - "in": "formData", - "required": true - }, - { - "type": "string", - "description": "数据源 地址", - "name": "host", - "in": "formData", - "required": true - }, - { - "type": "integer", - "description": "数据源 端口号", - "name": "port", - "in": "formData", - "required": true - }, - { - "type": "string", - "description": "数据源 数据库名称", - "name": "db_name", - "in": "formData", - "required": true - }, - { - "type": "string", - "description": "数据源 用户名", - "name": "username", - "in": "formData", - "required": true - }, - { - "type": "string", - "description": "数据源 密码", - "name": "password", - "in": "formData", - "required": true - }, - { - "type": "string", - "description": "数据源 备注", - "name": "comment", - "in": "formData" - }, - { - "type": "integer", - "description": "系统厂商 id", - "name": "vendor_id", - "in": "formData", - "required": true - }, - { - "type": "integer", - "description": "业务系统 id", - "name": "business_id", - "in": "formData", - "required": true - } - ], - "responses": { - "200": { - "description": "{\"code\": 200, data: {}, \"msg\" : \"ok\"}", - "schema": { - "type": "string" - } - } - } - } - }, - "/v1/meta_databases/{id}": { - "delete": { - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "meta_databases" - ], - "summary": "删除数据源", - "parameters": [ - { - "type": "string", - "description": "auth by /auth", - "name": "Authorization", - "in": "header", - "required": true - }, - { - "type": "integer", - "description": "数据源 ID", - "name": "id", - "in": "path", - "required": true - } - ], - "responses": { - "200": { - "description": "{\"code\" : 200, \"msg\" : \"ok\"}", - "schema": { - "type": "string" - } - } - } - }, - "patch": { - "consumes": [ - "multipart/form-data" - ], - "produces": [ - "application/json" - ], - "tags": [ - "meta_databases" - ], - "summary": "修改数据源", - "parameters": [ - { - "type": "string", - "description": "auth by /auth", - "name": "Authorization", - "in": "header", - "required": true - }, - { - "type": "integer", - "description": "数据源 ID", - "name": "id", - "in": "path", - "required": true - }, - { - "type": "string", - "description": "数据源 名称", - "name": "name", - "in": "formData" - }, - { - "type": "string", - "description": "数据源 地址", - "name": "host", - "in": "formData" - }, - { - "type": "integer", - "description": "数据源 端口号", - "name": "port", - "in": "formData" - }, - { - "type": "string", - "description": "数据源 数据库名称", - "name": "db_name", - "in": "formData" - }, - { - "type": "string", - "description": "数据源 用户名", - "name": "username", - "in": "formData" - }, - { - "type": "string", - "description": "数据源 密码", - "name": "password", - "in": "formData" - }, - { - "type": "string", - "description": "数据源 备注", - "name": "comment", - "in": "formData" - }, - { - "type": "integer", - "description": "系统厂商 id", - "name": "vendor_id", - "in": "formData" - }, - { - "type": "integer", - "description": "业务系统 id", - "name": "business_id", - "in": "formData" - } - ], - "responses": { - "200": { - "description": "{\"code\" : 200, \"msg\" : \"ok\"}", - "schema": { - "type": "string" - } - } - } - } - }, - "/v1/meta_databases/{id}/meta_tables": { - "get": { - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "meta_tables" - ], - "summary": "元数据列表", - "parameters": [ - { - "type": "string", - "description": "auth by /auth", - "name": "Authorization", - "in": "header", - "required": true - }, - { - "type": "integer", - "description": "数据源 ID", - "name": "id", - "in": "path", - "required": true - } - ], - "responses": { - "200": { - "description": "{\"code\" : 200, \"basic\" : {}, \"msg\": \"ok\" }", - "schema": { - "type": "string" - } - } - } - } - }, - "/v1/users": { - "get": { - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "users" - ], - "summary": "用户列表", - "responses": { - "200": { - "description": "{\"code\" : 200, \"data\" : {}, \"msg\" : \"ok\"}", - "schema": { - "type": "string" - } - } - } - }, - "post": { - "consumes": [ - "multipart/form-data" - ], - "produces": [ - "application/json" - ], - "tags": [ - "users" - ], - "summary": "新增用户", - "parameters": [ - { - "type": "string", - "description": "用户 用户名", - "name": "username", - "in": "formData", - "required": true - }, - { - "type": "string", - "description": "用户 密码", - "name": "password", - "in": "formData", - "required": true - }, - { - "type": "string", - "description": "用户 真实姓名", - "name": "nickname", - "in": "formData", - "required": true - } - ], - "responses": { - "200": { - "description": "{\"code\" : 200, data: {}, \"msg\" : \"ok\"}", + "description": "{\"success\":true,\"data\":{},\"msg\":\"创建成功\"}", "schema": { "type": "string" } @@ -586,11 +87,29 @@ "name": "Authorization", "in": "header", "required": true + }, + { + "type": "string", + "description": "系统厂商名称", + "name": "name", + "in": "query" + }, + { + "type": "integer", + "description": "页码", + "name": "page", + "in": "query" + }, + { + "type": "integer", + "description": "每页数量", + "name": "page_size", + "in": "query" } ], "responses": { "200": { - "description": "{\"code\" : 200, \"data\" : {}, \"msg\" : \"ok\"}", + "description": "{\"success\":true,\"data\":{},\"msg\":\"获取成功\"}", "schema": { "type": "string" } @@ -617,22 +136,18 @@ "required": true }, { - "type": "string", - "description": "系统厂商 名称", - "name": "name", - "in": "formData", - "required": true - }, - { - "type": "integer", - "description": "系统厂商 云端id", - "name": "c_id", - "in": "formData" + "description": "Vendor模型", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/basic.CreateVendorRequest" + } } ], "responses": { "200": { - "description": "{\"code\" : 200, \"msg\" : \"ok\"}", + "description": "{\"success\":true,\"data\":{},\"msg\":\"创建成功\"}", "schema": { "type": "string" } @@ -670,7 +185,7 @@ ], "responses": { "200": { - "description": "{\"code\" : 200, \"msg\" : \"ok\"}", + "description": "{\"success\":true,\"data\":{},\"msg\":\"删除成功\"}", "schema": { "type": "string" } @@ -704,21 +219,18 @@ "required": true }, { - "type": "string", - "description": "系统厂商 名称", - "name": "name", - "in": "formData" - }, - { - "type": "integer", - "description": "系统厂商 云端id", - "name": "c_id", - "in": "formData" + "description": "Vendor模型", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/basic.UpdateVendorRequest" + } } ], "responses": { "200": { - "description": "{\"code\" : 200, \"msg\" : \"ok\"}", + "description": "{\"success\":true,\"data\":{},\"msg\":\"更新成功\"}", "schema": { "type": "string" } @@ -726,5 +238,63 @@ } } } + }, + "definitions": { + "basic.CreateVendorRequest": { + "type": "object", + "required": [ + "name" + ], + "properties": { + "name": { + "type": "string" + }, + "uuid": { + "type": "integer" + } + } + }, + "basic.UpdateVendorRequest": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "uuid": { + "type": "integer" + } + } + }, + "v1.AuthRequest": { + "type": "object", + "required": [ + "username" + ], + "properties": { + "password": { + "type": "string" + }, + "username": { + "type": "string" + } + } + }, + "v1.RegisterRequest": { + "type": "object", + "required": [ + "username" + ], + "properties": { + "nickname": { + "type": "string" + }, + "password": { + "type": "string" + }, + "username": { + "type": "string" + } + } + } } } \ No newline at end of file diff --git a/docs/swagger.yaml b/docs/swagger.yaml index a4499d7..5d93389 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1,4 +1,41 @@ basePath: /api +definitions: + basic.CreateVendorRequest: + properties: + name: + type: string + uuid: + type: integer + required: + - name + type: object + basic.UpdateVendorRequest: + properties: + name: + type: string + uuid: + type: integer + type: object + v1.AuthRequest: + properties: + password: + type: string + username: + type: string + required: + - username + type: object + v1.RegisterRequest: + properties: + nickname: + type: string + password: + type: string + username: + type: string + required: + - username + type: object info: contact: {} description: This is a data_govern use golang @@ -6,20 +43,16 @@ info: version: "1.0" paths: /v1/auth: - get: + post: consumes: - application/json parameters: - - description: 用户 用户名 - in: query - name: username + - description: 用户名,密码 + in: body + name: data required: true - type: string - - description: 用户 密码 - in: query - name: password - required: true - type: string + schema: + $ref: '#/definitions/v1.AuthRequest' produces: - application/json responses: @@ -28,355 +61,25 @@ paths: schema: type: string summary: 用户验证 - /v1/businesses: - get: - consumes: - - application/json - parameters: - - description: auth by /auth - in: header - name: Authorization - required: true - type: string - produces: - - application/json - responses: - "200": - description: '{"code" : 200, "data" : {}, "msg" : "ok"}' - schema: - type: string - summary: 业务系统列表 - tags: - - businesses + /v1/register: post: - consumes: - - multipart/form-data - parameters: - - description: auth by /auth - in: header - name: Authorization - required: true - type: string - - description: 业务系统 名称 - in: formData - name: name - required: true - type: string - - description: 业务系统 描述 - in: formData - name: desc - type: string - - description: 业务系统 云端id - in: formData - name: c_id - type: integer - produces: - - application/json - responses: - "200": - description: '{"code" : 200, "msg" : "ok"}' - schema: - type: string - summary: 新增业务系统 - tags: - - businesses - /v1/businesses/{id}: - delete: consumes: - application/json parameters: - - description: auth by /auth - in: header - name: Authorization + - description: 用户名 + in: body + name: data required: true - type: string - - description: 业务系统 ID - in: path - name: id - required: true - type: integer + schema: + $ref: '#/definitions/v1.RegisterRequest' produces: - application/json responses: "200": - description: '{"code" : 200, "msg" : "ok"}' + description: '{"success":true,"data":{},"msg":"创建成功"}' schema: type: string - summary: 删除业务系统 - tags: - - businesses - patch: - consumes: - - multipart/form-data - parameters: - - description: auth by /auth - in: header - name: Authorization - required: true - type: string - - description: 业务系统 ID - in: path - name: id - required: true - type: integer - - description: 业务系统 名称 - in: formData - name: name - type: string - - description: 业务系统 描述 - in: formData - name: desc - type: string - - description: 业务系统 云端id - in: formData - name: c_id - type: string - produces: - - application/json - responses: - "200": - description: '{"code" : 200, "msg" : "ok"}' - schema: - type: string - summary: 修改业务系统 - tags: - - businesses - /v1/meta_databases: - get: - consumes: - - application/json - parameters: - - description: auth by /auth - in: header - name: Authorization - required: true - type: string - produces: - - application/json - responses: - "200": - description: '{"code" : 200, "basic" : {}, "msg": "ok" }' - schema: - type: string - summary: 数据源列表 - tags: - - meta_databases - post: - consumes: - - multipart/form-data - parameters: - - description: auth by /auth - in: header - name: Authorization - required: true - type: string - - description: 数据源 名称 - in: formData - name: name - required: true - type: string - - description: 数据源 地址 - in: formData - name: host - required: true - type: string - - description: 数据源 端口号 - in: formData - name: port - required: true - type: integer - - description: 数据源 数据库名称 - in: formData - name: db_name - required: true - type: string - - description: 数据源 用户名 - in: formData - name: username - required: true - type: string - - description: 数据源 密码 - in: formData - name: password - required: true - type: string - - description: 数据源 备注 - in: formData - name: comment - type: string - - description: 系统厂商 id - in: formData - name: vendor_id - required: true - type: integer - - description: 业务系统 id - in: formData - name: business_id - required: true - type: integer - produces: - - application/json - responses: - "200": - description: '{"code": 200, data: {}, "msg" : "ok"}' - schema: - type: string - summary: 新增数据源 - tags: - - meta_databases - /v1/meta_databases/{id}: - delete: - consumes: - - application/json - parameters: - - description: auth by /auth - in: header - name: Authorization - required: true - type: string - - description: 数据源 ID - in: path - name: id - required: true - type: integer - produces: - - application/json - responses: - "200": - description: '{"code" : 200, "msg" : "ok"}' - schema: - type: string - summary: 删除数据源 - tags: - - meta_databases - patch: - consumes: - - multipart/form-data - parameters: - - description: auth by /auth - in: header - name: Authorization - required: true - type: string - - description: 数据源 ID - in: path - name: id - required: true - type: integer - - description: 数据源 名称 - in: formData - name: name - type: string - - description: 数据源 地址 - in: formData - name: host - type: string - - description: 数据源 端口号 - in: formData - name: port - type: integer - - description: 数据源 数据库名称 - in: formData - name: db_name - type: string - - description: 数据源 用户名 - in: formData - name: username - type: string - - description: 数据源 密码 - in: formData - name: password - type: string - - description: 数据源 备注 - in: formData - name: comment - type: string - - description: 系统厂商 id - in: formData - name: vendor_id - type: integer - - description: 业务系统 id - in: formData - name: business_id - type: integer - produces: - - application/json - responses: - "200": - description: '{"code" : 200, "msg" : "ok"}' - schema: - type: string - summary: 修改数据源 - tags: - - meta_databases - /v1/meta_databases/{id}/meta_tables: - get: - consumes: - - application/json - parameters: - - description: auth by /auth - in: header - name: Authorization - required: true - type: string - - description: 数据源 ID - in: path - name: id - required: true - type: integer - produces: - - application/json - responses: - "200": - description: '{"code" : 200, "basic" : {}, "msg": "ok" }' - schema: - type: string - summary: 元数据列表 - tags: - - meta_tables - /v1/users: - get: - consumes: - - application/json - produces: - - application/json - responses: - "200": - description: '{"code" : 200, "data" : {}, "msg" : "ok"}' - schema: - type: string - summary: 用户列表 - tags: - - users - post: - consumes: - - multipart/form-data - parameters: - - description: 用户 用户名 - in: formData - name: username - required: true - type: string - - description: 用户 密码 - in: formData - name: password - required: true - type: string - - description: 用户 真实姓名 - in: formData - name: nickname - required: true - type: string - produces: - - application/json - responses: - "200": - description: '{"code" : 200, data: {}, "msg" : "ok"}' - schema: - type: string - summary: 新增用户 - tags: - - users + summary: 注册用户 /v1/vendors: get: consumes: @@ -387,11 +90,23 @@ paths: name: Authorization required: true type: string + - description: 系统厂商名称 + in: query + name: name + type: string + - description: 页码 + in: query + name: page + type: integer + - description: 每页数量 + in: query + name: page_size + type: integer produces: - application/json responses: "200": - description: '{"code" : 200, "data" : {}, "msg" : "ok"}' + description: '{"success":true,"data":{},"msg":"获取成功"}' schema: type: string summary: 系统厂商列表 @@ -406,20 +121,17 @@ paths: name: Authorization required: true type: string - - description: 系统厂商 名称 - in: formData - name: name + - description: Vendor模型 + in: body + name: data required: true - type: string - - description: 系统厂商 云端id - in: formData - name: c_id - type: integer + schema: + $ref: '#/definitions/basic.CreateVendorRequest' produces: - application/json responses: "200": - description: '{"code" : 200, "msg" : "ok"}' + description: '{"success":true,"data":{},"msg":"创建成功"}' schema: type: string summary: 新增系统厂商 @@ -444,7 +156,7 @@ paths: - application/json responses: "200": - description: '{"code" : 200, "msg" : "ok"}' + description: '{"success":true,"data":{},"msg":"删除成功"}' schema: type: string summary: 删除系统厂商 @@ -464,19 +176,17 @@ paths: name: id required: true type: integer - - description: 系统厂商 名称 - in: formData - name: name - type: string - - description: 系统厂商 云端id - in: formData - name: c_id - type: integer + - description: Vendor模型 + in: body + name: data + required: true + schema: + $ref: '#/definitions/basic.UpdateVendorRequest' produces: - application/json responses: "200": - description: '{"code" : 200, "msg" : "ok"}' + description: '{"success":true,"data":{},"msg":"更新成功"}' schema: type: string summary: 修改系统厂商 diff --git a/middleware/jwt.go b/middleware/jwt.go index e777361..2bc91c0 100644 --- a/middleware/jwt.go +++ b/middleware/jwt.go @@ -1,7 +1,7 @@ /* * @Date: 2021-03-21 19:54:57 * @LastEditors: viletyy - * @LastEditTime: 2021-03-23 00:50:00 + * @LastEditTime: 2021-03-24 11:05:16 * @FilePath: /potato/middleware/jwt.go */ package middleware @@ -24,6 +24,7 @@ func JWT() gin.HandlerFunc { "error": "请求参数错误", }) c.Abort() + return } else { claims, err := utils.ParseToken(token) if err != nil { @@ -31,11 +32,13 @@ func JWT() gin.HandlerFunc { "error": "token验证失败", }) c.Abort() + return } else if time.Now().Unix() > claims.ExpiresAt { c.JSON(http.StatusBadRequest, gin.H{ "error": "token已超时", }) c.Abort() + return } if claims != nil { userId := claims.UserId @@ -46,12 +49,14 @@ func JWT() gin.HandlerFunc { "error": "token鉴权失败", }) c.Abort() + return } } else { c.JSON(http.StatusBadRequest, gin.H{ "error": "token鉴权失败", }) c.Abort() + return } } diff --git a/models/basic/business.go b/models/basic/business.go index d313717..53958c3 100644 --- a/models/basic/business.go +++ b/models/basic/business.go @@ -19,7 +19,7 @@ type BusinessSearch struct { type Business struct { global.Model - Name string `json:"name" binding:"required"` + Name string `json:"name"` Description string `json:"description"` Uuid int `json:"uuid"` } diff --git a/models/basic/meta_database.go b/models/basic/meta_database.go index b697fa4..bb968c5 100644 --- a/models/basic/meta_database.go +++ b/models/basic/meta_database.go @@ -1,7 +1,7 @@ /* * @Date: 2021-03-21 19:54:57 * @LastEditors: viletyy - * @LastEditTime: 2021-03-22 23:54:39 + * @LastEditTime: 2021-03-24 11:10:19 * @FilePath: /potato/models/basic/meta_database.go */ package basic @@ -20,18 +20,18 @@ type MetaDatabaseSearch struct { type MetaDatabase struct { global.Model - Name string `json:"name" binding:"required"` - Adapter string `json:"adapter" binding:"required"` - Host string `json:"host" binding:"required"` - Port string `json:"port" binding:"required"` - DbName string `json:"db_name" binding:"required"` - Username string `json:"username" binding:"required"` - Password string `json:"password" binding:"required"` + Name string `json:"name"` + Adapter string `json:"adapter"` + Host string `json:"host"` + Port string `json:"port"` + DbName string `json:"db_name"` + Username string `json:"username"` + Password string `json:"password"` LastConnectTime time.Time `json:"last_connect_time"` Usable bool `json:"usable"` Comment string `json:"comment"` - VendorId int64 `json:"vendor_id" binding:"required"` - BusinessId int64 `json:"business_id" binding:"required"` + VendorId int64 `json:"vendor_id"` + BusinessId int64 `json:"business_id"` Vendor Vendor Business Business } diff --git a/models/basic/vendor.go b/models/basic/vendor.go index 068f54f..a745540 100644 --- a/models/basic/vendor.go +++ b/models/basic/vendor.go @@ -1,7 +1,7 @@ /* * @Date: 2021-03-21 19:54:57 * @LastEditors: viletyy - * @LastEditTime: 2021-03-22 23:52:13 + * @LastEditTime: 2021-03-24 11:16:27 * @FilePath: /potato/models/basic/vendor.go */ package basic @@ -19,15 +19,15 @@ type VendorSearch struct { type Vendor struct { global.Model - Name string `json:"name" binding:"require"` + Name string `json:"name"` Uuid int `json:"uuid"` } func GetVendors(search *VendorSearch) (searchResult utils.SearchResult, err error) { var vendors []Vendor offset := search.PageInfo.PageSize * (search.PageInfo.Page - 1) - limit := search.PageInfo.Page - db := global.GO_DB.Where(search.Vendor) + limit := search.PageInfo.PageSize + db := global.GO_DB.Model(&Vendor{}).Where(search.Vendor) err = db.Count(&searchResult.Total).Error if err != nil { return @@ -42,7 +42,7 @@ func GetVendors(search *VendorSearch) (searchResult utils.SearchResult, err erro return } -func GetVendorById(id int) (vendor Vendor, err error) { +func GetVendorById(id interface{}) (vendor Vendor, err error) { err = global.GO_DB.Where("id = ?", id).First(&vendor).Error return } @@ -52,12 +52,12 @@ func GetVendorByName(name string) (vendor Vendor, err error) { return } -func GetVendorByUuid(uuid int64) (vendor Vendor, err error) { +func GetVendorByUuid(uuid interface{}) (vendor Vendor, err error) { err = global.GO_DB.Where("uuid = ?", uuid).First(&vendor).Error return } -func ExistVendorById(id int) bool { +func ExistVendorById(id interface{}) bool { var vendor Vendor global.GO_DB.Where("id = ?", id).First(&vendor) @@ -71,7 +71,7 @@ func ExistVendorByName(name string) bool { return vendor.ID > 0 } -func ExistVendorByUuid(uuid int64) bool { +func ExistVendorByUuid(uuid interface{}) bool { var vendor Vendor global.GO_DB.Where("uuid = ?", uuid).First(&vendor) diff --git a/routers/basic.go b/routers/basic.go index 1d0182b..80daa1e 100644 --- a/routers/basic.go +++ b/routers/basic.go @@ -1,7 +1,7 @@ /* * @Date: 2021-03-21 19:54:57 * @LastEditors: viletyy - * @LastEditTime: 2021-03-23 00:50:38 + * @LastEditTime: 2021-03-24 11:23:01 * @FilePath: /potato/routers/basic.go */ package routers @@ -22,7 +22,7 @@ func V1InitBasicRouter() { vendors := V1RouterGroup.Group("/vendors") { vendors.GET("", basic.GetVendors) - vendors.POST("", basic.AddVendor) + vendors.POST("", basic.CreateVendor) vendors.PATCH("/:id", basic.UpdateVendor) vendors.DELETE("/:id", basic.DeleteVendor) } diff --git a/routers/router.go b/routers/router.go index ffc45b3..f0ee600 100644 --- a/routers/router.go +++ b/routers/router.go @@ -1,7 +1,7 @@ /* * @Date: 2021-03-21 19:54:57 * @LastEditors: viletyy - * @LastEditTime: 2021-03-23 00:50:54 + * @LastEditTime: 2021-03-23 14:39:19 * @FilePath: /potato/routers/router.go */ package routers @@ -32,7 +32,8 @@ func InitRouter() *gin.Engine { Engine.Use(middleware.CORS()) - Engine.GET("/api/v1/auth", v1.GetUserAuth) + Engine.POST("/api/v1/auth", v1.Auth) + Engine.POST("/api/v1/register", v1.Register) Engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) V1InitModule() @@ -42,7 +43,5 @@ func InitRouter() *gin.Engine { func V1InitModule() { V1RouterGroup.Use(middleware.JWT()) - users := V1RouterGroup.Group("users") - users.POST("", v1.AddUser) V1InitBasicRouter() } diff --git a/utils/helper.go b/utils/helper.go new file mode 100644 index 0000000..9b0c7c9 --- /dev/null +++ b/utils/helper.go @@ -0,0 +1,35 @@ +/* + * @Date: 2021-03-24 10:18:40 + * @LastEditors: viletyy + * @LastEditTime: 2021-03-24 10:30:27 + * @FilePath: /potato/utils/helper.go + */ +package utils + +import "strconv" + +func ToString(i interface{}) string { + switch i.(type) { + case string: + return i.(string) + case int: + return strconv.Itoa(i.(int)) + case int64: + return strconv.FormatInt(i.(int64), 10) + } + return "" +} + +func ToInt(i interface{}) int { + switch i.(type) { + case string: + result, err := strconv.Atoi(i.(string)) + if err != nil { + return 0 + } + return result + case int64: + return int(i.(int64)) + } + return 0 +} diff --git a/utils/paginator.go b/utils/paginator.go new file mode 100644 index 0000000..e38595f --- /dev/null +++ b/utils/paginator.go @@ -0,0 +1,15 @@ +/* + * @Date: 2021-03-24 10:12:24 + * @LastEditors: viletyy + * @LastEditTime: 2021-03-24 10:30:58 + * @FilePath: /potato/utils/paginator.go + */ +package utils + +import "github.com/gin-gonic/gin" + +func GetPageInfo(c *gin.Context) (page, pageSize int) { + page = ToInt(c.DefaultQuery("page", "1")) + pageSize = ToInt(c.DefaultQuery("page_size", "10")) + return +}