123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- package user
- import (
- "github.com/gin-gonic/gin"
- "hongze/hongze_yb/controller/response"
- userLogic "hongze/hongze_yb/logic/user"
- "hongze/hongze_yb/models/request/user"
- userResp "hongze/hongze_yb/models/response/user"
- userService "hongze/hongze_yb/services/user"
- "hongze/hongze_yb/utils"
- "strings"
- )
- func Login(c *gin.Context) {
- var req user.LoginReq
- if c.ShouldBind(&req) != nil {
- response.Fail("参数异常", c)
- return
- }
- userInfo := userService.GetInfoByClaims(c)
- openId := userInfo.OpenID
- if openId == "" {
- response.Fail("参数异常", c)
- return
- }
- if req.LoginType != 1 && req.LoginType != 2 {
- response.Fail("无效的登录方式", c)
- return
- }
- if req.LoginType == 1 {
-
- req.Mobile = strings.Trim(req.Mobile, " ")
- if req.Mobile == "" {
- response.Fail("手机号不能为空,请输入手机号", c)
- return
- }
- }
- if req.LoginType == 2 {
-
- req.Email = strings.Trim(req.Email, " ")
- if req.Email == "" {
- response.Fail("邮箱不能为空,请输入邮箱", c)
- return
- }
- if !utils.ValidateEmailFormatat(req.Email) {
- response.Fail("邮箱格式错误,请重新输入", c)
- return
- }
- }
- token, newUserInfo, err, errMsg := userService.BindWxUser(openId, req.Mobile, req.Email, req.VerifyCode, req.LoginType, req.AreaNum, 1)
- if err != nil {
- if errMsg == "" {
- errMsg = "登录失败"
- }
- response.Fail(errMsg, c)
- return
- }
- response.OkData("登录成功", userResp.LoginResp{
- UserID: int(newUserInfo.UserID),
- Token: token,
- Email: newUserInfo.Email,
- Mobile: newUserInfo.Mobile,
- }, c)
- }
- func GetSmsCode(c *gin.Context) {
- userInfo := userService.GetInfoByClaims(c)
- openId := userInfo.OpenID
- if openId == "" {
- response.Fail("参数异常", c)
- return
- }
- mobile := c.DefaultQuery("mobile", "")
- areaNum := c.DefaultQuery("area_num", "")
- err, errMsg := userLogic.SendSmsCode(openId, mobile, areaNum)
- if err != nil {
- if errMsg != "" {
- errMsg = "获取验证码失败"
- }
- response.Fail(errMsg, c)
- return
- }
- response.Ok("获取验证码成功", c)
- }
- func GetEmailCode(c *gin.Context) {
- userInfo := userService.GetInfoByClaims(c)
- openId := userInfo.OpenID
- if openId == "" {
- response.Fail("参数异常", c)
- return
- }
- email := c.DefaultQuery("email", "")
- if email == "" {
- response.Fail("请输入邮箱地址", c)
- return
- }
- if !utils.ValidateEmailFormatat(email) {
- response.Fail("邮箱格式错误,请重新输入", c)
- return
- }
- err, errMsg := userLogic.SendEmailCode(openId, email)
- if err != nil {
- if errMsg != "" {
- errMsg = "获取验证码失败"
- }
- response.Fail(errMsg, c)
- return
- }
- response.Ok("获取验证码成功", c)
- }
- func Info(c *gin.Context) {
- userInfo := userService.GetInfoByClaims(c)
- list, err, errMsg := userLogic.GetUserInfo(userInfo)
- if err != nil {
- if errMsg != "" {
- errMsg = "获取失败"
- }
- response.Fail(errMsg, c)
- return
- }
- response.OkData("获取成功", list, c)
- }
- func GetLastApplyRecord(c *gin.Context) {
- userInfo := userService.GetInfoByClaims(c)
- record, err := userLogic.GetLastApplyInfo(int(userInfo.UserID))
- if err != nil {
- response.Fail("获取失败", c)
- return
- }
- if record.RealName == "" {
- response.Ok("获取成功", c)
- } else {
- response.OkData("获取成功", record, c)
- }
- }
- func Apply(c *gin.Context) {
- var req user.ApplyReq
- if c.ShouldBind(&req) != nil {
- response.Fail("参数异常", c)
- return
- }
- userInfo := userService.GetInfoByClaims(c)
- userId := userInfo.UserID
- if userId == 0 {
- response.Fail("参数异常", c)
- return
- }
- _, err := userLogic.Apply(int(userInfo.UserID), userInfo.CompanyID, userInfo.Mobile, userInfo.Email, req)
- if err != nil {
- response.Fail("申请失败", c)
- return
- }
- response.Ok("申请成功", c)
- }
- func GetTabBar(c *gin.Context) {
- userInfo := userService.GetInfoByClaims(c)
- version := c.Request.Header.Get("version")
- tabBarList, err := userLogic.GetUserTabBar(userInfo, version)
- if err != nil {
- response.Fail("获取失败", c)
- return
- }
- response.OkData("获取成功", tabBarList, c)
- }
- func SetUserInfo(c *gin.Context) {
- var req user.SetUserInfoReq
- if c.ShouldBind(&req) != nil {
- response.Fail("参数异常", c)
- return
- }
-
- req.NickName = strings.Trim(req.NickName, "")
- req.HeadImgUrl = strings.Trim(req.HeadImgUrl, "")
- if req.NickName == "" {
- response.Fail("请输入昵称", c)
- return
- }
- if req.HeadImgUrl == "" {
- response.Fail("请上传头像", c)
- return
- }
- userInfo := userService.GetInfoByClaims(c)
- err := userLogic.SetUserInfo(userInfo, req)
- if err != nil {
- response.Fail(err.Error(), c)
- return
- }
- response.Ok("更新成功", c)
- }
- func GetTopTab(c *gin.Context) {
- userInfo := userService.GetInfoByClaims(c)
- version := c.Request.Header.Get("version")
- tabBarList, err := userLogic.GetTopTab(userInfo, version)
- if err != nil {
- response.Fail("获取失败", c)
- return
- }
- response.OkData("获取成功", tabBarList, c)
- }
|