[ADD]第一个接口
This commit is contained in:
parent
d81d3b6ca9
commit
a7aa671660
|
@ -5,7 +5,7 @@ PAGE_SIZE = 10
|
|||
JWT_SECRET = 23347$040412
|
||||
|
||||
[server]
|
||||
HTTP_PORT = 8001
|
||||
HTTP_PORT = 8002
|
||||
READ_TIMEOUT = 60
|
||||
WRITE_TIMEOUT = 60
|
||||
|
||||
|
|
|
@ -0,0 +1,151 @@
|
|||
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
|
||||
// This file was generated by swaggo/swag
|
||||
|
||||
package docs
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"github.com/alecthomas/template"
|
||||
"github.com/swaggo/swag"
|
||||
)
|
||||
|
||||
var doc = `{
|
||||
"schemes": {{ marshal .Schemes }},
|
||||
"swagger": "2.0",
|
||||
"info": {
|
||||
"description": "{{.Description}}",
|
||||
"title": "{{.Title}}",
|
||||
"contact": {},
|
||||
"license": {},
|
||||
"version": "{{.Version}}"
|
||||
},
|
||||
"host": "{{.Host}}",
|
||||
"basePath": "{{.BasePath}}",
|
||||
"paths": {
|
||||
"/admin/login": {
|
||||
"post": {
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"summary": "后台登录",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "用户名",
|
||||
"name": "username",
|
||||
"in": "query",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "密码",
|
||||
"name": "password",
|
||||
"in": "query",
|
||||
"required": true
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "{\"code\":200,\"data\":{},\"msg\":\"ok\"}",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/admin/v1/employees": {
|
||||
"get": {
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"summary": "获取员工列表",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "Token",
|
||||
"name": "token",
|
||||
"in": "query",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "Department",
|
||||
"name": "department",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "Position",
|
||||
"name": "position",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"description": "State",
|
||||
"name": "state",
|
||||
"in": "query"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "{\"code\": 200, \"data\": {}, \"msg\":\"ok\"}",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`
|
||||
|
||||
type swaggerInfo struct {
|
||||
Version string
|
||||
Host string
|
||||
BasePath string
|
||||
Schemes []string
|
||||
Title string
|
||||
Description string
|
||||
}
|
||||
|
||||
// SwaggerInfo holds exported Swagger Info so clients can modify it
|
||||
var SwaggerInfo = swaggerInfo{
|
||||
Version: "",
|
||||
Host: "",
|
||||
BasePath: "",
|
||||
Schemes: []string{},
|
||||
Title: "",
|
||||
Description: "",
|
||||
}
|
||||
|
||||
type s struct{}
|
||||
|
||||
func (s *s) ReadDoc() string {
|
||||
sInfo := SwaggerInfo
|
||||
sInfo.Description = strings.Replace(sInfo.Description, "\n", "\\n", -1)
|
||||
|
||||
t, err := template.New("swagger_info").Funcs(template.FuncMap{
|
||||
"marshal": func(v interface{}) string {
|
||||
a, _ := json.Marshal(v)
|
||||
return string(a)
|
||||
},
|
||||
}).Parse(doc)
|
||||
if err != nil {
|
||||
return doc
|
||||
}
|
||||
|
||||
var tpl bytes.Buffer
|
||||
if err := t.Execute(&tpl, sInfo); err != nil {
|
||||
return doc
|
||||
}
|
||||
|
||||
return tpl.String()
|
||||
}
|
||||
|
||||
func init() {
|
||||
swag.Register(swag.Name, &s{})
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
{
|
||||
"swagger": "2.0",
|
||||
"info": {
|
||||
"contact": {},
|
||||
"license": {}
|
||||
},
|
||||
"paths": {
|
||||
"/admin/login": {
|
||||
"post": {
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"summary": "后台登录",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "用户名",
|
||||
"name": "username",
|
||||
"in": "query",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "密码",
|
||||
"name": "password",
|
||||
"in": "query",
|
||||
"required": true
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "{\"code\":200,\"data\":{},\"msg\":\"ok\"}",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/admin/v1/employees": {
|
||||
"get": {
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"summary": "获取员工列表",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "Token",
|
||||
"name": "token",
|
||||
"in": "query",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "Department",
|
||||
"name": "department",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "Position",
|
||||
"name": "position",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"description": "State",
|
||||
"name": "state",
|
||||
"in": "query"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "{\"code\": 200, \"data\": {}, \"msg\":\"ok\"}",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
info:
|
||||
contact: {}
|
||||
license: {}
|
||||
paths:
|
||||
/admin/login:
|
||||
post:
|
||||
parameters:
|
||||
- description: 用户名
|
||||
in: query
|
||||
name: username
|
||||
required: true
|
||||
type: string
|
||||
- description: 密码
|
||||
in: query
|
||||
name: password
|
||||
required: true
|
||||
type: string
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: '{"code":200,"data":{},"msg":"ok"}'
|
||||
schema:
|
||||
type: string
|
||||
summary: 后台登录
|
||||
/admin/v1/employees:
|
||||
get:
|
||||
parameters:
|
||||
- description: Token
|
||||
in: query
|
||||
name: token
|
||||
required: true
|
||||
type: string
|
||||
- description: Department
|
||||
in: query
|
||||
name: department
|
||||
type: string
|
||||
- description: Position
|
||||
in: query
|
||||
name: position
|
||||
type: string
|
||||
- description: State
|
||||
in: query
|
||||
name: state
|
||||
type: integer
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: '{"code": 200, "data": {}, "msg":"ok"}'
|
||||
schema:
|
||||
type: string
|
||||
summary: 获取员工列表
|
||||
swagger: "2.0"
|
2
go.mod
2
go.mod
|
@ -3,6 +3,7 @@ module github.com/go-pripro/shop
|
|||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
|
||||
github.com/astaxie/beego v1.12.2
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
||||
github.com/gin-gonic/gin v1.6.3
|
||||
|
@ -12,6 +13,7 @@ require (
|
|||
github.com/lib/pq v1.2.0 // indirect
|
||||
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect
|
||||
github.com/swaggo/gin-swagger v1.2.0
|
||||
github.com/swaggo/swag v1.5.1
|
||||
github.com/ugorji/go v1.1.8 // indirect
|
||||
github.com/unknwon/com v1.0.1
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
|
||||
|
|
|
@ -6,6 +6,7 @@ type Employee struct {
|
|||
Password string `json:"password"`
|
||||
Department string `json:"department"`
|
||||
Position string `json:"position"`
|
||||
State int `json:"state"`
|
||||
}
|
||||
|
||||
func CheckEmployee(username, password string) bool {
|
||||
|
@ -15,4 +16,16 @@ func CheckEmployee(username, password string) bool {
|
|||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func GetEmployeeTotal(maps interface{}) (count int) {
|
||||
db.Model(&Employee{}).Where(maps).Count(&count)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func GetEmployees(pageNum int, pageSize int, maps interface{}) (employees []Employee) {
|
||||
db.Where(maps).Offset(pageNum).Limit(pageSize).Find(&employees)
|
||||
|
||||
return
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package routers
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-pripro/shop/middleware/jwt"
|
||||
"github.com/go-pripro/shop/routers/admin"
|
||||
v1 "github.com/go-pripro/shop/routers/admin/v1"
|
||||
)
|
||||
|
||||
func InitAdmin(r *gin.Engine) *gin.Engine {
|
||||
|
||||
r.GET("/admin/login", admin.GetLogin)
|
||||
|
||||
adminv1 := r.Group("/admin/v1")
|
||||
adminv1.Use(jwt.JWT())
|
||||
{
|
||||
// 获取员工列表
|
||||
adminv1.GET("/employees", v1.GetEmployees)
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
|
@ -15,7 +15,13 @@ type Employee struct {
|
|||
Password string `valid:"Required; MaxSize(50)"`
|
||||
}
|
||||
|
||||
func GetEmployee(c *gin.Context) {
|
||||
// @Summary 后台登录
|
||||
// @Produce json
|
||||
// @Param username query string true "用户名"
|
||||
// @Param password query string true "密码"
|
||||
// @Success 200 {string} json "{"code":200,"data":{},"msg":"ok"}"
|
||||
// @Router /admin/login [post]
|
||||
func GetLogin(c *gin.Context) {
|
||||
username := c.Query("username")
|
||||
password := c.Query("password")
|
||||
valid := validation.Validation{}
|
|
@ -0,0 +1,51 @@
|
|||
package v1
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego/validation"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-pripro/shop/models"
|
||||
"github.com/go-pripro/shop/pkg/e"
|
||||
"github.com/go-pripro/shop/pkg/logging"
|
||||
"github.com/go-pripro/shop/pkg/setting"
|
||||
"github.com/go-pripro/shop/pkg/util"
|
||||
"github.com/unknwon/com"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// @Summary 获取员工列表
|
||||
// @Produce json
|
||||
// @Param token query string true "Token"
|
||||
// @Param department query string false "Department"
|
||||
// @Param position query string false "Position"
|
||||
// @Param state query int false "State"
|
||||
// @Success 200 {string} json "{"code": 200, "data": {}, "msg":"ok"}"
|
||||
// @Router /admin/v1/employees [get]
|
||||
func GetEmployees(c *gin.Context) {
|
||||
data := make(map[string]interface{})
|
||||
maps := make(map[string]interface{})
|
||||
valid := validation.Validation{}
|
||||
var state int = -1
|
||||
if arg := c.Query("state"); arg != "" {
|
||||
state = com.StrTo(arg).MustInt()
|
||||
maps["state"] = state
|
||||
valid.Range(state, 0, 1, "state").Message("状态只允许0或1")
|
||||
}
|
||||
|
||||
code := e.INVALID_PARAMS
|
||||
|
||||
if ! valid.HasErrors() {
|
||||
code = e.SUCCESS
|
||||
data["lists"] = models.GetEmployees(util.GetPage(c), setting.PageSize, maps)
|
||||
data["total"] = models.GetEmployeeTotal(maps)
|
||||
} else {
|
||||
for _, err := range valid.Errors {
|
||||
logging.Info(err.Key, err.Message)
|
||||
//log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code" : code,
|
||||
"msg" : e.GetMsg(code),
|
||||
"data" : data,
|
||||
})
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
package routers
|
|
@ -2,8 +2,8 @@ package routers
|
|||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
_ "github.com/go-pripro/shop/docs"
|
||||
"github.com/go-pripro/shop/pkg/setting"
|
||||
"github.com/go-pripro/shop/routers/admin"
|
||||
ginSwagger "github.com/swaggo/gin-swagger"
|
||||
"github.com/swaggo/gin-swagger/swaggerFiles"
|
||||
)
|
||||
|
@ -30,7 +30,8 @@ func InitRouter() *gin.Engine {
|
|||
|
||||
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||
|
||||
r.GET("/login", admin.GetEmployee)
|
||||
// 初始化admin
|
||||
r = InitAdmin(r)
|
||||
|
||||
return r
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue