Authored by wuxiao

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

# Conflicts:
#	common/models/Video.php
1 <?php 1 <?php
  2 +namespace common\config;
  3 +class Params {
  4 + const YOHOBUY = 1;
  5 + const YOHOMARS = 2;
  6 + const YOHONOW = 3;
  7 +
  8 + /**
  9 + * 直播未开始
  10 + */
  11 + const LIVE_NOT_START = 0;
  12 + /**
  13 + * 直播中
  14 + */
  15 + const LIVING = 1;
  16 + /**
  17 + * 直播已结束
  18 + */
  19 + const LIVE_END = 2;
  20 +}
2 return [ 21 return [
3 'adminEmail' => 'admin@example.com', 22 'adminEmail' => 'admin@example.com',
4 'supportEmail' => 'support@example.com', 23 'supportEmail' => 'support@example.com',
@@ -7,9 +26,9 @@ return [ @@ -7,9 +26,9 @@ return [
7 'config'=>[ 26 'config'=>[
8 //所属应用 27 //所属应用
9 'app'=>[ 28 'app'=>[
10 - 1=>'有货',  
11 - 2=>'火星',  
12 - 3=>'媒体' 29 + Params::YOHOBUY=>'有货',
  30 + Params::YOHOMARS=>'火星',
  31 + Params::YOHONOW=>'媒体'
13 ], 32 ],
14 //是否精选 33 //是否精选
15 'best'=>[ 34 'best'=>[
@@ -26,9 +45,9 @@ return [ @@ -26,9 +45,9 @@ return [
26 ], 45 ],
27 //直播状态 46 //直播状态
28 'room_living'=>[ 47 'room_living'=>[
29 - 0=>'未开始',  
30 - 1=>'直播中',  
31 - 2=>'已结束', 48 + Params::LIVE_NOT_START=>'未开始',
  49 + Params::LIVING=>'直播中',
  50 + Params::LIVE_END=>'已结束',
32 ], 51 ],
33 //未被禁言状态 52 //未被禁言状态
34 'un_forbid_stat'=>0, 53 'un_forbid_stat'=>0,
@@ -53,6 +72,5 @@ return [ @@ -53,6 +72,5 @@ return [
53 'failed_code'=>8001, 72 'failed_code'=>8001,
54 //异常状态 73 //异常状态
55 'exception_code'=>500, 74 'exception_code'=>500,
56 - //redis prefix  
57 - 'cache_prefix'=>'Live' 75 + 'news_domain'=>'http://newboys.test.yoho.cn/',
58 ]; 76 ];
  1 +<?php
  2 +/**
  3 + * curl类库
  4 + * Created by PhpStorm.
  5 + * User: DELL
  6 + * Date: 2016/6/16
  7 + * Time: 10:20
  8 + */
  9 +namespace common\lib;
  10 +
  11 +class Curl
  12 +{
  13 + /**
  14 + * GET方式网络请求
  15 + * @param $url
  16 + * @param array $data
  17 + * @param int $timeout
  18 + * @return mixed
  19 + */
  20 + public static function get($url, array $data = array(), $timeout = 20)
  21 + {
  22 + $ch = curl_init(self::makeUrl($url, $data));
  23 + curl_setopt($ch, CURLOPT_HEADER, 0);
  24 + curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  25 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  26 + $result = curl_exec($ch);
  27 + curl_close($ch);
  28 + return $result;
  29 + }
  30 +
  31 + /**
  32 + * post提交数据
  33 + * @param $url
  34 + * @param $data
  35 + * @param int $timeout
  36 + * @param array $header
  37 + * @param array $cookie
  38 + * @return mixed
  39 + */
  40 + public static function post($url, $data, $timeout = 20, array $header = array(), array $cookie = array())
  41 + {
  42 + $ch = curl_init($url);
  43 + curl_setopt($ch, CURLOPT_HEADER, 0);
  44 + curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  45 + if (!empty($header)) {
  46 + curl_setopt($ch, CURLOPT_HTTPHEADER, $header);// array('Content-Type:application/json;charset=UTF-8'));
  47 + }
  48 +
  49 + if (!empty($cookie)) {
  50 + $cookie_str = array();
  51 + foreach ($cookie as $key => $val) {
  52 + $cookie_str[] = urlencode($key) . '=' . urlencode($val);
  53 + }
  54 + curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookie_str));
  55 + }
  56 +
  57 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  58 + curl_setopt($ch, CURLOPT_POST, true);
  59 + if (!empty($data)) {
  60 + curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  61 + }
  62 + $result = curl_exec($ch);
  63 +// $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  64 + curl_close($ch);
  65 + return $result;
  66 + }
  67 +
  68 + public static function makeUrl($url, array $data)
  69 + {
  70 + $params = '';
  71 + if (!empty($data)) {
  72 + $params = http_build_query($data, '', '&');
  73 + }
  74 + if (strpos($url, '?') === false) {
  75 + $url = $url . '?' . $params;
  76 + } else {
  77 + if (!empty($params)) {
  78 + $url = $url . '&' . $params;
  79 + }
  80 + }
  81 + return $url;
  82 + }
  83 +}
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: DELL
  5 + * Date: 2016/8/26
  6 + * Time: 18:21
  7 + */
  8 +namespace common\lib\YohonowApi;
  9 +
  10 +use common\lib\Curl;
  11 +
  12 +class Request
  13 +{
  14 +
  15 + private static $success = 400000;
  16 + private static $failed = 200000;
  17 + private static $self;
  18 + public $errors;
  19 +
  20 + const SET_LIVE_START = "yohoboyins/v5/qcloud/startLive";//?room_id=1001&living_stat=0
  21 +
  22 + /**
  23 + * 实例化自身 单例
  24 + */
  25 + public static function getInstance()
  26 + {
  27 + if (self::$self instanceof self === false) {
  28 + self::$self = new self();
  29 + }
  30 + return self::$self;
  31 + }
  32 +
  33 + /**
  34 + * 设置开始结束状态
  35 + * @param $room_id 房间号
  36 + * @param $living_stat 直播状态 1直播,0 关闭
  37 + * @return bool
  38 + */
  39 + public function setLiveStat($room_id, $living_stat)
  40 + {
  41 + $params = [
  42 + 'room_id' => $room_id,
  43 + 'living_stat' => $living_stat
  44 + ];
  45 + return $this->_send(SET_LIVE_START, $params);
  46 + }
  47 +
  48 + private function makeUrl($uri, array $data)
  49 + {
  50 + $domain = \Yii::$app->params['news_domain'];
  51 + $packageList = array();
  52 + foreach ($data as $key => $val) {
  53 + $packageList[] = trim($key . '=' . $val);
  54 + }
  55 + $url = sprintf("%s%s?", $domain, $uri, implode('&', $packageList));
  56 + return $url;
  57 + }
  58 +
  59 + private function _send($uri, array $data)
  60 + {
  61 + $response = Curl::get($this->makeUrl($uri, $data));
  62 + if ($response['code'] == self::$success) {
  63 + return true;
  64 + } else {
  65 + $this->errors = $response['message'];
  66 + return false;
  67 + }
  68 + }
  69 +}
@@ -5,6 +5,7 @@ namespace common\models; @@ -5,6 +5,7 @@ namespace common\models;
5 use Yii; 5 use Yii;
6 use common\lib\QcloudApi\Client as QcloudApiClient; 6 use common\lib\QcloudApi\Client as QcloudApiClient;
7 use common\lib\QcloudApi\QcloudApi as Qapi; 7 use common\lib\QcloudApi\QcloudApi as Qapi;
  8 +use common\lib\YohonowApi\Request as YHNowApiReq;
8 9
9 /** 10 /**
10 * This is the model class for table "{{%video}}". 11 * This is the model class for table "{{%video}}".
@@ -114,11 +115,11 @@ class Video extends \yii\db\ActiveRecord @@ -114,11 +115,11 @@ class Video extends \yii\db\ActiveRecord
114 { 115 {
115 $transaction = $this->getDb()->beginTransaction(); 116 $transaction = $this->getDb()->beginTransaction();
116 try { 117 try {
117 - $qchannel = (new \yii\db\Query()) 118 + /*$qchannel = (new \yii\db\Query())
118 ->select('channel_id') 119 ->select('channel_id')
119 ->from('tbl_room_qchannel') 120 ->from('tbl_room_qchannel')
120 ->where(['room_id' => $room_id]) 121 ->where(['room_id' => $room_id])
121 - ->one(); 122 + ->one();*/
122 $room = (new \yii\db\Query()) 123 $room = (new \yii\db\Query())
123 ->select('app,title,master_id') 124 ->select('app,title,master_id')
124 ->from('tbl_room') 125 ->from('tbl_room')
@@ -146,8 +147,20 @@ class Video extends \yii\db\ActiveRecord @@ -146,8 +147,20 @@ class Video extends \yii\db\ActiveRecord
146 $result = Yii::$app->db->createCommand() 147 $result = Yii::$app->db->createCommand()
147 ->update('{{%room}}', ['living' => 1,'update_time'=>time()], ['room_id' => $room_id]) 148 ->update('{{%room}}', ['living' => 1,'update_time'=>time()], ['room_id' => $room_id])
148 ->execute(); 149 ->execute();
149 - $cache_prefix = Yii::$app->params['cache_prefix']; 150 + if (!$result) {
  151 + throw new \Exception('tbl_room update error');
  152 + }
  153 +
  154 + //该房间如果包含资讯业务线,那么要更新资讯那边状态
  155 + if(in_array(\common\config\Params::YOHONOW,explode(',',$room['app']))){
  156 + $news_ret = YHNowApiReq::getInstance()->setLiveStat($room_id,1);
  157 + if(!$news_ret){
  158 + throw new \Exception(YHNowApiReq::getInstance()->errors);
  159 + }
  160 + }
  161 +
150 /*------------清除弹幕相关redis--------------*/ 162 /*------------清除弹幕相关redis--------------*/
  163 + $cache_prefix = Yii::$app->params['cache_prefix'];
151 //##主播app 向弹幕服务器发送开始命令时也清除,这里也清除是为了防止弹幕服务器连不上没有清掉 164 //##主播app 向弹幕服务器发送开始命令时也清除,这里也清除是为了防止弹幕服务器连不上没有清掉
152 //清掉在线马甲数 165 //清掉在线马甲数
153 Yii::$app->redisIm->del(sprintf($cache_prefix . ":vestnum_%s", $room_id)); 166 Yii::$app->redisIm->del(sprintf($cache_prefix . ":vestnum_%s", $room_id));
@@ -158,9 +171,6 @@ class Video extends \yii\db\ActiveRecord @@ -158,9 +171,6 @@ class Video extends \yii\db\ActiveRecord
158 //清直播状态 171 //清直播状态
159 Yii::$app->redisIm->del(sprintf($cache_prefix . ":play_stat_room_%s", $room_id)); 172 Yii::$app->redisIm->del(sprintf($cache_prefix . ":play_stat_room_%s", $room_id));
160 /*------------清除弹幕相关redis--------------*/ 173 /*------------清除弹幕相关redis--------------*/
161 - if (!$result) {  
162 - throw new \Exception('tbl_room update error');  
163 - }  
164 $transaction->commit(); 174 $transaction->commit();
165 return true; 175 return true;
166 } else { 176 } else {
@@ -181,11 +191,6 @@ class Video extends \yii\db\ActiveRecord @@ -181,11 +191,6 @@ class Video extends \yii\db\ActiveRecord
181 ->from('tbl_room_qchannel') 191 ->from('tbl_room_qchannel')
182 ->where(['room_id' => $room_id]) 192 ->where(['room_id' => $room_id])
183 ->one();*/ 193 ->one();*/
184 - $room = (new \yii\db\Query())  
185 - ->select('starting_time')  
186 - ->from('tbl_room')  
187 - ->where(['room_id' => $room_id])  
188 - ->one();  
189 $video = $this->find() 194 $video = $this->find()
190 ->where(['room_id' => $room_id, 'live_end_time' => 0]) 195 ->where(['room_id' => $room_id, 'live_end_time' => 0])
191 ->orderBy(['live_start_time' => SORT_DESC]) 196 ->orderBy(['live_start_time' => SORT_DESC])
@@ -194,6 +199,11 @@ class Video extends \yii\db\ActiveRecord @@ -194,6 +199,11 @@ class Video extends \yii\db\ActiveRecord
194 //此处有bug,channel_id必须要转成字符串,不然底层会转换成科学计数法的格式 199 //此处有bug,channel_id必须要转成字符串,不然底层会转换成科学计数法的格式
195 //$ret = QcloudApiClient::self()->StopRecord(strval($qchannel['channel_id']), $video['task_id']); 200 //$ret = QcloudApiClient::self()->StopRecord(strval($qchannel['channel_id']), $video['task_id']);
196 //file_put_contents("/tmp/live.log",date('Y-m-d H:i:s')."|stop ".var_export($ret,true),FILE_APPEND)."\n"; 201 //file_put_contents("/tmp/live.log",date('Y-m-d H:i:s')."|stop ".var_export($ret,true),FILE_APPEND)."\n";
  202 + $room = (new \yii\db\Query())
  203 + ->select('app,title,master_id')
  204 + ->from('tbl_room')
  205 + ->where(['room_id' => $room_id])
  206 + ->one();
197 if (1) {//$ret['code'] == 0 207 if (1) {//$ret['code'] == 0
198 $video->setAttributes([ 208 $video->setAttributes([
199 'live_end_time' => time(), 209 'live_end_time' => time(),
@@ -210,6 +220,13 @@ class Video extends \yii\db\ActiveRecord @@ -210,6 +220,13 @@ class Video extends \yii\db\ActiveRecord
210 if (!$result) { 220 if (!$result) {
211 throw new \Exception('tbl_room update error'); 221 throw new \Exception('tbl_room update error');
212 } 222 }
  223 + //该房间如果包含资讯业务线,那么要更新资讯那边状态
  224 + if(in_array(\common\config\Params::YOHONOW,explode(',',$room['app']))){
  225 + $news_ret = YHNowApiReq::getInstance()->setLiveStat($room_id,0);
  226 + if(!$news_ret){
  227 + throw new \Exception(YHNowApiReq::getInstance()->errors);
  228 + }
  229 + }
213 $transaction->commit(); 230 $transaction->commit();
214 return true; 231 return true;
215 } else { 232 } else {