Device.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. /**
  11. * Device.php.
  12. *
  13. * @author soone <66812590@qq.com>
  14. * @copyright 2016 soone <66812590@qq.com>
  15. */
  16. namespace EasyWeChat\Device;
  17. use EasyWeChat\Core\AbstractAPI;
  18. use EasyWeChat\Core\AccessToken;
  19. /**
  20. * Class Device.
  21. */
  22. class Device extends AbstractAPI
  23. {
  24. protected $deviceType;
  25. protected $productId;
  26. protected $config;
  27. const API_TRANS_MSG = 'https://api.weixin.qq.com/device/transmsg';
  28. const API_CREATE = 'https://api.weixin.qq.com/device/create_qrcode';
  29. const API_DEV_STAT = 'https://api.weixin.qq.com/device/get_stat';
  30. const API_DEV_AUTH = 'https://api.weixin.qq.com/device/authorize_device';
  31. const API_DEV_GET_QRCODE = 'https://api.weixin.qq.com/device/getqrcode';
  32. const API_DEV_VERIFY_QRCODE = 'https://api.weixin.qq.com/device/verify_qrcode';
  33. const API_DEV_BIND = 'https://api.weixin.qq.com/device/bind';
  34. const API_DEV_UNBIND = 'https://api.weixin.qq.com/device/unbind';
  35. const API_DEV_COMPEL_BIND = 'https://api.weixin.qq.com/device/compel_bind';
  36. const API_DEV_COMPEL_UNBIND = 'https://api.weixin.qq.com/device/compel_unbind';
  37. const API_DEV_GET_OPENID = 'https://api.weixin.qq.com/device/get_openid';
  38. const API_USER_DEV_BIND = 'https://api.weixin.qq.com/device/get_bind_device';
  39. public function __construct(AccessToken $accessToken, $config)
  40. {
  41. parent::setAccessToken($accessToken);
  42. $this->config = $config;
  43. $this->deviceType = $this->config['device_type'];
  44. $this->productId = $this->config['product_id'];
  45. }
  46. public function setProductId($productId)
  47. {
  48. $this->productId = $productId;
  49. return $this;
  50. }
  51. /**
  52. * Send message to device.
  53. *
  54. * @param int $sceneValue
  55. *
  56. * @return \EasyWeChat\Support\Collection
  57. */
  58. public function sendToDevice($deviceId, $openId, $content)
  59. {
  60. $params = [
  61. 'device_type' => $this->deviceType,
  62. 'device_id' => $deviceId,
  63. 'open_id' => $openId,
  64. 'content' => base64_decode($content, true),
  65. ];
  66. return $this->parseJSON('json', [self::API_TRANS_MSG, $params]);
  67. }
  68. public function getDeviceQrcode(array $deviceIds)
  69. {
  70. $params = [
  71. 'device_num' => count($deviceIds),
  72. 'device_id_list' => $deviceIds,
  73. ];
  74. return $this->parseJSON('json', [self::API_CREATE, $params]);
  75. }
  76. public function authorizeDevice(array $deviceInfos, $opType = 0)
  77. {
  78. $params = [
  79. 'device_num' => count($deviceInfos),
  80. 'device_list' => $this->getDeviceList($deviceInfos),
  81. 'op_type' => $opType,
  82. 'product_id' => $this->productId,
  83. ];
  84. return $this->parseJSON('json', [self::API_DEV_AUTH, $params]);
  85. }
  86. protected function getDeviceList($deviceInfos)
  87. {
  88. $res = [];
  89. foreach ($deviceInfos as $dInfo) {
  90. $data = [
  91. 'id' => $dInfo['deviceId'],
  92. 'mac' => $dInfo['mac'],
  93. 'connect_protocol' => $this->config['connect_protocol'],
  94. 'auth_key' => $this->config['auth_key'],
  95. 'close_strategy' => $this->config['close_strategy'],
  96. 'conn_strategy' => $this->config['conn_strategy'],
  97. 'crypt_method' => $this->config['crypt_method'],
  98. 'auth_ver' => $this->config['auth_ver'],
  99. 'manu_mac_pos' => $this->config['manu_mac_pos'],
  100. 'ser_mac_pos' => $this->config['ser_mac_pos'],
  101. ];
  102. !empty($this->config['ble_simple_protocol']) ? $data['ble_simple_protocol'] = $this->config['ble_simple_protocol'] : '';
  103. $res[] = $data;
  104. }
  105. return $res;
  106. }
  107. public function createDeviceId()
  108. {
  109. $params = [
  110. 'product_id' => $this->productId,
  111. ];
  112. return $this->parseJSON('get', [self::API_DEV_GET_QRCODE, $params]);
  113. }
  114. public function bind($openId, $deviceId, $ticket)
  115. {
  116. $params = [
  117. 'ticket' => $ticket,
  118. 'device_id' => $deviceId,
  119. 'openid' => $openId,
  120. ];
  121. return $this->parseJSON('json', [self::API_DEV_BIND, $params]);
  122. }
  123. public function unbind($openId, $deviceId, $ticket)
  124. {
  125. $params = [
  126. 'ticket' => $ticket,
  127. 'device_id' => $deviceId,
  128. 'openid' => $openId,
  129. ];
  130. return $this->parseJSON('json', [self::API_DEV_UNBIND, $params]);
  131. }
  132. public function compelBind($openId, $deviceId)
  133. {
  134. $params = [
  135. 'device_id' => $deviceId,
  136. 'openid' => $openId,
  137. ];
  138. return $this->parseJSON('json', [self::API_DEV_COMPEL_BIND, $params]);
  139. }
  140. public function compelUnbind($openId, $deviceId)
  141. {
  142. $params = [
  143. 'device_id' => $deviceId,
  144. 'openid' => $openId,
  145. ];
  146. return $this->parseJSON('json', [self::API_DEV_COMPEL_UNBIND, $params]);
  147. }
  148. public function getDeviceStatus($deviceId)
  149. {
  150. $params = [
  151. 'device_id' => $deviceId,
  152. ];
  153. return $this->parseJSON('get', [self::API_DEV_STAT, $params]);
  154. }
  155. public function verifyQrcode($ticket)
  156. {
  157. $params = [
  158. 'ticket' => $ticket,
  159. ];
  160. return $this->parseJSON('post', [self::API_DEV_VERIFY_QRCODE, $params]);
  161. }
  162. public function getOpenid($deviceId)
  163. {
  164. $params = [
  165. 'device_type' => $this->deviceType,
  166. 'device_id' => $deviceId,
  167. ];
  168. return $this->parseJSON('get', [self::API_DEV_GET_OPENID, $params]);
  169. }
  170. public function getDeviceidByOpenid($openid)
  171. {
  172. $params = [
  173. 'openid' => $openid,
  174. ];
  175. return $this->parseJSON('get', [self::API_USER_DEV_BIND, $params]);
  176. }
  177. }