Files
liche/api/controllers/plan/Syt.php
T
2022-10-25 16:21:12 +08:00

315 lines
14 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' => 'wxbdec8558931efffd',
'appsecret' => '583b3cd0d8e145009eda61ece003ca14',
'encodingaeskey' => '',
'debug' => false
];
$this->load->model('market/market_sylive_activity_model');
$this->load->model('live/Live_polyv_session_model', 'mdPolyvSession');
$this->load->model('live/Live_polyv_viewlog_model', 'mdPolyvViewlog');
$this->load->library('PolyvApi');
}
/**
* Notes:发送一次性订阅
* Created on: 2020/11/30 17:02
* Created by: dengbw
* https://liche-api-dev.xiaoyu.com/plan/syt/subscribemsg
* https://api.liche.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('+10 minute'));//提前10分钟发送
$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);
}
/**
* Notes:私域通直播间数据同步
* https://liche-api-dev.xiaoyu.com/plan/syt/syn_polyv
* https://api.liche.cn/plan/syt/syn_polyv
* Created on: 2022/9/30 15:05
* Created by: dengbw
*/
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']) * 1000,
'endTime' => strtotime($item['timeEnd']) * 1000
];
$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'),
];
$session = ['channelId' => $item['channelId'], 'sessionId' => $value['sessionId']];
if ($re) {//同步更新数据
$session['id'] = $re['id'];
$session['viewlog_time'] = $re['viewlog_time'];
$this->mdPolyvSession->update($data, ['id' => $re['id']]);
} else {//新增
$data['c_time'] = time();
$session['id'] = $this->mdPolyvSession->add($data);
$session['startTime'] = $value['startTime'];
}
$this->api_viewlog($session);
}
$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 "暂无同步数据";
}
}
/**
* Notes:同步保利威分页查询频道直播观看详情数据
* Created on: 2022/9/30 14:56
* Created by: dengbw
* @param $session
*/
private function api_viewlog($session)
{
if ($session['viewlog_time'] && $session['viewlog_time'] != '0000-00-00 00:00:00') {
$startTime = strtotime($session['viewlog_time']);
} else {
$startTime = $session['startTime'] / 1000;
}
$endTime = strtotime(date('Y-m-d H:i:s'));
//不同月份时开始取本月第1天
if (date('m', $startTime) != date('m', $endTime)) {
$startTime = strtotime(date('Y-m', time()) . '-01 00:00:00');
}
$params['channelId'] = $session['channelId'];
$params['sessionIds'] = $session['sessionId'];
$params['startTime'] = $startTime * 1000;
$params['endTime'] = $endTime * 1000;
$params['page'] = 1;
$results = $this->getViewLog($params);
if ($results['status'] == 'success') {
$i = 10;
while (($i--) > 0) {
if ($results['status'] == 'success' && !$results['lastPage']
&& $results['pageNumber'] != $results['nextPageNumber']) {
$params['page'] = $results['nextPageNumber'];
$results = $this->getViewLog($params);
continue;
} else {
break;
}
}
//同步观看数据时间
$this->mdPolyvSession->update(['viewlog_time' => date('Y-m-d H:i:s', $endTime)], ['id' => $session['id']]);
}
}
/**
* Notes:增加观看日志
* Created on: 2022/9/30 14:56
* Created by: dengbw
* @param $params
* @return bool|string
*/
private function getViewLog($params)
{
$this->load->library('PolyvApi');
$polyv = new PolyvApi(['channel_id' => $params['channelId']]);
$results = $polyv->getViewLog($params);
if ($results['status'] == 'success') {
$addData = [];
foreach ($results['data']['contents'] as $key => $value) {
$re = $this->mdPolyvViewlog->get(['playId' => $value['playId']]);
$data = [
'channelId' => $value['channelId'], 'sessionId' => $value['sessionId'], 'playId' => $value['playId'],
'userId' => $value['userId'], 'playDuration' => $value['playDuration'], 'stayDuration' => $value['stayDuration'],
'flowSize' => $value['flowSize'], 'param1' => $value['param1'], 'param2' => $value['param2'],
'param3' => $value['param3'], 'param4' => $value['param4'], 'param5' => $value['param5'], 'ptype' => $value['ptype'],
'ipAddress' => $value['ipAddress'], 'country' => $value['country'], 'province' => $value['province'],
'city' => $value['city'], 'isp' => $value['isp'], 'referer' => $value['referer'], 'userAgent' => $value['userAgent'],
'operatingSystem' => $value['operatingSystem'], 'browser' => $value['browser'], 'isMobile' => $value['isMobile'],
'currentDay' => $value['currentDay'], 'createdTime' => $value['createdTime'], 'lastModified' => $value['lastModified'],
];
if ($re) {//同步更新数据
$this->mdPolyvViewlog->update($data, ['id' => $re['id']]);
} else {//新增
$data['c_time'] = time();
$addData[] = $data;
}
}
if ($addData && count($addData)) {//新增数据
$this->mdPolyvViewlog->add_batch($addData);
}
}
return $results;
}
//订单过期
public function out_time(){
$this->load->model('market/market_sylive_activity_orders_model','mdSytActivityOrders');
$this->load->model('market/market_sylive_order_model');
//商家订单过期
$where = [
'expireTime>' => 0,
'expireTime<' => time(),
'status' => 0
];
$this->mdSytActivityOrders->update(['status'=>-1],$where);
//用户订单过期
$where = [
'expire_time>' => 0,
'expire_time<' => time(),
'status' => 0
];
$rows = $this->market_sylive_order_model->select($where,'id asc',1,20);
if($rows){
foreach ($rows as $key => $val) {
$res = $this->market_sylive_order_model->update(['status'=>-1],['id'=>$val['id']]);
if($res){
$up_data = [
'stock = stock+1' => null
];
$this->market_sylive_activity_model->update($up_data,['activityId'=>$val['item_id']]);
}
}
}
}
//关联观看日志
public function union_user(){
$this->load->library('market/sylive_entity');
$this->load->model('market/market_sylive_user_model', 'user_model');
$this->load->model('market/market_sylive_activity_user_model', 'act_user_model');
$this->load->model('market/market_sylive_viewlog_model');
$size = 30;
$t1 = 'lc_market_sylive_viewlog';
$t2 = 'lc_live_polyv_viewlog';
$t3 = 'lc_market_sylive_activity';
$where = [
"$t2.id>" => 0,
"$t2.id is null" => null
];
$fileds = "{$t2}.*,{$t3}.activityId";
$rows = $this->db->select($fileds)->from("$t3")
->join("$t2", "{$t2}.channelId={$t3}.activityId", 'left')
->join("$t1", "{$t1}.vlog_id={$t2}.id", 'left')
->where($where)->order_by("{$t2}.id asc")->limit($size, 0)
->get()->result_array();
if (!$rows) {
echo "不存在直播日志数据";
}else{
foreach ($rows as $key => $value) {
$ac_user = $p_user = '';
$addData = [
'vlog_id' => $value['id'],
'a_id' => $value['activityId'],
'playId' => $value['playId'],
'playDuration' => $value['playDuration'],
'stayDuration' => $value['stayDuration'],
'c_time' => time()
];
$user = $this->user_model->get(['unionid'=>$value['param1']]);
$user && $ac_user = $this->act_user_model->get(['activityId'=>$value['activityId'],'userId'=>$user['userId']]);
if($ac_user['channelId']){
$p_user = $this->user_model->get(['userId'=>$ac_user['channelId']]);
}
if($p_user['teamId']){
$addData['type'] = 1;
$team_lists = $this->sylive_entity->get_team_lists($p_user['teamId']);
$area_id = $team_lists[0]['teamId']; //大团长id
$biz_id = $team_lists[1]['teamId']; //团长id
}
if($p_user['organizationId']){
$addData['type'] = 0;
$team_lists = $this->sylive_entity->get_level_lists($p_user['organizationId']);
$area_id = $team_lists[0]['teamId']; //大团长id
$biz_id = $team_lists[1]['teamId']; //团长id
}
$area_id && $addData['area_id'] = $area_id;
$biz_id && $addData['biz_id'] = $biz_id;
$user && $addData['userId'] = $user['userId'];
$this->market_sylive_viewlog_model->add($addData);
}
}
}
}