-
Notifications
You must be signed in to change notification settings - Fork 0
/
wechat.go
89 lines (71 loc) · 1.48 KB
/
wechat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package notify
import (
"github.com/astaxie/beego"
"github.com/george518/PPGo_Job/libs"
"log"
"time"
"encoding/json"
"github.com/pkg/errors"
)
type WechatAjaxReturn struct {
Status int `json:"status"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
type Wechat struct {
Accounts map[string]string
Param map[string]string
}
var WechatChan chan *Wechat
var WechatUrl string
func init() {
WechatUrl = beego.AppConfig.String("wechat.url")
poolSize, _ := beego.AppConfig.Int("wechat.pool")
//创建通道
WechatChan = make(chan *Wechat, poolSize)
go func() {
for {
select {
case m, ok := <-WechatChan:
if !ok {
return
}
if err := m.SendWechat(); err != nil {
beego.Error("SendWechat:", err.Error())
}
}
}
}()
}
func SendWechatToChan(accounts map[string]string, param map[string]string) bool {
wechat := &Wechat{
Accounts: accounts,
Param: param,
}
select {
case WechatChan <- wechat:
return true
case <-time.After(time.Second * 3):
return false
}
}
func (s *Wechat) SendWechat() error {
for _, v := range s.Accounts {
s.Param["account"] = v
res, err := libs.HttpGet(WechatUrl, s.Param)
if err != nil {
log.Println(err)
return err
}
ajaxData := WechatAjaxReturn{}
jsonErr := json.Unmarshal([]byte(res), &ajaxData)
if jsonErr != nil {
return jsonErr
}
if ajaxData.Status != 200 {
return errors.Errorf("msg %s", ajaxData.Message)
}
return nil
}
return nil
}