文件
mashangyoujiu/Application/SmallProgramApi/Controller/ProgramLiveController.class.php

104 行
3.5 KiB
PHP
可执行文件

<?php
namespace SmallProgramApi\Controller;
use Think\Controller;
//[1.4.96]
class ProgramLiveController extends BaseController
{
// 获取直播间列表
public function getLiveRoomList()
{
$liveModel = M('mini_program_list');
$merchant_id = self::$merchant_id;
$where['merchant_id'] = $merchant_id;
$live_room = $liveModel->field('updated_at, live_json')->where($where)->find();
if ($live_room) {
if (time() > $live_room['updated_at'] + 60) { // 2020-05-08 1分钟左右更新一次
$this->ajaxReturn($this->requestProgramApi(1));
}
$this->ajaxReturn(array(
'status' => 1,
'msg' => '获取成功1',
'data' => json_decode($live_room['live_json'], true),
));
} else {
$this->ajaxReturn($this->requestProgramApi());
}
}
// 获取直播间--单个
public function getLiveRoomFind()
{
$liveModel = M('mini_program_list');
$merchant_id = self::$merchant_id;
$where['merchant_id'] = $merchant_id;
$live_room = $liveModel->field('updated_at, live_json')->where($where)->find();
if ($live_room) {
if (time() > $live_room['updated_at'] + 60) { // 2020-05-08 1分钟左右更新一次
$result = $this->requestProgramApi(1);
$result['data'] = $result['data'][0];
$this->ajaxReturn($result);
}
$this->ajaxReturn(array(
'status' => 1,
'msg' => '获取成功1',
'data' => json_decode($live_room['live_json'], true)[0],
));
} else {
$this->ajaxReturn($this->requestProgramApi());
$this->requestProgramApi();
}
}
// 直播间列表数据--时间更新频次限制
protected function requestProgramApi($type)
{
$liveModel = M('mini_program_list');
$merchant_id = self::$merchant_id;
$access_token = get_authorizer_access_tokens($merchant_id);//获取token
if (empty($access_token)) {
$result['status'] = 0;
$result['msg'] = '未获取到有效token';
$this->ajaxReturn($result);
}
$url = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token={$access_token}";
$data = array(
'start' => 0,
'limit' => 30,
);
$res = http_request($url, json_encode($data));//获取用户openid
$res = json_decode($res, true);
if (isset($res['errcode']) && $res['errcode'] != 0) {
$this->ajaxReturn(array(
'status' => 0,
'msg' => '授权信息有误,请稍后重试!'
));
}
if ($res && isset($res['errcode']) && $res['errcode'] == 0) {
if ($type == 1) { // 更新数据
$data['updated_at'] = time();
$data['live_json'] = json_encode($res['room_info']);
$liveModel->where(['merchant_id' => $merchant_id])->save($data);
} else {
$data['merchant_id'] = $merchant_id;
$data['updated_at'] = time();
$data['live_json'] = json_encode($res['room_info']);
$liveModel->data($data)->add();
}
}
return array(
'status' => 1,
'msg' => '获取成功',
'data' => $res['room_info'],
);
}
}