From 188703be231c2b2c6ec2d1b929c1d6094953f938 Mon Sep 17 00:00:00 2001 From: Chyroc Date: Fri, 14 Sep 2018 09:22:36 +0800 Subject: [PATCH] add: user update-remark api * add DecodeWithCommonError function * add UpdateRemark method --- cache/redis.go | 2 +- material/material.go | 11 ++--------- menu/menu.go | 44 ++++++++------------------------------------ user/user.go | 23 +++++++++++++++++++++-- util/error.go | 18 ++++++++++++++++++ 5 files changed, 50 insertions(+), 48 deletions(-) diff --git a/cache/redis.go b/cache/redis.go index b4039a5..7440b9e 100644 --- a/cache/redis.go +++ b/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 获取一个值 diff --git a/material/material.go b/material/material.go index a9c512e..62a5215 100644 --- a/material/material.go +++ b/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") } diff --git a/menu/menu.go b/menu/menu.go index 67fe015..aff335c 100644 --- a/menu/menu.go +++ b/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 菜单匹配 diff --git a/user/user.go b/user/user.go index 73f127c..f2c5ef9 100644 --- a/user/user.go +++ b/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") +} diff --git a/util/error.go b/util/error.go index 4a9ab64..79ad14d 100644 --- a/util/error.go +++ b/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 +}