-
Notifications
You must be signed in to change notification settings - Fork 34
/
initializer.go
52 lines (45 loc) · 1.49 KB
/
initializer.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
package weixin
import (
"encoding/base64"
"os"
"regexp"
"github.com/arstd/log"
)
// TConfig 配置
var (
OriginId string // 原始ID
AppId string // 应用ID
AppSecret string // 应用密钥
Token string // 令牌
EncodingAESKey []byte // 消息加解密密钥
)
// Initialize 配置并初始化
func Initialize(originId, appId, appSecret, token, encodingAESKey string) {
if matched, err := regexp.MatchString("^gh_[0-9a-f]{12}$", originId); err != nil || !matched {
log.Fatalf("originId format error: %s", err)
}
if matched, err := regexp.MatchString("^wx[0-9a-f]{16}$", appId); err != nil || !matched {
log.Fatalf("appId format error: %s", err)
}
if matched, err := regexp.MatchString("^[0-9a-f]{32}$", appSecret); err != nil || !matched {
log.Fatalf("appSecret format error: %s", err)
}
if matched, err := regexp.MatchString("^[0-9a-zA-Z]{3,32}$", token); err != nil || !matched {
log.Fatalf("token format error: %s", err)
}
if matched, err := regexp.MatchString("^[0-9a-zA-Z]{43}$", encodingAESKey); err != nil || !matched {
log.Fatalf("encodingAESKey format error: %s", err)
}
OriginId = originId // 原始ID
AppId = appId // 应用ID
AppSecret = appSecret // 应用密钥
Token = token // 令牌
var err error
EncodingAESKey, err = base64.StdEncoding.DecodeString(encodingAESKey + "=")
if err != nil {
log.Fatalf("appSecret config error: %s", err)
os.Exit(1)
}
// refresh access token
RefreshAccessToken(AppId, AppSecret)
}