<?php

namespace addons\fastim\library\controller;

/**
 * IM控制器基类
 */
class Base
{
    public $userInfo = [];

    protected $server;

    protected $frame;

    /**
     * swoole Common类实例
     */
    protected $swooleCommon;

    public $pageCount = 15;

    public function __construct($base)
    {
        [$this->server, $this->frame, $this->swooleCommon] = $base;
        $this->swooleCommon->setFrame($this->frame);
        $userId = $this->swooleCommon->getUidByFd($this->frame->fd);
        if ($userId) {
            $this->userInfo['id'] = $userId;
        }
    }

    /**
     * 方法的登录权限检查
     * @param string $funName 要访问的方法名
     * @return bool
     */
    public function authCheck($funName)
    {
        $noNeedLogin = array_map('strtolower', $this->noNeedLogin);
        if (!in_array(strtolower($funName), $noNeedLogin) && (!isset($this->userInfo['id']) || !$this->userInfo['id'])) {
            $this->server->push($this->frame->fd, json_encode([
                'event' => 'show_msg',
                'data'  => '请登录后再进行操作！'
            ]));
            return false;
        }
        return true;
    }
}