Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
U
utils
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
library
utils
Commits
d915f380
Verified
Commit
d915f380
authored
Jun 19, 2019
by
Cui
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix compatible with jiguang sms
parent
6923ab00
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
133 additions
and
4 deletions
+133
-4
request.go
request.go
+29
-1
jsms.go
sender/jsms.go
+83
-0
sms.go
sender/sms.go
+3
-2
wrapper.go
sender/wrapper.go
+18
-1
No files found.
request.go
View file @
d915f380
...
@@ -7,12 +7,13 @@ package utils
...
@@ -7,12 +7,13 @@ package utils
import
(
import
(
"crypto/tls"
"crypto/tls"
"github.com/valyala/fasthttp"
"io"
"io"
"io/ioutil"
"io/ioutil"
"net"
"net"
"net/http"
"net/http"
"time"
"time"
"github.com/valyala/fasthttp"
)
)
func
HttpGet
(
uri
string
)
([]
byte
,
int
,
error
)
{
func
HttpGet
(
uri
string
)
([]
byte
,
int
,
error
)
{
...
@@ -70,3 +71,30 @@ func FasthttpPost(url string, bodyjson []byte) ([]byte, int, error) {
...
@@ -70,3 +71,30 @@ func FasthttpPost(url string, bodyjson []byte) ([]byte, int, error) {
return
resp
.
Body
(),
resp
.
StatusCode
(),
err
return
resp
.
Body
(),
resp
.
StatusCode
(),
err
}
}
func
FasthttpPostWithAuthorization
(
authorization
,
url
string
,
bodyjson
[]
byte
)
([]
byte
,
int
,
error
)
{
total
:=
500
req
:=
&
fasthttp
.
Request
{}
resp
:=
&
fasthttp
.
Response
{}
req
.
Header
.
SetMethod
(
"POST"
)
req
.
Header
.
Add
(
"Authorization"
,
"Basic "
+
authorization
)
req
.
Header
.
SetContentType
(
"application/json"
)
req
.
SetBody
(
bodyjson
)
req
.
SetRequestURI
(
url
)
c
:=
&
fasthttp
.
Client
{
TLSConfig
:
&
tls
.
Config
{
InsecureSkipVerify
:
true
,
},
Dial
:
func
(
addr
string
)
(
net
.
Conn
,
error
)
{
return
fasthttp
.
DialTimeout
(
addr
,
time
.
Second
*
10
)
},
MaxConnsPerHost
:
total
,
}
err
:=
c
.
Do
(
req
,
resp
)
return
resp
.
Body
(),
resp
.
StatusCode
(),
err
}
sender/jsms.go
0 → 100644
View file @
d915f380
package
sender
import
(
"encoding/base64"
"encoding/json"
"strconv"
"github.com/sirupsen/logrus"
.
"gitlab.qingclass.cn/library/utils"
)
const
(
JpushSMSSendUri
=
"https://api.sms.jpush.cn/v1/messages"
JpushCodeSMSSendUri
=
"https://api.sms.jpush.cn/v1/codes"
)
type
SmsError
struct
{
Code
int
`json:"code"`
Message
string
`json:"message"`
}
type
JpushSmsResponse
struct
{
MsgId
int
`json:"msg_id,omitempty"`
Error
SmsError
`json:"error,omitempty"`
}
type
SmsRequest
struct
{
Mobile
string
`json:"mobile"`
// 手机号码
SignId
string
`json:"sign_id,omitempty"`
// 签名ID,该字段为空则使用应用默认签名
TempId
string
`json:"temp_id"`
// 模板ID
TempPara
map
[
string
]
string
`json:"temp_para,omitempty"`
// 模板参数,需要替换的参数名和 value 的键值对
}
func
JpushSmsMessageSender
(
sm
SmsMessage
,
appKey
,
masterSecret
string
)
(
int
,
string
)
{
log
:=
Logger
.
WithFields
(
logrus
.
Fields
{
"position"
:
"sms.go"
,
"func"
:
"SmsMessageSender"
,
"appKey"
:
appKey
,
})
sr
:=
ConvertParam
(
sm
)
reqBody
,
_
:=
json
.
Marshal
(
sr
)
authorization
:=
base64
.
StdEncoding
.
EncodeToString
([]
byte
(
appKey
+
":"
+
masterSecret
))
body
,
statusCode
,
err
:=
FasthttpPostWithAuthorization
(
authorization
,
JpushSMSSendUri
,
reqBody
)
var
res
JpushSmsResponse
var
errorStr
string
_
=
json
.
Unmarshal
(
body
,
&
res
)
if
err
!=
nil
||
res
.
MsgId
==
0
{
log
.
WithField
(
"error"
,
err
)
.
Error
(
"SmsMessageSender Error!"
)
statusCode
=
-
1
if
err
!=
nil
{
errorStr
=
"SmsMessageSender Error -"
+
err
.
Error
()
}
else
{
errorStr
=
"SmsMessageSender Error -"
+
strconv
.
Itoa
(
res
.
Error
.
Code
)
+
" "
+
res
.
Error
.
Message
}
}
else
{
log
.
WithFields
(
sm
.
ToFields
())
.
WithField
(
"response"
,
string
(
body
))
.
Info
(
"SmsMessageSender send success !"
)
}
return
statusCode
,
errorStr
}
func
ConvertParam
(
sm
SmsMessage
)
(
sr
SmsRequest
)
{
sr
.
Mobile
=
sm
.
Tel
.
Mobile
sr
.
TempId
=
strconv
.
Itoa
(
sm
.
TplId
)
length
:=
len
(
sm
.
Params
)
if
length
>
0
{
sr
.
TempPara
=
make
(
map
[
string
]
string
)
for
i
:=
0
;
i
<
length
;
i
++
{
sr
.
TempPara
[
strconv
.
Itoa
(
i
+
1
)]
=
sm
.
Params
[
i
]
}
}
return
}
sender/sms.go
View file @
d915f380
...
@@ -2,10 +2,11 @@ package sender
...
@@ -2,10 +2,11 @@ package sender
import
(
import
(
"encoding/json"
"encoding/json"
"github.com/sirupsen/logrus"
.
"gitlab.qingclass.cn/library/utils"
"strconv"
"strconv"
"time"
"time"
"github.com/sirupsen/logrus"
.
"gitlab.qingclass.cn/library/utils"
)
)
const
(
const
(
...
...
sender/wrapper.go
View file @
d915f380
...
@@ -8,6 +8,8 @@ import (
...
@@ -8,6 +8,8 @@ import (
.
"gitlab.qingclass.cn/library/utils"
.
"gitlab.qingclass.cn/library/utils"
)
)
var
TencentSmsIds
=
[]
int
{
228304
,
230043
,
277995
,
292569
,
292570
,
292573
,
330224
,
330284
,
330286
,
330461
,
103765
,
104921
,
110327
,
228298
,
228299
,
228300
,
228301
,
228303
}
func
Sender
(
accessToken
string
,
message
MessageStruct
)
(
code
int
,
msg
string
)
{
func
Sender
(
accessToken
string
,
message
MessageStruct
)
(
code
int
,
msg
string
)
{
log
:=
Logger
.
WithFields
(
logrus
.
Fields
{
log
:=
Logger
.
WithFields
(
logrus
.
Fields
{
"position"
:
"wrapper.go"
,
"position"
:
"wrapper.go"
,
...
@@ -197,7 +199,12 @@ func Sender(accessToken string, message MessageStruct) (code int, msg string) {
...
@@ -197,7 +199,12 @@ func Sender(accessToken string, message MessageStruct) (code int, msg string) {
break
break
}
}
// 区分极光短信和腾讯短信,极光短信 id
if
indexOf
(
TencentSmsIds
,
sm
.
TplId
)
!=
-
1
{
code
,
msg
=
JpushSmsMessageSender
(
sm
,
message
.
JAppKey
,
message
.
JAppSecret
)
}
else
{
code
,
msg
=
SmsMessageSender
(
sm
,
message
.
SmsAppId
,
message
.
SmsAppKey
)
code
,
msg
=
SmsMessageSender
(
sm
,
message
.
SmsAppId
,
message
.
SmsAppKey
)
}
break
break
case
"APP"
:
case
"APP"
:
var
am
AppMessage
var
am
AppMessage
...
@@ -225,3 +232,13 @@ func Sender(accessToken string, message MessageStruct) (code int, msg string) {
...
@@ -225,3 +232,13 @@ func Sender(accessToken string, message MessageStruct) (code int, msg string) {
return
return
}
}
func
indexOf
(
slice
[]
int
,
item
int
)
int
{
for
i
:=
range
slice
{
if
slice
[
i
]
==
item
{
return
i
}
}
return
-
1
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment