<?php
/**
 *
 * @author：llbjj
 * @DateTime：2022/5/5 20:27
 * @Description：
 *
 */

namespace Application\Common;

use Application\Common\Crypt;
use Laminas\Stdlib\ArrayUtils;

class Token
{
    /**
     * Notes: 创建Token值
     * User: llbjj
     * DateTime: 2022/5/6 9:02
     *
     * @param array $data
     * @return string
     */
    static function createToken(array $data) {
        $data['ip'] = $_SERVER['REMOTE_ADDR'];
        return rtrim(Crypt::blockCipher()->encrypt(json_encode($data)), '==');
    }

    /**
     * Notes: 验证Token是否为有效值
     * User: llbjj
     * DateTime: 2022/5/6 9:03
     *
     * @param string $token
     * @return bool
     */
    static function verifyToken(string $token) {
        $decryptData = self::decryptToken($token);
        return !empty($decryptData);
    }

    /**
     * Notes: 解密token值
     * User: llbjj
     * DateTime: 2022/5/6 9:03
     *
     * @param string $data
     * @return mixed|void
     */
    static function decryptToken(string $data) {
        try {
            $data = $data.'==';
            return json_decode(Crypt::blockCipher()->decrypt($data));
        }catch (\Exception $e){

        }
    }
}