DeleteController.class.php 6.85 KB
<?php
/**
 * 【后台】删除直播活动接口
 * Created by PhpStorm.
 * User: yingcai
 * Date: 2018/1/11
 * Time: 下午3:21
 */

namespace Apicp\Controller\Room;

use Com\LiveSDK;
use Com\QcloudApi\QcloudApi;
use Common\Common\AttachOperation;
use Common\Common\Constant;
use Common\Model\StudioModel;
use Common\Service\MainService;
use Common\Service\ParticipateService;
use Common\Service\RangeService;
use Common\Service\RoleService;
use Common\Service\CronService;
use Common\Service\MsgLogService;
use Common\Service\StudioFileService;
use Common\Service\StudioService;
use Org\Net\Snoopy;
use VcySDK\Cron;
use VcySDK\FileConvert;
use VcySDK\Service;

class DeleteController extends AbstractController
{
    /**
     * Delete
     * @author houyingcai
     * @desc 删除直播活动接口
     * @param Array lm_ids:true 直播活动ID
     * @return bool
     */
    public function Index_post()
    {
        $lm_ids = I('post.lm_ids');
        if (empty($lm_ids) || !is_array($lm_ids)) {

            E('_ERR_LIVE_ID_PARAM_FORMAT');
        }

        try {

            $mainServ = new MainService();

            $mainServ->start_trans();

            // 删除直播主表数据
            $mainServ->delete($lm_ids);

            $del_conds = ['lm_id' => $lm_ids];

            // 删除直播范围表数据
            $rangeServ = new RangeService();
            $rangeServ->delete_by_conds($del_conds);

            // 删除直播角色表数据
            $roleServ = new RoleService();
            $roleServ->delete_by_conds($del_conds);

            // 删除直播参与表数据
            $participateServ = new ParticipateService();
            $participateServ->delete_by_conds($del_conds);

            // 删除直播消息通知记录表数据
            $msgLogServ = new MsgLogService();
            $msgLogServ->delete_by_conds($del_conds);

            $liveCronServ = new CronService();
            // 获取计划任务
            $liveCrons = $liveCronServ->list_by_conds($del_conds, [], [], 'cron_id, type');

            // 存在计划任务
            if (!empty($liveCrons)) {
                // 删除业务计划任务表数据
                $liveCronServ->delete_by_conds($del_conds);
            }

            // 禁止推流
            $studioServ = new StudioService();
            $studioList = $studioServ->list_by_conds(['lm_id' => $lm_ids]);
            $liveSdk = new LiveSDK();
            foreach ($studioList as $item) {
                $liveSdk->channelSetStatus($item['stream_id'], $liveSdk::CHANNEL_STATUS_DISABLED);
            }
            // 删除聊天室
            $studioServ->update_by_conds(['lm_id' => $lm_ids], ['stream_status' => Constant::STREAM_TYPE_STATUS_DISABLED]);

            $mainServ->commit();
        } catch (\Exception $e) {

            \Think\Log::record('删除错误:' . $e->getMessage());
            $mainServ->rollback();
            E($e->getCode() . ':' . $e->getMessage());
        }

        // 删除首页推荐feed流
        foreach ($lm_ids as $lm_id) {

            $url = rpcUrl('/Public/Rpc/Recommender/ArticleDelete');
            $params = [
                'app' => APP_DIR,
                'dataCategoryId' => '',
                'dataId' => $lm_id,
            ];

            \Com\Rpc::phprpc($url)->invoke('Index', $params);
        }

        // 删除计划任务
        if (!empty($liveCrons)) {

            $ucCronServ = new Cron(Service::instance());

            foreach ($liveCrons as $cron) {
                // 不需要UC删除的计划任务
                if (in_array($cron['type'], Constant::CRON_TYPE_NOT_DELETE)) {

                    continue;
                }
                try {
                    // 根据cron_id获取UC的计划任务
                    $ucCron = $ucCronServ->get($cron['cron_id']);
                    if (!empty($ucCron)) {
                        // 删除计划任务
                        $ucCronServ->delete($cron['cron_id']);
                    }
                } catch (\Exception $e) {
                    // 不处理
                }
            }
        }

        // 删除点播视频资源
        $fileServ = new StudioFileService();
        $fileList = $fileServ->list_by_conds(['lm_id' => $lm_ids], null, [], 'file_id');
        $fileSdk = new FileConvert(Service::instance());

        $fileIds = array_unique(array_column($fileList, 'file_id'));
        foreach ($fileIds as $fileId) {
            // 提交删除
            $vodUtilInstance = (QcloudApi::load('Vod', [
                'SecretId' => cfg('TENCENT_VOD_SECREAT_ID'),
                'SecretKey' => cfg('TENCENT_VOD_SECREAT_KEY'),
                'DefaultRegion' => 'sh',
            ]));
            $vodDelUrl = $vodUtilInstance->generateUrl('DeleteVodFile', [
                    'fileId' => $fileId,
                    // 可填0;优先级0:中 1:高 2:低
                    'priority' => 0,
                ]);
            $this->request($vodDelUrl);

            // 删除 UC 那视频信息
            $fileSdk->videoDelete($fileId);
        }

        // 删除UC附件
        $attach_serv = new AttachOperation();
        $attach_serv->delete_attach(APP_DIR, 'main', $lm_ids);

        return true;
    }

    /**
     * 请求接口数据
     * @param string       $url 请求URL
     * @param string|array $reqParams 请求数据
     * @param array        $headers 请求头部
     * @param string       $method 请求方式, 如: GET/POST/DELETE/PUT
     * @param mixed        $files 文件路径
     * @param bool         $retry 是否重试
     * @return bool
     */
    private function request($url, $reqParams = [], $headers = [], $method = 'GET', $files = null, $retry = false)
    {
        $snoopy = new Snoopy();

        // 使用自定义的头字段,格式为 array(字段名 => 值, ... ...)
        $snoopy->rawheaders = $headers;
        // 非 GET 协议, 需要设置
        $method = rstrtoupper($method);
        $methods = array('GET', 'POST', 'PUT', 'DELETE');
        if (!in_array($method, $methods)) {
            $method = 'GET';
        }

        // 设置协议
        if (!empty($files)) {
            // 如果需要传文件
            $method = 'POST';
            $snoopy->set_submit_multipart();
        } else {
            $snoopy->set_submit_normal('application/json');
        }

        // 判断协议
        $snoopy->set_submit_method($method);
        switch (rstrtoupper($method)) {
            case 'POST' :
            case 'PUT' :
            case 'DELETE' :
                $result = $snoopy->submit($url, $reqParams, $files);
                break;
            default :
                $result = $snoopy->fetch($url);
                break;
        }

        // 如果读取错误
        if (!$result || 200 != $snoopy->status) {
            \Think\Log::record('请求错误::' . var_export($snoopy, true));
        }

        return $snoopy->results;
    }
}