123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- 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"
- )
- // @Title 获取图形验证码
- // @Description 获取图形验证码
- // @Success 200 {object} models.GraphicVerifyCodeResp
- // @router /pc/getGraphicVerifyCode [get]
- 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().Unix())
- 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 登录
- // @Description 登录接口
- // @Param request body models.LoginReq true "type json string"
- // @Success 200 {object} models.LoginResp
- // @router /pc/login [post]
- func (this *UserCommonController) Login() {
- 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
- }
- newUserId := 0
- userId:=0
- var isAdd bool
- if req.LoginType == 1 {
- if req.VerifyId == "" {
- br.Msg = "参数错误!"
- return
- }
- if req.VerifyCode == "" {
- br.Msg="请输入验证码"
- return
- }
- item,err:=models.GetGraphicVerifyById(req.VerifyId)
- if err!=nil {
- if err.Error()==utils.ErrNoRow() {
- br.Msg = "验证码不存在,请刷新页面!"
- return
- }
- br.Msg = "校验验证码失败!"
- return
- }
- if item.VerifyCode!=req.VerifyCode {
- br.Msg = "验证码错误,请重新输入!"
- return
- }
- checkoutTime:=time.Now().Add(15*time.Minute)
- if item.CreateTime.After(checkoutTime) {
- br.Msg = "验证码过期,请重新输入!"
- return
- }
- //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
- }
- 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 = "无效的登录方式,Err:" + err.Error()
- return
- }
- if isAdd {
- user := new(models.WxUser)
- user.CompanyId = 1
- user.CreatedTime = time.Now()
- user.FirstLogin = 1
- user.Enabled = 1
- user.RegisterTime=time.Now()
- 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(1, 0, 0)
- session.AccessToken = token
- err = models.AddSession(session)
- if err != nil {
- br.Msg = "登录失败"
- br.ErrMsg = "登录失败,新增用户session信息失败:" + err.Error()
- return
- }
- }
- userPermission, err := services.CheckUserPermission(newUserId)
- if err != nil {
- br.Msg = "登录失败"
- br.ErrMsg = "登录失败,判断权限失败:" + err.Error()
- return
- }
- err = models.ModifyFirstLogin(userId)
- if err != nil {
- br.Msg = "登录失败"
- br.ErrMsg = "登录失败,判断权限失败:" + err.Error()
- return
- }
- resp := new(models.LoginResp)
- resp.UserId = newUserId
- resp.UserPermission = userPermission
- resp.Authorization = token
- br.Ret = 200
- br.Success = true
- br.Data = resp
- br.Msg = "登录成功"
- }
- /*
- 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: "晚上好"}
- searcher.Index("1", data)
- searcher.Index("2", data1)
- searcher.IndexDoc("3", data2)
- searcher.IndexDoc("4", data3)
- searcher.IndexDoc("5", data4)
- searcher.Flush()
- req := types.SearchReq{Text: "你好"}
- search := searcher.Search(req)
- jsonStr,_:=json.Marshal(search)
- fmt.Println(string(jsonStr))
- utils.FileLog.Info("%s",string(jsonStr))
- fmt.Sprintf("end")
- }
- */
|