forked from silenceper/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.go
60 lines (53 loc) · 1.56 KB
/
device.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
package device
import (
"encoding/json"
"fmt"
"github.com/silenceper/wechat/context"
"github.com/silenceper/wechat/util"
)
const (
uriAuthorize = "https://api.weixin.qq.com/device/authorize_device"
uriQRCode = "https://api.weixin.qq.com/device/create_qrcode"
uriVerifyQRCode = "https://api.weixin.qq.com/device/verify_qrcode"
uriBind = "https://api.weixin.qq.com/device/bind"
uriUnbind = "https://api.weixin.qq.com/device/unbind"
uriCompelBind = "https://api.weixin.qq.com/device/compel_bind"
uriCompelUnbind = "https://api.weixin.qq.com/device/compel_unbind"
uriState = "https://api.weixin.qq.com/device/get_stat"
)
//Device struct
type Device struct {
*context.Context
}
//NewDevice 实例
func NewDevice(context *context.Context) *Device {
device := new(Device)
device.Context = context
return device
}
// ResDeviceState 设备状态响应实体
type ResDeviceState struct {
util.CommonError
Status int `json:"status"`
StatusInfo string `json:"status_info"`
}
// State 设备状态查询
func (d *Device) State(device string) (res ResDeviceState, err error) {
var accessToken string
if accessToken, err = d.GetAccessToken(); err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s&device_id=%s", uriState, accessToken, device)
var response []byte
if response, err = util.HTTPGet(uri); err != nil {
return
}
if err = json.Unmarshal(response, &res); err != nil {
return
}
if res.ErrCode != 0 {
err = fmt.Errorf("DeviceState Error , errcode=%d , errmsg=%s", res.ErrCode, res.ErrMsg)
return
}
return
}