|
@ -2,10 +2,14 @@ package api |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"fmt" |
|
|
"fmt" |
|
|
|
|
|
"github.com/dgrijalva/jwt-go" |
|
|
"github.com/gin-gonic/gin" |
|
|
"github.com/gin-gonic/gin" |
|
|
"main/controller/support" |
|
|
|
|
|
|
|
|
"main/controller/servers" |
|
|
|
|
|
"main/middleware" |
|
|
"main/model/dbModel" |
|
|
"main/model/dbModel" |
|
|
|
|
|
"main/model/modelInterface" |
|
|
"mime/multipart" |
|
|
"mime/multipart" |
|
|
|
|
|
"time" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
var ( |
|
|
var ( |
|
@ -18,12 +22,12 @@ type RegistAndLoginStuct struct { |
|
|
PassWord string `json:"passWord"` |
|
|
PassWord string `json:"passWord"` |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// @Tags User
|
|
|
|
|
|
|
|
|
// @Tags Base
|
|
|
// @Summary 用户注册账号
|
|
|
// @Summary 用户注册账号
|
|
|
// @Produce application/json
|
|
|
// @Produce application/json
|
|
|
// @Param data body api.RegistAndLoginStuct true "用户注册接口"
|
|
|
// @Param data body api.RegistAndLoginStuct true "用户注册接口"
|
|
|
// @Success 200 {string} json "{"success":true,"data":{},"msg":"注册成功"}"
|
|
|
// @Success 200 {string} json "{"success":true,"data":{},"msg":"注册成功"}"
|
|
|
// @Router /user/regist [post]
|
|
|
|
|
|
|
|
|
// @Router /base/regist [post]
|
|
|
func Regist(c *gin.Context) { |
|
|
func Regist(c *gin.Context) { |
|
|
var R RegistAndLoginStuct |
|
|
var R RegistAndLoginStuct |
|
|
_ = c.BindJSON(&R) |
|
|
_ = c.BindJSON(&R) |
|
@ -31,30 +35,54 @@ func Regist(c *gin.Context) { |
|
|
U := &dbModel.User{UserName: R.UserName, PassWord: R.PassWord} |
|
|
U := &dbModel.User{UserName: R.UserName, PassWord: R.PassWord} |
|
|
err, user := U.Regist() |
|
|
err, user := U.Regist() |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
ReportFormat(c, false, fmt.Sprintf("%v", err), gin.H{ |
|
|
|
|
|
|
|
|
servers.ReportFormat(c, false, fmt.Sprintf("%v", err), gin.H{ |
|
|
"user": user, |
|
|
"user": user, |
|
|
}) |
|
|
}) |
|
|
} else { |
|
|
} else { |
|
|
ReportFormat(c, false, "创建成功", gin.H{ |
|
|
|
|
|
|
|
|
servers.ReportFormat(c, false, "创建成功", gin.H{ |
|
|
"user": user, |
|
|
"user": user, |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// @Tags User
|
|
|
|
|
|
|
|
|
// @Tags Base
|
|
|
// @Summary 用户登录
|
|
|
// @Summary 用户登录
|
|
|
// @Produce application/json
|
|
|
// @Produce application/json
|
|
|
// @Param data body api.RegistAndLoginStuct true "用户登录接口"
|
|
|
// @Param data body api.RegistAndLoginStuct true "用户登录接口"
|
|
|
// @Success 200 {string} json "{"success":true,"data":{},"msg":"登陆成功"}"
|
|
|
// @Success 200 {string} json "{"success":true,"data":{},"msg":"登陆成功"}"
|
|
|
// @Router /user/login [post]
|
|
|
|
|
|
|
|
|
// @Router /base/login [post]
|
|
|
func Login(c *gin.Context) { |
|
|
func Login(c *gin.Context) { |
|
|
var L RegistAndLoginStuct |
|
|
var L RegistAndLoginStuct |
|
|
_ = c.BindJSON(&L) |
|
|
_ = c.BindJSON(&L) |
|
|
U := &dbModel.User{UserName: L.UserName, PassWord: L.PassWord} |
|
|
U := &dbModel.User{UserName: L.UserName, PassWord: L.PassWord} |
|
|
if err, user := U.Login(); err != nil { |
|
|
if err, user := U.Login(); err != nil { |
|
|
ReportFormat(c, false, "用户名密码错误", gin.H{"user": user}) |
|
|
|
|
|
|
|
|
servers.ReportFormat(c, false, "用户名密码错误", gin.H{"user": user}) |
|
|
|
|
|
} else { |
|
|
|
|
|
tokenNext(c, *user) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//登录以后签发jwt
|
|
|
|
|
|
func tokenNext(c *gin.Context, user dbModel.User) { |
|
|
|
|
|
j := &middleware.JWT{ |
|
|
|
|
|
[]byte("qmPlus"), // 唯一签名
|
|
|
|
|
|
} |
|
|
|
|
|
clams := middleware.CustomClaims{ |
|
|
|
|
|
UUID: user.UUID, |
|
|
|
|
|
ID: user.ID, |
|
|
|
|
|
NickName: user.NickName, |
|
|
|
|
|
AuthorityId: user.AuthorityId, |
|
|
|
|
|
StandardClaims: jwt.StandardClaims{ |
|
|
|
|
|
NotBefore: int64(time.Now().Unix() - 1000), // 签名生效时间
|
|
|
|
|
|
ExpiresAt: int64(time.Now().Unix() + 3600*7), // 过期时间 一周
|
|
|
|
|
|
Issuer: "qmPlus", //签名的发行者
|
|
|
|
|
|
}, |
|
|
|
|
|
} |
|
|
|
|
|
token, err := j.CreateToken(clams) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
servers.ReportFormat(c, false, "获取token失败", gin.H{}) |
|
|
} else { |
|
|
} else { |
|
|
ReportFormat(c, true, "登录成功", gin.H{"user": user}) |
|
|
|
|
|
|
|
|
servers.ReportFormat(c, true, "登录成功", gin.H{"user": user, "token": token}) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -66,6 +94,7 @@ type ChangePassWordStutrc struct { |
|
|
|
|
|
|
|
|
// @Tags User
|
|
|
// @Tags User
|
|
|
// @Summary 用户修改密码
|
|
|
// @Summary 用户修改密码
|
|
|
|
|
|
// @Security ApiKeyAuth
|
|
|
// @Produce application/json
|
|
|
// @Produce application/json
|
|
|
// @Param data body api.ChangePassWordStutrc true "用户修改密码"
|
|
|
// @Param data body api.ChangePassWordStutrc true "用户修改密码"
|
|
|
// @Success 200 {string} json "{"success":true,"data":{},"msg":"修改成功"}"
|
|
|
// @Success 200 {string} json "{"success":true,"data":{},"msg":"修改成功"}"
|
|
@ -75,9 +104,9 @@ func ChangePassWord(c *gin.Context) { |
|
|
_ = c.BindJSON(¶ms) |
|
|
_ = c.BindJSON(¶ms) |
|
|
U := &dbModel.User{UserName: params.UserName, PassWord: params.PassWord} |
|
|
U := &dbModel.User{UserName: params.UserName, PassWord: params.PassWord} |
|
|
if err, _ := U.ChangePassWord(params.NewPassWord); err != nil { |
|
|
if err, _ := U.ChangePassWord(params.NewPassWord); err != nil { |
|
|
ReportFormat(c, false, "修改失败,请检查用户名密码", gin.H{}) |
|
|
|
|
|
|
|
|
servers.ReportFormat(c, false, "修改失败,请检查用户名密码", gin.H{}) |
|
|
} else { |
|
|
} else { |
|
|
ReportFormat(c, true, "修改成功", gin.H{}) |
|
|
|
|
|
|
|
|
servers.ReportFormat(c, true, "修改成功", gin.H{}) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -87,6 +116,7 @@ type UserHeaderImg struct { |
|
|
|
|
|
|
|
|
// @Tags User
|
|
|
// @Tags User
|
|
|
// @Summary 用户上传头像
|
|
|
// @Summary 用户上传头像
|
|
|
|
|
|
// @Security ApiKeyAuth
|
|
|
// @accept multipart/form-data
|
|
|
// @accept multipart/form-data
|
|
|
// @Produce application/json
|
|
|
// @Produce application/json
|
|
|
// @Param headerImg formData file true "用户上传头像"
|
|
|
// @Param headerImg formData file true "用户上传头像"
|
|
@ -94,26 +124,54 @@ type UserHeaderImg struct { |
|
|
// @Success 200 {string} json "{"success":true,"data":{},"msg":"上传成功"}"
|
|
|
// @Success 200 {string} json "{"success":true,"data":{},"msg":"上传成功"}"
|
|
|
// @Router /user/uploadHeaderImg [post]
|
|
|
// @Router /user/uploadHeaderImg [post]
|
|
|
func UploadHeaderImg(c *gin.Context) { |
|
|
func UploadHeaderImg(c *gin.Context) { |
|
|
|
|
|
claims, _ := c.Get("claims") |
|
|
//获取头像文件
|
|
|
//获取头像文件
|
|
|
|
|
|
// 这里我们通过断言获取 claims内的所有内容
|
|
|
|
|
|
waitUse := claims.(*middleware.CustomClaims) |
|
|
|
|
|
fmt.Println(waitUse.NickName) |
|
|
_, header, err := c.Request.FormFile("headerImg") |
|
|
_, header, err := c.Request.FormFile("headerImg") |
|
|
//便于找到用户 以后从jwt中取
|
|
|
//便于找到用户 以后从jwt中取
|
|
|
userName := c.PostForm("userName") |
|
|
userName := c.PostForm("userName") |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
ReportFormat(c, false, fmt.Sprintf("上传文件失败,%v", err), gin.H{}) |
|
|
|
|
|
|
|
|
servers.ReportFormat(c, false, fmt.Sprintf("上传文件失败,%v", err), gin.H{}) |
|
|
} else { |
|
|
} else { |
|
|
//文件上传后拿到文件路径
|
|
|
//文件上传后拿到文件路径
|
|
|
err, filePath := support.Upload(header, USER_HEADER_BUCKET, USER_HEADER_IMG_PATH) |
|
|
|
|
|
|
|
|
err, filePath := servers.Upload(header, USER_HEADER_BUCKET, USER_HEADER_IMG_PATH) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
ReportFormat(c, false, fmt.Sprintf("接收返回值失败,%v", err), gin.H{}) |
|
|
|
|
|
|
|
|
servers.ReportFormat(c, false, fmt.Sprintf("接收返回值失败,%v", err), gin.H{}) |
|
|
} else { |
|
|
} else { |
|
|
//修改数据库后得到修改后的user并且返回供前端使用
|
|
|
//修改数据库后得到修改后的user并且返回供前端使用
|
|
|
err, user := new(dbModel.User).UploadHeaderImg(userName, filePath) |
|
|
err, user := new(dbModel.User).UploadHeaderImg(userName, filePath) |
|
|
|
|
|
|
|
|
if err != nil { |
|
|
if err != nil { |
|
|
ReportFormat(c, false, fmt.Sprintf("修改数据库链接失败,%v", err), gin.H{}) |
|
|
|
|
|
|
|
|
servers.ReportFormat(c, false, fmt.Sprintf("修改数据库链接失败,%v", err), gin.H{}) |
|
|
} else { |
|
|
} else { |
|
|
ReportFormat(c, true, "上传成功", gin.H{"user": user}) |
|
|
|
|
|
|
|
|
servers.ReportFormat(c, true, "上传成功", gin.H{"user": user}) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// @Tags User
|
|
|
|
|
|
// @Summary 分页获取用户列表
|
|
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
|
|
// @accept application/json
|
|
|
|
|
|
// @Produce application/json
|
|
|
|
|
|
// @Param data body modelInterface.PageInfo true "分页获取用户列表"
|
|
|
|
|
|
// @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
|
|
|
|
|
|
// @Router /user/getInfoList [post]
|
|
|
|
|
|
func GetInfoList(c *gin.Context) { |
|
|
|
|
|
var pageInfo modelInterface.PageInfo |
|
|
|
|
|
_ = c.BindJSON(&pageInfo) |
|
|
|
|
|
err, list, total := new(dbModel.User).GetInfoList(pageInfo) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
servers.ReportFormat(c, false, fmt.Sprintf("获取数据失败,%v", err), gin.H{}) |
|
|
|
|
|
} else { |
|
|
|
|
|
servers.ReportFormat(c, true, "获取数据成功", gin.H{ |
|
|
|
|
|
"userList": list, |
|
|
|
|
|
"total": total, |
|
|
|
|
|
"page": pageInfo.Page, |
|
|
|
|
|
"pageSize": pageInfo.PageSize, |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
} |