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

namespace app\index\controller;


use app\common\service\CommonService;
use app\index\BaseController;
use app\index\exception\ApiException;
use app\index\model\UserModel;
use app\index\model\VisitorModel;
use think\facade\Cache;
use think\facade\Request;
use think\facade\Session;

class User extends BaseController {

    public function login() {
        $channelId = input('channelId');
        if (empty($channelId)){
            throw new ApiException('渠道id不为空');
        }
        file_put_contents('log', date('Y-m-d H:i:s').Request::url(true).'-'.json_encode(input()).PHP_EOL, FILE_APPEND);
//        if (empty(input('unionid'))){
//            throw new ApiException('unionid不为空');
//        }
        $params = [
            'id'     => $channelId,
            'userid' => input('unionid'),
            'name'   => input('nickName'),
            'avatar' => input('avatar'),
            'key'    => md5(input('unionid').CommonService::getSetData($channelId)['channel_auth_code'])
        ];
        Session::set('wxId',input('unionid'));
        (new UserModel())->insertGetId([
            'channel_id' => $channelId,
            'unionid'    => input('unionid'),
            'name'       => input('nickName'),
            'avatar'     => input('avatar'),
            'key'        => $params['key'],
        ]);
        header('Location:http://webcasting.bizconfstreaming.com/activity.php?a=userAssign&'.http_build_query($params));
    }

    public function clearSession() {
//        var_dump($_SERVER);
        Session::clear();
//        var_dump($_SERVER);
    }
}