LivevideoModel.class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace Home\Model;
  3. use Think\Model;
  4. /**
  5. * 直播模型模型
  6. * @author fish
  7. *
  8. */
  9. class LivevideoModel {
  10. function syncRoomList()
  11. {
  12. $accessToken = $this->getAccessToken();
  13. if (empty($accessToken)) {
  14. return array('errcode'=>40001, 'accessToken为空');
  15. }
  16. $page = 1;
  17. $start = 0;
  18. $pageSize = 30;
  19. $param = array(
  20. "start" => $start,
  21. "limit" => $pageSize
  22. );
  23. $roomIds = array();
  24. $url = 'https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=' . $accessToken;
  25. $model = M('lionfish_comshop_wxlive');
  26. S('_inc_live_expirtime_', time());
  27. while (true) {
  28. $response = $this->_post($url, $param);
  29. $result = json_decode($response, true);
  30. $roomReqNum = S('_inc_live_roominfo_reqnum_');
  31. $num = intval($roomReqNum) + 1;
  32. S('_inc_live_roominfo_reqnum_', $num);
  33. if ($result['errcode'] != 0) {
  34. if ($result['errcode'] == 1) {
  35. return array('errcode'=>$result['errcode'], 'msg'=>'直播间列表为空');
  36. }
  37. if ($result['errcode'] == 48001) {
  38. return array('errcode'=>$result['errcode'], 'msg'=>'小程序没有直播权限');
  39. }
  40. return array('errcode'=>$result['errcode'], 'msg'=>$result['errmsg']);
  41. }
  42. foreach ($result['room_info'] as $room) {
  43. $roomId = (int) $room['roomid'];
  44. $roomIds[] = $roomId;
  45. $wxlive = $model->where(array('roomid'=>$roomId))->find();
  46. $updateData = array('name' => (string) $room['name'], 'cover_img' => (string) $room['cover_img'], 'live_status' => (int) $room['live_status'], 'start_time' => (int) $room['start_time'], 'end_time' => (int) $room['end_time'], 'anchor_name' => (string) $room['anchor_name'], 'anchor_img' => (string) $room['anchor_img'], 'share_img' => (string) $room['share_img'], 'goods' => json_encode($room['goods']));
  47. if (empty($wxlive)) {
  48. $insertData = array_merge($updateData, array('roomid' => $roomId));
  49. $model->add($insertData);
  50. // if($room['live_status'] == '103') {
  51. // $this->syncLiveReplay($room['roomid']);
  52. // }
  53. // continue;
  54. }
  55. // $live_replay_lv = unserialize($wxlive['live_replay']);
  56. // if(!empty($wxlive) && empty($live_replay_lv) && $room['live_status']=='103') {
  57. // $this->syncLiveReplay($room['roomid']);
  58. // }
  59. $model->where( array('roomid' => $room['roomid'] ) )->save($updateData);
  60. }
  61. if ($result['total'] < $pageSize*$page) {
  62. break;
  63. }
  64. $page++;
  65. unset($room);
  66. }
  67. unset($result);
  68. $result = $model->where('roomid not in ( ' . implode(',', $roomIds) . ')' )->delete();
  69. }
  70. function syncLiveReplay($room_id)
  71. {
  72. $accessToken = $this->getAccessToken();
  73. if(!$accessToken) {
  74. return '';
  75. die();
  76. }
  77. $url = 'https://api.weixin.qq.com/wxa/business/getliveinfo?access_token='.$accessToken;
  78. $param = array(
  79. "action" => "get_replay",
  80. "room_id" => $room_id,
  81. "start" => 0,
  82. "limit" => 1
  83. );
  84. $res = $this->_post($url, $param);
  85. $res = json_decode($res);
  86. $replayReqNum = S('_inc_live_replay_reqnum_');
  87. $num = intval($replayReqNum) + 1;
  88. S('_inc_live_replay_reqnum_', $num);
  89. if($res->errcode == 0) {
  90. $live_replay = $res->live_replay;
  91. $updateData = array('live_replay'=>serialize($live_replay));
  92. M('lionfish_comshop_wxlive')->where( array('roomid' => $room_id ) )->save($updateData);
  93. return $live_replay;
  94. } else {
  95. // 代表未创建直播房间
  96. return '';
  97. }
  98. }
  99. function getRoomInfo($roomid)
  100. {
  101. $model = M('lionfish_comshop_wxlive');
  102. $res = $model->where( array('roomid' => $roomid ) )->find();
  103. return $res;
  104. }
  105. /**
  106. * 新版同步回放1.0.5
  107. * @param [type] $room_id [description]
  108. * @return [type] [description]
  109. */
  110. function syncLiveReplayNew($room_id, $begin = 0, $end = 1)
  111. {
  112. $accessToken = $this->getAccessToken();
  113. if(!$accessToken) {
  114. return '';
  115. die();
  116. }
  117. $url = 'https://api.weixin.qq.com/wxa/business/getliveinfo?access_token='.$accessToken;
  118. $param = array(
  119. "action" => "get_replay",
  120. "room_id" => $room_id,
  121. "start" => $begin,
  122. "limit" => $end
  123. );
  124. $res = $this->_post($url, $param);
  125. return json_decode($res, true);
  126. }
  127. /**
  128. * 获取accessToken
  129. * @return [String] [accessToken]
  130. */
  131. private function getAccessToken()
  132. {
  133. $weixin_config = array();
  134. $weixin_config['appid'] = D('Home/Front')->get_config_by_name('wepro_appid');
  135. $weixin_config['appscert'] = D('Home/Front')->get_config_by_name('wepro_appsecret');
  136. $jssdk = new \Lib\Weixin\Jssdk( $weixin_config['appid'], $weixin_config['appscert']);
  137. return $jssdk->getweAccessToken();
  138. }
  139. private function _post($url, $data=array()) {
  140. //初使化init方法
  141. $ch = curl_init();
  142. //指定URL
  143. curl_setopt($ch, CURLOPT_URL, $url);
  144. //设定请求后返回结果
  145. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  146. //声明使用POST方式来进行发送
  147. curl_setopt($ch, CURLOPT_POST, 1);
  148. //发送什么数据呢
  149. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  150. //忽略证书
  151. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  152. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  153. //忽略header头信息
  154. curl_setopt($ch, CURLOPT_HEADER, 0);
  155. //设置超时时间
  156. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  157. //发送请求
  158. $output = curl_exec($ch);
  159. //关闭curl
  160. curl_close($ch);
  161. //返回数据
  162. return $output;
  163. }
  164. }