package controller import ( "Ic_ouath/app/e" "Ic_ouath/app/service" "Ic_ouath/app/service/imp" "Ic_ouath/global" "Ic_ouath/models" "Ic_ouath/page" "Ic_ouath/simple_zap" "Ic_ouath/utils" "context" "github.com/astaxie/beego/validation" "github.com/gin-gonic/gin" "github.com/go-playground/validator/v10" ) var users service.User = &imp.User{} // PhoneRegist 手机号注册 // @Tags 用户管理 // @Summary 手机号注册 // @Success 200 {object} e.R // @Fail 400 {object} e.R // @Param req body models.UserRegist true "注册信息" // @Accept application/json // @Router /user [post] func PhoneRegist(c *gin.Context) { var userRegist models.UserRegist err := c.ShouldBindJSON(&userRegist) if err != nil { e.ResponseError(c, e.JSONParsingFailed) return } rescode := users.PhoneRegist(userRegist) e.ResponseError(c, rescode) } // SendCode 发送验证码 // @Tags 用户管理 // @Summary 发送验证码 // @Success 200 {object} e.R // @Fail 400 {object} e.R // @Param phone query string true "手机号" // @Accept application/json // @Router /sendcode [post] func SendCode(c *gin.Context) { phone := c.PostForm("phone") utils.SendModel(phone) v := validation.Validation{} v.Mobile(phone, "phone") v.Required(phone, "phone") if v.HasErrors() { e.ResponseWithMsg(c, e.ThePhoneNumberIsWrong, "手机号码格式不正确") } else { rescode := utils.SendModel(phone) if rescode != e.SUCCESS { e.ResponseWithMsg(c, rescode, rescode.GetMsg()) } else { e.ResponseSuccess(c, "发送成功") } } } // Login 账号登录 // @Tags 用户管理 // @Summary 账号密码登录 // @Success 200 {object} e.R // @Fail 400 {object} e.R // @Param req body models.UserRegist true "账号信息" // @Accept application/json // @Router /user [post] func Login(c *gin.Context) { var userRegist models.UserRegist err := c.ShouldBindJSON(&userRegist) if err != nil { e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg()) return } else { login, rescode := users.Login(userRegist) if rescode == e.SUCCESS { e.ResponseSuccess(c, login) } else { e.ResponseWithMsg(c, rescode, rescode.GetMsg()) } } } // CodeLogin 验证码登录 // @Tags 用户管理 // @Summary 验证码登录 // @Success 200 {object} e.R // @Fail 400 {object} e.R // @Param req body models.UserRegist true "账号信息" // @Accept application/json // @Router /codelogin [post] func CodeLogin(c *gin.Context) { var userRegist models.UserRegist err := c.ShouldBindJSON(&userRegist) if err != nil { e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg()) simple_zap.WithCtx(context.Background()).Sugar().Warn(err, e.JSONParsingFailed.GetMsg()) return } login, rescode := users.CodeLogin(userRegist) if rescode == e.SUCCESS { e.ResponseSuccess(c, login) } else { e.ResponseWithMsg(c, rescode, rescode.GetMsg()) simple_zap.WithCtx(context.Background()).Sugar().Warn(err, rescode.GetMsg()) } } // GetUserAll 获取所有用户 func GetUserAll(c *gin.Context) { user, rescode := users.GetUserAll() if rescode == e.SUCCESS { e.ResponseSuccess(c, user) } else { e.ResponseWithMsg(c, rescode, rescode.GetMsg()) } } // GetUserAlls 获取所有用户 // @Tags 用户管理 // @Summary 获取所有用户 // @Success 200 {object} e.R // @Fail 400 {object} e.R // @Param req body page.PageParams true "分页信息" // @Accept application/json // @Router /admin/getalluser [post] func GetUserAlls(c *gin.Context) { var pages page.PageParams err := c.ShouldBindJSON(&pages) if err != nil { e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg()) simple_zap.WithCtx(context.Background()).Sugar().Warn(err, e.JSONParsingFailed.GetMsg()) return } result, total, err := page.Paginate(global.DBLink, pages, models.User{}) if err != nil { e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg()) simple_zap.WithCtx(context.Background()).Sugar().Warn(err, e.PaginationFailed.GetMsg()) return } e.ResPonsePage(c, result, total, pages) } // UpdateUserById 修改用户信息 // @Tags 用户管理 // @Summary 获取所有用户 // @Success 200 {object} e.R // @Fail 400 {object} e.R // @Param req body page.PageParams true "分页信息" // @Accept application/json // @Router /admin/user [post] func UpdateUserById(c *gin.Context) { var userVo models.UserVo userId := c.MustGet("user_id") err := c.ShouldBindJSON(&userVo) if err != nil { e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg()) simple_zap.WithCtx(context.Background()).Sugar().Warn(err, e.JSONParsingFailed.GetMsg()) return } recode := users.UpdateUser(userId, userVo) if recode == e.SUCCESS { e.ResponseSuccess(c, "修改成功") } else { e.ResponseWithMsg(c, recode, recode.GetMsg()) simple_zap.WithCtx(context.Background()).Sugar().Info(err, recode.GetMsg()) } } // DeleteUserById 删除用户信息 // @Tags 用户管理 // @Summary 删除用户 // @Success 200 {object} e.R // @Fail 400 {object} e.R // @Param id query string true "id" // @Accept application/json // @Router /admin/user [delete] func DeleteUserById(c *gin.Context) { ids := c.QueryArray("ids") recode := users.DeleteUser(ids) if recode == e.SUCCESS { e.ResponseSuccess(c, "删除成功") } else { e.ResponseWithMsg(c, recode, recode.GetMsg()) simple_zap.WithCtx(context.Background()).Sugar().Info(recode.GetMsg()) } } // UpdatePassword 修改账号密码 // @Tags 用户管理 // @Summary 手机号注册 // @Success 200 {object} e.R // @Fail 400 {object} e.R // @Param req body models.UserRegist true "注册信息" // @Accept application/json // @Router /user [post] func UpdatePassword(c *gin.Context) { var userRegist models.UserUpdate err := c.ShouldBindJSON(&userRegist) if err != nil { e.ResponseError(c, e.JSONParsingFailed) return } validate := validator.New() err = validate.Struct(userRegist) if err != nil { e.ResponseWithMsg(c, e.ERROR, err.Error()) return } rescode := users.UpdatePassword(userRegist) if rescode == e.SUCCESS { e.ResponseSuccess(c, nil) return } else { e.ResponseWithMsg(c, rescode, rescode.GetMsg()) } }