<?php
/**
 * Created by PhpStorm.
 * User: phperstar
 * Date: 2020/11/6
 * Time: 11:15 AM
 */
namespace Service;

use Mall\Framework\Factory;

class BaseController {

    /**
     * webstock链接保存在那个key下面
     */
    static $webstockAndKey = 'webstockAndKey';

    public function sendOutput($data, $errorcode = 0)
    {
        $errorcode = intval($errorcode);

        if ($errorcode) {
            $sendMessage = [
                'state' => false,
                'data' => $data,
                'errorcode' => $errorcode
            ];
        } else {
            $sendMessage = [
                'state' => true,
                'data' => $data,
                'errorcode' => $errorcode
            ];
        }

        return json_encode($sendMessage, JSON_UNESCAPED_UNICODE);
    }

    // 暂存一下每个webstock链接保存的位置
    public function webstockConnectInfo($fromId, $redisKey)
    {
        // webstock连接和存储关系
        $writeCache = Factory::cache('default')->hset(self::$webstockAndKey, $fromId, $redisKey);
    }

    // 销毁连接
    public function closeWebstock($fromId)
    {
        $data = Factory::cache('default')->hget(self::$webstockAndKey, $fromId);
        if(empty($data)){
            return false;
        }

        $data = explode('|', $data);
        $result = Factory::cache('default')->hdel($data[0], $data[1]);
        $result = Factory::cache('default')->hdel(self::$webstockAndKey, $fromId);
        return $result;
    }
}
