sys_user.go 7.2 KB
Newer Older
Mr.奇淼('s avatar
Mr.奇淼( 已提交
1
package v1
2 3 4

import (
	"fmt"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
5 6
	"gin-vue-admin/global"
	"gin-vue-admin/global/response"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
7
	"gin-vue-admin/middleware"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
8
	"gin-vue-admin/model"
9
	"gin-vue-admin/model/request"
10
	resp "gin-vue-admin/model/response"
11
	"gin-vue-admin/service"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
12
	"gin-vue-admin/utils"
13
	"github.com/dchest/captcha"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
14
	"github.com/dgrijalva/jwt-go"
15
	"github.com/gin-gonic/gin"
16
	"github.com/go-redis/redis"
17
	"mime/multipart"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
18
	"time"
19 20
)

R
rainyan 已提交
21
const (
22 23
	USER_HEADER_IMG_PATH string = "http://qmplusimg.henrongyi.top"
	USER_HEADER_BUCKET   string = "qm-plus-img"
24
)
25

Mr.奇淼('s avatar
Mr.奇淼( 已提交
26
// @Tags Base
27 28
// @Summary 用户注册账号
// @Produce  application/json
29
// @Param data body model.SysUser true "用户注册接口"
30
// @Success 200 {string} string "{"success":true,"data":{},"msg":"注册成功"}"
31 32
// @Router /base/register [post]
func Register(c *gin.Context) {
33
	var R request.RegisterStruct
34
	_ = c.ShouldBindJSON(&R)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
35
	user := &model.SysUser{Username: R.Username, NickName: R.NickName, Password: R.Password, HeaderImg: R.HeaderImg, AuthorityId: R.AuthorityId}
36
	err, user := service.Register(user)
37
	if err != nil {
38
		response.FailWithDetailed(response.ERROR, resp.SysUserResponse{User: *user}, fmt.Sprintf("%v", err), c)
39
	} else {
40
		response.OkDetailed(resp.SysUserResponse{User: *user}, "注册成功", c)
41 42 43
	}
}

Mr.奇淼('s avatar
Mr.奇淼( 已提交
44
// @Tags Base
45 46
// @Summary 用户登录
// @Produce  application/json
47
// @Param data body model.RegisterAndLoginStruct true "用户登录接口"
48
// @Success 200 {string} string "{"success":true,"data":{},"msg":"登陆成功"}"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
49
// @Router /base/login [post]
50
func Login(c *gin.Context) {
51
	var L request.RegisterAndLoginStruct
52 53
	_ = c.ShouldBindJSON(&L)
	if captcha.VerifyString(L.CaptchaId, L.Captcha) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
54
		U := &model.SysUser{Username: L.Username, Password: L.Password}
55
		if err, user := service.Login(U); err != nil {
56
			response.FailWithMessage(fmt.Sprintf("用户名密码错误或%v", err), c)
57 58 59
		} else {
			tokenNext(c, *user)
		}
60
	} else {
61
		response.FailWithMessage("验证码错误", c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
62
	}
63

Mr.奇淼('s avatar
Mr.奇淼( 已提交
64 65 66
}

//登录以后签发jwt
Mr.奇淼('s avatar
Mr.奇淼( 已提交
67
func tokenNext(c *gin.Context, user model.SysUser) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
68
	j := &middleware.JWT{
Mr.奇淼('s avatar
Mr.奇淼( 已提交
69
		[]byte(global.GVA_CONFIG.JWT.SigningKey), // 唯一签名
Mr.奇淼('s avatar
Mr.奇淼( 已提交
70
	}
71
	clams := request.CustomClaims{
Mr.奇淼('s avatar
Mr.奇淼( 已提交
72 73 74 75 76
		UUID:        user.UUID,
		ID:          user.ID,
		NickName:    user.NickName,
		AuthorityId: user.AuthorityId,
		StandardClaims: jwt.StandardClaims{
Mr.奇淼('s avatar
Mr.奇淼( 已提交
77 78 79
			NotBefore: int64(time.Now().Unix() - 1000),       // 签名生效时间
			ExpiresAt: int64(time.Now().Unix() + 60*60*24*7), // 过期时间 一周
			Issuer:    "qmPlus",                              //签名的发行者
Mr.奇淼('s avatar
Mr.奇淼( 已提交
80 81 82 83
		},
	}
	token, err := j.CreateToken(clams)
	if err != nil {
84
		response.FailWithMessage("获取token失败", c)
85
	} else {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
86 87
		if global.GVA_CONFIG.System.UseMultipoint {
			var loginJwt model.JwtBlacklist
88
			loginJwt.Jwt = token
89
			err, jwtStr := service.GetRedisJWT(user.Username)
90
			if err == redis.Nil {
91
				err2 := service.SetRedisJWT(loginJwt, user.Username)
92
				if err2 != nil {
93
					response.FailWithMessage("设置登录状态失败", c)
94
				} else {
95 96 97 98 99
					response.OkWithData(resp.LoginResponse{
						User:      user,
						Token:     token,
						ExpiresAt: clams.StandardClaims.ExpiresAt * 1000,
					}, c)
100 101
				}
			} else if err != nil {
102
				response.FailWithMessage(fmt.Sprintf("%v", err), c)
103
			} else {
104 105 106
				var blackJWT model.JwtBlacklist
				blackJWT.Jwt = jwtStr
				err3 := service.JsonInBlacklist(blackJWT)
107
				if err3 != nil {
108
					response.FailWithMessage("jwt作废失败", c)
109
				} else {
110
					err2 := service.SetRedisJWT(loginJwt, user.Username)
111
					if err2 != nil {
112
						response.FailWithMessage("设置登录状态失败", c)
113
					} else {
114 115 116 117 118
						response.OkWithData(resp.LoginResponse{
							User:      user,
							Token:     token,
							ExpiresAt: clams.StandardClaims.ExpiresAt * 1000,
						}, c)
119 120 121 122
					}
				}
			}
		} else {
123 124 125 126 127
			response.OkWithData(resp.LoginResponse{
				User:      user,
				Token:     token,
				ExpiresAt: clams.StandardClaims.ExpiresAt * 1000,
			}, c)
128
		}
129 130 131
	}
}

132
// @Tags SysUser
133
// @Summary 用户修改密码
Mr.奇淼('s avatar
Mr.奇淼( 已提交
134
// @Security ApiKeyAuth
135
// @Produce  application/json
136
// @Param data body model.ChangePasswordStutrc true "用户修改密码"
137
// @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
138
// @Router /user/changePassword [put]
Mr.奇淼('s avatar
Mr.奇淼( 已提交
139
func ChangePassword(c *gin.Context) {
140
	var params request.ChangePasswordStruct
141
	_ = c.ShouldBindJSON(&params)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
142
	U := &model.SysUser{Username: params.Username, Password: params.Password}
143
	if err, _ := service.ChangePassword(U, params.NewPassword); err != nil {
144
		response.FailWithMessage("修改失败,请检查用户名密码", c)
145
	} else {
146
		response.OkWithMessage("修改成功", c)
147 148 149 150 151 152 153
	}
}

type UserHeaderImg struct {
	HeaderImg multipart.File `json:"headerImg"`
}

154
// @Tags SysUser
155
// @Summary 用户上传头像
Mr.奇淼('s avatar
Mr.奇淼( 已提交
156
// @Security ApiKeyAuth
157 158 159
// @accept multipart/form-data
// @Produce  application/json
// @Param headerImg formData file true "用户上传头像"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
160
// @Param username formData string true "用户上传头像"
161
// @Success 200 {string} string "{"success":true,"data":{},"msg":"上传成功"}"
162 163
// @Router /user/uploadHeaderImg [post]
func UploadHeaderImg(c *gin.Context) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
164
	claims, _ := c.Get("claims")
165
	//获取头像文件
Mr.奇淼('s avatar
Mr.奇淼( 已提交
166
	// 这里我们通过断言获取 claims内的所有内容
167
	waitUse := claims.(*request.CustomClaims)
168
	uuid := waitUse.UUID
169 170 171
	_, header, err := c.Request.FormFile("headerImg")
	//便于找到用户 以后从jwt中取
	if err != nil {
172
		response.FailWithMessage(fmt.Sprintf("上传文件失败,%v", err), c)
173 174
	} else {
		//文件上传后拿到文件路径
Mr.奇淼('s avatar
Mr.奇淼( 已提交
175
		err, filePath, _ := utils.Upload(header, USER_HEADER_BUCKET, USER_HEADER_IMG_PATH)
176
		if err != nil {
177
			response.FailWithMessage(fmt.Sprintf("接收返回值失败,%v", err), c)
178 179
		} else {
			//修改数据库后得到修改后的user并且返回供前端使用
180
			err, user := service.UploadHeaderImg(uuid, filePath)
181
			if err != nil {
182
				response.FailWithMessage(fmt.Sprintf("修改数据库链接失败,%v", err), c)
183
			} else {
184
				response.OkWithData(resp.SysUserResponse{User: *user}, c)
185 186
			}
		}
187
	}
188
}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
189

190
// @Tags SysUser
Mr.奇淼('s avatar
Mr.奇淼( 已提交
191 192 193 194
// @Summary 分页获取用户列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
Mr.奇淼('s avatar
Mr.奇淼( 已提交
195
// @Param data body model.PageInfo true "分页获取用户列表"
196
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
197 198
// @Router /user/getUserList [post]
func GetUserList(c *gin.Context) {
199
	var pageInfo request.PageInfo
200
	_ = c.ShouldBindJSON(&pageInfo)
201
	err, list, total := service.GetUserInfoList(pageInfo)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
202
	if err != nil {
203
		response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
204
	} else {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
205 206 207 208 209 210
		response.OkWithData(resp.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
211 212
	}
}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
213

214
// @Tags SysUser
Mr.奇淼('s avatar
Mr.奇淼( 已提交
215 216 217 218
// @Summary 设置用户权限
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
219
// @Param data body model.SetUserAuth true "设置用户权限"
220
// @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
221 222
// @Router /user/setUserAuthority [post]
func SetUserAuthority(c *gin.Context) {
223
	var sua request.SetUserAuth
224
	_ = c.ShouldBindJSON(&sua)
225
	err := service.SetUserAuthority(sua.UUID, sua.AuthorityId)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
226
	if err != nil {
227
		response.FailWithMessage(fmt.Sprintf("修改失败,%v", err), c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
228
	} else {
229
		response.OkWithMessage("修改成功", c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
230 231
	}
}