| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\librarys\YsOpen;
- use app\utils\CurlHelper;
- use yii\helpers\Json;
- class YsClient
- {
- public $appKey;
- public $secret;
- public $accessToken;
- public $curl;
- const API = "https://open.ys7.com/api/lapp/";
- const API2 = "https://open.ys7.com/api/lapp/v2/";
- // 获取播放地址
- const GETADDRESS = "live/address/get";
- // 获取accessToken
- const GETACCESSTOKEN = "token/get";
- // 获取设备列表
- const DEVICELIST = "device/list";
- // 添加设备
- const ADD = "device/add";
- // 删除设备
- const DEL = "device/delete";
- public function __construct($appKey,$secret){
- $this->appKey = $appKey;
- $this->secret = $secret;
- $this->curl = new CurlHelper();
- }
- public function getAccessToken(){
- $accessToken = \Yii::$app->cache->get("ys_access_token");
- if($accessToken){
- $this->accessToken = $accessToken;
- return $this;
- }else{
- // 去生成access_token
- $ressult = $this->curl::post(self::API.self::GETACCESSTOKEN,['appKey'=>$this->appKey,'appSecret'=>$this->secret]);
- $ressult = Json::decode($ressult);
- if($ressult['code'] == '200'){
- \Yii::$app->cache->set('ys_access_token',$ressult['data']['accessToken'],(bcdiv($ressult['data']['expireTime'],1000,0) - time()));
- $this->accessToken = $ressult['data']['accessToken'];
- return $this;
- }else{
- return false;
- }
- }
- }
- // 刷新设备列表
- public function getDeviceList(){
- $data = [
- 'accessToken' => $this->accessToken,
- // todo 个人版不做分页处理
- // 'pageStart' => 0,// 默认从0开始
- // 'pageSize' => 50,// 最大是50
- ];
- $result = $this->curl::post(self::API.self::DEVICELIST,$data);
- return Json::decode($result);
- }
- // 添加设备
- public function addDeviceItem($deviceSerial,$validateCode){
- $data = [
- 'accessToken' => $this->accessToken,
- 'deviceSerial' => $deviceSerial,
- 'validateCode' => $validateCode,
- ];
- $result = $this->curl::post(self::API.self::ADD,$data);
- return Json::decode($result);
- }
- // 删除设备
- public function delDeviceItem($deviceSerial){
- $data = [
- 'accessToken' => $this->accessToken,
- 'deviceSerial' => $deviceSerial,
- ];
- $result = $this->curl::post(self::API.self::DEL,$data);
- return Json::decode($result);
- }
- // 获取设备播放地址
- public function getAddress($source){
- $data = [
- 'accessToken' => $this->accessToken,
- //'source' => $source,
- 'deviceSerial' =>$source,
- 'protocol' => 1,
- 'quality' => 2,
- ];
- $result = $this->curl::post(self::API2.self::GETADDRESS,$data);
- return Json::decode($result);
- }
- }
|