-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmsmgr.go
195 lines (166 loc) · 4.51 KB
/
smsmgr.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2014 [email protected]
//
// 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 sms
import (
"fmt"
"github.com/aiwuTech/devKit/random"
"github.com/astaxie/beego/cache"
"log"
"math"
"time"
)
const (
// default sms auth code attribute
defaultLen = 6
defaultExpire = time.Hour
defaultChan = 100
)
type SmsManager struct {
min int
max int
expire time.Duration
company string
store cache.Cache
service SmsServicer
smsChan chan []string // 0: text 1 - n-1: mobiles
smsChanTpl chan map[int64][2][]string // key: template id, value[0]:mobiles, value[1]:args
}
func (s *SmsManager) GetCompany() string {
return s.company
}
func (s *SmsManager) GetService() SmsServicer {
return s.service
}
// 内容直接发送协程
func (s *SmsManager) sendSMS(messages chan []string) {
ticker := time.NewTicker(time.Hour)
for {
select {
case message := <-messages:
result, err := s.service.SendSMS(message[0], message[1:])
if err != nil {
log.Printf("---SMS Manager---: err: %v\n", err)
} else {
log.Printf("---SMS Manager---: result: %+v\n", result)
}
case <-ticker.C:
log.Printf("---SMS Manager---: current sms chan len: %v\n", len(s.smsChan))
}
}
}
// 模版发送协程
func (s *SmsManager) sendSMS_tpl(messages chan map[int64][2][]string) {
ticker := time.NewTicker(time.Hour)
for {
select {
case message := <-messages:
for k, v := range message {
result, err := s.service.SendSMS_Tpl(k, v[0], v[1])
if err != nil {
log.Printf("---SMS Manager---: err: %v\n", err)
} else {
log.Printf("---SMS Manager---: result: %+v\n", result)
}
}
case <-ticker.C:
log.Printf("---SMS Manager---: current sms template chan len: %v\n", len(s.smsChan))
}
}
}
// 新建短信manager
func NewSmsManager(len int, expire time.Duration, store cache.Cache, service SmsServicer, cLen int, company string) *SmsManager {
manager := &SmsManager{
min: int(math.Pow(10, float64(len-1))),
max: int(math.Pow(10, float64(len))),
expire: expire,
store: store,
service: service,
smsChan: make(chan []string, cLen),
smsChanTpl: make(chan map[int64][2][]string, cLen),
company: company,
}
// listen up for send sms by text
go manager.sendSMS(manager.smsChan)
// listen up for send sms by template
go manager.sendSMS_tpl(manager.smsChanTpl)
return manager
}
// 默认短信manager
func NewDefaultSmsManager(company string, service SmsServicer) *SmsManager {
return NewSmsManager(defaultLen, defaultExpire, cache.NewMemoryCache(), service, defaultChan, company)
}
// generate sms auth code
func (s *SmsManager) getRandCode() string {
return random.RandIntStr(s.min, s.max)
}
// generate cache key
func (s *SmsManager) key(tel string) string {
return fmt.Sprintf("%v_%v", s.company, tel)
}
// sms code
func (s *SmsManager) Code(tel string) (string, error) {
// if exist, directly return
k := s.key(tel)
if s.store.IsExist(k) {
if v, ok := s.store.Get(k).(string); ok {
return v, nil
}
}
// get the auth code
chars := s.getRandCode()
// save to store
if err := s.store.Put(k, chars, s.expire); err != nil {
return "", err
}
return chars, nil
}
// varify sms code
func (s *SmsManager) Verify(tel, code string) (success bool) {
if len(tel) == 0 || len(code) == 0 {
return
}
var chars string
k := s.key(tel)
// log.Printf("chars: %v", chars)
if v, ok := s.store.Get(k).(string); ok {
chars = v
} else {
return
}
// log.Printf("chars: %v", chars)
defer func() {
// finally remove it
s.store.Delete(k)
}()
if len(chars) != len(code) || chars != code {
return
}
return true
}
// 文字短信
func (s *SmsManager) SendSMS(text string, mobiles []string) {
args := make([]string, 0)
args = append(args, text)
args = append(args, mobiles...)
s.smsChan <- args
}
// 模版短信
func (s *SmsManager) SendSMS_tpl(tplid int64, mobiles []string, arg ...string) {
args := make(map[int64][2][]string)
args[tplid] = [2][]string{
mobiles,
arg,
}
s.smsChanTpl <- args
}