mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-08 06:32:27 +08:00
完善微信智能硬件接口
This commit is contained in:
@@ -8,8 +8,12 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
deviceAuthorize = "https://api.weixin.qq.com/device/authorize_device"
|
||||
deviceQRCode = "https://api.weixin.qq.com/device/create_qrcode"
|
||||
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"
|
||||
uriState = "https://api.weixin.qq.com/device/get_stat"
|
||||
)
|
||||
|
||||
//Device struct
|
||||
@@ -24,64 +28,29 @@ func NewDevice(context *context.Context) *Device {
|
||||
return device
|
||||
}
|
||||
|
||||
// DeviceAuthorize 设备授权
|
||||
func (d *Device) DeviceAuthorize(devices []ReqDevice, opType int, productId string) (res []resBaseInfo, err error) {
|
||||
var accessToken string
|
||||
accessToken, err = d.GetAccessToken()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
uri := fmt.Sprintf("%s?access_token=%s", deviceAuthorize, accessToken)
|
||||
req := reqDeviceAuthorize{
|
||||
DeviceNum: fmt.Sprintf("%d", len(devices)),
|
||||
DeviceList: devices,
|
||||
OpType: fmt.Sprintf("%d", opType),
|
||||
ProductId: productId,
|
||||
}
|
||||
response, err := util.PostJSON(uri, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result resDeviceAuthorize
|
||||
err = json.Unmarshal(response, &result)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if result.ErrCode != 0 {
|
||||
err = fmt.Errorf("DeviceAuthorize Error , errcode=%d , errmsg=%s", result.ErrCode, result.ErrMsg)
|
||||
return
|
||||
}
|
||||
res = result.Resp
|
||||
return
|
||||
type resDeviceState struct {
|
||||
util.CommonError
|
||||
Status int `json:"status"`
|
||||
StatusInfo string `json:"status_info"`
|
||||
}
|
||||
|
||||
// CreateQRCode 获取设备二维码
|
||||
func (d *Device) CreateQRCode(devices []string) (res []resQRCode, err error) {
|
||||
// State 设备状态查询
|
||||
func (d *Device) State(deviceId string) (res resDeviceState, err error) {
|
||||
var accessToken string
|
||||
accessToken, err = d.GetAccessToken()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
uri := fmt.Sprintf("%s?access_token=%s", deviceQRCode, accessToken)
|
||||
req := map[string]interface{}{
|
||||
"device_num": len(devices),
|
||||
"device_id_list": devices,
|
||||
}
|
||||
fmt.Println(req)
|
||||
response, err := util.PostJSON(uri, req)
|
||||
if err != nil {
|
||||
if accessToken, err = d.GetAccessToken(); err != nil {
|
||||
return
|
||||
}
|
||||
var result resCreateQRCode
|
||||
err = json.Unmarshal(response, &result)
|
||||
if err != nil {
|
||||
uri := fmt.Sprintf("%s?access_token=%s&device_id=%s", uriState, accessToken, deviceId)
|
||||
var response []byte
|
||||
if response, err = util.HTTPGet(uri); err != nil {
|
||||
return
|
||||
}
|
||||
if result.ErrCode != 0 {
|
||||
err = fmt.Errorf("CreateQRCode Error , errcode=%d , errmsg=%s", result.ErrCode, result.ErrMsg)
|
||||
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
|
||||
}
|
||||
res = result.CodeList
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user