-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e858415
commit ede382b
Showing
23 changed files
with
1,274 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package e | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
"takeout/common" | ||
) | ||
|
||
var ErrMsg = map[int]string{ | ||
SUCCESS: "ok", | ||
ERROR: "内部错误", | ||
UNKNOW_IDENTITY: "未知身份", | ||
} | ||
|
||
func GetMsg(code int) string { | ||
return ErrMsg[code] | ||
} | ||
|
||
func Send(ctx *gin.Context, code int) { | ||
ctx.JSON(http.StatusOK, common.Result{Code: code, Msg: GetMsg(code)}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package enum | ||
|
||
const ( | ||
CurrentId = "currentId" | ||
CurrentName = "currentName" | ||
) | ||
|
||
type PageNum = int | ||
|
||
const ( | ||
MaxPageSize PageNum = 100 // 单页最大数量 | ||
MinPageSize PageNum = 10 // 单页最小数量 | ||
) | ||
|
||
type CommonInt = int | ||
|
||
const ( | ||
MaxUrl CommonInt = 1 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,45 @@ | ||
package utils | ||
|
||
import "github.com/golang-jwt/jwt/v5" | ||
import ( | ||
"errors" | ||
"github.com/golang-jwt/jwt/v5" | ||
"time" | ||
) | ||
|
||
// CustomPayload 自定义载荷继承原有接口并附带自己的字段 | ||
type CustomPayload struct { | ||
UserId uint64 | ||
GrantScope string | ||
jwt.RegisteredClaims | ||
} | ||
|
||
// GenerateToken 生成Token uid 用户id grantScope 签发对象 secret 加盐 | ||
func GenerateToken(userId uint64, grantScope string, secret string) (string, error) { | ||
claims := CustomPayload{ | ||
UserId: userId, | ||
GrantScope: grantScope, | ||
RegisteredClaims: jwt.RegisteredClaims{ | ||
Issuer: "Auth_Server", //签发者 | ||
Subject: grantScope, //签发对象 | ||
Audience: jwt.ClaimStrings{"PC", "Wechat_Program"}, //签发受众 | ||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour)), //过期时间 | ||
NotBefore: jwt.NewNumericDate(time.Now().Add(time.Second)), //最早使用时间 | ||
IssuedAt: jwt.NewNumericDate(time.Now()), //签发时间 | ||
}, | ||
} | ||
token, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString([]byte(secret)) | ||
return token, err | ||
} | ||
|
||
func ParseToken(token string, secret string) (*CustomPayload, error) { | ||
parseToken, err := jwt.ParseWithClaims(token, &CustomPayload{}, func(token *jwt.Token) (i interface{}, err error) { | ||
return []byte(secret), nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if claims, ok := parseToken.Claims.(*CustomPayload); ok && parseToken.Valid { // 校验token | ||
return claims, nil | ||
} | ||
return nil, errors.New("invalid token") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
// Package docs Code generated by swaggo/swag. DO NOT EDIT | ||
package docs | ||
|
||
import "github.com/swaggo/swag" | ||
|
||
const docTemplate = `{ | ||
"schemes": {{ marshal .Schemes }}, | ||
"swagger": "2.0", | ||
"info": { | ||
"description": "{{escape .Description}}", | ||
"title": "{{.Title}}", | ||
"termsOfService": "http://swagger.io/terms/", | ||
"contact": { | ||
"name": "API Support", | ||
"url": "http://www.swagger.io/support", | ||
"email": "[email protected]" | ||
}, | ||
"license": { | ||
"name": "Apache 2.0", | ||
"url": "http://www.apache.org/licenses/LICENSE-2.0.html" | ||
}, | ||
"version": "{{.Version}}" | ||
}, | ||
"host": "{{.Host}}", | ||
"basePath": "{{.BasePath}}", | ||
"paths": { | ||
"/admin/employee/": { | ||
"post": { | ||
"security": [ | ||
{ | ||
"JWTAuth": [] | ||
} | ||
], | ||
"produces": [ | ||
"application/json" | ||
], | ||
"tags": [ | ||
"Employee" | ||
], | ||
"parameters": [ | ||
{ | ||
"description": "新增员工信息", | ||
"name": "data", | ||
"in": "body", | ||
"required": true, | ||
"schema": { | ||
"$ref": "#/definitions/request.EmployeeDTO" | ||
} | ||
} | ||
], | ||
"responses": { | ||
"200": { | ||
"description": "success", | ||
"schema": { | ||
"$ref": "#/definitions/common.Result" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"/admin/employee/login": { | ||
"post": { | ||
"produces": [ | ||
"application/json" | ||
], | ||
"tags": [ | ||
"Employee" | ||
], | ||
"parameters": [ | ||
{ | ||
"description": "员工登录信息", | ||
"name": "data", | ||
"in": "body", | ||
"required": true, | ||
"schema": { | ||
"$ref": "#/definitions/request.EmployeeLogin" | ||
} | ||
} | ||
], | ||
"responses": { | ||
"200": { | ||
"description": "success", | ||
"schema": { | ||
"allOf": [ | ||
{ | ||
"$ref": "#/definitions/common.Result" | ||
}, | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"data": { | ||
"$ref": "#/definitions/response.EmployeeLogin" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"400": { | ||
"description": "Invalid request payload", | ||
"schema": { | ||
"$ref": "#/definitions/common.Result" | ||
} | ||
}, | ||
"401": { | ||
"description": "wrong password or error on json web token generate", | ||
"schema": { | ||
"$ref": "#/definitions/common.Result" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"/admin/employee/logout": { | ||
"post": { | ||
"security": [ | ||
{ | ||
"JWTAuth": [] | ||
} | ||
], | ||
"produces": [ | ||
"application/json" | ||
], | ||
"tags": [ | ||
"Employee" | ||
], | ||
"responses": { | ||
"200": { | ||
"description": "success", | ||
"schema": { | ||
"$ref": "#/definitions/common.Result" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"definitions": { | ||
"common.Result": { | ||
"type": "object", | ||
"properties": { | ||
"code": { | ||
"type": "integer" | ||
}, | ||
"data": {}, | ||
"msg": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"request.EmployeeDTO": { | ||
"type": "object", | ||
"required": [ | ||
"idNumber", | ||
"name", | ||
"phone", | ||
"sex", | ||
"username" | ||
], | ||
"properties": { | ||
"id": { | ||
"description": "员工id", | ||
"type": "integer" | ||
}, | ||
"idNumber": { | ||
"description": "身份证", | ||
"type": "string" | ||
}, | ||
"name": { | ||
"description": "姓名", | ||
"type": "string" | ||
}, | ||
"phone": { | ||
"description": "手机号", | ||
"type": "string" | ||
}, | ||
"sex": { | ||
"description": "性别", | ||
"type": "string" | ||
}, | ||
"username": { | ||
"description": "用户名", | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"request.EmployeeLogin": { | ||
"type": "object", | ||
"required": [ | ||
"password", | ||
"username" | ||
], | ||
"properties": { | ||
"password": { | ||
"type": "string", | ||
"example": "123456" | ||
}, | ||
"username": { | ||
"type": "string", | ||
"example": "admin" | ||
} | ||
} | ||
}, | ||
"response.EmployeeLogin": { | ||
"type": "object", | ||
"properties": { | ||
"id": { | ||
"type": "integer" | ||
}, | ||
"name": { | ||
"type": "string" | ||
}, | ||
"token": { | ||
"type": "string" | ||
}, | ||
"userName": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
}, | ||
"securityDefinitions": { | ||
"JWTAuth": { | ||
"type": "apiKey", | ||
"name": "token", | ||
"in": "header" | ||
} | ||
} | ||
}` | ||
|
||
// SwaggerInfo holds exported Swagger Info so clients can modify it | ||
var SwaggerInfo = &swag.Spec{ | ||
Version: "1.0", | ||
Host: "localhost:8080", | ||
BasePath: "/", | ||
Schemes: []string{}, | ||
Title: "Swagger Example API", | ||
Description: "This is a sample server celler server.", | ||
InfoInstanceName: "swagger", | ||
SwaggerTemplate: docTemplate, | ||
LeftDelim: "{{", | ||
RightDelim: "}}", | ||
} | ||
|
||
func init() { | ||
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo) | ||
} |
Oops, something went wrong.