123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680 |
- 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 := "尊敬的用户:</br>您好,感谢您使用弘则研究,您正在进行邮箱验证,本次请求的验证码为:" + msgCode + "(为了保障您账号的安全性,请在15分钟内完成验证。)</br>弘则研究团队 </br>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 = "验证码正确"
- }
|