<?php
/**
 * Author: luzheng.liu
 * Time: 2020/12/5 17:55
 */

namespace app\index\controller;


use app\common\service\CommonService;
use app\index\exception\ApiException;
use app\index\model\UserModel;
use app\index\model\VisitorModel;
use think\console\command\make\Model;
use think\facade\Cache;
use think\facade\Session;

class Index {

    public function index() {
        $visitorId = input('visitorId');
        $notifyUrl = input('notify_url');
        $channelId = input('channelId');
        if (empty($channelId)){
            throw new ApiException('渠道id不为空');
        }
        $data = CommonService::getSetData($channelId);
        $info = (new UserModel())::where(['unionid' => Session::get('wxId'), 'channel_id' => $channelId])
            ->order(['id' => 'desc'])->find();
        if (empty(Session::get('wxId')) || empty($info)){
            header("Location:".CommonService::getAuthUrl($channelId));
            die();
        }
        $params = [
            'id'     => $info['channel_id'],
            'userid' => $info['unionid'],
            'name'   => $info['name'],
            'avatar' => $info['avatar'],
            'key'    => $info['key']
        ];
        header('Location:http://webcasting.bizconfstreaming.com/activity.php?a=userAssign&'.http_build_query($params));
        die();
        //增加客户自己的验证逻辑,例如登录,关注,付费,填手机号等
        $key = md5($visitorId.$data['channel_auth_code']);
        if(strpos($notifyUrl,'?') !== false){//url参数处理,将key加到url参数中
            $returnUrl = $notifyUrl."&key=".$key."&expire=1";
        }else{
            $returnUrl = $notifyUrl."?key=".$key."&expire=1";
        }
        header("Location:".$returnUrl);//跳转到直播观看页
        //        redirect($returnUrl);
    }
}