Authored by wuxiao

Merge branch 'develop' of http://git.yoho.cn/web/yoho-live into develop

# Conflicts:
#	common/models/Video.php
<?php
namespace common\config;
class Params {
const YOHOBUY = 1;
const YOHOMARS = 2;
const YOHONOW = 3;
/**
* 直播未开始
*/
const LIVE_NOT_START = 0;
/**
* 直播中
*/
const LIVING = 1;
/**
* 直播已结束
*/
const LIVE_END = 2;
}
return [
'adminEmail' => 'admin@example.com',
'supportEmail' => 'support@example.com',
... ... @@ -7,9 +26,9 @@ return [
'config'=>[
//所属应用
'app'=>[
1=>'有货',
2=>'火星',
3=>'媒体'
Params::YOHOBUY=>'有货',
Params::YOHOMARS=>'火星',
Params::YOHONOW=>'媒体'
],
//是否精选
'best'=>[
... ... @@ -26,9 +45,9 @@ return [
],
//直播状态
'room_living'=>[
0=>'未开始',
1=>'直播中',
2=>'已结束',
Params::LIVE_NOT_START=>'未开始',
Params::LIVING=>'直播中',
Params::LIVE_END=>'已结束',
],
//未被禁言状态
'un_forbid_stat'=>0,
... ... @@ -53,6 +72,5 @@ return [
'failed_code'=>8001,
//异常状态
'exception_code'=>500,
//redis prefix
'cache_prefix'=>'Live'
'news_domain'=>'http://newboys.test.yoho.cn/',
];
... ...
<?php
/**
* curl类库
* Created by PhpStorm.
* User: DELL
* Date: 2016/6/16
* Time: 10:20
*/
namespace common\lib;
class Curl
{
/**
* GET方式网络请求
* @param $url
* @param array $data
* @param int $timeout
* @return mixed
*/
public static function get($url, array $data = array(), $timeout = 20)
{
$ch = curl_init(self::makeUrl($url, $data));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
/**
* post提交数据
* @param $url
* @param $data
* @param int $timeout
* @param array $header
* @param array $cookie
* @return mixed
*/
public static function post($url, $data, $timeout = 20, array $header = array(), array $cookie = array())
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);// array('Content-Type:application/json;charset=UTF-8'));
}
if (!empty($cookie)) {
$cookie_str = array();
foreach ($cookie as $key => $val) {
$cookie_str[] = urlencode($key) . '=' . urlencode($val);
}
curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookie_str));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$result = curl_exec($ch);
// $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $result;
}
public static function makeUrl($url, array $data)
{
$params = '';
if (!empty($data)) {
$params = http_build_query($data, '', '&');
}
if (strpos($url, '?') === false) {
$url = $url . '?' . $params;
} else {
if (!empty($params)) {
$url = $url . '&' . $params;
}
}
return $url;
}
}
\ No newline at end of file
... ...
<?php
/**
* Created by PhpStorm.
* User: DELL
* Date: 2016/8/26
* Time: 18:21
*/
namespace common\lib\YohonowApi;
use common\lib\Curl;
class Request
{
private static $success = 400000;
private static $failed = 200000;
private static $self;
public $errors;
const SET_LIVE_START = "yohoboyins/v5/qcloud/startLive";//?room_id=1001&living_stat=0
/**
* 实例化自身 单例
*/
public static function getInstance()
{
if (self::$self instanceof self === false) {
self::$self = new self();
}
return self::$self;
}
/**
* 设置开始结束状态
* @param $room_id 房间号
* @param $living_stat 直播状态 1直播,0 关闭
* @return bool
*/
public function setLiveStat($room_id, $living_stat)
{
$params = [
'room_id' => $room_id,
'living_stat' => $living_stat
];
return $this->_send(SET_LIVE_START, $params);
}
private function makeUrl($uri, array $data)
{
$domain = \Yii::$app->params['news_domain'];
$packageList = array();
foreach ($data as $key => $val) {
$packageList[] = trim($key . '=' . $val);
}
$url = sprintf("%s%s?", $domain, $uri, implode('&', $packageList));
return $url;
}
private function _send($uri, array $data)
{
$response = Curl::get($this->makeUrl($uri, $data));
if ($response['code'] == self::$success) {
return true;
} else {
$this->errors = $response['message'];
return false;
}
}
}
\ No newline at end of file
... ...
... ... @@ -5,6 +5,7 @@ namespace common\models;
use Yii;
use common\lib\QcloudApi\Client as QcloudApiClient;
use common\lib\QcloudApi\QcloudApi as Qapi;
use common\lib\YohonowApi\Request as YHNowApiReq;
/**
* This is the model class for table "{{%video}}".
... ... @@ -114,11 +115,11 @@ class Video extends \yii\db\ActiveRecord
{
$transaction = $this->getDb()->beginTransaction();
try {
$qchannel = (new \yii\db\Query())
/*$qchannel = (new \yii\db\Query())
->select('channel_id')
->from('tbl_room_qchannel')
->where(['room_id' => $room_id])
->one();
->one();*/
$room = (new \yii\db\Query())
->select('app,title,master_id')
->from('tbl_room')
... ... @@ -146,8 +147,20 @@ class Video extends \yii\db\ActiveRecord
$result = Yii::$app->db->createCommand()
->update('{{%room}}', ['living' => 1,'update_time'=>time()], ['room_id' => $room_id])
->execute();
$cache_prefix = Yii::$app->params['cache_prefix'];
if (!$result) {
throw new \Exception('tbl_room update error');
}
//该房间如果包含资讯业务线,那么要更新资讯那边状态
if(in_array(\common\config\Params::YOHONOW,explode(',',$room['app']))){
$news_ret = YHNowApiReq::getInstance()->setLiveStat($room_id,1);
if(!$news_ret){
throw new \Exception(YHNowApiReq::getInstance()->errors);
}
}
/*------------清除弹幕相关redis--------------*/
$cache_prefix = Yii::$app->params['cache_prefix'];
//##主播app 向弹幕服务器发送开始命令时也清除,这里也清除是为了防止弹幕服务器连不上没有清掉
//清掉在线马甲数
Yii::$app->redisIm->del(sprintf($cache_prefix . ":vestnum_%s", $room_id));
... ... @@ -158,9 +171,6 @@ class Video extends \yii\db\ActiveRecord
//清直播状态
Yii::$app->redisIm->del(sprintf($cache_prefix . ":play_stat_room_%s", $room_id));
/*------------清除弹幕相关redis--------------*/
if (!$result) {
throw new \Exception('tbl_room update error');
}
$transaction->commit();
return true;
} else {
... ... @@ -181,11 +191,6 @@ class Video extends \yii\db\ActiveRecord
->from('tbl_room_qchannel')
->where(['room_id' => $room_id])
->one();*/
$room = (new \yii\db\Query())
->select('starting_time')
->from('tbl_room')
->where(['room_id' => $room_id])
->one();
$video = $this->find()
->where(['room_id' => $room_id, 'live_end_time' => 0])
->orderBy(['live_start_time' => SORT_DESC])
... ... @@ -194,6 +199,11 @@ class Video extends \yii\db\ActiveRecord
//此处有bug,channel_id必须要转成字符串,不然底层会转换成科学计数法的格式
//$ret = QcloudApiClient::self()->StopRecord(strval($qchannel['channel_id']), $video['task_id']);
//file_put_contents("/tmp/live.log",date('Y-m-d H:i:s')."|stop ".var_export($ret,true),FILE_APPEND)."\n";
$room = (new \yii\db\Query())
->select('app,title,master_id')
->from('tbl_room')
->where(['room_id' => $room_id])
->one();
if (1) {//$ret['code'] == 0
$video->setAttributes([
'live_end_time' => time(),
... ... @@ -210,6 +220,13 @@ class Video extends \yii\db\ActiveRecord
if (!$result) {
throw new \Exception('tbl_room update error');
}
//该房间如果包含资讯业务线,那么要更新资讯那边状态
if(in_array(\common\config\Params::YOHONOW,explode(',',$room['app']))){
$news_ret = YHNowApiReq::getInstance()->setLiveStat($room_id,0);
if(!$news_ret){
throw new \Exception(YHNowApiReq::getInstance()->errors);
}
}
$transaction->commit();
return true;
} else {
... ...