Skip to content

Commit

Permalink
2020-09-11
Browse files Browse the repository at this point in the history
  • Loading branch information
xufqing committed Sep 11, 2020
1 parent c9f4618 commit 7c0f2a7
Show file tree
Hide file tree
Showing 31 changed files with 724 additions and 36 deletions.
1 change: 1 addition & 0 deletions api/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package api
5 changes: 2 additions & 3 deletions api/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (

// 检查服务器是否通畅
func Ping(c *gin.Context) {
c.JSON(200,gin.H{
"msg":"ok",
c.JSON(200, gin.H{
"msg": "ok",
})
}

1 change: 1 addition & 0 deletions api/v1/sys_menu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package v1
38 changes: 38 additions & 0 deletions api/v1/sys_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package v1

import (
"anew-server/dto/request"
"anew-server/dto/response"
"anew-server/dto/service"
"anew-server/utils"
"github.com/gin-gonic/gin"
)

// 创建用户
func CreateUser(c *gin.Context) {
// 请求校验
var user request.CreateUserReq
err := c.ShouldBindJSON(&user)
if err != nil{
response.Resp(c, 400,user)
return
}
code := service.CheckUser(user.Username)
if code == response.SUCCSE {
code = service.CreateUser(&user)
response.Resp(c, code,user)
} else {
response.Resp(c, code,user)
}
}

// 获取用户列表
func GetUsers(c *gin.Context) {
pageSize := utils.Str2Int(c.Query("pagesize"))
pageNum := utils.Str2Int(c.Query("pagenum"))
users := service.GetUsers(pageSize, pageNum)
// 转为Resp Struct, 隐藏部分字段
var respUsers []response.UserListResp
utils.Struct2StructByJson(users, &respUsers)
response.Resp(c, response.SUCCSE, respUsers)
}
3 changes: 2 additions & 1 deletion common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ type Configuration struct {
}

type SystemConfiguration struct {
UrlPathPrefix string `mapstructure:"url-path-prefix" json:"urlPathPrefix"`
AppMode string `mapstructure:"app-mode" json:"appMode"`
Port int `mapstructure:"port" json:"port"`
Port int `mapstructure:"port" json:"port"`
}

type LogsConfiguration struct {
Expand Down
7 changes: 5 additions & 2 deletions common/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"github.com/gobuffalo/packr/v2"
"github.com/jinzhu/gorm"
"go.uber.org/zap"
)

Expand All @@ -13,12 +14,14 @@ var (
ConfBox *packr.Box
// zap日志
Log *zap.SugaredLogger
// Mysql实例
Mysql *gorm.DB
)

// 全局常量
const (
// 本地时间格式
MsecLocalTimeFormat = "2006-01-02 15:04:05.000"
SecLocalTimeFormat = "2006-01-02 15:04:05"
SecLocalTimeFormat = "2006-01-02 15:04:05"
DateLocalTimeFormat = "2006-01-02"
)
)
3 changes: 2 additions & 1 deletion common/logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package common

import (
"fmt"
"github.com/natefinch/lumberjack"
Expand Down Expand Up @@ -42,7 +43,7 @@ func InitLogger() {
core := zapcore.NewCore(
zapcore.NewConsoleEncoder(enConfig), // 编码器配置
zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(hook)), // 打印到控制台和文件
Conf.Logs.Level, // 日志等级
Conf.Logs.Level, // 日志等级
)

logger := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
Expand Down
4 changes: 3 additions & 1 deletion conf/config-dev.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# development
system:
# url前缀
url-path-prefix: api
# gin模式 debug|release
app-mode: debug
# 程序监听端口
Expand All @@ -18,7 +20,7 @@ mysql:
port: 3306
# 连接字符串查询参数
query: charset=utf8&parseTime=True&loc=Local&timeout=10000ms
# 是否打印日志
# 是否打印SQL日志
log-mode: true
# 数据库表前缀
table-prefix: tb_
Expand Down
26 changes: 26 additions & 0 deletions dto/request/sys_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package request

// User login structure
type RegisterAndLoginReq struct {
Username string `json:"username"`
Password string `json:"password"`
}

// 修改密码结构体
type ChangePwdReq struct {
OldPassword string `json:"oldPassword" form:"oldPassword"`
NewPassword string `json:"newPassword" form:"newPassword"`
}

// 创建用户结构体
type CreateUserReq struct {
Username string `json:"username" validate:"required"`
Password string `json:"password" validate:"required"` // 不使用SysUser的Password字段, 避免请求劫持绕过系统校验
Mobile string `json:"mobile" validate:"eq=11"`
Avatar string `json:"avatar"`
Name string `json:"name"`
Email string `json:"mail"`
Status *bool `json:"status"`
RoleId uint `json:"roleId" validate:"required"`
Creator string `json:"creator"`
}
19 changes: 19 additions & 0 deletions dto/response/custom_msg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package response

const (
SUCCSE = 200
FAILURE = 400
FORBIDDEN = 403
TOKEN_UNAUTHORIZED = 1000
ERROR_USERNAME_USED = 1001
ERROR_LOGIN_WRONG = 1002
)

var Custom_Msg = map[int]string{
SUCCSE: "操作完成",
FAILURE: "操作失败",
FORBIDDEN: "无权操作",
TOKEN_UNAUTHORIZED: "未经授权",
ERROR_USERNAME_USED: "用户已存在",
ERROR_LOGIN_WRONG: "用户名或密码错误",
}
11 changes: 11 additions & 0 deletions dto/response/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package response

import "github.com/gin-gonic/gin"

func Resp(c *gin.Context, code int, resp interface{}) {
c.JSON(SUCCSE, gin.H{
"status": code,
"data": resp,
"msg": Custom_Msg[code],
})
}
37 changes: 37 additions & 0 deletions dto/response/sys_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package response

import "anew-server/models"

// User login response structure
type LoginResp struct {
Username string `json:"username"` // 登录用户名
Token string `json:"token"` // jwt令牌
ExpiresAt int64 `json:"expiresAt"` // 过期时间, 秒
}

// 用户信息响应
type UserInfoResp struct {
Id uint `json:"id"`
Username string `json:"username"`
Mobile string `json:"mobile"`
Avatar string `json:"avatar"`
Name string `json:"name"`
Email string `json:"mail"`
Roles []string `json:"roles"`
Permissions []string `json:"permissions"`
}

// 用户列表信息响应, 字段含义见models.SysUser
type UserListResp struct {
Id uint `json:"id"`
Username string `json:"username"`
Mobile string `json:"mobile"`
Avatar string `json:"avatar"`
Name string `json:"name"`
Email string `json:"mail"`
Status *bool `json:"status"`
RoleId uint `json:"roleId"`
Creator string `json:"creator"`
CreatedAt models.LocalTime `json:"createdAt"`
UpdatedAt models.LocalTime `json:"updatedAt"`
}
42 changes: 42 additions & 0 deletions dto/service/sys_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package service

import (
"anew-server/common"
"anew-server/dto/request"
"anew-server/dto/response"
"anew-server/models"
"anew-server/utils"
)

// 用户是否存在
func CheckUser(username string) int {
var users models.SysUser
common.Mysql.Select("id").Where("username = ?", username).First(&users)
if users.Id > 0 {
return response.ERROR_USERNAME_USED
}
return response.SUCCSE
}

// 创建用户
func CreateUser(req *request.CreateUserReq) int {
var user models.SysUser
utils.Struct2StructByJson(req, &user)
// 将密码转为密文
user.Password = utils.GenPwd(req.Password)
err := common.Mysql.Create(&user)
if err != nil {
return response.FAILURE
}
return response.SUCCSE
}

// 用户列表
func GetUsers(pageSize int, pageNum int) []models.SysUser {
var users []models.SysUser
err := common.Mysql.Limit(pageSize).Offset((pageNum - 1) * pageSize).Find(&users).Error
if err != nil {
return nil
}
return users
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ go 1.15
require (
github.com/gin-gonic/gin v1.6.3
github.com/go-playground/validator/v10 v10.3.0 // indirect
github.com/go-sql-driver/mysql v1.5.0
github.com/gobuffalo/packr/v2 v2.8.0
github.com/golang/protobuf v1.4.2 // indirect
github.com/jinzhu/gorm v1.9.16
github.com/jinzhu/now v1.1.1 // indirect
github.com/json-iterator/go v1.1.10 // indirect
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/spf13/viper v1.7.1
go.uber.org/zap v1.16.0
golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd
golang.org/x/sys v0.0.0-20200908134130-d2e65c121b96 // indirect
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
gorm.io/gorm v1.20.0 // indirect
)
Loading

0 comments on commit 7c0f2a7

Please sign in to comment.