YsClient.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\librarys\YsOpen;
  8. use app\utils\CurlHelper;
  9. use yii\helpers\Json;
  10. class YsClient
  11. {
  12. public $appKey;
  13. public $secret;
  14. public $accessToken;
  15. public $curl;
  16. const API = "https://open.ys7.com/api/lapp/";
  17. const API2 = "https://open.ys7.com/api/lapp/v2/";
  18. // 获取播放地址
  19. const GETADDRESS = "live/address/get";
  20. // 获取accessToken
  21. const GETACCESSTOKEN = "token/get";
  22. // 获取设备列表
  23. const DEVICELIST = "device/list";
  24. // 添加设备
  25. const ADD = "device/add";
  26. // 删除设备
  27. const DEL = "device/delete";
  28. public function __construct($appKey,$secret){
  29. $this->appKey = $appKey;
  30. $this->secret = $secret;
  31. $this->curl = new CurlHelper();
  32. }
  33. public function getAccessToken(){
  34. $accessToken = \Yii::$app->cache->get("ys_access_token");
  35. if($accessToken){
  36. $this->accessToken = $accessToken;
  37. return $this;
  38. }else{
  39. // 去生成access_token
  40. $ressult = $this->curl::post(self::API.self::GETACCESSTOKEN,['appKey'=>$this->appKey,'appSecret'=>$this->secret]);
  41. $ressult = Json::decode($ressult);
  42. if($ressult['code'] == '200'){
  43. \Yii::$app->cache->set('ys_access_token',$ressult['data']['accessToken'],(bcdiv($ressult['data']['expireTime'],1000,0) - time()));
  44. $this->accessToken = $ressult['data']['accessToken'];
  45. return $this;
  46. }else{
  47. return false;
  48. }
  49. }
  50. }
  51. // 刷新设备列表
  52. public function getDeviceList(){
  53. $data = [
  54. 'accessToken' => $this->accessToken,
  55. // todo 个人版不做分页处理
  56. // 'pageStart' => 0,// 默认从0开始
  57. // 'pageSize' => 50,// 最大是50
  58. ];
  59. $result = $this->curl::post(self::API.self::DEVICELIST,$data);
  60. return Json::decode($result);
  61. }
  62. // 添加设备
  63. public function addDeviceItem($deviceSerial,$validateCode){
  64. $data = [
  65. 'accessToken' => $this->accessToken,
  66. 'deviceSerial' => $deviceSerial,
  67. 'validateCode' => $validateCode,
  68. ];
  69. $result = $this->curl::post(self::API.self::ADD,$data);
  70. return Json::decode($result);
  71. }
  72. // 删除设备
  73. public function delDeviceItem($deviceSerial){
  74. $data = [
  75. 'accessToken' => $this->accessToken,
  76. 'deviceSerial' => $deviceSerial,
  77. ];
  78. $result = $this->curl::post(self::API.self::DEL,$data);
  79. return Json::decode($result);
  80. }
  81. // 获取设备播放地址
  82. public function getAddress($source){
  83. $data = [
  84. 'accessToken' => $this->accessToken,
  85. //'source' => $source,
  86. 'deviceSerial' =>$source,
  87. 'protocol' => 1,
  88. 'quality' => 2,
  89. ];
  90. $result = $this->curl::post(self::API2.self::GETADDRESS,$data);
  91. return Json::decode($result);
  92. }
  93. }