Selaa lähdekoodia

小助手优化更新头像

xingzai 3 vuotta sitten
vanhempi
commit
23f3802625
3 muutettua tiedostoa jossa 52 lisäystä ja 12 poistoa
  1. 19 4
      controllers/wechat.go
  2. 25 3
      models/cygx_user_record.go
  3. 8 5
      services/wechat.go

+ 19 - 4
controllers/wechat.go

@@ -51,6 +51,7 @@ func (this *WechatCommonController) WechatLoginByxzs() {
 		br.ErrMsg = "获取openid失败,openid:" + item.Openid
 		return
 	}
+
 	resp := new(models.UserDetailByUserLogin)
 	accessToken, err := services.GetWxAccessTokenByXzs()
 	if err != nil {
@@ -87,17 +88,30 @@ func (this *WechatCommonController) WechatLoginByxzs() {
 		br.ErrMsg = "查询数量失败,Err:" + err.Error()
 		return
 	}
+	items := new(models.CygxUserRecord)
+	items.OpenId = openId
+	items.UnionId = unionId
+	items.NickName = wxUserInfo.Nickname
+	items.Sex = wxUserInfo.Sex
+	items.Province = wxUserInfo.Province
+	items.City = wxUserInfo.City
+	items.Country = wxUserInfo.Country
+	items.Headimgurl = wxUserInfo.Headimgurl
+	items.CreateTime = time.Now()
 	if total == 0 {
-		items := new(models.CygxUserRecord)
-		items.OpenId = openId
-		items.UnionId = unionId
-		items.CreateTime = time.Now()
 		_, err = models.AddCygxUserRecord(items)
 		if err != nil {
 			br.Msg = "获取用户信息失败"
 			br.ErrMsg = "添加openid失败,Err:" + err.Error()
 			return
 		}
+	} else {
+		err = models.UpdateCygxUserRecord(items)
+		if err != nil {
+			br.Msg = "获取用户信息失败"
+			br.ErrMsg = "添加openid失败,Err:" + err.Error()
+			return
+		}
 	}
 	user, err := models.GetWxUserItemByUserUnionId(unionId)
 	if err != nil && err.Error() != utils.ErrNoRow() {
@@ -125,6 +139,7 @@ func (this *WechatCommonController) WechatLoginByxzs() {
 			resp.RealName = user.RealName
 			resp.HasPermission = 2
 		}
+		_ = models.UpdateUserRecord(items)
 	}
 	resp.Headimgurl = wxUserInfo.Headimgurl
 	br.Ret = 200

+ 25 - 3
models/cygx_user_record.go

@@ -6,13 +6,19 @@ import (
 )
 
 type CygxUserRecord struct {
-	UserRecordId int       `orm:"column(user_record_id);pk" description:"id"`
+	UserRecordId int       `orm:"column(user_record_id);pk"`
 	OpenId       string    `description:"用户openid,最大长度:32"`
 	UnionId      string    `description:"用户unionid,最大长度:64"`
-	CreateTime   time.Time `description:"提交建议时间"`
+	NickName     string    `descritpion:"用户昵称,最大长度:32"`
+	Sex          int       `descritpion:"普通用户性别,1为男性,2为女性"`
+	Province     string    `description:"普通用户个人资料填写的省份,最大长度:30"`
+	City         string    `description:"普通用户个人资料填写的城市,最大长度:30"`
+	Country      string    `description:"国家,如中国为CN,最大长度:30"`
+	Headimgurl   string    `description:"用户第三方(微信)头像,最大长度:512"`
+	CreateTime   time.Time `description:"创建时间,关系添加时间、用户授权时间"`
 }
 
-//添加优化建议
+//添加
 func AddCygxUserRecord(item *CygxUserRecord) (lastId int64, err error) {
 	o := orm.NewOrm()
 	lastId, err = o.Insert(item)
@@ -26,3 +32,19 @@ func GetCygxUserRecordCount(openId string) (count int, err error) {
 	err = o.Raw(sqlCount, openId).QueryRow(&count)
 	return
 }
+
+//修改
+func UpdateCygxUserRecord(item *CygxUserRecord) (err error) {
+	o := orm.NewOrm()
+	msql := ` UPDATE cygx_user_record SET nick_name = ?,sex=?,province=?,city=? ,country=? ,headimgurl=?  WHERE open_id = ? `
+	_, err = o.Raw(msql, item.NickName, item.Sex, item.Province, item.City, item.Country, item.Headimgurl, item.OpenId).Exec()
+	return
+}
+
+//修改
+func UpdateUserRecord(item *CygxUserRecord) (err error) {
+	o := orm.NewOrm()
+	msql := ` UPDATE user_record SET nick_name = ?,sex=?,province=?,city=? ,country=? ,headimgurl=?  WHERE union_id = ? AND create_platform = 4  `
+	_, err = o.Raw(msql, item.NickName, item.Sex, item.Province, item.City, item.Country, item.Headimgurl, item.UnionId).Exec()
+	return
+}

+ 8 - 5
services/wechat.go

@@ -64,17 +64,20 @@ type WxToken struct {
 func WxGetToken() (item *WxToken, err error) {
 	requestUrl := `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s`
 	requestUrl = fmt.Sprintf(requestUrl, utils.WxPublicAppId, utils.WxPublicAppSecret)
-	fmt.Println("requestUrl:", requestUrl)
 	result, err := http.Get(requestUrl)
-	utils.FileLog.Info("WxGetToken Result:%s ", string(result))
 	if err != nil {
 		utils.FileLog.Info("获取wxToken失败,Err:%s", err.Error())
 		return nil, err
 	}
 	err = json.Unmarshal(result, &item)
-	fmt.Println("WxGetToken start")
-	fmt.Println(string(result))
-	fmt.Println("WxGetToken end")
+	if err != nil {
+		fmt.Println(fmt.Sprintf("GetWxToken Unmarshal Err:%s", err.Error()))
+		return
+	}
+	if item.Errmsg != "" {
+		err = fmt.Errorf(item.Errmsg)
+		utils.FileLog.Info(fmt.Sprintf("GetWxToken fail result:%s", string(result)))
+	}
 	return
 }