[ADD]查看员工信息
This commit is contained in:
parent
a7aa671660
commit
4acbcc8932
|
@ -0,0 +1,20 @@
|
||||||
|
# Created by .ignore support plugin (hsz.mobi)
|
||||||
|
### Go template
|
||||||
|
# Binaries for programs and plugins
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Test binary, built with `go test -c`
|
||||||
|
*.test
|
||||||
|
|
||||||
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
|
*.out
|
||||||
|
|
||||||
|
# Dependency directories (remove the comment below to include it)
|
||||||
|
# vendor/
|
||||||
|
go.sum
|
||||||
|
.idea/*
|
||||||
|
runtime/*
|
|
@ -11,9 +11,9 @@ WRITE_TIMEOUT = 60
|
||||||
|
|
||||||
[database]
|
[database]
|
||||||
TYPE = postgres
|
TYPE = postgres
|
||||||
USER = postgres
|
USER = virus
|
||||||
PASSWORD = a19960425
|
PASSWORD = a19960425
|
||||||
HOST = 0.0.0.0
|
HOST = 0.0.0.0
|
||||||
PORT = 5432
|
PORT = 5432
|
||||||
NAME = blog
|
NAME = shop
|
||||||
TABLE_PREFIX = blog_
|
TABLE_PREFIX = shop_
|
||||||
|
|
34
docs/docs.go
34
docs/docs.go
|
@ -26,7 +26,7 @@ var doc = `{
|
||||||
"basePath": "{{.BasePath}}",
|
"basePath": "{{.BasePath}}",
|
||||||
"paths": {
|
"paths": {
|
||||||
"/admin/login": {
|
"/admin/login": {
|
||||||
"post": {
|
"get": {
|
||||||
"produces": [
|
"produces": [
|
||||||
"application/json"
|
"application/json"
|
||||||
],
|
],
|
||||||
|
@ -99,6 +99,38 @@ var doc = `{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"/admin/v1/employees/{id}": {
|
||||||
|
"get": {
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"summary": "获取单个员工",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"type": "string",
|
||||||
|
"description": "Token",
|
||||||
|
"name": "token",
|
||||||
|
"in": "query",
|
||||||
|
"required": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "integer",
|
||||||
|
"description": "ID",
|
||||||
|
"name": "id",
|
||||||
|
"in": "path",
|
||||||
|
"required": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "{\"code\": 200, \"data\": {}, \"msg\": \"ok\"}",
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}`
|
}`
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
},
|
},
|
||||||
"paths": {
|
"paths": {
|
||||||
"/admin/login": {
|
"/admin/login": {
|
||||||
"post": {
|
"get": {
|
||||||
"produces": [
|
"produces": [
|
||||||
"application/json"
|
"application/json"
|
||||||
],
|
],
|
||||||
|
@ -79,6 +79,38 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"/admin/v1/employees/{id}": {
|
||||||
|
"get": {
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"summary": "获取单个员工",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"type": "string",
|
||||||
|
"description": "Token",
|
||||||
|
"name": "token",
|
||||||
|
"in": "query",
|
||||||
|
"required": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "integer",
|
||||||
|
"description": "ID",
|
||||||
|
"name": "id",
|
||||||
|
"in": "path",
|
||||||
|
"required": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "{\"code\": 200, \"data\": {}, \"msg\": \"ok\"}",
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,7 +3,7 @@ info:
|
||||||
license: {}
|
license: {}
|
||||||
paths:
|
paths:
|
||||||
/admin/login:
|
/admin/login:
|
||||||
post:
|
get:
|
||||||
parameters:
|
parameters:
|
||||||
- description: 用户名
|
- description: 用户名
|
||||||
in: query
|
in: query
|
||||||
|
@ -51,4 +51,25 @@ paths:
|
||||||
schema:
|
schema:
|
||||||
type: string
|
type: string
|
||||||
summary: 获取员工列表
|
summary: 获取员工列表
|
||||||
|
/admin/v1/employees/{id}:
|
||||||
|
get:
|
||||||
|
parameters:
|
||||||
|
- description: Token
|
||||||
|
in: query
|
||||||
|
name: token
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
- description: ID
|
||||||
|
in: path
|
||||||
|
name: id
|
||||||
|
required: true
|
||||||
|
type: integer
|
||||||
|
produces:
|
||||||
|
- application/json
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: '{"code": 200, "data": {}, "msg": "ok"}'
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
summary: 获取单个员工
|
||||||
swagger: "2.0"
|
swagger: "2.0"
|
||||||
|
|
11
go.mod
11
go.mod
|
@ -5,19 +5,28 @@ go 1.15
|
||||||
require (
|
require (
|
||||||
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
|
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
|
||||||
github.com/astaxie/beego v1.12.2
|
github.com/astaxie/beego v1.12.2
|
||||||
|
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
|
||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
||||||
github.com/gin-gonic/gin v1.6.3
|
github.com/gin-gonic/gin v1.6.3
|
||||||
github.com/go-ini/ini v1.61.0
|
github.com/go-ini/ini v1.61.0
|
||||||
|
github.com/go-openapi/spec v0.19.9 // indirect
|
||||||
|
github.com/go-openapi/swag v0.19.9 // indirect
|
||||||
github.com/go-playground/validator/v10 v10.3.0 // indirect
|
github.com/go-playground/validator/v10 v10.3.0 // indirect
|
||||||
github.com/jinzhu/gorm v1.9.16
|
github.com/jinzhu/gorm v1.9.16
|
||||||
github.com/lib/pq v1.2.0 // indirect
|
github.com/lib/pq v1.2.0 // indirect
|
||||||
|
github.com/mailru/easyjson v0.7.6 // indirect
|
||||||
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect
|
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect
|
||||||
github.com/swaggo/gin-swagger v1.2.0
|
github.com/swaggo/gin-swagger v1.2.0
|
||||||
github.com/swaggo/swag v1.5.1
|
github.com/swaggo/swag v1.6.7
|
||||||
github.com/ugorji/go v1.1.8 // indirect
|
github.com/ugorji/go v1.1.8 // indirect
|
||||||
github.com/unknwon/com v1.0.1
|
github.com/unknwon/com v1.0.1
|
||||||
|
github.com/urfave/cli v1.22.4 // indirect
|
||||||
|
github.com/urfave/cli/v2 v2.2.0 // indirect
|
||||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
|
||||||
|
golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect
|
||||||
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 // indirect
|
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 // indirect
|
||||||
|
golang.org/x/tools v0.0.0-20200921210052-fa0125251cc4 // indirect
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||||
google.golang.org/protobuf v1.25.0 // indirect
|
google.golang.org/protobuf v1.25.0 // indirect
|
||||||
gopkg.in/ini.v1 v1.61.0 // indirect
|
gopkg.in/ini.v1 v1.61.0 // indirect
|
||||||
|
|
|
@ -15,13 +15,13 @@ func JWT() gin.HandlerFunc {
|
||||||
code = e.SUCCESS
|
code = e.SUCCESS
|
||||||
token := c.Query("token")
|
token := c.Query("token")
|
||||||
if token == "" {
|
if token == "" {
|
||||||
code = e.INVALID_PARAMS
|
code = e.InvalidParams
|
||||||
} else {
|
} else {
|
||||||
claims, err := util.ParseToken(token)
|
claims, err := util.ParseToken(token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
code = e.ERROR_AUTH_CHECK_TOKEN_FAIL
|
code = e.ErrorAuthCheckTokenFail
|
||||||
} else if time.Now().Unix() > claims.ExpiresAt {
|
} else if time.Now().Unix() > claims.ExpiresAt {
|
||||||
code = e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT
|
code = e.ErrorAuthCheckTokenTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if code != e.SUCCESS {
|
if code != e.SUCCESS {
|
||||||
|
|
|
@ -1,23 +1,42 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type Employee struct {
|
type Employee struct {
|
||||||
ID int `gorm:"primary_key" json:"id"`
|
ID int `gorm:"primary_key" json:"id"`
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
Department string `json:"department"`
|
Department string `json:"department"`
|
||||||
Position string `json:"position"`
|
Position string `json:"position"`
|
||||||
State int `json:"state"`
|
State int `json:"state"`
|
||||||
|
CreatedOn int `json:"created_on"`
|
||||||
|
ModifiedOn int `json:"modified_on"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckEmployee(username, password string) bool {
|
func CheckEmployee(username, password string) bool {
|
||||||
var employee Employee
|
var employee Employee
|
||||||
db.Select("id").Where(Employee{Username: username, Password: password}).First(&employee)
|
db.Select("id").Where(Employee{Username: username, Password: password}).First(&employee)
|
||||||
|
|
||||||
if employee.ID > 0 {
|
if employee.ID > 0 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExistEmployeeByID(id int) bool {
|
||||||
|
var employee Employee
|
||||||
|
db.Select("id").Where("id = ?", id).First(&employee)
|
||||||
|
|
||||||
|
if employee.ID > 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func GetEmployeeTotal(maps interface{}) (count int) {
|
func GetEmployeeTotal(maps interface{}) (count int) {
|
||||||
db.Model(&Employee{}).Where(maps).Count(&count)
|
db.Model(&Employee{}).Where(maps).Count(&count)
|
||||||
|
|
||||||
|
@ -28,4 +47,32 @@ func GetEmployees(pageNum int, pageSize int, maps interface{}) (employees []Empl
|
||||||
db.Where(maps).Offset(pageNum).Limit(pageSize).Find(&employees)
|
db.Where(maps).Offset(pageNum).Limit(pageSize).Find(&employees)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetEmployee(id int) (employee Employee) {
|
||||||
|
db.Where("id = ?", id).First(&employee)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func EditEmployee(id int, data interface{}) bool {
|
||||||
|
db.Model(&Employee{}).Where("id = ?", id).Updates(data)
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddArticle(data map[string]interface{}) bool {
|
||||||
|
db.Create(&Employee{
|
||||||
|
Username: data["username"].(string),
|
||||||
|
Password: data["password"].(string),
|
||||||
|
Department: data["department"].(string),
|
||||||
|
Position: data["position"].(string),
|
||||||
|
State: data["state"].(int),
|
||||||
|
})
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (employee *Employee) BeforeUpdate(scope *gorm.Scope) error {
|
||||||
|
scope.SetColumn("ModifiedOn", time.Now().Unix())
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -1,16 +1,14 @@
|
||||||
package e
|
package e
|
||||||
|
|
||||||
const (
|
const (
|
||||||
SUCCESS = 200
|
SUCCESS = 200
|
||||||
ERROR = 500
|
ERROR = 500
|
||||||
INVALID_PARAMS = 400
|
InvalidParams = 400
|
||||||
|
ErrorAuthCheckTokenFail = 10001
|
||||||
|
ErrorAuthCheckTokenTimeout = 10002
|
||||||
|
ErrorAuthToken = 10003
|
||||||
|
ErrorAuth = 10004
|
||||||
|
|
||||||
ERROR_EXIST_TAG = 10001
|
ErrorExistEmployee = 20001
|
||||||
ERROR_NOT_EXIST_TAG = 10002
|
ErrorNotExistEmployee = 20002
|
||||||
ERROR_NOT_EXIST_ARTICLE = 10003
|
|
||||||
|
|
||||||
ERROR_AUTH_CHECK_TOKEN_FAIL = 20001
|
|
||||||
ERROR_AUTH_CHECK_TOKEN_TIMEOUT = 20002
|
|
||||||
ERROR_AUTH_TOKEN = 20003
|
|
||||||
ERROR_AUTH = 20004
|
|
||||||
)
|
)
|
||||||
|
|
19
pkg/e/msg.go
19
pkg/e/msg.go
|
@ -1,16 +1,15 @@
|
||||||
package e
|
package e
|
||||||
|
|
||||||
var MsgFlags = map[int]string{
|
var MsgFlags = map[int]string{
|
||||||
SUCCESS: "ok",
|
SUCCESS: "ok",
|
||||||
ERROR: "fail",
|
ERROR: "fail",
|
||||||
INVALID_PARAMS: "请求参数错误",
|
InvalidParams: "请求参数错误",
|
||||||
ERROR_EXIST_TAG: "已存在该标签名称",
|
ErrorAuthCheckTokenFail: "TOKEN鉴权失败",
|
||||||
ERROR_NOT_EXIST_TAG: "该标签不存在",
|
ErrorAuthCheckTokenTimeout: "TOKEN已超时",
|
||||||
ERROR_NOT_EXIST_ARTICLE: "该文章不存在",
|
ErrorAuthToken: "TOKEN生成失败",
|
||||||
ERROR_AUTH_CHECK_TOKEN_FAIL: "TOKEN鉴权失败",
|
ErrorAuth: "TOKEN错误",
|
||||||
ERROR_AUTH_CHECK_TOKEN_TIMEOUT: "TOKEN已超时",
|
ErrorExistEmployee: "已存在该员工用户名",
|
||||||
ERROR_AUTH_TOKEN: "TOKEN生成失败",
|
ErrorNotExistEmployee: "该员工不存在",
|
||||||
ERROR_AUTH: "TOKEN错误",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetMsg(code int) string {
|
func GetMsg(code int) string {
|
||||||
|
|
|
@ -16,6 +16,12 @@ func InitAdmin(r *gin.Engine) *gin.Engine {
|
||||||
{
|
{
|
||||||
// 获取员工列表
|
// 获取员工列表
|
||||||
adminv1.GET("/employees", v1.GetEmployees)
|
adminv1.GET("/employees", v1.GetEmployees)
|
||||||
|
// 获取指定员工列表
|
||||||
|
adminv1.GET("/employees/:id", v1.GetEmployee)
|
||||||
|
//// 新建员工
|
||||||
|
//adminv1.POST("/employees", v1.AddEmployee)
|
||||||
|
//// 更新指定员工
|
||||||
|
//adminv1.PUT("/employees/:id", v1.EditArticle)
|
||||||
}
|
}
|
||||||
|
|
||||||
return r
|
return r
|
||||||
|
|
|
@ -20,7 +20,7 @@ type Employee struct {
|
||||||
// @Param username query string true "用户名"
|
// @Param username query string true "用户名"
|
||||||
// @Param password query string true "密码"
|
// @Param password query string true "密码"
|
||||||
// @Success 200 {string} json "{"code":200,"data":{},"msg":"ok"}"
|
// @Success 200 {string} json "{"code":200,"data":{},"msg":"ok"}"
|
||||||
// @Router /admin/login [post]
|
// @Router /admin/login [get]
|
||||||
func GetLogin(c *gin.Context) {
|
func GetLogin(c *gin.Context) {
|
||||||
username := c.Query("username")
|
username := c.Query("username")
|
||||||
password := c.Query("password")
|
password := c.Query("password")
|
||||||
|
@ -28,19 +28,19 @@ func GetLogin(c *gin.Context) {
|
||||||
a := Employee{Username: username, Password: password}
|
a := Employee{Username: username, Password: password}
|
||||||
ok, _ := valid.Valid(&a)
|
ok, _ := valid.Valid(&a)
|
||||||
data := make(map[string]interface{})
|
data := make(map[string]interface{})
|
||||||
code := e.INVALID_PARAMS
|
code := e.InvalidParams
|
||||||
if ok {
|
if ok {
|
||||||
isExist := models.CheckEmployee(username, password)
|
isExist := models.CheckEmployee(username, password)
|
||||||
if isExist {
|
if isExist {
|
||||||
token, err := util.GenerateToken(username, password)
|
token, err := util.GenerateToken(username, password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
code = e.ERROR_AUTH_TOKEN
|
code = e.ErrorAuthToken
|
||||||
} else {
|
} else {
|
||||||
data["token"] = token
|
data["token"] = token
|
||||||
code = e.SUCCESS
|
code = e.SUCCESS
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
code = e.ERROR_AUTH
|
code = e.ErrorAuth
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for _, err := range valid.Errors {
|
for _, err := range valid.Errors {
|
||||||
|
|
|
@ -12,6 +12,40 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// @Summary 获取单个员工
|
||||||
|
// @Produce json
|
||||||
|
// @Param token query string true "Token"
|
||||||
|
// @Param id path int true "ID"
|
||||||
|
// @Success 200 {string} json "{"code": 200, "data": {}, "msg": "ok"}"
|
||||||
|
// @Router /admin/v1/employees/{id} [GET]
|
||||||
|
func GetEmployee(c *gin.Context) {
|
||||||
|
id := com.StrTo(c.Param("id")).MustInt()
|
||||||
|
|
||||||
|
valid := validation.Validation{}
|
||||||
|
valid.Min(id, 1, "id").Message("ID必须大于0")
|
||||||
|
|
||||||
|
code := e.InvalidParams
|
||||||
|
var data interface{}
|
||||||
|
if !valid.HasErrors() {
|
||||||
|
if models.ExistEmployeeByID(id) {
|
||||||
|
data = models.GetEmployee(id)
|
||||||
|
code = e.SUCCESS
|
||||||
|
} else {
|
||||||
|
code = e.ErrorNotExistEmployee
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _, err := range valid.Errors {
|
||||||
|
logging.Info("err.key: %s, err.message: %s", err.Key, err.Message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"code" : code,
|
||||||
|
"msg" : e.GetMsg(code),
|
||||||
|
"data" : data,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// @Summary 获取员工列表
|
// @Summary 获取员工列表
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Param token query string true "Token"
|
// @Param token query string true "Token"
|
||||||
|
@ -31,7 +65,7 @@ func GetEmployees(c *gin.Context) {
|
||||||
valid.Range(state, 0, 1, "state").Message("状态只允许0或1")
|
valid.Range(state, 0, 1, "state").Message("状态只允许0或1")
|
||||||
}
|
}
|
||||||
|
|
||||||
code := e.INVALID_PARAMS
|
code := e.InvalidParams
|
||||||
|
|
||||||
if ! valid.HasErrors() {
|
if ! valid.HasErrors() {
|
||||||
code = e.SUCCESS
|
code = e.SUCCESS
|
||||||
|
@ -49,3 +83,4 @@ func GetEmployees(c *gin.Context) {
|
||||||
"data" : data,
|
"data" : data,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue