fix(notify): refresh workwx token

This commit is contained in:
马鸿飞
2023-04-27 15:02:40 +08:00
parent 5967ea4076
commit 8be733384a
5 changed files with 51 additions and 8 deletions
+26
View File
@@ -0,0 +1,26 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package notify
type SWorkwxSendMessageResp struct {
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
InvalidUser string `json:"invaliduser"`
InvalidParty string `json:"invalidparty" `
InvalidTag string `json:"invalidtag"`
UnlicensedUser string `json:"unlicenseduser" `
MsgId string `json:"msgid"`
ResponseCode string `json:"response_code"`
}
-1
View File
@@ -88,7 +88,6 @@ func (cm *SConfigManager) ValidateCreateData(ctx context.Context, userCred mccli
return input, err
}
driver := GetDriver(input.Type)
// validate
message, err := driver.ValidateConfig(api.NotifyConfig{
SNotifyConfigContent: *input.Content,
+1 -1
View File
@@ -89,7 +89,7 @@ func (dingSender *SDingTalkSender) ValidateConfig(config api.NotifyConfig) (stri
_, err := dingSender.getAccessToken(config.AppKey, config.AppSecret)
if err != nil {
if strings.Contains(err.Error(), "40089") {
return "invalid AppKey or AppSecret", nil
return "invalid AppKey or AppSecret", err
}
return "", err
}
+2 -2
View File
@@ -80,7 +80,7 @@ func (feishuSender *SFeishuSender) Send(args api.SendParams) error {
func (feishuSender *SFeishuSender) ValidateConfig(config api.NotifyConfig) (string, error) {
rep, err := feishuSender.getAccessToken(config.AppId, config.AppSecret)
if err == nil {
return "", err
return "", nil
}
var msg string
switch rep.Code {
@@ -89,7 +89,7 @@ func (feishuSender *SFeishuSender) ValidateConfig(config api.NotifyConfig) (stri
case 10014:
msg = "invalid AppSecret"
}
return msg, nil
return msg, err
}
// 根据用户手机号获取用户的open_id
+22 -4
View File
@@ -45,12 +45,31 @@ func (workwxSender *SWorkwxSender) Send(args api.SendParams) error {
},
"touser": args.Receivers.Contact,
}
_, err := workwxSender.sendMessageWithToken(ApiWorkwxSendMessage, fmt.Sprintf("%s-%s", api.WORKWX, args.DomainId), httputils.POST, nil, nil, jsonutils.Marshal(body))
respObj, err := workwxSender.sendMessageWithToken(ApiWorkwxSendMessage, fmt.Sprintf("%s-%s", api.WORKWX, args.DomainId), httputils.POST, nil, nil, jsonutils.Marshal(body))
if err != nil {
return errors.Wrap(err, "workwx send message")
}
return err
resp := api.SWorkwxSendMessageResp{}
respObj.Unmarshal(&resp)
// errcode大于0时返回错误
if resp.ErrCode > 0 {
// 对于token过期情况,进行重新获取token,并重新发消息
if len(resp.UnlicensedUser) > 0 || resp.ErrCode == 42001 {
err = workwxSender.GetAccessToken(args.DomainId)
if err != nil {
return errors.Wrap(err, "retenant token invalid && getToken err")
}
secRespObj, err := workwxSender.sendMessageWithToken(ApiWorkwxSendMessage, fmt.Sprintf("%s-%s", api.WORKWX, args.DomainId), httputils.POST, nil, nil, jsonutils.Marshal(body))
secRespObj.Unmarshal(&resp)
if err == nil && resp.ErrCode == 0 {
return nil
} else {
return errors.Errorf(resp.ErrMsg)
}
}
return errors.Errorf(resp.ErrMsg)
}
return nil
}
func (workwxSender *SWorkwxSender) ValidateConfig(config api.NotifyConfig) (string, error) {
@@ -123,7 +142,6 @@ func (workwxSender *SWorkwxSender) GetAccessToken(domainId string) error {
}
func (workwxSender *SWorkwxSender) getAccessToken(corpId, secret string) (string, error) {
// url := ApiWorkwxGetToken + fmt.Sprintf("?corpid=%s&corpsecret=%s", corpId, secret)
params := url.Values{}
params.Set("corpid", corpId)
params.Set("corpsecret", secret)