package controllers import ( "encoding/json" "fmt" "hongze/hongze_api/models" "hongze/hongze_api/services" "hongze/hongze_api/utils" "strconv" "strings" "time" ) //用户 type UserController struct { BaseAuthController } //用户 type UserCommonController struct { BaseCommonController } //用户不登录 type UserNotAuthController struct { BaseNotAuthController } // @Title 获取用户详情 // @Description 获取用户详情接口 // @Success 200 {object} models.UserDetail // @router /detail [get] func (this *UserController) Detail() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User if user == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } item, err := models.GetUserDetailByUserId(user.UserId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } userPermission, err := services.CheckUserPermission(user.UserId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "判断用户权限信息失败,Err:" + err.Error() return } if item.Mobile == "" && item.Email == "" { item.FirstLogin = 1 } //新增访问日志 { record := new(models.UserAccessRecord) record.Uid = user.UserId record.Token = this.Token record.CreateTime = time.Now() go models.AddUserAccessRecord(record) } item.UserPermission = userPermission br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = item } // @Title 获取短信验证码 // @Description 获取短信验证码接口 // @Param Mobile query string true "手机号码" // @Param AreaNum query string true "地区编码" // @Success Ret=200 获取成功 // @router /getSmsCode [get] func (this *UserNotAuthController) GetSmsCode() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() mobile := this.GetString("Mobile") if mobile == "" { br.Msg = "请输入手机号" return } areaNum := this.GetString("AreaNum") msgCode := utils.GetRandDigit(4) var result bool if areaNum == "86" || areaNum == "" || areaNum == "0" { result = services.SendSmsCode(mobile, msgCode) } else { result = services.SendSmsCodeGj(mobile, msgCode, areaNum) } //发送成功 if result { item := new(models.MsgCode) item.OpenId = "" item.Code = msgCode item.Mobile = mobile item.ExpiredIn = time.Now().Add(15 * time.Minute).Unix() item.Enabled = 1 item.CreatedTime = time.Now() err := models.AddMsgCode(item) if err != nil { br.Msg = "发送失败" br.ErrMsg = "发送失败,Err:" + err.Error() return } br.Msg = "发送成功" } else { br.Msg = "发送失败" } br.Ret = 200 br.Success = true } // @Title 校验短信验证码 // @Description 校验短信验证码接口 // @Param request body models.CheckSmsCodeReq true "type json string" // @Success Ret=200 校验成功 // @router /checkSmsCode [post] func (this *UserNotAuthController) CheckSmsCode() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.CheckSmsCodeReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.Mobile == "" { br.Msg = "请输入手机号" br.ErrMsg = "请输入手机号" return } if req.SmsCode == "" { br.Msg = "请输入验证码" br.ErrMsg = "请输入验证码" return } item, err := models.GetMsgCode(req.Mobile, req.SmsCode) if err != nil { if err.Error() == utils.ErrNoRow() { br.Msg = "验证码错误,请重新输入" br.ErrMsg = "校验验证码失败,Err:" + err.Error() return } else { br.Msg = "验证码错误,请重新输入" br.ErrMsg = "校验验证码失败,Err:" + err.Error() return } } if item == nil { br.Msg = "验证码错误,请重新输入" return } br.Ret = 200 br.Success = true br.Msg = "验证码正确" } // @Title 获取邮件验证码 // @Description 获取邮件验证码接口 // @Param Email query string true "邮箱" // @Success Ret=200 获取成功 // @router /getEmailCode [get] func (this *UserNotAuthController) GetEmailCode() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() email := this.GetString("Email") if email == "" { br.Msg = "请输入邮箱地址" return } if !utils.ValidateEmailFormatat(email) { br.Msg = "邮箱格式错误,请重新输入" return } msgCode := utils.GetRandDigit(4) content := "尊敬的用户:
您好,感谢您使用弘则研究,您正在进行邮箱验证,本次请求的验证码为:" + msgCode + "(为了保障您账号的安全性,请在15分钟内完成验证。)
弘则研究团队
2019年05月11日" title := "弘则研究登陆验证" //发送邮件 result, err := utils.SendEmailByHz(title, content, email) if err != nil { br.Msg = "发送失败" br.ErrMsg = "发送失败,Err:" + err.Error() return } if result { item := new(models.MsgCode) item.OpenId = "" item.Code = msgCode item.Mobile = email item.ExpiredIn = time.Now().Add(15 * time.Minute).Unix() item.Enabled = 1 item.CreatedTime = time.Now() err := models.AddMsgCode(item) if err != nil { br.Msg = "发送失败" br.ErrMsg = "发送失败,Err:" + err.Error() return } br.Msg = "发送成功" } else { br.Msg = "发送失败" } br.Ret = 200 br.Success = true } // @Title 校验邮箱验证码 // @Description 校验邮箱验证码接口 // @Param request body models.CheckEmailCodeReq true "type json string" // @Success Ret=200 校验成功 // @router /checkEmailCode [post] func (this *UserNotAuthController) CheckEmailCode() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.CheckEmailCodeReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.Email == "" { br.Msg = "请输入手机号" br.ErrMsg = "请输入手机号" return } if req.SmsCode == "" { br.Msg = "请输入验证码" br.ErrMsg = "请输入验证码" return } item, err := models.GetMsgCode(req.Email, req.SmsCode) if err != nil { if err.Error() == utils.ErrNoRow() { br.Msg = "验证码错误,请重新输入" br.ErrMsg = "校验验证码失败,Err:" + err.Error() return } else { br.Msg = "验证码错误,请重新输入" br.ErrMsg = "校验验证码失败,Err:" + err.Error() return } } if item == nil { br.Msg = "验证码错误,请重新输入" return } br.Ret = 200 br.Success = true br.Msg = "验证码正确" } // @Title 登录 // @Description 登录接口 // @Param request body models.LoginReq true "type json string" // @Success 200 {object} models.LoginResp // @router /login [post] func (this *UserNotAuthController) Login() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req models.LoginReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } user := this.User if user == nil { br.Msg = "请登录" br.ErrMsg = "请登录" br.Ret = 408 return } openId := user.OpenId if openId == "" { br.Msg = "参数错误" br.ErrMsg = "参数错误,openid 为空" return } if req.LoginType == 1 { //手机登录 if req.Mobile == "" { br.ErrMsg = "手机号不能为空,请输入手机号" br.Msg = "手机号不能为空,请输入手机号" return } req.Mobile = strings.Trim(req.Mobile, " ") } else if req.LoginType == 2 { //邮箱登录 if req.Email == "" { br.ErrMsg = "邮箱不能为空,请输入邮箱" br.Msg = "邮箱不能为空,请输入邮箱" return } if !utils.ValidateEmailFormatat(req.Email) { br.ErrMsg = "邮箱格式错误,请重新输入" br.Msg = "邮箱格式错误,请重新输入" return } } else { br.Msg = "无效的登录方式" br.ErrMsg = "无效的登录方式,Err:" + err.Error() return } user, errMsg, err := services.BindWxUser(openId, req.Mobile, req.Email, req.AreaNum, 1) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "登录失败,系统处理中,请稍后重试" if errMsg != `` { br.Msg = errMsg } br.ErrMsg = "登录失败:" + err.Error() return } userId := user.UserId var token string tokenItem, err := models.GetTokenByOpenId(openId) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "登录失败" br.ErrMsg = "登录失败,获取token失败:" + err.Error() return } if tokenItem == nil || (err != nil && err.Error() == utils.ErrNoRow()) { timeUnix := time.Now().Unix() timeUnixStr := strconv.FormatInt(timeUnix, 10) token := utils.MD5(openId) + utils.MD5(timeUnixStr) //新增session { session := new(models.Session) session.OpenId = openId session.UserId = userId session.CreatedTime = time.Now() session.LastUpdatedTime = time.Now() session.ExpireTime = time.Now().AddDate(0, 1, 0) session.AccessToken = token err = models.AddSession(session) if err != nil { br.Msg = "登录失败" br.ErrMsg = "登录失败,新增用户session信息失败:" + err.Error() return } } } else { token = tokenItem.AccessToken _ = models.UpdateSession(tokenItem.SessionId, userId, time.Now().AddDate(0, 1, 0)) } userPermission, err := services.CheckUserPermission(userId) if err != nil { br.Msg = "登录失败" br.ErrMsg = "登录失败,判断权限失败:" + err.Error() return } err = models.ModifyFirstLogin(user.UserId) if err != nil { br.Msg = "登录失败" br.ErrMsg = "登录失败,判断权限失败:" + err.Error() return } //新增登录日志 { loginLog := new(models.WxUserLog) loginLog.UserId = userId loginLog.OpenId = openId loginLog.Mobile = req.Mobile loginLog.Email = req.Email loginLog.CreateTime = time.Now() loginLog.Handle = "wechat_user_login" loginLog.Remark = token go models.AddWxUserLog(loginLog) } resp := new(models.LoginResp) resp.UserId = userId resp.UserPermission = userPermission resp.Authorization = token br.Ret = 200 br.Success = true br.Data = resp br.Msg = "登录成功" } //func (this *UserController) Login() { // br := new(models.BaseResponse).Init() // defer func() { // this.Data["json"] = br // this.ServeJSON() // }() // var req models.LoginReq // err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) // if err != nil { // br.Msg = "参数解析异常!" // br.ErrMsg = "参数解析失败,Err:" + err.Error() // return // } // user := this.User // if user == nil { // br.Msg = "请登录" // br.ErrMsg = "请登录" // br.Ret = 408 // return // } // // openId := user.OpenId // if openId == "" { // br.Msg = "参数错误" // br.ErrMsg = "参数错误,openid 为空" // return // } // userId := user.UserId // newUserId := 0 // if req.LoginType == 1 { // //BindMobile(openId, mobile string, userId, loginType int) (err error) { // req.Mobile = strings.Trim(req.Mobile, " ") // newUserId, err = models.BindMobile(openId, req.Mobile, userId, req.LoginType) // } else if req.LoginType == 2 { // if req.Email == "" { // br.ErrMsg = "邮箱不能为空,请输入邮箱" // br.Msg = "邮箱不能为空,请输入邮箱" // return // } // if !utils.ValidateEmailFormatat(req.Email) { // br.ErrMsg = "邮箱格式错误,请重新输入" // br.Msg = "邮箱格式错误,请重新输入" // return // } // newUserId, err = models.BindMobile(openId, req.Email, userId, req.LoginType) // } else { // br.Msg = "无效的登录方式" // br.ErrMsg = "无效的登录方式,Err:" + err.Error() // return // } // var token string // tokenItem, err := models.GetTokenByUid(newUserId) // if err != nil && err.Error() != utils.ErrNoRow() { // br.Msg = "登录失败" // br.ErrMsg = "登录失败,获取token失败:" + err.Error() // return // } // // if tokenItem == nil || (err != nil && err.Error() == utils.ErrNoRow()) { // timeUnix := time.Now().Unix() // timeUnixStr := strconv.FormatInt(timeUnix, 10) // token := utils.MD5(strconv.Itoa(userId)) + utils.MD5(timeUnixStr) // //新增session // { // session := new(models.Session) // session.OpenId = openId // session.UserId = userId // session.CreatedTime = time.Now() // session.LastUpdatedTime = time.Now() // session.ExpireTime = time.Now().AddDate(0, 1, 0) // session.AccessToken = token // err = models.AddSession(session) // if err != nil { // br.Msg = "登录失败" // br.ErrMsg = "登录失败,新增用户session信息失败:" + err.Error() // return // } // } // } else { // token = tokenItem.AccessToken // } // userPermission, err := services.CheckUserPermission(newUserId) // if err != nil { // br.Msg = "登录失败" // br.ErrMsg = "登录失败,判断权限失败:" + err.Error() // return // } // err = models.ModifyFirstLogin(user.UserId) // if err != nil { // br.Msg = "登录失败" // br.ErrMsg = "登录失败,判断权限失败:" + err.Error() // return // } // // //新增登录日志 // { // loginLog := new(models.WxUserLog) // loginLog.UserId = userId // loginLog.OpenId = openId // loginLog.Mobile=req.Mobile // loginLog.Email=req.Email // loginLog.CreateTime = time.Now() // loginLog.Handle="wechat_user_login" // loginLog.Remark=token // go models.AddWxUserLog(loginLog) // } // // resp := new(models.LoginResp) // resp.UserId = newUserId // resp.UserPermission = userPermission // resp.Authorization = token // br.Ret = 200 // br.Success = true // br.Data = resp // br.Msg = "登录成功" //} // @Title 申请试用 // @Description 申请试用接口 // @Param request body models.ApplyReq true "type json string" // @Success Ret=200 申请成功 // @router /apply [post] func (this *UserController) Apply() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User if user == nil { br.Msg = "请登录" br.ErrMsg = "请登录" br.Ret = 408 return } var req models.ApplyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } realName := req.RealName userId := user.UserId if req.ApplyMethod == 2 { if realName == "" { br.Msg = "姓名不能为空!" return } } if realName == "" { realName = user.RealName } err = models.Apply(userId, req.ApplyMethod, user.Mobile, user.Email, req.CompanyName, realName, user.OpenId) if err != nil { br.Msg = "申请失败!" br.ErrMsg = "申请失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "申请成功" } // @Title 是否小套餐客户 // @Description 是否小套餐客户接口 // @Success 200 {object} models.SmallLimitResp // @router /smallLimit [get] func (this *UserController) SmallLimit() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User if user == nil { br.Msg = "请登录" br.ErrMsg = "请登录" br.Ret = 408 return } userItem, err := models.GetWxUserItemByUserId(user.UserId) if err != nil { br.Msg = "获取用户信息失败" br.ErrMsg = "获取用户信息失败,Err:" + err.Error() return } if userItem == nil { br.Msg = "获取用户信息失败" br.ErrMsg = "用户信息不存在,UserId:" + strconv.Itoa(user.UserId) return } maxCount, err := models.GetUserIsMaxPermission(userItem.CompanyId) if err != nil { br.Msg = "获取用户信息失败" br.ErrMsg = "判断是否最大客户权限失败,Err:" + err.Error() return } resp := new(models.SmallLimitResp) maxPermissionCount, err := models.GetDefaultChartPermissionCountByCache(1) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取产品默认最大权限数量失败,Err:" + err.Error() return } if maxCount >= maxPermissionCount { resp.IsMaxPermission = 1 } else { resp.IsMaxPermission = 0 } br.Ret = 200 br.Success = true br.Data = resp } // @Title test // @Description test接口 // @Param request body models.CheckEmailCodeReq true "type json string" // @Success Ret=200 校验成功 // @router /test [get] func (this *UserNotAuthController) Test() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() openid := "oN0jD1S3P-FVosLhq-YiVOXldtRo" wxUser, err := services.GetWxUserItemByOpenId(openid) fmt.Println(err) fmt.Println(wxUser) fmt.Println("-----------") //wxUser,err = models.GetWxUserItemByOpenId(openid) //fmt.Println(err) //fmt.Println(wxUser) //fmt.Println("-----------") userId := 12274 userId = 12018 platform := 2 wxUser, err = services.GetWxUserItemByUserId(userId, platform) fmt.Println(err) fmt.Println(wxUser) fmt.Println("根据用户id获取结束-----------") mobile := "18170239278" wxUser, err = services.GetWxUserItemByMobile(mobile, platform) fmt.Println(err) fmt.Println(wxUser) fmt.Println("根据手机号获取结束-----------") email := "984198890@qq.com" wxUser, err = services.GetWxUserItemByEmail(email, platform) fmt.Println(err) fmt.Println(wxUser) fmt.Println("根据邮箱获取结束-----------") unionId := "o1q5cwOzXXA9Hy9PTEIwxP81Casg" wxUser, err = services.GetWxUserItemByUnionId(unionId, platform) fmt.Println(err) fmt.Println(wxUser) fmt.Println("根据unionid获取结束-----------") br.Ret = 200 br.Success = true br.Msg = "验证码正确" }