rdluck 4 年之前
父节点
当前提交
19ec733a78
共有 3 个文件被更改,包括 24 次插入5 次删除
  1. 5 0
      controllers/wechat.go
  2. 9 4
      controllers/wechat_pc.go
  3. 10 1
      models/wechat.go

+ 5 - 0
controllers/wechat.go

@@ -184,6 +184,11 @@ func (this *WechatCommonController) WechatLogin() {
 	} else {
 		token = tokenItem.AccessToken
 	}
+
+	if wxUserInfo != nil {
+		go models.ModifyWxUserInfo(wxUserInfo.Nickname, wxUserInfo.Headimgurl, wxUserInfo.City, wxUserInfo.Province, wxUserInfo.Country, wxUserInfo.Sex, userId)
+	}
+
 	resp := new(models.WxLoginResp)
 	resp.UserId = userId
 	resp.Code = 0

+ 9 - 4
controllers/wechat_pc.go

@@ -111,7 +111,7 @@ func (this *WechatCommonController) PcWechatLogin() {
 			utils.FileLog.Info("用户已经存在,用户id:%d", userId)
 		}
 	} else {
-		if openId!="" {
+		if openId != "" {
 			wxUser, err := models.GetWxUserItemByOpenId(openId)
 			if err != nil {
 				br.Msg = "获取用户信息失败"
@@ -133,17 +133,17 @@ func (this *WechatCommonController) PcWechatLogin() {
 				user.Headimgurl = wxUserInfo.Headimgurl
 				user.FirstLogin = 1
 				user.Enabled = 1
-				user.RegisterPlatform=1
+				user.RegisterPlatform = 1
 				user.RegisterTime = time.Now()
 				_, err = models.AddWxUser(user)
 				wxUser, err = models.GetWxUserItemByOpenId(openId)
-				if err != nil{
+				if err != nil {
 					br.Msg = "获取用户信息失败"
 					br.ErrMsg = "unionid登录,获取微信用户信息失败,Err:" + err.Error()
 					return
 				}
 				userId = wxUser.UserId
-			}else{
+			} else {
 				userId = wxUser.UserId
 			}
 		}
@@ -155,6 +155,11 @@ func (this *WechatCommonController) PcWechatLogin() {
 		br.ErrMsg = "登录失败,判断权限失败:" + err.Error()
 		return
 	}
+
+	//更新用户微信信息
+	if wxUserInfo != nil {
+		models.ModifyWxUserInfo(wxUserInfo.Nickname, wxUserInfo.Headimgurl, wxUserInfo.City, wxUserInfo.Province, wxUserInfo.Country, wxUserInfo.Sex, userId)
+	}
 	newUser, _ := models.GetWxUserItemByUserId(userId)
 	utils.FileLog.Info("获取用户信息:%d", userId)
 

+ 10 - 1
models/wechat.go

@@ -1,5 +1,7 @@
 package models
 
+import "rdluck_tools/orm"
+
 type WechatSign struct {
 	AppId     string
 	NonceStr  string
@@ -13,4 +15,11 @@ type WxTicket struct {
 	Errcode int    `json:"errcode"`
 	Errmsg  string `json:"errmsg"`
 	Ticket  string `json:"ticket"`
-}
+}
+
+func ModifyWxUserInfo(nickName, headimgUrl, city, province, country string, sex, userId int) (err error) {
+	o := orm.NewOrm()
+	sql := `UPDATE wx_user SET nick_name=?,headimgurl=?,sex=?,city=?,province=?,country=? WHERE user_id=? `
+	_, err = o.Raw(sql, nickName, headimgUrl, sex, city, province, country, userId).Exec()
+	return
+}