Parcourir la source

Merge pull request #85 from Chyroc/add-user-updare-remark

add: user update-remark api
silenceper il y a 7 ans
Parent
commit
952a03bdc5
5 fichiers modifiés avec 50 ajouts et 48 suppressions
  1. 1 1
      cache/redis.go
  2. 2 9
      material/material.go
  3. 8 36
      menu/menu.go
  4. 21 2
      user/user.go
  5. 18 0
      util/error.go

+ 1 - 1
cache/redis.go

@@ -47,7 +47,7 @@ func NewRedis(opts *RedisOpts) *Redis {
 
 //SetConn 设置conn
 func (r *Redis) SetConn(conn *redis.Pool) {
-	r.conn = conn	
+	r.conn = conn
 }
 
 //Get 获取一个值

+ 2 - 9
material/material.go

@@ -184,13 +184,6 @@ func (material *Material) DeleteMaterial(mediaID string) error {
 	if err != nil {
 		return err
 	}
-	var resDeleteMaterial util.CommonError
-	err = json.Unmarshal(response, &resDeleteMaterial)
-	if err != nil {
-		return err
-	}
-	if resDeleteMaterial.ErrCode != 0 {
-		return fmt.Errorf("DeleteMaterial error : errcode=%v , errmsg=%v", resDeleteMaterial.ErrCode, resDeleteMaterial.ErrMsg)
-	}
-	return nil
+
+	return util.DecodeWithCommonError(response, "DeleteMaterial")
 }

+ 8 - 36
menu/menu.go

@@ -134,15 +134,8 @@ func (menu *Menu) SetMenu(buttons []*Button) error {
 	if err != nil {
 		return err
 	}
-	var commError util.CommonError
-	err = json.Unmarshal(response, &commError)
-	if err != nil {
-		return err
-	}
-	if commError.ErrCode != 0 {
-		return fmt.Errorf("SetMenu Error , errcode=%d , errmsg=%s", commError.ErrCode, commError.ErrMsg)
-	}
-	return nil
+
+	return util.DecodeWithCommonError(response, "SetMenu")
 }
 
 //GetMenu 获取菜单配置
@@ -180,15 +173,8 @@ func (menu *Menu) DeleteMenu() error {
 	if err != nil {
 		return err
 	}
-	var commError util.CommonError
-	err = json.Unmarshal(response, &commError)
-	if err != nil {
-		return err
-	}
-	if commError.ErrCode != 0 {
-		return fmt.Errorf("GetMenu Error , errcode=%d , errmsg=%s", commError.ErrCode, commError.ErrMsg)
-	}
-	return nil
+
+	return util.DecodeWithCommonError(response, "GetMenu")
 }
 
 //AddConditional 添加个性化菜单
@@ -208,15 +194,8 @@ func (menu *Menu) AddConditional(buttons []*Button, matchRule *MatchRule) error
 	if err != nil {
 		return err
 	}
-	var commError util.CommonError
-	err = json.Unmarshal(response, &commError)
-	if err != nil {
-		return err
-	}
-	if commError.ErrCode != 0 {
-		return fmt.Errorf("AddConditional Error , errcode=%d , errmsg=%s", commError.ErrCode, commError.ErrMsg)
-	}
-	return nil
+
+	return util.DecodeWithCommonError(response, "AddConditional")
 }
 
 //DeleteConditional 删除个性化菜单
@@ -235,15 +214,8 @@ func (menu *Menu) DeleteConditional(menuID int64) error {
 	if err != nil {
 		return err
 	}
-	var commError util.CommonError
-	err = json.Unmarshal(response, &commError)
-	if err != nil {
-		return err
-	}
-	if commError.ErrCode != 0 {
-		return fmt.Errorf("DeleteConditional Error , errcode=%d , errmsg=%s", commError.ErrCode, commError.ErrMsg)
-	}
-	return nil
+
+	return util.DecodeWithCommonError(response, "DeleteConditional")
 }
 
 //MenuTryMatch 菜单匹配

+ 21 - 2
user/user.go

@@ -9,7 +9,8 @@ import (
 )
 
 const (
-	userInfoURL = "https://api.weixin.qq.com/cgi-bin/user/info"
+	userInfoURL     = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN"
+	updateRemarkURL = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=%s"
 )
 
 //User 用户管理
@@ -52,7 +53,7 @@ func (user *User) GetUserInfo(openID string) (userInfo *Info, err error) {
 		return
 	}
 
-	uri := fmt.Sprintf("%s?access_token=%s&openid=%s&lang=zh_CN", userInfoURL, accessToken, openID)
+	uri := fmt.Sprintf(userInfoURL, accessToken, openID)
 	var response []byte
 	response, err = util.HTTPGet(uri)
 	if err != nil {
@@ -69,3 +70,21 @@ func (user *User) GetUserInfo(openID string) (userInfo *Info, err error) {
 	}
 	return
 }
+
+// UpdateRemark 设置用户备注名
+func (user *User) UpdateRemark(openID, remark string) (err error) {
+	var accessToken string
+	accessToken, err = user.GetAccessToken()
+	if err != nil {
+		return
+	}
+
+	uri := fmt.Sprintf(updateRemarkURL, accessToken)
+	var response []byte
+	response, err = util.PostJSON(uri, map[string]string{"openid": openID, "remark": remark})
+	if err != nil {
+		return
+	}
+
+	return util.DecodeWithCommonError(response, "UpdateRemark")
+}

+ 18 - 0
util/error.go

@@ -1,7 +1,25 @@
 package util
 
+import (
+	"encoding/json"
+	"fmt"
+)
+
 // CommonError 微信返回的通用错误json
 type CommonError struct {
 	ErrCode int64  `json:"errcode"`
 	ErrMsg  string `json:"errmsg"`
 }
+
+// DecodeWithCommonError 将返回值按照CommonError解析
+func DecodeWithCommonError(response []byte, apiName string) (err error) {
+	var commError CommonError
+	err = json.Unmarshal(response, &commError)
+	if err != nil {
+		return
+	}
+	if commError.ErrCode != 0 {
+		return fmt.Errorf("%s Error , errcode=%d , errmsg=%s", apiName, commError.ErrCode, commError.ErrMsg)
+	}
+	return nil
+}