rdluck 4 years ago
parent
commit
476ef44482
3 changed files with 61 additions and 1 deletions
  1. 29 1
      controllers/wechat.go
  2. 1 0
      models/db.go
  3. 31 0
      models/wx_user_code.go

+ 29 - 1
controllers/wechat.go

@@ -29,9 +29,26 @@ func (this *WechatCommonController) WechatLogin() {
 		this.Data["json"] = br
 		this.ServeJSON()
 	}()
+	resp := new(models.WxLoginResp)
+
 	code := this.GetString("Code")
 	fmt.Println("code:", code)
 	utils.FileLog.Info("WechatLogin code:%s", code)
+	wxCodeInfo, err := models.GetWxUserCode(code)
+	if err == nil && wxCodeInfo != nil && wxCodeInfo.Id > 0 {
+		utils.FileLog.Info("WechatLogin code exist:%s", code)
+		resp.UserId = wxCodeInfo.UserId
+		resp.Code = 0
+		resp.FirstLogin = wxCodeInfo.FirstLogin
+		resp.Authorization = wxCodeInfo.Authorization
+		resp.UserPermission = wxCodeInfo.UserPermission
+		br.Ret = 200
+		br.Success = true
+		br.Msg = "登录成功"
+		br.Data = resp
+		return
+	}
+
 	item, err := services.WxGetUserOpenIdByCode(code)
 	if err != nil {
 		br.Msg = "获取用户信息失败"
@@ -176,7 +193,18 @@ func (this *WechatCommonController) WechatLogin() {
 		loginLog.Remark = token
 		go models.AddWxUserLog(loginLog)
 	}
-	resp := new(models.WxLoginResp)
+
+	{
+		codeLog := new(models.WxUserCode)
+		codeLog.WxCode = code
+		codeLog.UserId = userId
+		codeLog.Code = 0
+		codeLog.FirstLogin = firstLogin
+		codeLog.Authorization = token
+		codeLog.UserPermission = permission
+		models.AddWxUserCode(codeLog)
+	}
+
 	resp.UserId = userId
 	resp.Code = 0
 	resp.FirstLogin = firstLogin

+ 1 - 0
models/db.go

@@ -44,5 +44,6 @@ func init() {
 		new(AnnualReport),
 		new(WxUserLog),
 		new(UserAccessRecord),
+		new(WxUserCode),
 	)
 }

+ 31 - 0
models/wx_user_code.go

@@ -0,0 +1,31 @@
+package models
+
+import (
+	"rdluck_tools/orm"
+	"time"
+)
+
+type WxUserCode struct {
+	Id             int `orm:"column(id);pk"`
+	WxCode         string
+	UserId         int
+	Code           int
+	FirstLogin     int
+	Authorization  string
+	UserPermission int
+	CreateTime     time.Time
+}
+
+//添加联系人日志信息
+func AddWxUserCode(item *WxUserCode) (lastId int64, err error) {
+	o := orm.NewOrm()
+	lastId, err = o.Insert(item)
+	return
+}
+
+func GetWxUserCode(wxCode string)(item *WxUserCode,err error)  {
+	o := orm.NewOrm()
+	sql:=`SELECT * FROM wx_user_code WHERE wx_code=? `
+	err=o.Raw(sql,wxCode).QueryRow(&item)
+	return
+}