Browse Source

新增pc端相关接口

rdluck 4 years ago
parent
commit
ce9dc3e062
5 changed files with 304 additions and 3 deletions
  1. 256 0
      controllers/user_pc.go
  2. 1 1
      controllers/wechat.go
  3. 1 0
      models/db.go
  4. 37 0
      models/graphic_verify_code.go
  5. 9 2
      models/wx_user.go

+ 256 - 0
controllers/user_pc.go

@@ -0,0 +1,256 @@
+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.CheckGraphicVerifyCodeReq true "type json string"
+// @Success ret=200 校验验证码成功
+// @router /pc/checkGraphicVerifyCode [post]
+func (this *UserCommonController) CheckGraphicVerifyCode() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	var req models.CheckGraphicVerifyCodeReq
+	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
+	if err != nil {
+		br.Msg = "参数解析异常!"
+		br.ErrMsg = "参数解析失败,Err:" + err.Error()
+		return
+	}
+	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
+	}
+	br.Ret = 200
+	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.LoginReq
+	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 {
+		//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")
+}
+*/

+ 1 - 1
controllers/wechat.go

@@ -89,7 +89,7 @@ func (this *WechatCommonController) WechatLogin() {
 			user.FirstLogin = 1
 			user.Enabled = 1
 			user.RegisterTime=time.Now()
-			err = models.AddWxUser(user)
+			_,err = models.AddWxUser(user)
 			if wxUserInfo.Unionid != "" {
 				wxUser, err = models.GetWxUserItemByUnionid(wxUserInfo.Unionid)
 				if err != nil {

+ 1 - 0
models/db.go

@@ -40,5 +40,6 @@ func init() {
 		new(WxUser),
 		new(Session),
 		new(MsgCode),
+		new(GraphicVerifyCode),
 	)
 }

+ 37 - 0
models/graphic_verify_code.go

@@ -0,0 +1,37 @@
+package models
+
+import (
+	"rdluck_tools/orm"
+	"time"
+)
+
+type GraphicVerifyCode struct {
+	Id         int `orm:"column(id);pk"`
+	VerifyId   string
+	VerifyCode string
+	CreateTime time.Time
+}
+
+//添加用户session信息
+func AddGraphicVerifyCode(item *GraphicVerifyCode) (err error) {
+	o := orm.NewOrm()
+	_, err = o.Insert(item)
+	return
+}
+
+type GraphicVerifyCodeResp struct {
+	VerifyId   string
+	VerifyCode string
+}
+
+type CheckGraphicVerifyCodeReq struct {
+	VerifyId   string
+	VerifyCode string
+}
+
+func GetGraphicVerifyById(verifyId string) (item *GraphicVerifyCode, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT * FROM graphic_verify_code WHERE verify_id=? `
+	err = o.Raw(sql, verifyId).QueryRow(&item)
+	return
+}

+ 9 - 2
models/wx_user.go

@@ -98,9 +98,9 @@ func GetUserIsMaxPermission(companyId int) (count int, err error) {
 }
 
 //添加用户信息
-func AddWxUser(item *WxUser) (err error) {
+func AddWxUser(item *WxUser) (lastId int64,err error) {
 	o := orm.NewOrm()
-	_, err = o.Insert(item)
+	lastId, err = o.Insert(item)
 	return
 }
 
@@ -231,3 +231,10 @@ func ModifyFirstLogin(userId int) (err error) {
 	_, err = o.Raw(sql, userId).Exec()
 	return
 }
+
+
+func GetWxUserItemByEmail(email string) (item *WxUserItem, err error) {
+	sql := `SELECT * FROM wx_user WHERE email=? `
+	err = orm.NewOrm().Raw(sql, email).QueryRow(&item)
+	return
+}