|
@@ -81,22 +81,28 @@ func (stm *WxTmplPush) SendMsg(link, title, detail, date, row4 string) error {
|
|
|
if err := stm.getUserOpenIdAndWxPushState(); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- // 校验发送量及频率
|
|
|
- if err := stm.Check(); err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
// 获取消息
|
|
|
msg, err := stm.getMessage(title, detail, date, row4)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
+
|
|
|
+ // 校验发送量及频率
|
|
|
+ err, noteFunc := stm.IncrCount()
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
// 发送信息
|
|
|
autoLoginHref := fmt.Sprintf("%s/swordfish/SingleLogin?toHref=%s", config.ConfigJson.WxWebdomain, url.QueryEscape(link))
|
|
|
if _, err := stm.Send(autoLoginHref, msg); err != nil {
|
|
|
+ // 发送失败数量回滚
|
|
|
+ stm.RollBack()
|
|
|
return err
|
|
|
}
|
|
|
- // 发送数量计数
|
|
|
- stm.IncrCount()
|
|
|
+ if noteFunc != nil {
|
|
|
+ noteFunc()
|
|
|
+ }
|
|
|
return nil
|
|
|
}
|
|
|
|
|
@@ -132,39 +138,44 @@ func (stm *WxTmplPush) getMessage(title, detail, date, row4 string) (map[string]
|
|
|
|
|
|
}
|
|
|
|
|
|
-// Check 校验发送量和频率
|
|
|
-func (stm *WxTmplPush) Check() error {
|
|
|
+// RollBack 发送失败数量回滚
|
|
|
+func (stm *WxTmplPush) RollBack() {
|
|
|
uCache, allCache := getSendTotalRedisKey(stm.OpenId), getSendTotalRedisKey()
|
|
|
- //校验当日微信模版发送总量
|
|
|
- if total := redis.GetInt(CacheDb, allCache); total > config.ConfigJson.WxTmplConfig.Limit.Total {
|
|
|
- return fmt.Errorf("已达发送总量上限")
|
|
|
- }
|
|
|
- //校验当日单用户发送总量
|
|
|
- if uTotal := redis.GetInt(CacheDb, uCache); uTotal > config.ConfigJson.WxTmplConfig.Limit.OneDayLimit {
|
|
|
- return fmt.Errorf("已达单该用户发送总量上限")
|
|
|
- }
|
|
|
- //校验发送间隔
|
|
|
- if sendWait, _ := redis.Exists(CacheDb, fmt.Sprintf("%s_sendwait", uCache)); sendWait {
|
|
|
- return fmt.Errorf("发送模版消息频繁,稍后重试")
|
|
|
- }
|
|
|
- return nil
|
|
|
+ redis.Decrby(CacheDb, allCache, 1)
|
|
|
+ redis.Decrby(CacheDb, uCache, 1)
|
|
|
+ redis.Del(CacheDb, fmt.Sprintf("%s_sendwait", uCache)) //清除发送间隔
|
|
|
}
|
|
|
|
|
|
-func (stm *WxTmplPush) IncrCount() {
|
|
|
+func (stm *WxTmplPush) IncrCount() (error, func()) {
|
|
|
uCache, allCache := getSendTotalRedisKey(stm.OpenId), getSendTotalRedisKey() //当日微信模版消息发送总量
|
|
|
+ //校验发送间隔
|
|
|
+ if sendWait, _ := redis.Exists(CacheDb, fmt.Sprintf("%s_sendwait", uCache)); sendWait {
|
|
|
+ return fmt.Errorf("发送模版消息频繁,稍后重试"), nil
|
|
|
+ }
|
|
|
var total int64
|
|
|
if total = redis.Incr(CacheDb, allCache); total == 1 {
|
|
|
_ = redis.SetExpire(CacheDb, allCache, 60*60*24)
|
|
|
}
|
|
|
+ if total > config.ConfigJson.WxTmplConfig.Limit.Total {
|
|
|
+ return fmt.Errorf("已达发送总量上限"), nil
|
|
|
+ }
|
|
|
+
|
|
|
if uTotal := redis.Incr(CacheDb, uCache); uTotal == 1 {
|
|
|
_ = redis.SetExpire(CacheDb, uCache, 60*60*24)
|
|
|
} //当日用户发送数量
|
|
|
- redis.Put(CacheDb, fmt.Sprintf("%s_sendwait", uCache), 1, config.ConfigJson.WxTmplConfig.Limit.DuringMine*60) //下次发送时间
|
|
|
+ if uTotal := redis.GetInt(CacheDb, uCache); uTotal > config.ConfigJson.WxTmplConfig.Limit.OneDayLimit {
|
|
|
+ return fmt.Errorf("已达单该用户发送总量上限"), nil
|
|
|
+ }
|
|
|
+
|
|
|
+ //下次发送时间
|
|
|
+ redis.Put(CacheDb, fmt.Sprintf("%s_sendwait", uCache), 1, config.ConfigJson.WxTmplConfig.Limit.DuringMine*60)
|
|
|
|
|
|
- for _, num := range config.ConfigJson.WxTmplConfig.Limit.Alert.Nums {
|
|
|
- if total == num {
|
|
|
- util.SendRetryMail(3, strings.Join(config.ConfigJson.WxTmplConfig.Limit.Alert.ToMail, ","), strings.Join(config.ConfigJson.WxTmplConfig.Limit.Alert.CcMail, ","),
|
|
|
- "剑鱼微信模版告警邮件", fmt.Sprintf("今日发送微信模版信息数量已达%d条,总量%d条", total, config.ConfigJson.WxTmplConfig.Limit.Total), entity.GmailAuth)
|
|
|
+ return nil, func() {
|
|
|
+ for _, num := range config.ConfigJson.WxTmplConfig.Limit.Alert.Nums {
|
|
|
+ if total == num {
|
|
|
+ util.SendRetryMail(3, strings.Join(config.ConfigJson.WxTmplConfig.Limit.Alert.ToMail, ","), strings.Join(config.ConfigJson.WxTmplConfig.Limit.Alert.CcMail, ","),
|
|
|
+ "剑鱼微信模版告警邮件", fmt.Sprintf("今日发送微信模版信息数量已达%d条,总量%d条", total, config.ConfigJson.WxTmplConfig.Limit.Total), entity.GmailAuth)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|