123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692 |
- package controllers
- import (
- "encoding/json"
- "fmt"
- "github.com/mojocn/base64Captcha"
- "hongze/hongze_api/services"
- "hongze/hongze_api/utils"
- "strconv"
- "hongze/hongze_api/models"
- "image/color"
- "time"
- )
- func (this *UserCommonController) GetGraphicVerifyCode() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var configC = base64Captcha.ConfigCharacter{
- Height: 26,
- Width: 67,
- Mode: base64Captcha.CaptchaModeNumber,
- ComplexOfNoiseText: base64Captcha.CaptchaComplexLower,
- ComplexOfNoiseDot: base64Captcha.CaptchaComplexLower,
- IsUseSimpleFont: true,
- IsShowHollowLine: false,
- IsShowNoiseDot: true,
- IsShowNoiseText: true,
- IsShowSlimeLine: false,
- IsShowSineLine: false,
- CaptchaLen: 4,
- BgColor: &color.RGBA{255, 255, 255, 1},
- }
- char := base64Captcha.EngineCharCreate(configC)
- base64stringC := base64Captcha.CaptchaWriteToBase64Encoding(char)
- verifyId := fmt.Sprintf("%d", time.Now().UnixNano())
- item := new(models.GraphicVerifyCode)
- item.VerifyId = verifyId
- item.VerifyCode = char.CaptchaItem.VerifyValue
- item.CreateTime = time.Now()
- err := models.AddGraphicVerifyCode(item)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- resp := new(models.GraphicVerifyCodeResp)
- resp.VerifyId = verifyId
- resp.VerifyCode = base64stringC
- br.Ret = 200
- br.Data = resp
- br.Msg = "获取成功"
- br.Success = true
- }
- func (this *UserCommonController) PcGetSmsCode() {
- 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)
- verifyId := this.GetString("VerifyId")
- verifyCode := this.GetString("VerifyCode")
- item, err := models.GetGraphicVerifyById(verifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "验证码不存在,请刷新页面!"
- return
- }
- br.Msg = "校验验证码失败!"
- return
- }
- if item.VerifyCode != verifyCode {
- br.Msg = "验证码错误,请重新输入!"
- return
- }
- checkoutTime := time.Now().Add(15 * time.Minute)
- if item.CreateTime.After(checkoutTime) {
- br.Msg = "验证码过期,请重新输入!"
- return
- }
- 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
- }
- func (this *UserCommonController) PcLogin() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.PcLoginReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- userId := 0
- var isAdd bool
- if req.LoginType == 1 {
- 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
- }
- wxUser, err := models.GetWxUserItemByMobile(req.Mobile)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- isAdd = true
- } else {
- br.Msg = "登陆失败"
- br.ErrMsg = "根据手机号获取用户信息失败,Err:" + err.Error()
- return
- }
- }
- if wxUser == nil {
- isAdd = true
- } else {
- userId = wxUser.UserId
- }
-
-
- } else if req.LoginType == 2 {
- if req.Email == "" {
- br.ErrMsg = "邮箱不能为空,请输入邮箱"
- br.Msg = "邮箱不能为空,请输入邮箱"
- return
- }
- if !utils.ValidateEmailFormatat(req.Email) {
- br.ErrMsg = "邮箱格式错误,请重新输入"
- br.Msg = "邮箱格式错误,请重新输入"
- 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
- }
- wxUser, err := models.GetWxUserItemByEmail(req.Email)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- isAdd = true
- } else {
- br.Msg = "登陆失败"
- br.ErrMsg = "根据邮箱获取用户信息失败,Err:" + err.Error()
- return
- }
- }
- if wxUser == nil {
- isAdd = true
- } else {
- userId = wxUser.UserId
- }
- } else {
- br.Msg = "无效的登录方式"
- br.ErrMsg = "无效的登录方式,loginType:" + strconv.Itoa(req.LoginType)
- }
- if isAdd {
- user := new(models.WxUser)
- user.CompanyId = 1
- user.CreatedTime = time.Now()
- user.FirstLogin = 1
- user.Enabled = 1
- user.Email = req.Email
- user.Mobile = req.Mobile
- if req.LoginType == 1 {
- user.BindAccount = user.Mobile
- } else {
- user.BindAccount = user.Email
- }
- user.RegisterTime = time.Now()
- user.LoginTime = time.Now()
- user.IsFreeLogin = req.IsFreeLogin
- user.RegisterPlatform = 2
- lastId, err := models.AddWxUser(user)
- if err != nil {
- br.Msg = "登录失败"
- br.ErrMsg = "登录失败,新增客户信息失败,Err:" + err.Error()
- return
- }
- userId = int(lastId)
- timeUnix := time.Now().Unix()
- timeUnixStr := strconv.FormatInt(timeUnix, 10)
- token := utils.MD5(strconv.Itoa(userId)) + utils.MD5(timeUnixStr)
-
- {
- session := new(models.Session)
- 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 {
- err = models.ModifyLoginTime(userId, req.IsFreeLogin)
- if err != nil {
- br.Msg = "登录失败"
- br.ErrMsg = "登录失败,修改登录信息失败,Err:" + err.Error()
- return
- }
- }
- if userId == 0 {
- br.Msg = "登录失败"
- br.ErrMsg = "登录失败,id为 0"
- return
- }
- userPermission, err := services.CheckUserPermission(userId)
- if err != nil {
- br.Msg = "登录失败"
- br.ErrMsg = "登录失败,判断权限失败:" + err.Error()
- return
- }
- err = models.ModifyFirstLogin(userId)
- if err != nil {
- br.Msg = "登录失败"
- br.ErrMsg = "登录失败,判断权限失败:" + err.Error()
- return
- }
- var token string
- tokenItem, err := models.GetTokenByUid(userId)
- 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 := new(models.Session)
- 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
- }
- cp, err := models.GetCompanyProductsByUserId(userId)
- if err != nil {
- br.Msg = "登录失败"
- br.ErrMsg = "登录失败,获取客户信息失败:" + err.Error()
- return
- }
- var companyName, status, endDate, productName string
- for _, v := range cp {
- companyName = v.CompanyName
- if v.EndDate=="0000-00-00" {
- v.EndDate=""
- }
- if status != "" {
- status = status + "/" + v.Status
- if v.EndDate != "" {
- endDate = endDate + "/" + v.Status + ":" + v.EndDate
- }
- productName = productName + "/" + v.ProductName
- } else {
- status = v.Status
- if v.EndDate != "" {
- endDate = v.Status + ":" + v.EndDate
- }
- productName = v.ProductName
- }
- }
- newUser, _ := models.GetWxUserItemByUserId(userId)
- resp := new(models.LoginResp)
- resp.UserId = newUser.UserId
- resp.UserPermission = userPermission
- resp.Authorization = token
- resp.Headimgurl = newUser.Headimgurl
- resp.Mobile = newUser.Mobile
- resp.Email = newUser.Email
- resp.CompanyName = companyName
- resp.EndDate = endDate
- resp.Status = status
- resp.ProductName = productName
- br.Ret = 200
- br.Success = true
- br.Data = resp
- br.Msg = "登录成功"
- }
- func (this *UserController) PcBind() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.BindReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- unionId := this.User.UnionId
- userId := this.User.UserId
- utils.FileLog.Info("绑定unionId:%s", unionId)
- utils.FileLog.Info("userId:%d", userId)
- var newUserId int
- if req.BindType == 1 {
- if req.Mobile == "" {
- br.Msg = "请输入手机号"
- br.ErrMsg = "请输入手机号"
- return
- }
- if req.VerifyCode == "" {
- br.Msg = "请输入有效手机验证码"
- br.ErrMsg = "请输入有效手机验证码"
- return
- }
- item, err := models.GetMsgCode(req.Mobile, req.VerifyCode)
- 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
- }
- fmt.Println("bind")
- fmt.Println(unionId, req.Mobile, userId, req.BindType)
- newUserId, err = models.PcBindMobile(unionId, req.Mobile, userId, req.BindType)
- if err != nil {
- br.ErrMsg = "绑定失败"
- br.Msg = "绑定失败,Err:" + err.Error()
- return
- }
- } else if req.BindType == 2 {
- if req.Email == "" {
- br.ErrMsg = "邮箱不能为空,请输入邮箱"
- br.Msg = "邮箱不能为空,请输入邮箱"
- return
- }
- if req.VerifyCode == "" {
- br.Msg = "请填写有效的邮箱验证码"
- br.ErrMsg = "请填写有效的邮箱验证码"
- return
- }
- if !utils.ValidateEmailFormatat(req.Email) {
- br.ErrMsg = "邮箱格式错误,请重新输入"
- br.Msg = "邮箱格式错误,请重新输入"
- return
- }
- item, err := models.GetMsgCode(req.Email, req.VerifyCode)
- 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
- }
- newUserId, err = models.PcBindMobile(unionId, req.Email, userId, req.BindType)
- if err != nil {
- br.ErrMsg = "绑定失败"
- br.Msg = "绑定失败,Err:" + err.Error()
- return
- }
- } else {
- br.Msg = "无效的绑定方式"
- br.ErrMsg = "无效的绑定方式,bindType:" + strconv.Itoa(req.BindType)
- 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(newUserId)) + utils.MD5(timeUnixStr)
-
- {
- session := new(models.Session)
- session.UserId = newUserId
- 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(newUserId)
- if err != nil {
- br.Msg = "绑定失败"
- br.ErrMsg = "绑定失败,判断权限失败:" + err.Error()
- return
- }
- cp, err := models.GetCompanyProductsByUserId(newUserId)
- if err != nil {
- br.Msg = "登录失败"
- br.ErrMsg = "登录失败,获取客户信息失败:" + err.Error()
- return
- }
- var companyName, status, endDate, productName string
- for _, v := range cp {
- companyName = v.CompanyName
- if status != "" {
- status = status + "/" + v.Status
- endDate = endDate + "/" + v.EndDate
- productName = productName + "/" + v.ProductName
- } else {
- status = v.Status
- endDate = v.EndDate
- productName = v.ProductName
- }
- }
- newUser, err := models.GetWxUserItemByUserId(newUserId)
- if err != nil {
- br.Msg = "登录失败"
- br.ErrMsg = "获取客户信息失败 GetWxUserItemByUserId:" + err.Error()
- return
- }
- resp := new(models.LoginResp)
- resp.UserId = newUserId
- resp.UserPermission = userPermission
- resp.Authorization = token
- resp.Email = newUser.Email
- resp.Headimgurl = newUser.Headimgurl
- resp.Mobile = newUser.Mobile
- resp.CompanyName = companyName
- resp.EndDate = endDate
- resp.Status = status
- resp.ProductName = productName
- br.Ret = 200
- br.Success = true
- br.Data = resp
- br.Msg = "绑定成功"
- }
- func (this *UserController) CheckLogin() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- if this.User.IsFreeLogin == 1 {
- if this.User.LoginTime.AddDate(0, 0, 30).After(time.Now()) {
- br.Ret = 600
- } else {
- br.Ret = 408
- }
- } else {
- if this.User.LoginTime.AddDate(0, 0, 1).After(time.Now()) {
- br.Ret = 600
- } else {
- br.Ret = 408
- }
- }
- authorization := this.Ctx.Input.Header("Authorization")
- if authorization == "" {
- cookie := this.Ctx.GetCookie("rddp_access_token")
- utils.FileLog.Info("authorization:%s,cookie:%s", authorization, cookie)
- authorization = cookie
- }
- br.IsSendEmail = false
-
- if br.Ret == 408 {
- tokenCount, err := models.GetSessionCountByToken(authorization)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Ret = 408
- } else {
- br.ErrMsg = "登录失败,Err:" + err.Error()
- br.ErrMsg = "登录失败"
- br.IsSendEmail = true
- return
- }
- }
- if tokenCount > 0 {
- br.Ret = 600
- }
- }
- resp := new(models.CheckLoginResp)
- if br.Ret == 600 {
- checkPermissionCount, err := models.GetCustomPermission(this.User.CompanyId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "登录失败"
- br.ErrMsg = "校验提示权限信息失败 Err:" + err.Error()
- return
- }
- if checkPermissionCount <= 0 && this.User.CompanyId > 1 {
- resp.IsTips = true
- }
- if this.User.Mobile == "" && this.User.Email == "" {
- resp.IsBind = true
- }
- }
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
|