Files
liche/api/controllers/plan/Syt.php
T
2022-09-30 10:50:05 +08:00

132 lines
5.8 KiB
PHP

<?php
/**
* Notes:私域通
* Created on: 2020/11/30 17:00
* Created by: dengbw
*/
defined('BASEPATH') OR exit('No direct script access allowed');
require_once(COMMPATH . "/third_party/WeChat/WechatServer.php");
class Syt extends CI_Controller
{
private $log_file = 'plan_syt.log';
private $log_dir = 'market/sylive';
private $options;
public function __construct()
{
parent::__construct();
$this-> options = [
'token' => '',
'appid' => 'wxe66f905683582780',
'appsecret' => '9546cd4de877be13ce203f3e6140633f',
'encodingaeskey' => '',
'debug' => false
];
$this->load->model('market/market_sylive_activity_model');
$this->load->model('live/Live_polyv_session_model', 'mdPolyvSession');
$this->load->library('PolyvApi');
}
/**
* Notes:发送一次性订阅
* Created on: 2020/11/30 17:02
* Created by: dengbw
* https://hd-api-dev.xiaoyu.com/plan/syt/subscribemsg
* https://api.test.haodian.cn/plan/syt/subscribemsg
* https://api.haodian.cn/plan/syt/subscribemsg
*/
public function subscribemsg()
{
$this->load->model('market/market_sylive_subscribemsg_model','mdSytSubscribemsg');
$date = date('Y-m-d H:i:s', strtotime('+2 minute'));//提前2分钟发送
$where['s_time<='] = $date;
$where['status'] = 0;
$res = $this->mdSytSubscribemsg->select($where, 'id asc', 1, 200);
$log = array();
if ($res) {
$this->load->model('market/market_sylive_activity_model','mdSytActivity');
$wechatS = new WechatServer($this->options);
$wechatS->checkAuth();
foreach ($res as $key => $value) {
$re_a = $this->mdSytActivity->get(array("activityId" => $value['a_id'], "status" => 0));
if ($re_a) {
$content = '您预约“' . $re_a['title'] . '”直播已经开始啦!赶紧进入直播间观看直播领取福利吧~';
$temp_arr = array(
'touser' => $value['openid'],
'template_id' => $value['template_id'],
'url' => $value['url'],
'scene' => $value['scene'],
'title' => '开播提醒',
'data' => array(
'content' => array('value' => $content)
)
);
$re = $wechatS->subscribeTemplateMessage($temp_arr);
if ($re) {//errcode 0(发送成功) 43101(已发送过)
$this->mdSytSubscribemsg->update(['status' => 1], ['id' => $value['id']]);
if ($re['errcode'] == 0) {
$log[] = $value['a_id'] . '=' . $value['openid'];
}
}
}
}
}
echo '<br>数据库获取:';
echo json_encode($res, JSON_UNESCAPED_UNICODE);
echo '<br><br>成功发送:';
$log && print_r($log);
$total = count($res);
$do = count($log);
debug_log("[info] " . __FUNCTION__ . "# date:{$date}; total:{$total}; do:{$do};", $this->log_file);
}
public function syn_polyv(){
$a_id = $this->input->get('a_id');
$where = [
'status' => 0,
"channelId <> ''" => null,
"timeStart<" => date('Y-m-d H:i:s'),
"timeStart>" => date('Y-m-d H:i:s',strtotime("-1 day"))
];
$a_id && $where = ['activityId'=>$a_id];
$rows = $this->market_sylive_activity_model->select($where,'activityId desc',1,20,'activityId,channelId,timeStart,timeEnd');
if($rows){
foreach ($rows as $item) {
debug_log("开始同步{$item['activityId']}活动直播",$this->log_file,$this->log_dir);
$polyv = new PolyvApi(['channel_id' => $item['channelId']]);
$pl_where = [
'startTime' => strtotime($item['timeStart']),
'endTime' => strtotime($item['timeEnd'])
];
$results = $polyv->getSessionStats($pl_where);
if ($results['status'] == 'success') {
foreach ($results['data']['list'] as $key => $value) {
$re = $this->mdPolyvSession->get(['channelId' => $item['channelId'], 'sessionId' => $value['sessionId']]);
$data = [
'channelId' => $value['channelId'], 'sessionId' => $value['sessionId'], 'name' => $value['name'],
'startTime' => $value['startTime'], 'endTime' => $value['endTime'], 'duration' => $value['duration'],
'liveUV' => $value['liveUV'], 'livePV' => $value['livePV'], 'playbackUV' => $value['playbackUV'],
'playbackPV' => $value['playbackPV'], 'totalPlayDuration' => $value['totalPlayDuration'],
'session_time' => date('Y-m-d H:i:s'),
];
if ($re) {//同步更新数据
$this->mdPolyvSession->update($data, ['id' => $re['id']]);
} else {//新增
$data['c_time'] = time();
$this->mdPolyvSession->add($data);
}
}
$msg = "同步成功:".json_encode($results,JSON_UNESCAPED_UNICODE);
echo $msg."<br>";
} else {
$msg = "接口错误:".$results['message'];
echo $msg."<br>";
}
debug_log($msg,$this->log_file,$this->log_dir);
}
}else{
echo "暂无同步数据";
}
}
}