forked from wei193/ys7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathys7.go
162 lines (146 loc) · 3.25 KB
/
ys7.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package ys7
import (
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
)
//URL
const (
MASTERACC = 0 //主账号
RAMACC = 1 //子账号
//[用户]获取accessToken
ACCESSTOKEN = "https://open.ys7.com/api/lapp/token/get" //获取accessToken
//[用户]好友分享
// --全部待实现
)
//NewYs7 创建Ys7对象
func NewYs7(AppKey, Secret string) (ys *Ys7, err error) {
ys = &Ys7{
AppKey: AppKey,
Secret: Secret,
IsRAM: MASTERACC,
}
_, err = ys.GetAccessToken()
return
}
//NewRAMYs7 创建子账号对象
func NewRAMYs7(AppKey, Secret, AccountID string) (ys *Ys7, err error) {
ys = &Ys7{
AppKey: AppKey,
Secret: Secret,
IsRAM: RAMACC,
AccountID: AccountID,
}
_, err = ys.GetAccessToken()
return
}
//GetAccessToken 获取token
func (ys *Ys7) GetAccessToken() (ac *AccessToken, err error) {
params := make(map[string]interface{})
params["appKey"] = ys.AppKey
params["appSecret"] = ys.Secret
ac = &AccessToken{}
_, err = ys.requset("POST", ACCESSTOKEN, params, &ac)
if err != nil {
return nil, err
}
if ys.IsRAM == MASTERACC {
ys.AccessToken = ac.AccessToken
ys.ExpireTime = ac.ExpireTime
} else {
ys.AccessToken = ac.AccessToken
ac, err = ys.RAMGetAccessToken(ys.AccountID)
if err != nil {
ys.AccessToken = ""
return
}
ys.AccessToken = ac.AccessToken
ys.ExpireTime = ac.ExpireTime
}
return ac, nil
}
func (ys *Ys7) requset(method, url string, params map[string]interface{}, data interface{}, page ...interface{}) (status *Status, err error) {
defer func() {
if Rerr := recover(); Rerr != nil {
err = errors.New("recover error")
return
}
}()
var r http.Request
r.ParseForm()
for k, v := range params {
r.Form.Add(k, fmt.Sprint(v))
}
req, err := http.NewRequest(method, url, strings.NewReader(r.Form.Encode()))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := &http.Client{Timeout: 60 * time.Second}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var res respStatus
if len(page) == 1 {
res = respStatus{
Data: data,
Page: page[0],
}
} else {
res = respStatus{
Data: data,
}
}
err = json.Unmarshal(buf, &res)
if err != nil {
return nil, err
}
status = &Status{
Code: res.Code,
Msg: res.Msg,
Buf: buf,
}
if res.Code != "200" {
return status, errors.New(res.Msg)
}
return status, nil
}
func (ys *Ys7) authorizeRequset(method, url string, params map[string]interface{}, data interface{}, page ...interface{}) (status *Status, err error) {
exTime := time.Unix(ys.ExpireTime/1000, 0)
if exTime.Unix() < time.Now().Unix() {
ys.GetAccessToken()
}
defer func() {
if Rerr := recover(); Rerr != nil {
err = errors.New("recover error")
return
}
}()
params["accessToken"] = ys.AccessToken
status, err = ys.requset(method, url, params, data)
return
}
func getPage(buf []byte) Page {
type st struct {
Page Page
}
var gPage st
json.Unmarshal(buf, &gPage)
return gPage.Page
}
func getPasswd(appkey, password string) string {
hash := md5.New()
hash.Write([]byte(appkey + "#" + password))
return fmt.Sprintf("%x", hash.Sum(nil))
}