-
Notifications
You must be signed in to change notification settings - Fork 396
/
sms.go
96 lines (78 loc) · 1.67 KB
/
sms.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
90
91
92
93
94
95
96
/************************************************************
** @Description: notify
** @Author: george hao
** @Date: 2018-08-09 13:05
** @Last Modified by: Bee
** @Last Modified time: 2019-02-15 13:50
*************************************************************/
package notify
import (
"github.com/astaxie/beego"
"github.com/george518/PPGo_Job/libs"
"log"
"time"
"encoding/json"
"github.com/pkg/errors"
)
type SmsAjaxReturn struct {
Status int `json:"status"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
type Sms struct {
Mobiles map[string]string
Param map[string]string
}
var SmsChan chan *Sms
var SmsUrl string
func init() {
SmsUrl = beego.AppConfig.String("msg.url")
poolSize, _ := beego.AppConfig.Int("msg.pool")
//创建通道
SmsChan = make(chan *Sms, poolSize)
go func() {
for {
select {
case m, ok := <-SmsChan:
if !ok {
return
}
if err := m.SendSms(); err != nil {
beego.Error("SendSms:", err.Error())
}
}
}
}()
}
func SendSmsToChan(mobiles map[string]string, param map[string]string) bool {
sms := &Sms{
Mobiles: mobiles,
Param: param,
}
select {
case SmsChan <- sms:
return true
case <-time.After(time.Second * 3):
return false
}
}
func (s *Sms) SendSms() error {
for _, v := range s.Mobiles {
s.Param["mobile"] = v
res, err := libs.HttpGet(SmsUrl, s.Param)
if err != nil {
log.Println(err)
return err
}
ajaxData := SmsAjaxReturn{}
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
}