欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

萤石视频接口(tp后台)

程序员文章站 2022-07-01 21:01:04
...
<?php
class VideoAction extends CommonAction{

    /*获取主账户的token*/
    public function get_accessToken()
    {
        $appKey      = '';//主账户key
        $appSecret   = '';//主账户secret
        $url         = 'https://open.ys7.com/api/lapp/token/get';
        $data        = array('appKey'=>$appKey,'appSecret'=>$appSecret);
        $accessToken = $this->request_post($url,$data);
        $accessToken = json_decode($accessToken , true);
        $accessToken = $accessToken['data']['accessToken'];
        // 存于device_token表中
        $data_save['accessToken']= $accessToken;
        if(M('device_token')->where('nurseryid = '.session("D_NURSERYID"))->find()){
            M('device_token')->where('nurseryid = '.session("D_NURSERYID"))->save($data_save);
        }else{
            $data_save['nurseryid']  = session('D_NURSERYID');
            $data_save['appKey']     = $appKey;
            $data_save['appSecret']  = $appSecret;
            M('device_token')->add($data_save);
        }
    }
    /*添加子账户(目前需要一个)*/
    /*public function add_user()
    {
        // 主账户token
        $device_token= $this->device_token();
        $accessToken = $device_token['accessToken'];

        $url  = 'https://open.ys7.com/api/lapp/ram/account/list';
        $data = array(
            'accessToken'=>$accessToken,
            'accountName'=>time(),
            'password'   =>strtolower(MD5('AppKey#123456'))
        );
        $return      = $this->request_post($url,$data);
        $return_json = json_decode($return , true);
        // 主账户token过期
        if($code == '10002'){
            $accessToken = $this->get_accessToken();
            $this->add_user();
        }
        if($return_json['code'] == '200'){
            $save['accountId']   = $return_json['data']['accountId'];
            $save['password']    = '123456';
            M('device_token')->where('nurseryid = '.session("D_NURSERYID"))->save($save);
        }
    }*/
    /*获取子账户token*/
    public function get_user_accessToken()
    {
        $device_token= $this->device_token();
        $accountId   = $device_token['accountId'];
        $accessToken = $device_token['accessToken'];
        $url         = 'https://open.ys7.com/api/lapp/ram/token/get';
        $data        = array(
            'accessToken'=>$accessToken,
            'accountId'=>$accountId
        );
        $return      = $this->request_post($url,$data);
        $return_json = json_decode($return , true);
        $return      = $return_json['data']['accessToken'];
        if($return_json['code'] == '10002'){
            $accessToken = $this->get_accessToken();
            $this->get_user_accessToken();
        }
        if($return_json['code'] == '200'){
            $save['user_accessToken'] = $return_json['data']['accessToken'];
            M('device_token')->where('nurseryid = '.session("D_NURSERYID"))->save($save);
        }
    }

    /********************设备*********************************/
    /*子账户信息*/
    /*public function user_account()
    {
        $device_token       = $this->device_token();
        $user_accessToken   = $device_token['user_accessToken'];
        $url                = 'https://open.ys7.com/api/lapp/ram/account/get';
        $data               = array('accessToken'=>$user_accessToken);
        $return             = $this->request_post($url,$data);
        $return_json        = json_decode($return , true);
        var_dump($return_json);
    }*/
    /*子账户授权*/
    public function set_policy()
    {
        $device_token = $this->device_token();
        $accessToken  = $device_token['accessToken'];
        $accountId    = $device_token['accountId'];
        $url  = 'https://open.ys7.com/api/lapp/ram/statement/add';
        $data = array(
            'accessToken'=>$accessToken,
            'accountId'  =>$accountId,
            'statement'  =>'{"Permission": "Get,Update,DevCtrl", "Resource":  ["dev:107970588"]}'
        );
        $return      = $this->request_post($url,$data);
        $return_json = json_decode($return , true);
        if($return_json['code'] == '10002'){
            $accessToken = $this->get_accessToken();
            $this->set_policy();
        }
    }
    /*获取数据库账户信息*/
    public function device_token()
    {
        $device_token = M('device_token')->where('nurseryid = '.session("D_NURSERYID"))->find();
        return $device_token;
    }
    public function request_post($url, $data){
        $ch     = curl_init();
        $header = "Accept-Charset: utf-8";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        if (curl_errno($ch)) {
            return false;
        }else{
            return $tmpInfo;
        }
    }   
}