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" ) //用户不登录 type UserPcNotAuthController struct { BasePcNotAuthController } // @Title 获取图形验证码 // @Description 获取图形验证码 // @Success 200 {object} models.GraphicVerifyCodeResp // @router /pc/getGraphicVerifyCode [get] func (this *UserPcNotAuthController) 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 } // @Title pc端获取短信验证码 // @Description pc端获取短信验证码接口 // @Param Mobile query string true "手机号码" // @Param AreaNum query string true "地区编码" // @Param VerifyId query string true "图形验证码id" // @Param VerifyCode query string true "图形验证码code" // @Success Ret=200 获取成功 // @router /pc/getSmsCode [get] func (this *UserPcNotAuthController) 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 } // @Title 登录 // @Description 登录接口 // @Param request body models.LoginReq true "type json string" // @Success 200 {object} models.LoginResp // @router /pc/login [post] func (this *UserPcNotAuthController) 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 } //BindMobile(openId, mobile string, userId, loginType int) (err error) { //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 } 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 { session := new(models.Session) session.UserId = userId session.CreatedTime = time.Now() session.LastUpdatedTime = time.Now() session.ExpireTime = time.Now().AddDate(0, 3, 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 { session := new(models.Session) session.UserId = userId session.CreatedTime = time.Now() session.LastUpdatedTime = time.Now() session.ExpireTime = time.Now().AddDate(0, 3, 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 } } //新增登录日志 { loginLog := new(models.WxUserLog) loginLog.UserId = userId loginLog.Mobile = req.Mobile loginLog.Email = req.Email loginLog.Handle = "pc_login" loginLog.CreateTime = time.Now() go models.AddWxUserLog(loginLog) } //newUser, _ := models.GetWxUserItemByUserId(userId) newUser, _ := services.GetWxUserItemByUserId(userId, utils.WxPcPlatform) 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 = "登录成功" } // @Title 绑定手机号或邮箱 // @Description 绑定手机号或邮箱 // @Param request body models.BindReq true "type json string" // @Success Ret=200 绑定成功 // @router /pc/bind [post] func (this *UserPcNotAuthController) 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 openId := this.User.OpenId utils.FileLog.Info("绑定unionId:%s", unionId) 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 } } 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 } } else { br.Msg = "无效的绑定方式" br.ErrMsg = "无效的绑定方式,bindType:" + strconv.Itoa(req.BindType) return } user, errMsg, err := services.BindWxUser(openId, req.Mobile, req.Email, req.AreaNum, 2) if err != nil { br.Msg = "绑定失败:" + err.Error() if errMsg != `` { br.Msg = errMsg } 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 } 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 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(userId) if err != nil { br.Msg = "登录失败" br.ErrMsg = "获取客户信息失败 GetWxUserItemByUserId:" + err.Error() return } //新增登录日志 { loginLog := new(models.WxUserLog) loginLog.UnionId = unionId loginLog.UserId = userId loginLog.Mobile = req.Mobile loginLog.Email = req.Email loginLog.UnionId = unionId loginLog.Handle = "pc_bind" loginLog.Remark = strconv.Itoa(userId) loginLog.CreateTime = time.Now() go models.AddWxUserLog(loginLog) } resp := new(models.LoginResp) resp.UserId = userId 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 = "绑定成功" } // @Title 校验免登陆 // @Description 校验免登陆 // @Success 200 {object} models.CheckLoginResp // @router /pc/check/login [get] func (this *UserPcNotAuthController) CheckLogin() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() //没有用户信息,代表是没有登录 if this.User == nil { br.Success = true br.Ret = 408 return } 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 //判断token是否过期 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 } // //var ( // searcher = riot.New("zh") //) // //func init() { // fmt.Sprintf("start") // data := types.DocData{Content: `I wonder how, I wonder why // , I wonder where they are`} // data1 := types.DocData{Content: "所以, 你好, 再见"} // data2 := types.DocData{Content: "没有理由"} // data3 := types.DocData{Content: "你好,中国"} // data4 := types.DocData{Content: "晚上好"} // data5 := types.DocData{Content: "中国人民,你好啊,2020年"} // data6 := types.DocData{Content: "中国的新冠疫情算基本得到了有效的控制,随着各地的纷纷复工,甚至有些省市已经开始了复学,大量的外籍华人、旅居的、留学的平时召唤都召唤不来的那些人纷纷回国,这都说明了我们伟大的中国,伟大的中国人民战胜了这次肆虐全球的瘟疫。\n\n  在这这次的抗击新冠疫情中,我们的白衣天使们,我们的专家、学者、科学家们,纷纷的冲在了抗疫第一线。看着84岁高龄的钟南山院士坐在高铁座位上睡着的照片,看着73岁的第一个提出武汉封城的李兰娟院士数次往返于武汉,脸上那一道道的长时间佩戴口罩和护目镜留下的压痕,看着一个个的花季少女剪掉秀发只是为了更好的穿戴防护装备,看着一车车的物资涌向武汉,看着一张张按着手印的奔赴疫区申请书,听着一句句熟悉而又温暖的入党誓词,感动无不充盈心腔,眼泪也从未吝啬。对最美逆行者的赞美是多么的诚挚!"} // data7 := types.DocData{Content: "在此,我除了表达对他们由衷的感动,还想说:我们中国千万万的普普通通的老百姓也是最伟大、最可爱的人。从来没有一个朝代,从来没有一个国家,像我们中国的老百姓这样如此的对待疫情。平凡的老百姓节衣缩食,也要将物资援助最需要的地方,宁愿几个月不出门,也不给国家增加一分的负担。试问世界上有哪个国家的人民有我们中国的人民对国家如母亲般的感情呢?\n\n  近期看到的最多的新闻就是国外疫情的泛滥,不说在抗疫中每个国家的责任和力量,就说普通的百姓不戴口罩到处跑的,一只狗被遛了三十二次的,最硬核的就是用棍棒打腿的,这些都说明了他们的意识,他们面对疫情的态度和我们的同胞不是一个层次,他们对待国家的情感是不能和我们比拟的!当然,我们国家也不排除有个别的另类出现,但一定会收到最严厉的惩戒。\n\n  中国人民在这次的抗击新冠疫情的战役中表现出来的智慧、勇气、精神,都使我感到作为一个中国人是多么幸运。"} // // searcher.Index("1", data) // searcher.Index("2", data1) // searcher.IndexDoc("3", data2) // searcher.IndexDoc("4", data3) // searcher.IndexDoc("5", data4) // searcher.IndexDoc("6", data5) // searcher.IndexDoc("7", data6) // searcher.IndexDoc("8", data7) // searcher.Flush() // // req := types.SearchReq{Text: "中国人民",Orderless: false,CountDocsOnly: false} // search := searcher.Search(req) // jsonStr,_:=json.Marshal(search) // fmt.Println(string(jsonStr)) // utils.FileLog.Info("%s",string(jsonStr)) // fmt.Sprintf("end") //} //