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
12e7a5fb
Verified
Commit
12e7a5fb
authored
Jan 16, 2019
by
Cui
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add sender
parent
a3a23bc5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
506 additions
and
1 deletion
+506
-1
math.go
math.go
+6
-0
messageSender.go
messageSender.go
+189
-0
messageStructs.go
messageStructs.go
+282
-0
tools.go
tools.go
+29
-1
No files found.
math.go
View file @
12e7a5fb
...
...
@@ -7,9 +7,15 @@ package utils
import
(
"crypto/rand"
"math"
"math/big"
r
"math/rand"
)
func
RandInt
()
int
{
return
int
(
math
.
Round
(
r
.
Float64
()
*
99999
))
}
func
RandInt64
(
min
,
max
int64
)
int64
{
maxBigInt
:=
big
.
NewInt
(
max
)
i
,
_
:=
rand
.
Int
(
rand
.
Reader
,
maxBigInt
)
...
...
messageSender.go
0 → 100644
View file @
12e7a5fb
/*
* @Author: cuiweiqiang
* @Date: 2019-01-15 15:06
*/
package
utils
import
(
"encoding/json"
"github.com/sirupsen/logrus"
"strconv"
"time"
)
const
(
CUSTOMURI
=
"https://api.weixin.qq.com/cgi-bin/message/custom/send"
MASSURI
=
"https://api.weixin.qq.com/cgi-bin/message/mass/send"
MASSPREVIEWURI
=
"https://api.weixin.qq.com/cgi-bin/message/mass/preview"
TEMPLATEURI
=
"https://api.weixin.qq.com/cgi-bin/message/template/send"
SUBSCRIBEURI
=
"https://api.weixin.qq.com/cgi-bin/message/template/subscribe"
NationCode
=
"86"
SMSSendUri
=
"https://yun.tim.qq.com/v5/tlssmssvr/sendsms"
)
type
WxResponse
struct
{
Errcode
int
`json:"errcode"`
Errmsg
string
`json:"errmsg"`
Msgid
float32
`json:"msgid"`
}
func
(
wr
*
WxResponse
)
Tofields
()
(
fields
logrus
.
Fields
)
{
fields
=
make
(
logrus
.
Fields
)
fields
[
"errcode"
]
=
wr
.
Errcode
fields
[
"errmsg"
]
=
wr
.
Errmsg
fields
[
"msgid"
]
=
wr
.
Msgid
return
}
type
CustomerMessageInterface
interface
{
GetToUser
()
interface
{}
ToFieldsCustomerMessage
()
logrus
.
Fields
}
func
CustomerMessageSender
(
accessToken
string
,
cmi
CustomerMessageInterface
,
preview
bool
)
(
int
,
string
)
{
log
:=
Logger
.
WithFields
(
logrus
.
Fields
{
"position"
:
"messageSender.go"
,
"func"
:
"CustomerMessageSender"
,
"accessToken"
:
accessToken
,
})
var
sendUri
string
switch
v
:=
cmi
.
GetToUser
()
.
(
type
)
{
case
string
:
sendUri
=
CUSTOMURI
case
[]
string
:
sendUri
=
MASSURI
default
:
log
.
Print
(
"type "
+
v
.
(
string
)
+
" is not in list"
)
}
if
preview
{
sendUri
=
MASSPREVIEWURI
}
uri
:=
sendUri
+
"?access_token="
+
accessToken
data
,
_
:=
json
.
Marshal
(
cmi
)
body
,
_
,
err
:=
FasthttpPost
(
uri
,
data
)
var
result
WxResponse
if
err
!=
nil
{
log
.
WithFields
(
cmi
.
ToFieldsCustomerMessage
())
.
Warn
(
"send message to user fail"
)
result
.
Errcode
=
-
100001
result
.
Errmsg
=
err
.
Error
()
}
else
{
_
=
json
.
Unmarshal
(
body
,
&
result
)
log
.
WithFields
(
result
.
Tofields
())
.
WithFields
(
cmi
.
ToFieldsCustomerMessage
())
.
Info
(
"send message to user"
)
}
return
result
.
Errcode
,
result
.
Errmsg
}
func
TemplateMessageSender
(
accessToken
string
,
tm
TemplateMessage
)
(
int
,
string
)
{
log
:=
Logger
.
WithFields
(
logrus
.
Fields
{
"position"
:
"messageSender.go"
,
"func"
:
"TemplateMessageSender"
,
"accessToken"
:
accessToken
,
})
uri
:=
TEMPLATEURI
+
"?access_token="
+
accessToken
data
,
_
:=
json
.
Marshal
(
tm
)
body
,
_
,
err
:=
FasthttpPost
(
uri
,
data
)
var
result
WxResponse
if
err
!=
nil
{
log
.
WithFields
(
tm
.
ToFields
())
.
Warn
(
"send TemplateMessage to user fail"
)
result
.
Errcode
=
-
100001
result
.
Errmsg
=
err
.
Error
()
}
else
{
_
=
json
.
Unmarshal
(
body
,
&
result
)
log
.
WithFields
(
result
.
Tofields
())
.
WithFields
(
tm
.
ToFields
())
.
Info
(
"send TemplateMessage to user"
)
}
return
result
.
Errcode
,
result
.
Errmsg
}
func
SubscribeMessageSender
(
accessToken
string
,
sm
SubscribeMessage
)
(
int
,
string
)
{
log
:=
Logger
.
WithFields
(
logrus
.
Fields
{
"position"
:
"messageSender.go"
,
"func"
:
"SubscribeMessageSender"
,
"accessToken"
:
accessToken
,
})
uri
:=
SUBSCRIBEURI
+
"?access_token="
+
accessToken
data
,
_
:=
json
.
Marshal
(
sm
)
body
,
_
,
err
:=
FasthttpPost
(
uri
,
data
)
var
result
WxResponse
if
err
!=
nil
{
log
.
WithFields
(
sm
.
ToFields
())
.
Warn
(
"send SubscribeMessage to user fail"
)
result
.
Errcode
=
-
100001
result
.
Errmsg
=
err
.
Error
()
}
else
{
_
=
json
.
Unmarshal
(
body
,
&
result
)
log
.
WithFields
(
result
.
Tofields
())
.
WithFields
(
sm
.
ToFields
())
.
Info
(
"send SubscribeMessage to user"
)
}
return
result
.
Errcode
,
result
.
Errmsg
}
func
SmsMessageSender
(
sm
SmsMessage
,
appId
int
,
appKey
string
)
(
int
,
string
)
{
log
:=
Logger
.
WithFields
(
logrus
.
Fields
{
"position"
:
"messageSender.go"
,
"func"
:
"SmsMessageSender"
,
"accessToken"
:
appId
,
})
randInt
:=
RandInt
()
uri
:=
SMSSendUri
+
"?sdkappid="
+
strconv
.
Itoa
(
appId
)
+
"&random="
+
strconv
.
Itoa
(
randInt
)
sm
.
Sign
=
""
sm
.
Ext
=
""
sm
.
Extend
=
""
sm
.
Time
=
time
.
Now
()
.
Unix
()
sm
.
Sig
=
SmsDoSign
(
appKey
,
[]
string
{
sm
.
Tel
.
Mobile
},
sm
.
Time
,
randInt
)
sm
.
Tel
.
Nationcode
=
NationCode
reqBody
,
_
:=
json
.
Marshal
(
sm
)
body
,
statusCode
,
err
:=
FasthttpPost
(
uri
,
reqBody
)
if
err
!=
nil
{
log
.
WithField
(
"error"
,
err
)
.
Error
(
"SmsMessageSender Error!"
)
}
else
{
log
.
WithFields
(
sm
.
ToFields
())
.
WithField
(
"response"
,
string
(
body
))
.
Info
(
"SmsMessageSender send success !"
)
}
return
statusCode
,
""
}
messageStructs.go
0 → 100644
View file @
12e7a5fb
This diff is collapsed.
Click to expand it.
tools.go
View file @
12e7a5fb
...
...
@@ -6,11 +6,14 @@
package
utils
import
(
"crypto/sha256"
"database/sql"
"encoding/hex"
"errors"
"github.com/sirupsen/logrus"
"io"
"reflect"
"strconv"
)
func
IOClose
(
fio
io
.
ReadCloser
)
{
...
...
@@ -21,10 +24,35 @@ func IOClose(fio io.ReadCloser) {
err
:=
fio
.
Close
()
if
err
!=
nil
{
log
.
WithField
(
"error"
,
err
)
.
Error
(
"io.ReadCloser close error"
)
log
.
WithField
(
"error"
,
err
)
.
Error
(
"io.ReadCloser close error"
)
}
}
func
SmsDoSign
(
appKey
string
,
tels
[]
string
,
time
int64
,
rand
int
)
string
{
var
err
error
h
:=
sha256
.
New
()
if
len
(
tels
)
!=
0
{
var
telStr
string
for
_
,
v
:=
range
tels
{
telStr
=
telStr
+
v
+
","
}
telStr
=
telStr
[
:
len
(
telStr
)
-
1
]
_
,
err
=
h
.
Write
([]
byte
(
"appkey="
+
appKey
+
"&random="
+
strconv
.
Itoa
(
rand
)
+
"&time="
+
strconv
.
FormatInt
(
time
,
10
)
+
"&mobile="
+
telStr
))
if
err
!=
nil
{
return
""
}
}
else
{
_
,
err
=
h
.
Write
([]
byte
(
"appkey="
+
appKey
+
"&random="
+
strconv
.
Itoa
(
rand
)
+
"&time="
+
strconv
.
FormatInt
(
time
,
10
)))
if
err
!=
nil
{
return
""
}
}
return
hex
.
EncodeToString
(
h
.
Sum
(
nil
))
}
// Deep Copy
func
Copy
(
toValue
interface
{},
fromValue
interface
{})
(
err
error
)
{
var
(
...
...
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