Skip to content

Commit

Permalink
员工注册和登录代码
Browse files Browse the repository at this point in the history
  • Loading branch information
luminescenceJ committed Dec 23, 2024
1 parent e858415 commit ede382b
Show file tree
Hide file tree
Showing 23 changed files with 1,274 additions and 78 deletions.
21 changes: 21 additions & 0 deletions common/e/msg.go
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)})
}
19 changes: 19 additions & 0 deletions common/enum/context.go
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
)
37 changes: 36 additions & 1 deletion common/utils/jwt.go
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")
}
24 changes: 24 additions & 0 deletions config/application-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@ server:
# debug | release | test 运行环境
level: debug

datasource:
host: 127.0.0.1
port: 3306
username: root
password: 123456
db_name: sky_take_out
config: charset=utf8mb4&parseTime=True&loc=Local

log:
level: debug
filepath: ./logger/systemLog.txt

jwt:
admin:
# 设置jwt签名加密时使用的秘钥
secret: admin
# 设置jwt过期时间
ttl: 7200000
# 设置前端传递过来的令牌名称
name: token
user:
# 设置jwt签名加密时使用的秘钥
secret: user
# 设置jwt过期时间
ttl: 7200000
# 设置前端传递过来的令牌名称
name: Authorization
17 changes: 15 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,28 @@ var envPtr = pflag.String("env", "dev", "Environment: dev or prod")

// AllConfig 整合Config
type AllConfig struct {
Server Server
//DataSource DataSource
Server Server
DataSource DataSource
//Redis Redis
Log Log
Jwt Jwt
//AliOss AliOss
//Wechat Wechat
}

type DataSource struct {
Host string
Port string
UserName string
Password string
DBName string `mapstructure:"db_name"`
Config string
}

func (d *DataSource) Dsn() string {
return d.UserName + ":" + d.Password + "@tcp(" + d.Host + ":" + d.Port + ")/" + d.DBName + "?" + d.Config
}

type Server struct {
Port string
Level string
Expand Down
247 changes: 247 additions & 0 deletions docs/docs.go
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)
}
Loading

0 comments on commit ede382b

Please sign in to comment.