123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package api
- import (
- "fmt"
- "github.com/gin-gonic/gin"
- "main/controller/support"
- "main/model/dbModel"
- "mime/multipart"
- )
- var (
- USER_HEADER_IMG_PATH string = "http://qmplusimg.henrongyi.top"
- USER_HEADER_BUCKET string = "qm-plus-img"
- )
- type RegistAndLoginStuct struct {
- UserName string `json:"userName"`
- PassWord string `json:"passWord"`
- }
- // @Tags User
- // @Summary 用户注册账号
- // @Produce application/json
- // @Param data body api.RegistAndLoginStuct true "用户注册接口"
- // @Success 200 {string} json "{"success":true,"data":{},"msg":"注册成功"}"
- // @Router /user/regist [post]
- func Regist(c *gin.Context) {
- var R RegistAndLoginStuct
- _ = c.BindJSON(&R)
- U := &dbModel.User{UserName: R.UserName, PassWord: R.PassWord}
- err, user := U.Regist()
- if err != nil {
- ReportFormat(c, false, fmt.Sprintf("%v", err), gin.H{
- "user": user,
- })
- } else {
- ReportFormat(c, false, "创建成功", gin.H{
- "user": user,
- })
- }
- }
- // @Tags User
- // @Summary 用户登录
- // @Produce application/json
- // @Param data body api.RegistAndLoginStuct true "用户登录接口"
- // @Success 200 {string} json "{"success":true,"data":{},"msg":"登陆成功"}"
- // @Router /user/login [post]
- func Login(c *gin.Context) {
- var L RegistAndLoginStuct
- _ = c.BindJSON(&L)
- U := &dbModel.User{UserName: L.UserName, PassWord: L.PassWord}
- if err, user := U.Login(); err != nil {
- ReportFormat(c, false, "用户名密码错误", gin.H{"user": user})
- } else {
- ReportFormat(c, true, "登录成功", gin.H{"user": user})
- }
- }
- type ChangePassWordStutrc struct {
- UserName string `json:"userName"`
- PassWord string `json:"passWord"`
- NewPassWord string `json:"newPassWord"`
- }
- // @Tags User
- // @Summary 用户修改密码
- // @Produce application/json
- // @Param data body api.ChangePassWordStutrc true "用户修改密码"
- // @Success 200 {string} json "{"success":true,"data":{},"msg":"修改成功"}"
- // @Router /user/changePassWord [post]
- func ChangePassWord(c *gin.Context) {
- var params ChangePassWordStutrc
- _ = c.BindJSON(¶ms)
- U := &dbModel.User{UserName: params.UserName, PassWord: params.PassWord}
- if err, _ := U.ChangePassWord(params.NewPassWord); err != nil {
- ReportFormat(c, false, "修改失败,请检查用户名密码", gin.H{})
- } else {
- ReportFormat(c, true, "修改成功", gin.H{})
- }
- }
- type UserHeaderImg struct {
- HeaderImg multipart.File `json:"headerImg"`
- }
- // @Tags User
- // @Summary 用户上传头像
- // @accept multipart/form-data
- // @Produce application/json
- // @Param headerImg formData file true "用户上传头像"
- // @Param userName formData string true "用户上传头像"
- // @Success 200 {string} json "{"success":true,"data":{},"msg":"上传成功"}"
- // @Router /user/uploadHeaderImg [post]
- func UploadHeaderImg(c *gin.Context) {
- //获取头像文件
- _, header, err := c.Request.FormFile("headerImg")
- //便于找到用户 以后从jwt中取
- userName := c.PostForm("userName")
- if err != nil {
- ReportFormat(c, false, fmt.Sprintf("上传文件失败,%v", err), gin.H{})
- } else {
- //文件上传后拿到文件路径
- err, filePath := support.Upload(header, USER_HEADER_BUCKET, USER_HEADER_IMG_PATH)
- if err != nil {
- ReportFormat(c, false, fmt.Sprintf("接收返回值失败,%v", err), gin.H{})
- } else {
- //修改数据库后得到修改后的user并且返回供前端使用
- err, user := new(dbModel.User).UploadHeaderImg(userName, filePath)
- if err != nil {
- ReportFormat(c, false, fmt.Sprintf("修改数据库链接失败,%v", err), gin.H{})
- } else {
- ReportFormat(c, true, "上传成功", gin.H{"user": user})
- }
- }
- }
- }
|