From 868b31cb3d1f3c6c87faeeda49eb70f1d938a912 Mon Sep 17 00:00:00 2001 From: silenceper Date: Wed, 24 Jun 2020 14:36:33 +0800 Subject: [PATCH] fix golangci-lint failed (#284) * fix golangci-lint error --- .golangci.yml | 53 ++++++++ cache/memory.go | 6 +- cache/redis.go | 5 +- credential/default_js_ticket.go | 3 + miniprogram/encryptor/encryptor.go | 21 ++-- miniprogram/qrcode/qrcode.go | 10 +- miniprogram/subscribe/subscribe.go | 4 +- miniprogram/tcb/file.go | 2 +- officialaccount/broadcast/broadcast.go | 113 +++++++++--------- officialaccount/datacube/broadcast.go | 1 + officialaccount/datacube/interface.go | 1 + officialaccount/datacube/message.go | 1 + officialaccount/datacube/publisher.go | 50 ++------ officialaccount/datacube/user.go | 1 + officialaccount/device/authorize.go | 4 +- officialaccount/js/js.go | 2 +- officialaccount/material/material.go | 9 +- officialaccount/material/media.go | 1 - officialaccount/menu/menu.go | 8 +- officialaccount/message/customer_message.go | 3 + officialaccount/message/template.go | 4 +- officialaccount/message/transfer_customer.go | 6 +- officialaccount/oauth/oauth.go | 2 +- officialaccount/officialaccount.go | 3 +- officialaccount/server/server.go | 4 +- openplatform/context/accessToken.go | 4 +- .../officialaccount/officialaccount.go | 1 - util/crypto.go | 2 +- util/signature.go | 2 +- util/time.go | 2 +- wechat.go | 2 - 31 files changed, 175 insertions(+), 155 deletions(-) create mode 100644 .golangci.yml diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..85e4d15 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,53 @@ +linters: + # please, do not use `enable-all`: it's deprecated and will be removed soon. + # inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint + disable-all: true + enable: + - bodyclose + - deadcode + - depguard + - dogsled + - dupl + - errcheck + - funlen + - goconst + - gocritic + - gocyclo + - gofmt + - goimports + - golint + - goprintffuncname + - gosimple + - govet + - ineffassign + - interfacer + - misspell + - nolintlint + - rowserrcheck + - scopelint + - staticcheck + - structcheck + - stylecheck + - typecheck + - unconvert + - unparam + - unused + - varcheck + - whitespace + +issues: + # Excluding configuration per-path, per-linter, per-text and per-source + exclude-rules: + - path: _test\.go + linters: + - gomnd + + # https://github.com/go-critic/go-critic/issues/926 + - linters: + - gocritic + text: "unnecessaryDefer:" + +linters-settings: + funlen: + lines: 66 + statements: 40 \ No newline at end of file diff --git a/cache/memory.go b/cache/memory.go index 5bf115b..7206637 100644 --- a/cache/memory.go +++ b/cache/memory.go @@ -62,13 +62,13 @@ func (mem *Memory) Set(key string, val interface{}, timeout time.Duration) (err //Delete delete value in memcache. func (mem *Memory) Delete(key string) error { - return mem.deleteKey(key) + mem.deleteKey(key) + return nil } // deleteKey -func (mem *Memory) deleteKey(key string) error { +func (mem *Memory) deleteKey(key string) { mem.Lock() defer mem.Unlock() delete(mem.data, key) - return nil } diff --git a/cache/redis.go b/cache/redis.go index 5d6b8f3..e314739 100644 --- a/cache/redis.go +++ b/cache/redis.go @@ -95,10 +95,7 @@ func (r *Redis) IsExist(key string) bool { a, _ := conn.Do("EXISTS", key) i := a.(int64) - if i > 0 { - return true - } - return false + return i > 0 } //Delete 删除 diff --git a/credential/default_js_ticket.go b/credential/default_js_ticket.go index 5567424..1d4d7a0 100644 --- a/credential/default_js_ticket.go +++ b/credential/default_js_ticket.go @@ -68,6 +68,9 @@ func GetTicketFromServer(accessToken string) (ticket ResTicket, err error) { var response []byte url := fmt.Sprintf(getTicketURL, accessToken) response, err = util.HTTPGet(url) + if err != nil { + return + } err = json.Unmarshal(response, &ticket) if err != nil { return diff --git a/miniprogram/encryptor/encryptor.go b/miniprogram/encryptor/encryptor.go index e9b4b76..788054a 100644 --- a/miniprogram/encryptor/encryptor.go +++ b/miniprogram/encryptor/encryptor.go @@ -22,7 +22,6 @@ func NewEncryptor(context *context.Context) *Encryptor { return basic } - var ( // ErrAppIDNotMatch appid不匹配 ErrAppIDNotMatch = errors.New("app id not match") @@ -36,19 +35,19 @@ var ( // PlainData 用户信息/手机号信息 type PlainData struct { - OpenID string `json:"openId"` - UnionID string `json:"unionId"` - NickName string `json:"nickName"` - Gender int `json:"gender"` - City string `json:"city"` - Province string `json:"province"` - Country string `json:"country"` - AvatarURL string `json:"avatarUrl"` - Language string `json:"language"` + OpenID string `json:"openId"` + UnionID string `json:"unionId"` + NickName string `json:"nickName"` + Gender int `json:"gender"` + City string `json:"city"` + Province string `json:"province"` + Country string `json:"country"` + AvatarURL string `json:"avatarUrl"` + Language string `json:"language"` PhoneNumber string `json:"phoneNumber"` PurePhoneNumber string `json:"purePhoneNumber"` CountryCode string `json:"countryCode"` - Watermark struct { + Watermark struct { Timestamp int64 `json:"timestamp"` AppID string `json:"appid"` } `json:"watermark"` diff --git a/miniprogram/qrcode/qrcode.go b/miniprogram/qrcode/qrcode.go index 995ffc0..26e3db7 100644 --- a/miniprogram/qrcode/qrcode.go +++ b/miniprogram/qrcode/qrcode.go @@ -74,15 +74,13 @@ func (qrCode *QRCode) fetchCode(urlStr string, body interface{}) (response []byt err = fmt.Errorf("fetchCode error : errcode=%v , errmsg=%v", result.ErrCode, result.ErrMsg) return nil, err } - } else if contentType == "image/jpeg" { + } + if contentType == "image/jpeg" { // 返回文件 return response, nil - } else { - err = fmt.Errorf("fetchCode error : unknown response content type - %v", contentType) - return nil, err } - - return + err = fmt.Errorf("fetchCode error : unknown response content type - %v", contentType) + return nil, err } // CreateWXAQRCode 获取小程序二维码,适用于需要的码数量较少的业务场景 diff --git a/miniprogram/subscribe/subscribe.go b/miniprogram/subscribe/subscribe.go index 86b9c26..89a0ad0 100644 --- a/miniprogram/subscribe/subscribe.go +++ b/miniprogram/subscribe/subscribe.go @@ -47,6 +47,8 @@ func (s *Subscribe) Send(msg *Message) (err error) { } uri := fmt.Sprintf("%s?access_token=%s", subscribeSendURL, accessToken) response, err := util.PostJSON(uri, msg) - + if err != nil { + return + } return util.DecodeWithCommonError(response, "Send") } diff --git a/miniprogram/tcb/file.go b/miniprogram/tcb/file.go index bfee9ea..89c8c3e 100644 --- a/miniprogram/tcb/file.go +++ b/miniprogram/tcb/file.go @@ -130,5 +130,5 @@ func (tcb *Tcb) BatchDeleteFile(env string, fileIDList []string) (*BatchDeleteFi } batchDeleteFileRes := &BatchDeleteFileRes{} err = util.DecodeWithError(response, batchDeleteFileRes, "BatchDeleteFile") - return batchDeleteFileRes, nil + return batchDeleteFileRes, err } diff --git a/officialaccount/broadcast/broadcast.go b/officialaccount/broadcast/broadcast.go index 84f016f..eab1acd 100644 --- a/officialaccount/broadcast/broadcast.go +++ b/officialaccount/broadcast/broadcast.go @@ -8,9 +8,9 @@ import ( ) const ( - sendURLByTag = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall" + sendURLByTag = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall" sendURLByOpenID = "https://api.weixin.qq.com/cgi-bin/message/mass/send" - deleteSendURL ="https://api.weixin.qq.com/cgi-bin/message/mass/delete" + deleteSendURL = "https://api.weixin.qq.com/cgi-bin/message/mass/delete" ) //MsgType 发送消息类型 @@ -69,17 +69,17 @@ type sendRequest struct { //发送图片 Images *Image `json:"images,omitempty"` //发送卡券 - WxCard map[string]interface{} `json:"wxcard,omitempty"` - MsgType MsgType `json:"msgtype"` - SendIgnoreReprint int32 `json:"send_ignore_reprint,omitempty"` + WxCard map[string]interface{} `json:"wxcard,omitempty"` + MsgType MsgType `json:"msgtype"` + SendIgnoreReprint int32 `json:"send_ignore_reprint,omitempty"` } //Image 发送图片 -type Image struct{ - MediaIDs []string `json:"media_ids"` - Recommend string `json:"recommend"` - NeedOpenComment int32 `json:"need_open_comment"` - OnlyFansCanComment int32 `json:"only_fans_can_comment"` +type Image struct { + MediaIDs []string `json:"media_ids"` + Recommend string `json:"recommend"` + NeedOpenComment int32 `json:"need_open_comment"` + OnlyFansCanComment int32 `json:"only_fans_can_comment"` } //SendText 群发文本 @@ -92,13 +92,13 @@ func (broadcast *Broadcast) SendText(user *User, content string) (*Result, error return nil, err } req := &sendRequest{ - ToUser: nil, + ToUser: nil, MsgType: MsgTypeText, } - req.Text=map[string]interface{}{ - "content":content, + req.Text = map[string]interface{}{ + "content": content, } - req,sendURL:=broadcast.chooseTagOrOpenID(user,req) + req, sendURL := broadcast.chooseTagOrOpenID(user, req) url := fmt.Sprintf("%s?access_token=%s", sendURL, ak) data, err := util.PostJSON(url, req) if err != nil { @@ -110,22 +110,22 @@ func (broadcast *Broadcast) SendText(user *User, content string) (*Result, error } //SendNews 发送图文 -func (broadcast *Broadcast) SendNews(user *User, mediaID string,ignoreReprint bool) (*Result, error) { +func (broadcast *Broadcast) SendNews(user *User, mediaID string, ignoreReprint bool) (*Result, error) { ak, err := broadcast.GetAccessToken() if err != nil { return nil, err } req := &sendRequest{ - ToUser: nil, + ToUser: nil, MsgType: MsgTypeNews, } - if ignoreReprint{ - req.SendIgnoreReprint=1 + if ignoreReprint { + req.SendIgnoreReprint = 1 } - req.Mpnews=map[string]interface{}{ - "media_id":mediaID, + req.Mpnews = map[string]interface{}{ + "media_id": mediaID, } - req,sendURL:=broadcast.chooseTagOrOpenID(user,req) + req, sendURL := broadcast.chooseTagOrOpenID(user, req) url := fmt.Sprintf("%s?access_token=%s", sendURL, ak) data, err := util.PostJSON(url, req) if err != nil { @@ -136,7 +136,6 @@ func (broadcast *Broadcast) SendNews(user *User, mediaID string,ignoreReprint bo return res, err } - //SendVoice 发送语音 func (broadcast *Broadcast) SendVoice(user *User, mediaID string) (*Result, error) { ak, err := broadcast.GetAccessToken() @@ -144,13 +143,13 @@ func (broadcast *Broadcast) SendVoice(user *User, mediaID string) (*Result, erro return nil, err } req := &sendRequest{ - ToUser: nil, + ToUser: nil, MsgType: MsgTypeVoice, } - req.Voice=map[string]interface{}{ - "media_id":mediaID, + req.Voice = map[string]interface{}{ + "media_id": mediaID, } - req,sendURL:=broadcast.chooseTagOrOpenID(user,req) + req, sendURL := broadcast.chooseTagOrOpenID(user, req) url := fmt.Sprintf("%s?access_token=%s", sendURL, ak) data, err := util.PostJSON(url, req) if err != nil { @@ -168,11 +167,11 @@ func (broadcast *Broadcast) SendImage(user *User, images *Image) (*Result, error return nil, err } req := &sendRequest{ - ToUser: nil, + ToUser: nil, MsgType: MsgTypeImage, } - req.Images=images - req,sendURL:=broadcast.chooseTagOrOpenID(user,req) + req.Images = images + req, sendURL := broadcast.chooseTagOrOpenID(user, req) url := fmt.Sprintf("%s?access_token=%s", sendURL, ak) data, err := util.PostJSON(url, req) if err != nil { @@ -183,23 +182,22 @@ func (broadcast *Broadcast) SendImage(user *User, images *Image) (*Result, error return res, err } - //SendVideo 发送视频 -func (broadcast *Broadcast) SendVideo(user *User, mediaID string,title,description string) (*Result, error) { +func (broadcast *Broadcast) SendVideo(user *User, mediaID string, title, description string) (*Result, error) { ak, err := broadcast.GetAccessToken() if err != nil { return nil, err } req := &sendRequest{ - ToUser: nil, + ToUser: nil, MsgType: MsgTypeVideo, } - req.Voice=map[string]interface{}{ - "media_id":mediaID, - "title":title, - "description":description, + req.Voice = map[string]interface{}{ + "media_id": mediaID, + "title": title, + "description": description, } - req,sendURL:=broadcast.chooseTagOrOpenID(user,req) + req, sendURL := broadcast.chooseTagOrOpenID(user, req) url := fmt.Sprintf("%s?access_token=%s", sendURL, ak) data, err := util.PostJSON(url, req) if err != nil { @@ -210,7 +208,6 @@ func (broadcast *Broadcast) SendVideo(user *User, mediaID string,title,descripti return res, err } - //SendWxCard 发送卡券 func (broadcast *Broadcast) SendWxCard(user *User, cardID string) (*Result, error) { ak, err := broadcast.GetAccessToken() @@ -218,13 +215,13 @@ func (broadcast *Broadcast) SendWxCard(user *User, cardID string) (*Result, erro return nil, err } req := &sendRequest{ - ToUser: nil, + ToUser: nil, MsgType: MsgTypeWxCard, } - req.WxCard=map[string]interface{}{ - "card_id":cardID, + req.WxCard = map[string]interface{}{ + "card_id": cardID, } - req,sendURL:=broadcast.chooseTagOrOpenID(user,req) + req, sendURL := broadcast.chooseTagOrOpenID(user, req) url := fmt.Sprintf("%s?access_token=%s", sendURL, ak) data, err := util.PostJSON(url, req) if err != nil { @@ -234,11 +231,12 @@ func (broadcast *Broadcast) SendWxCard(user *User, cardID string) (*Result, erro err = util.DecodeWithError(data, res, "SendWxCard") return res, err } + //Delete 删除群发消息 -func (broadcast *Broadcast) Delete(msgID int64 ,articleIDx int64) error { +func (broadcast *Broadcast) Delete(msgID int64, articleIDx int64) error { ak, err := broadcast.GetAccessToken() if err != nil { - return err + return err } req := map[string]interface{}{ "msg_id": msgID, @@ -247,33 +245,32 @@ func (broadcast *Broadcast) Delete(msgID int64 ,articleIDx int64) error { url := fmt.Sprintf("%s?access_token=%s", deleteSendURL, ak) data, err := util.PostJSON(url, req) if err != nil { - return err + return err } return util.DecodeWithCommonError(data, "Delete") } - //TODO 发送预览,群发消息状态,发送速度 -func (broadcast *Broadcast) chooseTagOrOpenID(user *User,req *sendRequest)(ret *sendRequest,url string){ - sendURL:="" +func (broadcast *Broadcast) chooseTagOrOpenID(user *User, req *sendRequest) (ret *sendRequest, url string) { + sendURL := "" if user == nil { - req.Filter=map[string]interface{}{ - "is_to_all":true, + req.Filter = map[string]interface{}{ + "is_to_all": true, } - sendURL=sendURLByTag + sendURL = sendURLByTag } else { if user.TagID != 0 { - req.Filter=map[string]interface{}{ - "is_to_all":false, - "tag_id":user.TagID, + req.Filter = map[string]interface{}{ + "is_to_all": false, + "tag_id": user.TagID, } - sendURL=sendURLByTag + sendURL = sendURLByTag } if len(user.OpenID) != 0 { req.ToUser = user.OpenID - sendURL=sendURLByOpenID + sendURL = sendURLByOpenID } } - return req,sendURL -} \ No newline at end of file + return req, sendURL +} diff --git a/officialaccount/datacube/broadcast.go b/officialaccount/datacube/broadcast.go index 2fd980b..4a3e1a4 100644 --- a/officialaccount/datacube/broadcast.go +++ b/officialaccount/datacube/broadcast.go @@ -2,6 +2,7 @@ package datacube import ( "fmt" + "github.com/silenceper/wechat/v2/util" ) diff --git a/officialaccount/datacube/interface.go b/officialaccount/datacube/interface.go index 23046df..ea6f839 100644 --- a/officialaccount/datacube/interface.go +++ b/officialaccount/datacube/interface.go @@ -2,6 +2,7 @@ package datacube import ( "fmt" + "github.com/silenceper/wechat/v2/util" ) diff --git a/officialaccount/datacube/message.go b/officialaccount/datacube/message.go index eccad61..4ec3fd7 100644 --- a/officialaccount/datacube/message.go +++ b/officialaccount/datacube/message.go @@ -2,6 +2,7 @@ package datacube import ( "fmt" + "github.com/silenceper/wechat/v2/util" ) diff --git a/officialaccount/datacube/publisher.go b/officialaccount/datacube/publisher.go index 29a4075..267a550 100644 --- a/officialaccount/datacube/publisher.go +++ b/officialaccount/datacube/publisher.go @@ -1,11 +1,11 @@ package datacube import ( - "encoding/json" "fmt" - "github.com/silenceper/wechat/v2/util" "net/url" "strconv" + + "github.com/silenceper/wechat/v2/util" ) //AdSlot 广告位类型 @@ -55,9 +55,8 @@ type BaseResp struct { //ResPublisherAdPos 公众号分广告位数据响应 type ResPublisherAdPos struct { util.CommonError - BaseResp - Base BaseResp `json:"base_resp"` + BaseResp BaseResp `json:"base_resp"` List []ResAdPosList `json:"list"` Summary ResAdPosSummary `json:"summary"` TotalNum int `json:"total_num"` @@ -91,9 +90,8 @@ type ResAdPosSummary struct { //ResPublisherCps 公众号返佣商品数据响应 type ResPublisherCps struct { util.CommonError - BaseResp - Base BaseResp `json:"base_resp"` + BaseResp BaseResp `json:"base_resp"` List []ResCpsList `json:"list"` Summary ResCpsSummary `json:"summary"` TotalNum int `json:"total_num"` @@ -125,9 +123,8 @@ type ResCpsSummary struct { //ResPublisherSettlement 公众号结算收入数据及结算主体信息响应 type ResPublisherSettlement struct { util.CommonError - BaseResp - Base BaseResp `json:"base_resp"` + BaseResp BaseResp `json:"base_resp"` Body string `json:"body"` PenaltyAll int `json:"penalty_all"` RevenueAll int64 `json:"revenue_all"` @@ -208,25 +205,15 @@ func (cube *DataCube) GetPublisherAdPosGeneral(startDate, endDate string, page, return } - err = json.Unmarshal(response, &resPublisherAdPos) + err = util.DecodeWithError(response, &resPublisherAdPos, "GetPublisherAdPosGeneral") if err != nil { return } - if resPublisherAdPos.CommonError.ErrCode != 0 { - err = fmt.Errorf("GetPublisherAdPosGeneral Error , errcode=%d , errmsg=%s", resPublisherAdPos.CommonError.ErrCode, resPublisherAdPos.CommonError.ErrMsg) - return - } - if resPublisherAdPos.BaseResp.Ret != 0 { err = fmt.Errorf("GetPublisherAdPosGeneral Error , errcode=%d , errmsg=%s", resPublisherAdPos.BaseResp.Ret, resPublisherAdPos.BaseResp.ErrMsg) return } - - if resPublisherAdPos.Base.Ret != 0 { - err = fmt.Errorf("GetPublisherAdPosGeneral Error , errcode=%d , errmsg=%s", resPublisherAdPos.Base.Ret, resPublisherAdPos.Base.ErrMsg) - return - } return } @@ -245,26 +232,15 @@ func (cube *DataCube) GetPublisherCpsGeneral(startDate, endDate string, page, pa return } - err = json.Unmarshal(response, &resPublisherCps) + err = util.DecodeWithError(response, &resPublisherCps, "GetPublisherCpsGeneral") if err != nil { return } - if resPublisherCps.CommonError.ErrCode != 0 { - err = fmt.Errorf("GetPublisherCpsGeneral Error , errcode=%d , errmsg=%s", resPublisherCps.CommonError.ErrCode, resPublisherCps.CommonError.ErrMsg) - return - } - if resPublisherCps.BaseResp.Ret != 0 { err = fmt.Errorf("GetPublisherCpsGeneral Error , errcode=%d , errmsg=%s", resPublisherCps.BaseResp.Ret, resPublisherCps.BaseResp.ErrMsg) return } - - if resPublisherCps.Base.Ret != 0 { - err = fmt.Errorf("GetPublisherCpsGeneral Error , errcode=%d , errmsg=%s", resPublisherCps.Base.Ret, resPublisherCps.Base.ErrMsg) - return - } - return } @@ -283,24 +259,14 @@ func (cube *DataCube) GetPublisherSettlement(startDate, endDate string, page, pa return } - err = json.Unmarshal(response, &resPublisherSettlement) + err = util.DecodeWithError(response, &resPublisherSettlement, "GetPublisherSettlement") if err != nil { return } - if resPublisherSettlement.CommonError.ErrCode != 0 { - err = fmt.Errorf("GetPublisherSettlement Error , errcode=%d , errmsg=%s", resPublisherSettlement.CommonError.ErrCode, resPublisherSettlement.CommonError.ErrMsg) - return - } - if resPublisherSettlement.BaseResp.Ret != 0 { err = fmt.Errorf("GetPublisherSettlement Error , errcode=%d , errmsg=%s", resPublisherSettlement.BaseResp.Ret, resPublisherSettlement.BaseResp.ErrMsg) return } - - if resPublisherSettlement.Base.Ret != 0 { - err = fmt.Errorf("GetPublisherSettlement Error , errcode=%d , errmsg=%s", resPublisherSettlement.Base.Ret, resPublisherSettlement.Base.ErrMsg) - return - } return } diff --git a/officialaccount/datacube/user.go b/officialaccount/datacube/user.go index 63ad0b1..e4242d0 100644 --- a/officialaccount/datacube/user.go +++ b/officialaccount/datacube/user.go @@ -2,6 +2,7 @@ package datacube import ( "fmt" + "github.com/silenceper/wechat/v2/util" ) diff --git a/officialaccount/device/authorize.go b/officialaccount/device/authorize.go index de02647..221bd83 100644 --- a/officialaccount/device/authorize.go +++ b/officialaccount/device/authorize.go @@ -41,7 +41,7 @@ type ReqDevice struct { // ble – 3 // wifi -- 4 // 一个设备可以支持多种连接类型,用符号"|"做分割,客户端优先选择靠前的连接方式(优先级按|关系的排序依次降低),举例: - // 1:表示设备仅支持andiod classic bluetooth 1|2:表示设备支持andiod 和ios 两种classic bluetooth,但是客户端优先选择andriod classic bluetooth 协议,如果andriod classic bluetooth协议连接失败,再选择ios classic bluetooth协议进行连接 + // 1:表示设备仅支持andiod classic bluetooth 1|2:表示设备支持android 和ios 两种classic bluetooth,但是客户端优先选择android classic bluetooth 协议,如果android classic bluetooth协议连接失败,再选择ios classic bluetooth协议进行连接 // (注:安卓平台不同时支持BLE和classic类型) ConnectProtocol string `json:"connect_protocol"` //auth及通信的加密key,第三方需要将key烧制在设备上(128bit),格式采用16进制串的方式(长度为32字节),不需要0X前缀,如: 1234567890ABCDEF1234567890ABCDEF @@ -56,7 +56,7 @@ type ReqDevice struct { // 0:不加密的version // 1:version 1 AuthVer string `json:"auth_ver"` - // 表示mac地址在厂商广播manufature data里含有mac地址的偏移,取值如下: + // 表示mac地址在厂商广播manufacture data里含有mac地址的偏移,取值如下: // -1:在尾部、 // -2:表示不包含mac地址 其他:非法偏移 ManuMacPos string `json:"manu_mac_pos"` diff --git a/officialaccount/js/js.go b/officialaccount/js/js.go index 1c1b77b..a2a0683 100644 --- a/officialaccount/js/js.go +++ b/officialaccount/js/js.go @@ -52,7 +52,7 @@ func (js *Js) GetConfig(uri string) (config *Config, err error) { } nonceStr := util.RandomStr(16) - timestamp := util.GetCurrTs() + timestamp := util.GetCurrTS() str := fmt.Sprintf("jsapi_ticket=%s&noncestr=%s×tamp=%d&url=%s", ticketStr, nonceStr, timestamp, uri) sigStr := util.Signature(str) diff --git a/officialaccount/material/material.go b/officialaccount/material/material.go index 1a05b0a..2fe3f4b 100644 --- a/officialaccount/material/material.go +++ b/officialaccount/material/material.go @@ -10,10 +10,10 @@ import ( ) const ( - addNewsURL = "https://api.weixin.qq.com/cgi-bin/material/add_news" - addMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/add_material" - delMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/del_material" - getMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/get_material" + addNewsURL = "https://api.weixin.qq.com/cgi-bin/material/add_news" + addMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/add_material" + delMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/del_material" + getMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/get_material" batchGetMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/batchget_material" ) @@ -30,6 +30,7 @@ const ( //PermanentMaterialTypeNews 永久素材图文类型(news) PermanentMaterialTypeNews PermanentMaterialType = "news" ) + //Material 素材管理 type Material struct { *context.Context diff --git a/officialaccount/material/media.go b/officialaccount/material/media.go index 093f206..7d85af1 100644 --- a/officialaccount/material/media.go +++ b/officialaccount/material/media.go @@ -106,5 +106,4 @@ func (material *Material) ImageUpload(filename string) (url string, err error) { } url = image.URL return - } diff --git a/officialaccount/menu/menu.go b/officialaccount/menu/menu.go index 5c31d4c..b495bb5 100644 --- a/officialaccount/menu/menu.go +++ b/officialaccount/menu/menu.go @@ -102,12 +102,12 @@ type ButtonNew struct { //MatchRule 个性化菜单规则 type MatchRule struct { - GroupID string `json:"group_id,omitempty"` - Sex string `json:"sex,omitempty"` + GroupID string `json:"group_id,omitempty"` + Sex string `json:"sex,omitempty"` Country string `json:"country,omitempty"` Province string `json:"province,omitempty"` City string `json:"city,omitempty"` - ClientPlatformType string `json:"client_platform_type,omitempty"` + ClientPlatformType string `json:"client_platform_type,omitempty"` Language string `json:"language,omitempty"` } @@ -138,7 +138,6 @@ func (menu *Menu) SetMenu(buttons []*Button) error { return util.DecodeWithCommonError(response, "SetMenu") } - //SetMenuByJSON 设置按钮 func (menu *Menu) SetMenuByJSON(jsonInfo string) error { accessToken, err := menu.GetAccessToken() @@ -216,7 +215,6 @@ func (menu *Menu) AddConditional(buttons []*Button, matchRule *MatchRule) error return util.DecodeWithCommonError(response, "AddConditional") } - //AddConditionalByJSON 添加个性化菜单 func (menu *Menu) AddConditionalByJSON(jsonInfo string) error { accessToken, err := menu.GetAccessToken() diff --git a/officialaccount/message/customer_message.go b/officialaccount/message/customer_message.go index ddbbdb9..c7abb65 100644 --- a/officialaccount/message/customer_message.go +++ b/officialaccount/message/customer_message.go @@ -147,6 +147,9 @@ func (manager *Manager) Send(msg *CustomerMessage) error { } uri := fmt.Sprintf("%s?access_token=%s", customerSendMessage, accessToken) response, err := util.PostJSON(uri, msg) + if err != nil { + return err + } var result util.CommonError err = json.Unmarshal(response, &result) if err != nil { diff --git a/officialaccount/message/template.go b/officialaccount/message/template.go index 65c85a4..355709d 100644 --- a/officialaccount/message/template.go +++ b/officialaccount/message/template.go @@ -59,7 +59,9 @@ func (tpl *Template) Send(msg *TemplateMessage) (msgID int64, err error) { } uri := fmt.Sprintf("%s?access_token=%s", templateSendURL, accessToken) response, err := util.PostJSON(uri, msg) - + if err != nil { + return + } var result resTemplateSend err = json.Unmarshal(response, &result) if err != nil { diff --git a/officialaccount/message/transfer_customer.go b/officialaccount/message/transfer_customer.go index e502cc0..af336e8 100644 --- a/officialaccount/message/transfer_customer.go +++ b/officialaccount/message/transfer_customer.go @@ -13,11 +13,11 @@ type TransInfo struct { } //NewTransferCustomer 实例化 -func NewTransferCustomer(KfAccount string) *TransferCustomer { +func NewTransferCustomer(kfAccount string) *TransferCustomer { tc := new(TransferCustomer) - if KfAccount != "" { + if kfAccount != "" { transInfo := new(TransInfo) - transInfo.KfAccount = KfAccount + transInfo.KfAccount = kfAccount tc.TransInfo = transInfo } return tc diff --git a/officialaccount/oauth/oauth.go b/officialaccount/oauth/oauth.go index c9290e9..94c84bd 100644 --- a/officialaccount/oauth/oauth.go +++ b/officialaccount/oauth/oauth.go @@ -50,7 +50,7 @@ func (oauth *Oauth) Redirect(writer http.ResponseWriter, req *http.Request, redi if err != nil { return err } - http.Redirect(writer, req, location, 302) + http.Redirect(writer, req, location, http.StatusFound) return nil } diff --git a/officialaccount/officialaccount.go b/officialaccount/officialaccount.go index 99283ee..d9226ac 100644 --- a/officialaccount/officialaccount.go +++ b/officialaccount/officialaccount.go @@ -1,9 +1,10 @@ package officialaccount import ( - "github.com/silenceper/wechat/v2/officialaccount/datacube" "net/http" + "github.com/silenceper/wechat/v2/officialaccount/datacube" + "github.com/silenceper/wechat/v2/credential" "github.com/silenceper/wechat/v2/officialaccount/basic" "github.com/silenceper/wechat/v2/officialaccount/broadcast" diff --git a/officialaccount/server/server.go b/officialaccount/server/server.go index ec8a635..9236f7a 100644 --- a/officialaccount/server/server.go +++ b/officialaccount/server/server.go @@ -199,7 +199,7 @@ func (srv *Server) buildResponse(reply *message.Reply) (err error) { value := reflect.ValueOf(msgData) //msgData must be a ptr kind := value.Kind().String() - if "ptr" != kind { + if kind != "ptr" { return message.ErrUnsupportReply } @@ -213,7 +213,7 @@ func (srv *Server) buildResponse(reply *message.Reply) (err error) { params[0] = reflect.ValueOf(msgType) value.MethodByName("SetMsgType").Call(params) - params[0] = reflect.ValueOf(util.GetCurrTs()) + params[0] = reflect.ValueOf(util.GetCurrTS()) value.MethodByName("SetCreateTime").Call(params) srv.ResponseMsg = msgData diff --git a/openplatform/context/accessToken.go b/openplatform/context/accessToken.go index 73fe747..35690d8 100644 --- a/openplatform/context/accessToken.go +++ b/openplatform/context/accessToken.go @@ -15,9 +15,9 @@ const ( refreshTokenURL = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=%s" getComponentInfoURL = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=%s" //TODO 获取授权方选项信息 - getComponentConfigURL = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_option?component_access_token=%s" + //getComponentConfigURL = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_option?component_access_token=%s" //TODO 获取已授权的账号信息 - getuthorizerListURL = "POST https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_list?component_access_token=%s" + //getuthorizerListURL = "POST https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_list?component_access_token=%s" ) // ComponentAccessToken 第三方平台 diff --git a/openplatform/officialaccount/officialaccount.go b/openplatform/officialaccount/officialaccount.go index a636ac8..fe4fbbf 100644 --- a/openplatform/officialaccount/officialaccount.go +++ b/openplatform/officialaccount/officialaccount.go @@ -12,7 +12,6 @@ type OfficialAccount struct { //授权的公众号的appID appID string *officialaccount.OfficialAccount - opContext *opContext.Context } //NewOfficialAccount 实例化 diff --git a/util/crypto.go b/util/crypto.go index 623ebd6..16b5ce6 100644 --- a/util/crypto.go +++ b/util/crypto.go @@ -65,7 +65,7 @@ func AESEncryptMsg(random, rawXMLMsg []byte, appID string, aesKey []byte) (ciphe } // 加密 - block, err := aes.NewCipher(aesKey[:]) + block, err := aes.NewCipher(aesKey) if err != nil { panic(err) } diff --git a/util/signature.go b/util/signature.go index a65ee23..22a9cc5 100644 --- a/util/signature.go +++ b/util/signature.go @@ -12,7 +12,7 @@ func Signature(params ...string) string { sort.Strings(params) h := sha1.New() for _, s := range params { - io.WriteString(h, s) + _, _ = io.WriteString(h, s) } return fmt.Sprintf("%x", h.Sum(nil)) } diff --git a/util/time.go b/util/time.go index 024839c..4306f83 100644 --- a/util/time.go +++ b/util/time.go @@ -3,6 +3,6 @@ package util import "time" //GetCurrTs return current timestamps -func GetCurrTs() int64 { +func GetCurrTS() int64 { return time.Now().Unix() } diff --git a/wechat.go b/wechat.go index 0d7e90d..a7023ff 100644 --- a/wechat.go +++ b/wechat.go @@ -16,7 +16,6 @@ import ( ) func init() { - // Log as JSON instead of the default ASCII formatter. log.SetFormatter(&log.TextFormatter{}) @@ -26,7 +25,6 @@ func init() { // Only log the warning severity or above. log.SetLevel(log.DebugLevel) - } // Wechat struct