-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwkteam.go
144 lines (133 loc) · 3.14 KB
/
wkteam.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
package wkteam
import (
"github.com/suboat/go-contrib"
"github.com/suboat/go-contrib/log"
"fmt"
"strings"
"sync"
)
// 文档地址 https://wkteam.gitbook.io/doc/api-wen-dang
var (
// 版本
Version = &contrib.Version{Major: 0, Minor: 0, Patch: 1, Model: "wkteam"}
// GitCommit 当前代码对应的git-commit记录
GitCommit = ""
// Settings 系统设置
Settings *Config
)
// 通用hook
var (
DefaultHookHookMsgGroup = func(msg *MsgGroup) error {
log.Debugf(`[default] HookHookMsgGroup %s <- %s`, msg.Account, PubJSON(msg))
return nil
}
DefaultHookHookMsgUser = func(msg *MsgUser) error {
log.Debugf(`[default] HookHookMsgUser %s -> %s : %s`, msg.GetFromName(), msg.GetToName(), PubJSON(msg))
return nil
}
)
// Config 系统配置参数
type Config struct {
contrib.Config `yaml:"-"`
//
ApiHost string // 微控api入口
Phone string // 开发者手机号
Password string // 开发者密码
Secret string // 开发者密钥
Account string // 默认要管理的微信号
CallbackLocal string // 实际监听回调地址
CallbackPublic string // 映射到公网的回调地址
}
//
type WkTeam struct {
// 账号信息
Phone string // 开发者手机号
Password string // 开发者密码
Secret string // 开发者密钥
Account string // 要管理的微信号
//
ApiHost string // 微控api入口
Log contrib.Logger //
// hooks
HookMsgUser func(group *MsgUser) error
HookMsgGroup func(group *MsgGroup) error
//
lock sync.RWMutex //
apiKey string //
//
inited bool
}
//
func (cfg *Config) Valid() (err error) {
if len(cfg.Phone) == 0 {
return fmt.Errorf(`phone undefined`)
} else if len(cfg.Password) == 0 {
return fmt.Errorf(`password undefined`)
} else if len(cfg.Secret) == 0 {
return fmt.Errorf(`secret undefined`)
}
return
}
//
func (api *WkTeam) init() (err error) {
if api.inited {
return
}
api.lock.Lock()
defer api.lock.Unlock()
if api.inited {
return
}
// defaults
if api.Log == nil {
api.Log = log.Log
}
if len(api.ApiHost) == 0 {
api.ApiHost = Settings.ApiHost
}
if len(api.Phone) == 0 {
api.Phone = Settings.Phone
}
if len(api.Password) == 0 {
api.Password = Settings.Password
}
if len(api.Secret) == 0 {
api.Secret = Settings.Secret
}
api.ApiHost = strings.TrimRight(api.ApiHost, "/")
api.inited = true
return
}
// NewWkTeam 新建一个微控对象
func NewWkTeam(s *WkTeam) (ret *WkTeam) {
if s != nil {
ret = s
} else {
ret = new(WkTeam)
}
_ = ret.init()
return
}
//
func init() {
// 默认设置
Settings = &Config{
ApiHost: "http://admin.wkgjhome.com",
CallbackLocal: "http://127.0.0.1:8080/v1/wkteam/",
CallbackPublic: "https://yourhost/api/wkteam/",
}
// 配置注释
_ = Settings.SetComments(map[string]string{
"ApiHost": "微控api入口",
"Phone": "开发者手机号",
"Password": "开发者密码",
"Secret": "开发者密钥",
"Account": "默认要管理的微信号",
"CallbackLocal": "实际监听回调地址",
"CallbackPublic": "映射到公网的回调地址",
})
// version
if len(GitCommit) > 0 {
Version.Commit = &GitCommit
}
}