-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsso.go
331 lines (301 loc) · 8.72 KB
/
sso.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package sso
import (
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/imroc/req/v3"
"github.com/pkg/errors"
"math/rand"
"net/http"
"time"
)
var (
Sdk Instance
)
type Instance struct {
Host string
PublicKey string
SecretKey string
Prefix string
}
func New(publicKey, secretKey string) Instance {
Sdk = Instance{
Host: "https://www.resok.cn",
PublicKey: publicKey,
SecretKey: secretKey,
Prefix: "/o",
}
return Sdk
}
func (c *Instance) SetHost(host string) {
c.Host = host
}
// 生成基本的public_key url参数
func (c *Instance) getParam() map[string]string {
return map[string]string{
"public_key": c.PublicKey,
}
}
// 生成一个请求req
func (c *Instance) getReq() *req.Request {
return req.R()
}
func (c *Instance) getJsonReq(body interface{}) *req.Request {
r := c.getReq()
r.SetBodyJsonMarshal(body)
return r
}
// 生成随机字符串
func (c *Instance) randomStr(n int) string {
randBytes := make([]byte, n/2)
rand.Read(randBytes)
return fmt.Sprintf("%x", randBytes)
}
// 获取当前时间戳文本
func (c *Instance) getTimeUnixStr() string {
return fmt.Sprintf("%d", time.Now().Unix())
}
// Sign 生成一次加密
func (c *Instance) Sign() (string, string, string) {
rs := c.randomStr(16)
us := c.getTimeUnixStr()
return c.sign(rs, us), rs, us
}
// 加密方法
func (c *Instance) sign(randomStr, timeUnix string) string {
h := md5.New()
h.Write([]byte(randomStr))
h.Write([]byte(c.SecretKey))
h.Write([]byte(timeUnix))
return hex.EncodeToString(h.Sum(nil))
}
// CheckSign 验证加密
func (c *Instance) CheckSign(sign, randomStr, timeUnix string) bool {
nowSign := c.sign(randomStr, timeUnix)
return sign == nowSign
}
// UrlGen 请求url路径生成
func (c *Instance) UrlGen(prefix string, p string) string {
return c.Host + prefix + p + "?public_key=" + c.PublicKey
}
// RunTr 发起交易 receipt 是否是商品收款
func (c *Instance) RunTr(data ProductReceipt, receipt bool) (ProductPayResp, error, int) {
data.GenSign()
var d ProductPayResp
var msg string
var url string
if receipt {
url = c.UrlGen(c.Prefix, "/receipt")
msg = "商品收款"
} else {
url = c.UrlGen(c.Prefix, "/payment")
msg = "转账"
}
resp, err := c.getJsonReq(data).SetQueryParams(c.getParam()).Post(url)
if err != nil {
return d, errors.Wrap(err, "发起交易出错"), 0
}
code := resp.StatusCode
if code != http.StatusOK {
// 余额不足
if code == http.StatusUpgradeRequired {
return d, errors.New("余额不足"), code
}
return d, errors.New(fmt.Sprintf("%s响应错误 %d %s", msg, code, resp.String())), code
}
err = resp.UnmarshalJson(&d)
if err != nil {
return d, errors.Wrap(err, fmt.Sprintf("%s解析返回信息出错", msg)), code
}
return d, nil, code
}
// ProductPreOrder 预下单
func (c *Instance) ProductPreOrder(data PreOrder) (PreOrderResp, error) {
data.GenSign()
var d PreOrderResp
url := c.UrlGen(c.Prefix, "/pre_order")
resp, err := c.getJsonReq(data).Post(url)
if err != nil {
return d, errors.Wrap(err, "预下单出错")
}
code := resp.StatusCode
if code != http.StatusOK {
return d, errors.New(fmt.Sprintf("预下单相应失败 %d %s", code, resp.String()))
}
err = resp.UnmarshalJson(&d)
if err != nil {
return d, errors.Wrap(err, "解析预下单返回失败")
}
return d, nil
}
// UidGetUserInfo 通过uid获取用户信息
func (c *Instance) UidGetUserInfo(uid string) (UidGetUserResp, error) {
var d UidGetUserResp
url := c.UrlGen(c.Prefix, "/get_user")
var p UidGetUserReq
p.Uid = uid
p.GenSign()
resp, err := c.getJsonReq(p).Post(url)
if err != nil {
return d, errors.Wrap(err, "获取用户信息请求出错")
}
code := resp.StatusCode
if code != http.StatusOK {
return d, errors.New(fmt.Sprintf("获取用户信息请求错误 %d %s", code, resp.String()))
}
err = resp.UnmarshalJson(&d)
if err != nil {
return d, errors.Wrap(err, "解析用户信息出错")
}
return d, nil
}
// ChangeUserPower 主动变更用户能力
func (c *Instance) ChangeUserPower(data PowerChangeReq) (bool, error) {
data.GenSign()
url := c.UrlGen(c.Prefix, "/power_change")
resp, err := c.getJsonReq(data).Post(url)
if err != nil {
return false, errors.Wrap(err, "变更用户能力出错")
}
code := resp.StatusCode
if code != http.StatusOK {
return false, errors.New(fmt.Sprintf("变更用户能力失败 %d %s", code, resp.String()))
}
return true, nil
}
// UidGetUserPowerSetting 获取用户能力设置
func (c *Instance) UidGetUserPowerSetting(uid string, eng string) (PowerSettingResp, error) {
var p PowerSettingReq
p.Uid = uid
p.Eng = eng
p.GenSign()
url := c.UrlGen(c.Prefix, "/power_setting_new")
var d PowerSettingResp
resp, err := c.getJsonReq(p).Post(url)
if err != nil {
return d, errors.Wrap(err, "获取能力设置失败")
}
code := resp.StatusCode
if code != http.StatusOK {
return d, errors.New(fmt.Sprintf("获取用户能力设置请求错误 %d %s ", code, resp.String()))
}
err = resp.UnmarshalJson(&d)
if err != nil {
return d, errors.Wrap(err, "解析用户能力设置出错")
}
return d, nil
}
// GetUploadKey 获取上传凭据
func (c *Instance) GetUploadKey() (UploadKeyResp, error) {
var d UploadKeyResp
url := c.UrlGen(c.Prefix, "/upload_key")
resp, err := c.getReq().Get(url)
if err != nil {
return d, errors.Wrap(err, "获取上传凭据请求出错")
}
code := resp.StatusCode
if code != http.StatusOK {
return d, errors.New(fmt.Sprintf("获取上传凭据请求出错 %d %s", code, resp.String()))
}
err = resp.UnmarshalJson(&d)
if err != nil {
return d, errors.Wrap(err, "解析上传凭据请求出错")
}
return d, nil
}
// PreOrderIdGetSuccessList 通过预下单ID获取成交列表
func (c *Instance) PreOrderIdGetSuccessList(preOrderId string, page, pageSize uint64) (*BalanceChangeHistoryResp, error) {
var r = new(BalanceChangeHistoryResp)
url := c.UrlGen(c.Prefix, "/pre_order_id")
params := map[string]interface{}{"pre_order_id": preOrderId, "page": page, "page_size": pageSize}
sign, st, t := Sdk.Sign()
params["sign"] = sign
params["random_str"] = st
params["t"] = t
resp, err := c.getReq().SetQueryParamsAnyType(params).Get(url)
if err != nil {
return nil, errors.Wrap(err, "获取成交列表失败")
}
code := resp.StatusCode
if code != http.StatusOK {
return nil, errors.New(fmt.Sprintf("获取成交列表请求出错 %d %s", code, resp.String()))
}
err = resp.UnmarshalJson(&r)
if err != nil {
return nil, errors.Wrap(err, "解析成交记录失败")
}
return r, nil
}
// OrderIdGetInfo 通过orderId获取成交记录
func (c *Instance) OrderIdGetInfo(orderId string) (GetOrderInfoResp, error) {
var r GetOrderInfoResp
url := c.UrlGen(c.Prefix, "/order_id")
params := map[string]string{"order_id": orderId}
sign, st, t := Sdk.Sign()
params["sign"] = sign
params["random_str"] = st
params["t"] = t
resp, err := c.getReq().SetQueryParams(params).Get(url)
if err != nil {
return r, errors.Wrap(err, "获取成交列表失败")
}
code := resp.StatusCode
if code != http.StatusOK {
return r, errors.New(fmt.Sprintf("获取成交列表请求出错 %d %s", code, resp.String()))
}
err = resp.UnmarshalJson(&r)
if err != nil {
return r, errors.Wrap(err, "解析成交记录失败")
}
return r, nil
}
// UploadImage 上传图片
func (c *Instance) UploadImage(imgPath string, maxWidth int) (UploadImageResp, error) {
var r UploadImageResp
url := c.UrlGen(c.Prefix, "/img_upload")
params := make(map[string]interface{})
sign, st, t := Sdk.Sign()
params["sign"] = sign
params["random_str"] = st
params["t"] = t
params["max_width"] = maxWidth
resp, err := c.getReq().SetQueryParamsAnyType(params).SetFile("file", imgPath).SetResult(&r).Post(url)
if err != nil {
return r, errors.Wrap(err, "发起图像上传失败")
}
code := resp.StatusCode
if code != http.StatusOK {
return r, errors.New(fmt.Sprintf("上传图像失败 %d %s", code, resp.String()))
}
err = resp.UnmarshalJson(&r)
if err != nil {
return r, errors.Wrap(err, "解析上传图像结果失败")
}
return r, nil
}
// HitText 检测文字是否违规
func (c *Instance) HitText(content string) (*HitTextResp, error) {
url := c.UrlGen("", "/hit_text")
body := map[string]string{
"content": content,
}
var r = new(HitTextResp)
resp, err := c.getReq().SetBodyJsonMarshal(body).SetResult(r).Post(url)
if err != nil && !resp.IsSuccess() {
return r, errors.Wrap(err, "校验文本失败")
}
return r, err
}
// HitImage 检测图像是否违规
func (c *Instance) HitImage(imageUrl string) (*HitImgResp, error) {
url := c.UrlGen("", "/hit_image")
body := map[string]string{
"uri": imageUrl,
}
var r = new(HitImgResp)
resp, err := c.getReq().SetBodyJsonMarshal(body).SetResult(r).Post(url)
if err != nil && !resp.IsSuccess() {
return r, errors.Wrap(err, "校验图片合规失败")
}
return r, err
}