authorize.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Package device 设备相关接口
  2. package device
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "github.com/silenceper/wechat/v2/util"
  7. )
  8. const (
  9. // DeviceAdd 添加设备标识
  10. DeviceAdd = iota
  11. // DeviceUpgrade 更新设备标识
  12. DeviceUpgrade
  13. )
  14. type reqDeviceAuthorize struct {
  15. // 设备 id 的个数
  16. DeviceNum string `json:"device_num"`
  17. // 设备 id 的列表,json 的 array 格式,其 size 必须等于 device_num
  18. DeviceList []ReqDevice `json:"device_list"`
  19. // 请求操作的类型,限定取值为:0:设备授权(缺省值为 0)1:设备更新(更新已授权设备的各属性值)
  20. OpType string `json:"op_type,omitempty"`
  21. // 设备的产品编号(由微信硬件平台分配)。可在公众号设备功能管理页面查询。
  22. // 当 op_type 为‘0’,product_id 为‘1’时,不要填写 product_id 字段(会引起不必要错误);
  23. // 当 op_typy 为‘0’,product_id 不为‘1’时,必须填写 product_id 字段;
  24. // 当 op_type 为 1 时,不要填写 product_id 字段。
  25. ProductID string `json:"product_id,omitempty"`
  26. }
  27. // ReqDevice 设备授权实体
  28. type ReqDevice struct {
  29. // 设备的 device id
  30. ID string `json:"id"`
  31. // 设备的 mac 地址 格式采用 16 进制串的方式(长度为 12 字节),
  32. // 不需要 0X 前缀,如:1234567890AB
  33. Mac string `json:"mac"`
  34. // 支持以下四种连接协议:
  35. // android classic bluetooth – 1
  36. // ios classic bluetooth – 2
  37. // ble – 3
  38. // wifi -- 4
  39. // 一个设备可以支持多种连接类型,用符号"|"做分割,客户端优先选择靠前的连接方式(优先级按 | 关系的排序依次降低),举例:
  40. // 1:表示设备仅支持 andiod classic bluetooth 1|2:表示设备支持 android 和 ios 两种 classic bluetooth,但是客户端优先选择 android classic bluetooth 协议,如果 android classic bluetooth 协议连接失败,再选择 ios classic bluetooth 协议进行连接
  41. // (注:安卓平台不同时支持 BLE 和 classic 类型)
  42. ConnectProtocol string `json:"connect_protocol"`
  43. // auth 及通信的加密 key,第三方需要将 key 烧制在设备上(128bit),格式采用 16 进制串的方式(长度为 32 字节),不需要 0X 前缀,如:1234567890ABCDEF1234567890ABCDEF
  44. AuthKey string `json:"auth_key"`
  45. // 断开策略,目前支持:1:退出公众号页面时即断开连接 2:退出公众号之后保持连接不断开
  46. CloseStrategy string `json:"close_strategy"`
  47. // 连接策略,32 位整型,按 bit 位置位,目前仅第 1bit 和第 3bit 位有效(bit 置 0 为无效,1 为有效;第 2bit 已被废弃),且 bit 位可以按或置位(如 1|4=5),各 bit 置位含义说明如下:
  48. // 1:(第 1bit 置位)在公众号对话页面,不停的尝试连接设备
  49. // 4:(第 3bit 置位)处于非公众号页面(如主界面等),微信自动连接。当用户切换微信到前台时,可能尝试去连接设备,连上后一定时间会断开
  50. ConnStrategy string `json:"conn_strategy"`
  51. // auth version,设备和微信进行 auth 时,会根据该版本号来确认 auth buf 和 auth key 的格式(各 version 对应的 auth buf 及 key 的具体格式可以参看“客户端蓝牙外设协议”),该字段目前支持取值:
  52. // 0:不加密的 version
  53. // 1:version 1
  54. AuthVer string `json:"auth_ver"`
  55. // 表示 mac 地址在厂商广播 manufacture data 里含有 mac 地址的偏移,取值如下:
  56. // -1:在尾部、
  57. // -2:表示不包含 mac 地址 其他:非法偏移
  58. ManuMacPos string `json:"manu_mac_pos"`
  59. // 表示 mac 地址在厂商 serial number 里含有 mac 地址的偏移,取值如下:
  60. // -1:表示在尾部
  61. // -2:表示不包含 mac 地址 其他:非法偏移
  62. SerMacPost string `json:"ser_mac_post"`
  63. // 精简协议类型,取值如下:计步设备精简协议:1(若该字段填 1,connect_protocol 必须包括 3。非精简协议设备切勿填写该字段)
  64. BleSimpleProtocol string `json:"ble_simple_protocol,omitempty"`
  65. }
  66. // ResBaseInfo 授权回调实体
  67. type ResBaseInfo struct {
  68. BaseInfo struct {
  69. DeviceType string `json:"device_type"`
  70. DeviceID string `json:"device_id"`
  71. } `json:"base_info"`
  72. }
  73. // 授权回调根信息
  74. type resDeviceAuthorize struct {
  75. util.CommonError
  76. Resp []ResBaseInfo `json:"resp"`
  77. }
  78. // DeviceAuthorize 设备授权
  79. func (d *Device) DeviceAuthorize(devices []ReqDevice, opType int, product string) (res []ResBaseInfo, err error) {
  80. var accessToken string
  81. accessToken, err = d.GetAccessToken()
  82. if err != nil {
  83. return
  84. }
  85. uri := fmt.Sprintf("%s?access_token=%s", uriAuthorize, accessToken)
  86. req := reqDeviceAuthorize{
  87. DeviceNum: fmt.Sprintf("%d", len(devices)),
  88. DeviceList: devices,
  89. OpType: fmt.Sprintf("%d", opType),
  90. ProductID: product,
  91. }
  92. var response []byte
  93. response, err = util.PostJSON(uri, req)
  94. if err != nil {
  95. return nil, err
  96. }
  97. var result resDeviceAuthorize
  98. err = json.Unmarshal(response, &result)
  99. if err != nil {
  100. return
  101. }
  102. if result.ErrCode != 0 {
  103. err = fmt.Errorf("DeviceAuthorize Error , errcode=%d , errmsg=%s", result.ErrCode, result.ErrMsg)
  104. return
  105. }
  106. res = result.Resp
  107. return
  108. }