/
wechatApp.go
129 lines (108 loc) · 2.97 KB
/
wechatApp.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
package send
import (
"encoding/json"
"fmt"
"strings"
"sync"
"time"
"github.com/go-resty/resty/v2"
"github.com/samber/lo"
"github.com/spf13/cast"
)
const (
wechatTokenURL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
wechatSendURL = "https://qyapi.weixin.qq.com/cgi-bin/message/send"
wechatGetUIDURL = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserid"
)
func init() {
registered["wechatApp"] = func(conf map[string]string) sender {
return &wechatApp{conf: conf}
}
}
type wechatApp struct {
conf map[string]string
mtx sync.Mutex
token string
tokenExpireAt time.Time
}
// send wechat app message
//
// https://developer.work.weixin.qq.com/document/path/90236
func (w *wechatApp) send(msg *message) (err error) {
if err = w.checkToken(); err != nil {
return
}
if msg.Simple {
switch msg.MsgType {
case simpleText, simpleMarkdown:
msg.ContentMap = map[string]any{
"content": msg.Content,
}
default:
return fmt.Errorf("sender type %s does not support simple type %s", w.conf["type"], msg.MsgType)
}
}
resp, err := rc.SetPreRequestHook(RecordHttpReq(msg)).R().
SetQueryParam("access_token", w.token).
SetBody(lo.Assign(msg.ExtraMap, map[string]any{
"touser": strings.Join(msg.Tos, "|"),
"agentid": w.conf["agentid"],
"msgtype": msg.MsgType,
msg.MsgType: msg.ContentMap,
})).
Post(wechatSendURL)
RecordResp(msg, err, resp)
return handleErr("send to wechat app failed", err, resp, func(dt map[string]any) bool { return dt["errcode"] == 0.0 })
}
func (w *wechatApp) getConf() map[string]string {
return w.conf
}
// getUIDByPhone
//
// https://developer.work.weixin.qq.com/document/path/95402
func (w *wechatApp) getUIDByPhone(phone string) (uid string, err error) {
if err = w.checkToken(); err != nil {
return
}
type res struct {
UserID string `json:"userid"`
}
r := &res{}
resp, err := rc.R().
SetQueryParam("access_token", w.token).
SetBody(map[string]any{
"mobile": phone,
}).
SetResult(r).
Post(wechatGetUIDURL)
if err = handleErr("get uid by phone with wechat app failed", err, resp, func(dt map[string]any) bool { return dt["errcode"] == 0.0 }); err != nil {
return
}
uid = r.UserID
return
}
func (w *wechatApp) checkToken() (err error) {
now := time.Now()
if !(w.token == "" || w.tokenExpireAt.Before(now)) {
return nil
}
w.mtx.Lock()
defer w.mtx.Unlock()
if w.token == "" || w.tokenExpireAt.Before(now) {
var resp *resty.Response
resp, err = rc.R().
SetQueryParams(map[string]string{
"corpid": w.conf["corpid"],
"corpsecret": w.conf["corpsecret"],
}).
Get(wechatTokenURL)
if err = handleErr("wechat get access token failed", err, resp, func(dt map[string]any) bool { return dt["errcode"] == 0.0 }); err != nil {
return
}
dt := make(map[string]any)
_ = json.Unmarshal(resp.Body(), &dt)
w.token = cast.ToString(dt["access_token"])
w.tokenExpireAt = now.Add(time.Second * time.Duration(cast.ToInt((dt["expires_in"]))))
}
return
}